Client.activeSalesPerson() method refactor
This commit is contained in:
parent
8970abe903
commit
749681ac73
|
@ -28,10 +28,9 @@
|
|||
url="/client/api/Clients/activeSalesPerson"
|
||||
show-field="firstName"
|
||||
value-field="id"
|
||||
select-fields="name"
|
||||
label="Salesperson"
|
||||
vn-acl="salesAssistant"
|
||||
where="{or: [{firstName: {regexp: 'search'}}, {name: {regexp: 'search'}}]}">
|
||||
vn-acl="salesAssistant">
|
||||
<tpl-item>{{firstName}} {{name}}</tpl-item>
|
||||
</vn-autocomplete>
|
||||
<vn-autocomplete vn-one
|
||||
initial-data="$ctrl.client.contactChannel"
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
<vn-horizontal>
|
||||
<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-check
|
||||
vn-one
|
||||
label="Is equalizated"
|
||||
|
@ -26,11 +25,10 @@
|
|||
<vn-autocomplete vn-one
|
||||
field="$ctrl.client.salesPersonFk"
|
||||
url="/client/api/Clients/activeSalesPerson"
|
||||
show-field="name"
|
||||
show-field="firstName"
|
||||
value-field="id"
|
||||
select-fields="name"
|
||||
label="Salesperson"
|
||||
where="{or: [{firstName: {regexp: 'search'}}, {name: {regexp: 'search'}}]}">
|
||||
label="Salesperson">
|
||||
<tpl-item>{{firstName}} {{name}}</tpl-item>
|
||||
</vn-autocomplete>
|
||||
</vn-horizontal>
|
||||
<vn-horizontal>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module.exports = Client => {
|
||||
Client.remoteMethod('activeSalesPerson', {
|
||||
module.exports = Self => {
|
||||
Self.remoteMethod('activeSalesPerson', {
|
||||
description: 'Returns actives workers with salesperson role',
|
||||
accessType: 'READ',
|
||||
accepts: [{
|
||||
|
@ -10,7 +10,6 @@ module.exports = Client => {
|
|||
http: {source: 'query'}
|
||||
}],
|
||||
returns: {
|
||||
arg: 'data',
|
||||
type: 'Worker',
|
||||
root: true
|
||||
},
|
||||
|
@ -20,54 +19,37 @@ module.exports = Client => {
|
|||
}
|
||||
});
|
||||
|
||||
Client.activeSalesPerson = (filter, callback) => {
|
||||
let skip = filter.skip || 0;
|
||||
let limit = filter.limit || 10;
|
||||
let where = getCondition(filter.where, limit, skip);
|
||||
Self.activeSalesPerson = async filter => {
|
||||
let where = filter.where;
|
||||
let sqlWhere = '';
|
||||
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 =
|
||||
`SELECT em.id, em.firstName, em.name
|
||||
FROM worker em
|
||||
JOIN account.user ac ON em.userFk = ac.id
|
||||
JOIN account.role r ON r.id = ac.role
|
||||
WHERE ac.active ${where.sql}
|
||||
WHERE ac.active ${sqlWhere}
|
||||
ORDER BY em.name ASC
|
||||
LIMIT ? OFFSET ?`;
|
||||
${sqlLimit} ${sqlOffset}`;
|
||||
|
||||
Client.rawSql(query, where.params).then(
|
||||
response => callback(null, formatSalesPerson(response)),
|
||||
err => callback(err)
|
||||
);
|
||||
return await Self.rawSql(query, params);
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,33 +1,12 @@
|
|||
const app = require('../../../../../client/server/server');
|
||||
const catchErrors = require('../../../../../../services/utils/jasmineHelpers').catchErrors;
|
||||
|
||||
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 = {
|
||||
limit: 1
|
||||
};
|
||||
|
||||
let callback = (error, result) => {
|
||||
if (error) return catchErrors(done)(error);
|
||||
let result = await app.models.Client.activeSalesPerson(filter);
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue