66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('getByWorker', {
|
|
description: 'Return the routes by worker',
|
|
accessType: 'READ',
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/getByWorker`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.getByWorker = async ctx => {
|
|
const models = Self.app.models;
|
|
const userId = ctx.req.accessToken.userId;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const canViewAll = await models.ACL.checkAccessAcl(ctx, 'Route', 'canViewAllRoute', 'READ');
|
|
let filterGrant = {};
|
|
|
|
if (canViewAll) {
|
|
const userConfig = await models.UserConfig.getUserConfig(ctx, myOptions);
|
|
filterGrant = {
|
|
where: {'warehouseFk': userConfig.warehouseFk}
|
|
};
|
|
} else {
|
|
filterGrant = {
|
|
where: {'workerFk': userId}
|
|
};
|
|
}
|
|
|
|
const currentDate = Date.vnNew();
|
|
currentDate.setHours(0, 0, 0, 0);
|
|
const nextDay = Date.vnNew();
|
|
nextDay.setDate(currentDate.getDate() + 1);
|
|
nextDay.setHours(0, 0, 0, 0);
|
|
const filter = {
|
|
where: {
|
|
and: [
|
|
{
|
|
or: [
|
|
{'dated': currentDate},
|
|
{'dated': nextDay}
|
|
]
|
|
}
|
|
]
|
|
},
|
|
order: [
|
|
'dated ASC',
|
|
'time ASC',
|
|
'agencyName ASC'
|
|
]
|
|
};
|
|
|
|
const result = await Self.filter(ctx, mergeFilters(filter, filterGrant));
|
|
return result;
|
|
};
|
|
};
|