feat: refs #7002 update direction if device exists
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2024-03-25 13:33:39 +01:00
parent 208f333b43
commit eb2b0d41de
1 changed files with 13 additions and 8 deletions

View File

@ -29,7 +29,7 @@ module.exports = Self => {
Self.updateTimeEntry = async(ctx, timeEntryId, direction, options) => { Self.updateTimeEntry = async(ctx, timeEntryId, direction, options) => {
const currentUserId = ctx.req.accessToken.userId; const currentUserId = ctx.req.accessToken.userId;
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {userId: currentUserId};
let tx; let tx;
if (typeof options == 'object') if (typeof options == 'object')
@ -41,19 +41,24 @@ module.exports = Self => {
} }
try { try {
const {id, userFk, timed} = await Self.findById(timeEntryId, null, myOptions); const timeEntry = await Self.findById(timeEntryId, null, myOptions);
const isSubordinate = await models.Worker.isSubordinate(ctx, userFk, myOptions); const isSubordinate = await models.Worker.isSubordinate(ctx, timeEntry.userFk, myOptions);
const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE');
const isHimself = currentUserId == userFk; const isHimself = currentUserId == timeEntry.userFk;
const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss); const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss);
if (notAllowed) throw new UserError(`You don't have enough privileges`); if (notAllowed) throw new UserError(`You don't have enough privileges`);
await models.WorkerTimeControl.deleteById(id, myOptions); let timeEntryUpdatedId;
const timeEntryUpdatedId = await Self.clockIn(userFk, timed, direction, null, myOptions); if (timeEntry.device) {
timeEntry.updateAttribute('direction', direction);
await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, userFk, timed, myOptions); timeEntryUpdatedId = timeEntry.id;
} else {
await models.WorkerTimeControl.deleteById(timeEntry.id, myOptions);
timeEntryUpdatedId = await Self.clockIn(timeEntry.userFk, timeEntry.timed, direction, null, myOptions);
}
await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, timeEntry.userFk, timeEntry.timed, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
return timeEntryUpdatedId; return timeEntryUpdatedId;
} catch (e) { } catch (e) {