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: {'a.warehouseFk': userConfig.warehouseFk} }; } else { filterGrant = { where: {'r.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: [ {'r.created': currentDate}, {'r.created': nextDay} ] } ] }, order: [ 'r.created ASC', 'r.time ASC', 'am.name ASC' ] }; const result = await Self.filter(ctx, mergeFilters(filter, filterGrant)); return result; }; };