bug fixed rawSQL

This commit is contained in:
Dani Herrero 2017-10-18 15:06:38 +02:00
parent 3d52031dc2
commit 59577bcb7f
1 changed files with 10 additions and 25 deletions

View File

@ -29,11 +29,11 @@ module.exports = (Client) => {
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}
WHERE ac.active AND Role.\`name\`='salesPerson' ${where.cond}
ORDER BY em.name ASC
LIMIT ${limit} OFFSET ${skip}`;
Client.rawSql(query, [], callback)
LIMIT ? OFFSET ?`;
Client.rawSql(query, [where.value, limit, skip], callback)
.then(response => {
callback(null, formatSalesPerson(response));
})
@ -43,31 +43,16 @@ module.exports = (Client) => {
};
function getCondition(where) {
let out = [];
if(typeof where === 'object') {
Object.keys(where).forEach((k) => {
let value = where[k];
if (typeof value === 'number') {
out.push(`em.${k}=${value}`);
} else if (typeof value === 'string') {
out.push(`em.${k}='${value}'`);
} else if (typeof value === 'boolean' || value === null) {
out.push(`em.${k} IS ${String(value).toUpperCase()}`);
} else if (Object.keys(value).length) {
let firstProperty = Object.keys(value)[0];
out.push(`em.${k} ${firstProperty} '${value[firstProperty]}'`);
} else {
throw new Error ('Error: unexpected type');
}
});
}
return out.length ? `AND (${out.join(' AND ')})` : '';
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 : '';
return out;
}
function formatSalesPerson (response) {
function formatSalesPerson(response) {
let results = [];
response.forEach( person => {
response.forEach(person => {
results.push({
id: person.id,
name: `${person.name} ${person.surname}`