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

44 lines
1.2 KiB
JavaScript

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