2021-06-17 13:09:04 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('updateTimeEntry', {
|
|
|
|
description: 'Updates a time entry for a worker if the user role is above the worker',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The time entry id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'direction',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateTimeEntry`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.updateTimeEntry = async(ctx, id, options) => {
|
|
|
|
const currentUserId = ctx.req.accessToken.userId;
|
|
|
|
const models = Self.app.models;
|
|
|
|
const args = ctx.args;
|
|
|
|
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2021-06-17 13:09:04 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const targetTimeEntry = await Self.findById(id, null, myOptions);
|
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, targetTimeEntry.userFk, 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 == targetTimeEntry.userFk;
|
|
|
|
|
|
|
|
const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss);
|
|
|
|
|
|
|
|
if (notAllowed)
|
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
2023-04-03 19:29:02 +00:00
|
|
|
const timeEntryUpdated = await targetTimeEntry.updateAttributes({
|
2021-06-17 13:09:04 +00:00
|
|
|
direction: args.direction
|
|
|
|
}, myOptions);
|
2023-04-03 19:29:02 +00:00
|
|
|
|
|
|
|
await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, targetTimeEntry.userFk, targetTimeEntry.timed, myOptions);
|
|
|
|
|
|
|
|
return timeEntryUpdated;
|
2021-06-17 13:09:04 +00:00
|
|
|
};
|
|
|
|
};
|