2019-05-17 11:27:51 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2019-11-05 07:59:48 +00:00
|
|
|
Self.remoteMethodCtx('addTimeEntry', {
|
2019-05-17 11:27:51 +00:00
|
|
|
description: 'Adds a new hour registry',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2021-06-17 13:09:04 +00:00
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
description: 'The worker id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'timed',
|
|
|
|
type: 'date',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'direction',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
2019-05-17 11:27:51 +00:00
|
|
|
}],
|
|
|
|
returns: [{
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
}],
|
|
|
|
http: {
|
2021-06-17 13:09:04 +00:00
|
|
|
path: `/:id/addTimeEntry`,
|
2019-05-17 11:27:51 +00:00
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-17 13:09:04 +00:00
|
|
|
Self.addTimeEntry = async(ctx, workerId, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const args = ctx.args;
|
|
|
|
const currentUserId = ctx.req.accessToken.userId;
|
2019-05-17 11:27:51 +00:00
|
|
|
|
2021-06-17 13:09:04 +00:00
|
|
|
let myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, workerId, myOptions);
|
|
|
|
const isTeamBoss = await models.Account.hasRole(currentUserId, 'teamBoss', myOptions);
|
|
|
|
const isHimself = currentUserId == workerId;
|
2019-05-17 11:27:51 +00:00
|
|
|
|
2021-06-17 13:09:04 +00:00
|
|
|
if (isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss))
|
|
|
|
throw new UserError(`You don't have enough privileges`);
|
2019-05-17 11:27:51 +00:00
|
|
|
|
2021-06-17 13:09:04 +00:00
|
|
|
const timed = new Date(args.timed);
|
2019-11-21 08:19:03 +00:00
|
|
|
|
2021-06-17 13:09:04 +00:00
|
|
|
return models.WorkerTimeControl.create({
|
|
|
|
userFk: workerId,
|
|
|
|
direction: args.direction,
|
|
|
|
timed: timed,
|
|
|
|
manual: true
|
|
|
|
}, myOptions);
|
2019-05-17 11:27:51 +00:00
|
|
|
};
|
|
|
|
};
|