bug fixed in salesperson query

This commit is contained in:
Daniel Herrero 2017-11-21 11:50:37 +01:00
parent 13c15bb5b3
commit 730d52169e
1 changed files with 18 additions and 7 deletions

View File

@ -23,17 +23,17 @@ module.exports = (Client) => {
Client.activeSalesPerson = (filter, callback) => {
let skip = filter.skip || 0;
let limit = filter.limit || 10;
let where = getCondition(filter.where);
let where = getCondition(filter.where, limit, skip);
let query = `SELECT em.id, em.name, em.surname
FROM Employee em
JOIN Account ac ON em.userFk = ac.id
JOIN Role ON Role.id = ac.roleFK
WHERE ac.active AND Role.\`name\`='salesPerson' ${where.cond}
WHERE ac.active AND Role.\`name\`='salesPerson' ${where.sql}
ORDER BY em.name ASC
LIMIT ? OFFSET ?`;
Client.rawSql(query, [where.value, parseInt(limit, 10), parseInt(skip, 10)], callback)
Client.rawSql(query, where.params, callback)
.then(response => {
callback(null, formatSalesPerson(response));
})
@ -42,10 +42,21 @@ module.exports = (Client) => {
});
};
function getCondition(where) {
let out = {};
out.cond = (typeof where === 'object' && where.name && where.name.regexp) ? `AND em.name regexp ?` : '?';
out.value = (typeof where === 'object' && where.name && where.name.regexp) ? where.name.regexp : '';
function getCondition(where, limit, skip) {
let out = {
sql: '',
params: []
};
if (where && where.or) {
out.sql = `AND (em.name regexp ? OR em.surname 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;
}