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',
|
2024-01-15 06:45:40 +00:00
|
|
|
accessType: 'WRITE',
|
2021-06-17 13:09:04 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-22 11:48:07 +00:00
|
|
|
Self.updateTimeEntry = async(ctx, timeEntryId, direction, options) => {
|
2021-06-17 13:09:04 +00:00
|
|
|
const currentUserId = ctx.req.accessToken.userId;
|
|
|
|
const models = Self.app.models;
|
2024-03-25 12:33:39 +00:00
|
|
|
const myOptions = {userId: currentUserId};
|
2024-02-22 11:48:07 +00:00
|
|
|
let tx;
|
2021-06-17 13:09:04 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2024-02-22 11:48:07 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2021-06-17 13:09:04 +00:00
|
|
|
|
2024-02-22 11:48:07 +00:00
|
|
|
try {
|
2024-03-26 13:25:25 +00:00
|
|
|
const {id, userFk, timed, manual} = await Self.findById(timeEntryId, null, myOptions);
|
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, userFk, myOptions);
|
2024-02-22 11:48:07 +00:00
|
|
|
const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE');
|
2024-03-26 13:25:25 +00:00
|
|
|
const isHimself = currentUserId == userFk;
|
2021-06-17 13:09:04 +00:00
|
|
|
|
2024-02-22 11:48:07 +00:00
|
|
|
const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss);
|
|
|
|
if (notAllowed) throw new UserError(`You don't have enough privileges`);
|
2021-06-17 13:09:04 +00:00
|
|
|
|
2024-03-26 13:25:25 +00:00
|
|
|
await models.WorkerTimeControl.deleteById(id, myOptions);
|
|
|
|
const {id: newTimeEntryId} = await Self.clockIn(
|
|
|
|
userFk, timed, direction, null, myOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!manual) await Self.updateAll({id: newTimeEntryId}, {manual: false}, myOptions);
|
|
|
|
await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, userFk, timed, myOptions);
|
2023-04-03 19:29:02 +00:00
|
|
|
|
2024-02-22 11:48:07 +00:00
|
|
|
if (tx) await tx.commit();
|
2024-03-26 13:25:25 +00:00
|
|
|
return newTimeEntryId;
|
2024-02-22 11:48:07 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-06-17 13:09:04 +00:00
|
|
|
};
|
|
|
|
};
|