salix/modules/worker/back/methods/worker-time-control/addTimeEntry.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

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;
2023-06-01 06:32:06 +00:00
const userId = ctx.req.accessToken.userId;
const myOptions = {userId};
2019-05-17 11:27:51 +00:00
2021-06-17 13:09:04 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
const isSubordinate = await models.Worker.isSubordinate(ctx, workerId, myOptions);
const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE');
2023-06-01 06:32:06 +00:00
const isHimself = userId == workerId;
2019-05-17 11:27:51 +00:00
2023-12-07 13:47:22 +00:00
if (!isSubordinate || (isHimself && !isTeamBoss))
2021-06-17 13:09:04 +00:00
throw new UserError(`You don't have enough privileges`);
2019-05-17 11:27:51 +00:00
2024-01-31 12:16:56 +00:00
return Self.clockIn(workerId, args.timed, args.direction, null, myOptions);
2019-05-17 11:27:51 +00:00
};
};