const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('activeContract', { description: 'Returns an array of contracts from an specified worker', accepts: [{ arg: 'id', type: 'number', required: true, description: 'The worker id', http: {source: 'path'} }], returns: { type: ['object'], root: true }, http: { path: `/:id/activeContract`, verb: 'GET' } }); Self.activeContract = async(ctx, id) => { const models = Self.app.models; const isSubordinate = await models.Worker.isSubordinate(ctx, id); if (!isSubordinate) throw new UserError(`You don't have enough privileges`); const now = Date.vnNew(); return models.WorkerLabour.findOne({ where: { and: [ {workerFk: id}, {started: {lte: now}}, { or: [ {ended: {gte: now}}, {ended: null} ] } ] } }); }; };