From eb2b0d41de4e4cdabce5587c72c292fba3c1dc04 Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 25 Mar 2024 13:33:39 +0100 Subject: [PATCH 1/3] feat: refs #7002 update direction if device exists --- .../worker-time-control/updateTimeEntry.js | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/modules/worker/back/methods/worker-time-control/updateTimeEntry.js b/modules/worker/back/methods/worker-time-control/updateTimeEntry.js index 7e4455447..e86834502 100644 --- a/modules/worker/back/methods/worker-time-control/updateTimeEntry.js +++ b/modules/worker/back/methods/worker-time-control/updateTimeEntry.js @@ -29,7 +29,7 @@ module.exports = Self => { Self.updateTimeEntry = async(ctx, timeEntryId, direction, options) => { const currentUserId = ctx.req.accessToken.userId; const models = Self.app.models; - const myOptions = {}; + const myOptions = {userId: currentUserId}; let tx; if (typeof options == 'object') @@ -41,19 +41,24 @@ module.exports = Self => { } try { - const {id, userFk, timed} = await Self.findById(timeEntryId, null, myOptions); - const isSubordinate = await models.Worker.isSubordinate(ctx, userFk, myOptions); + const timeEntry = await Self.findById(timeEntryId, null, myOptions); + const isSubordinate = await models.Worker.isSubordinate(ctx, timeEntry.userFk, myOptions); 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); if (notAllowed) throw new UserError(`You don't have enough privileges`); - await models.WorkerTimeControl.deleteById(id, myOptions); - const timeEntryUpdatedId = await Self.clockIn(userFk, timed, direction, null, myOptions); - - await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, userFk, timed, myOptions); + let timeEntryUpdatedId; + if (timeEntry.device) { + timeEntry.updateAttribute('direction', direction); + 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(); return timeEntryUpdatedId; } catch (e) { From 6ded14b593b0fa378b99b65512d022c7397f240f Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 26 Mar 2024 14:25:25 +0100 Subject: [PATCH 2/3] feat: refs #7002 update direction if it was not manual --- .../worker-time-control/updateTimeEntry.js | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/modules/worker/back/methods/worker-time-control/updateTimeEntry.js b/modules/worker/back/methods/worker-time-control/updateTimeEntry.js index e86834502..bbe8fd0fe 100644 --- a/modules/worker/back/methods/worker-time-control/updateTimeEntry.js +++ b/modules/worker/back/methods/worker-time-control/updateTimeEntry.js @@ -41,26 +41,24 @@ module.exports = Self => { } try { - const timeEntry = await Self.findById(timeEntryId, null, myOptions); - const isSubordinate = await models.Worker.isSubordinate(ctx, timeEntry.userFk, myOptions); + const {id, userFk, timed, manual} = await Self.findById(timeEntryId, null, myOptions); + const isSubordinate = await models.Worker.isSubordinate(ctx, userFk, myOptions); const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); - const isHimself = currentUserId == timeEntry.userFk; + const isHimself = currentUserId == userFk; const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss); if (notAllowed) throw new UserError(`You don't have enough privileges`); - let timeEntryUpdatedId; - if (timeEntry.device) { - timeEntry.updateAttribute('direction', direction); - 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.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); - await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, timeEntry.userFk, timeEntry.timed, myOptions); if (tx) await tx.commit(); - return timeEntryUpdatedId; + return newTimeEntryId; } catch (e) { if (tx) await tx.rollback(); throw e; From e7a29119d0ca53d37b6b9696eaf78d0d0ddb82da Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 27 Mar 2024 13:38:04 +0100 Subject: [PATCH 3/3] feat: refs #7002 add test --- .../worker-time-control/specs/clockIn.spec.js | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js b/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js index 0ee439941..343eb2a71 100644 --- a/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js +++ b/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js @@ -71,6 +71,34 @@ describe('workerTimeControl clockIn()', () => { } }); + it('should updates the time entry direction and remaining not be manual', async() => { + activeCtx.accessToken.userId = HHRRId; + const workerId = teamBossId; + + const tx = await models.WorkerTimeControl.beginTransaction({}); + try { + const options = {transaction: tx}; + + const entryTime = "2000-12-25T11:00:00.000Z"; + ctx.args = {timed: entryTime, direction: 'in'}; + await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + + const middleTime ="2000-12-25T16:00:00.000Z"; + ctx.args = {timed: middleTime, direction: 'middle'}; + const middleEntryTime = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + middleEntryTime.updateAttribute('manual', false); + + const direction = 'out'; + const outTimeEntryId = await models.WorkerTimeControl.updateTimeEntry(ctx, middleEntryTime.id, direction, options); + + const outTimeEntry = await models.WorkerTimeControl.findById(outTimeEntryId, null, options); + expect(outTimeEntry.manual).toBeFalsy(); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + } + }); + describe('as Role errors', () => { it('should add if the current user is team boss and the target user is himself', async() => { activeCtx.accessToken.userId = teamBossId; @@ -144,7 +172,7 @@ describe('workerTimeControl clockIn()', () => { const middleTime = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); const direction = 'out'; - const {id:outTimeEntryId} = await models.WorkerTimeControl.updateTimeEntry( + const outTimeEntryId = await models.WorkerTimeControl.updateTimeEntry( ctx, middleTime.id, direction, options );