salix/modules/worker/back/methods/worker/activeContract.js

48 lines
1.3 KiB
JavaScript

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 = new Date();
return models.WorkerLabour.findOne({
where: {
and: [
{workerFk: id},
{started: {lte: now}},
{
or: [
{ended: {gte: now}},
{ended: null}
]
}
]
}
});
};
};