diff --git a/client/route/src/create/create.html b/client/route/src/create/create.html index d1f8d77e9..06049e378 100644 --- a/client/route/src/create/create.html +++ b/client/route/src/create/create.html @@ -20,7 +20,12 @@ - + + + diff --git a/services/route/common/methods/drivers.js b/services/route/common/methods/drivers.js new file mode 100644 index 000000000..bc23f139c --- /dev/null +++ b/services/route/common/methods/drivers.js @@ -0,0 +1,79 @@ +module.exports = (Delivery) => { + Delivery.remoteMethod('activeDrivers', { + description: 'returns actives employees with driver role', + accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'Object', + required: false, + description: 'Filter defining where and paginated data', + http: {source: 'query'} + }], + returns: { + arg: 'data', + type: 'Employee', + root: true + }, + http: { + path: `/activeDrivers`, + verb: 'get' + } + }); + + Delivery.activeDrivers = (filter, callback) => { + let skip = filter.skip || 0; + let limit = filter.limit || 10; + let where = getCondition(filter.where); + // TODO: change salesPerson role to Driver role when it will be created + 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} + ORDER BY em.name ASC + LIMIT ${limit} OFFSET ${skip}`; + + Delivery.rawSql(query, [], callback) + .then(response => { + callback(null, formatDriver(response)); + }) + .catch(reject => { + callback(reject, null); + }); + }; + + 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 ')})` : ''; + } + + function formatDriver (response) { + let results = []; + + response.forEach( person => { + results.push({ + id: person.id, + name: `${person.name} ${person.surname}` + }); + }); + + return results; + } +}; \ No newline at end of file diff --git a/services/route/common/models/delivery.js b/services/route/common/models/delivery.js index 04b9610cf..90d8a1600 100644 --- a/services/route/common/models/delivery.js +++ b/services/route/common/models/delivery.js @@ -1,4 +1,5 @@ module.exports = function(Self) { require('../methods/filter.js')(Self); + require('../methods/drivers.js')(Self); };