Client.activeSalesPerson() method refactor

This commit is contained in:
Juan 2018-04-03 19:29:01 +02:00
parent 8970abe903
commit 749681ac73
4 changed files with 37 additions and 79 deletions

View File

@ -28,10 +28,9 @@
url="/client/api/Clients/activeSalesPerson" url="/client/api/Clients/activeSalesPerson"
show-field="firstName" show-field="firstName"
value-field="id" value-field="id"
select-fields="name"
label="Salesperson" label="Salesperson"
vn-acl="salesAssistant" vn-acl="salesAssistant">
where="{or: [{firstName: {regexp: 'search'}}, {name: {regexp: 'search'}}]}"> <tpl-item>{{firstName}} {{name}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete vn-one <vn-autocomplete vn-one
initial-data="$ctrl.client.contactChannel" initial-data="$ctrl.client.contactChannel"

View File

@ -12,7 +12,6 @@
<vn-horizontal> <vn-horizontal>
<vn-textfield vn-two label="Business name" field="$ctrl.client.socialName"></vn-textfield> <vn-textfield vn-two label="Business name" field="$ctrl.client.socialName"></vn-textfield>
<vn-textfield vn-one label="Tax number" field="$ctrl.client.fi"></vn-textfield> <vn-textfield vn-one label="Tax number" field="$ctrl.client.fi"></vn-textfield>
<vn-check <vn-check
vn-one vn-one
label="Is equalizated" label="Is equalizated"
@ -26,11 +25,10 @@
<vn-autocomplete vn-one <vn-autocomplete vn-one
field="$ctrl.client.salesPersonFk" field="$ctrl.client.salesPersonFk"
url="/client/api/Clients/activeSalesPerson" url="/client/api/Clients/activeSalesPerson"
show-field="name" show-field="firstName"
value-field="id" value-field="id"
select-fields="name" label="Salesperson">
label="Salesperson" <tpl-item>{{firstName}} {{name}}</tpl-item>
where="{or: [{firstName: {regexp: 'search'}}, {name: {regexp: 'search'}}]}">
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>
<vn-horizontal> <vn-horizontal>

View File

@ -1,5 +1,5 @@
module.exports = Client => { module.exports = Self => {
Client.remoteMethod('activeSalesPerson', { Self.remoteMethod('activeSalesPerson', {
description: 'Returns actives workers with salesperson role', description: 'Returns actives workers with salesperson role',
accessType: 'READ', accessType: 'READ',
accepts: [{ accepts: [{
@ -10,7 +10,6 @@ module.exports = Client => {
http: {source: 'query'} http: {source: 'query'}
}], }],
returns: { returns: {
arg: 'data',
type: 'Worker', type: 'Worker',
root: true root: true
}, },
@ -20,54 +19,37 @@ module.exports = Client => {
} }
}); });
Client.activeSalesPerson = (filter, callback) => { Self.activeSalesPerson = async filter => {
let skip = filter.skip || 0; let where = filter.where;
let limit = filter.limit || 10; let sqlWhere = '';
let where = getCondition(filter.where, limit, skip); let sqlLimit = '';
let sqlOffset = '';
let params = [];
if (where) {
sqlWhere = `AND (em.firstName REGEXP ? OR em.name REGEXP ?)`;
let search = where.firstName.regexp;
params.push(search);
params.push(search);
}
if (filter.limit) {
sqlLimit = `LIMIT ?`;
params.push(filter.limit);
}
if (filter.skip) {
sqlOffset = `OFFSET ?`;
params.push(filter.skip);
}
let query = let query =
`SELECT em.id, em.firstName, em.name `SELECT em.id, em.firstName, em.name
FROM worker em FROM worker em
JOIN account.user ac ON em.userFk = ac.id JOIN account.user ac ON em.userFk = ac.id
JOIN account.role r ON r.id = ac.role JOIN account.role r ON r.id = ac.role
WHERE ac.active ${where.sql} WHERE ac.active ${sqlWhere}
ORDER BY em.name ASC ORDER BY em.name ASC
LIMIT ? OFFSET ?`; ${sqlLimit} ${sqlOffset}`;
Client.rawSql(query, where.params).then( return await Self.rawSql(query, params);
response => callback(null, formatSalesPerson(response)),
err => callback(err)
);
}; };
function getCondition(where, limit, skip) {
let out = {
sql: '',
params: []
};
if (where && where.or) {
out.sql = `AND (em.firstName regexp ? OR em.name regexp ?)`;
where.or.forEach(val => {
Object.keys(val).forEach(key => {
out.params.push(val[key].regexp);
});
});
}
out.params.push(parseInt(limit, 10));
out.params.push(parseInt(skip, 10));
return out;
}
function formatSalesPerson(response) {
let results = [];
response.forEach(person => {
results.push({
id: person.id,
name: `${person.firstName} ${person.name}`
});
});
return results;
}
}; };

View File

@ -1,33 +1,12 @@
const app = require('../../../../../client/server/server'); const app = require('../../../../../client/server/server');
const catchErrors = require('../../../../../../services/utils/jasmineHelpers').catchErrors;
describe('Client activeSalesPerson', () => { describe('Client activeSalesPerson', () => {
it('should call the activeSalesPerson() method with limit of 1', done => { it('should call the activeSalesPerson() method with limit of 1', async() => {
let filter = { let filter = {
limit: 1 limit: 1
}; };
let result = await app.models.Client.activeSalesPerson(filter);
let callback = (error, result) => {
if (error) return catchErrors(done)(error);
expect(result.length).toEqual(1); expect(result.length).toEqual(1);
done();
};
app.models.Client.activeSalesPerson(filter, callback);
});
it('should call the activeSalesPerson() method with no limit and receive all 10 salesPersons', done => {
let filter = {
};
let callback = (error, result) => {
if (error) return catchErrors(done)(error);
expect(result.length).toEqual(10);
done();
};
app.models.Client.activeSalesPerson(filter, callback);
}); });
}); });