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;
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
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);
|
2023-04-25 12:17:36 +00:00
|
|
|
const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE');
|
2021-06-17 13:09:04 +00:00
|
|
|
const isHimself = currentUserId == workerId;
|
2019-05-17 11:27:51 +00:00
|
|
|
|
2023-04-25 12:17:36 +00:00
|
|
|
if (!isSubordinate || (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
|
|
|
|
2022-06-23 12:07:53 +00:00
|
|
|
query = `CALL vn.workerTimeControl_clockIn(?,?,?)`;
|
|
|
|
const [response] = await Self.rawSql(query, [workerId, args.timed, args.direction], myOptions);
|
|
|
|
if (response[0] && response[0].error)
|
|
|
|
throw new UserError(response[0].error);
|
2021-11-30 08:11:23 +00:00
|
|
|
|
2023-04-03 19:29:02 +00:00
|
|
|
await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, workerId, args.timed, myOptions);
|
|
|
|
|
2022-06-23 12:07:53 +00:00
|
|
|
return response;
|
2019-05-17 11:27:51 +00:00
|
|
|
};
|
|
|
|
};
|