facke active drivers rest created
This commit is contained in:
parent
5514ec222a
commit
a366ef1fc1
|
@ -20,7 +20,12 @@
|
|||
</vn-autocomplete>
|
||||
</vn-horizontal>
|
||||
<vn-horizontal>
|
||||
<vn-textfield vn-one label="Driver" field="$ctrl.delivery.driver"></vn-textfield>
|
||||
|
||||
<vn-autocomplete vn-one
|
||||
field="$ctrl.delivery.driver"
|
||||
url="/route/api/Deliveries/activeDrivers"
|
||||
label="Driver"></vn-autocomplete>
|
||||
|
||||
<vn-textfield vn-one label="Vehicle" field="$ctrl.delivery.vehicle"></vn-textfield>
|
||||
</vn-horizontal>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
|
@ -1,4 +1,5 @@
|
|||
|
||||
module.exports = function(Self) {
|
||||
require('../methods/filter.js')(Self);
|
||||
require('../methods/drivers.js')(Self);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue