From cf862202ffa468d8a54fa7b7b222273763110a11 Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 2 Dec 2024 10:12:56 +0100 Subject: [PATCH 1/5] fix: hotfix 7323 createAbsence --- .../11372-azureMedeola/00-firstScript.sql | 2 ++ loopback/locale/en.json | 4 +-- loopback/locale/es.json | 3 +- .../back/methods/worker/createAbsence.js | 6 ++++ .../worker/specs/createAbsence.spec.js | 29 +++++++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 db/versions/11372-azureMedeola/00-firstScript.sql diff --git a/db/versions/11372-azureMedeola/00-firstScript.sql b/db/versions/11372-azureMedeola/00-firstScript.sql new file mode 100644 index 000000000..b98701509 --- /dev/null +++ b/db/versions/11372-azureMedeola/00-firstScript.sql @@ -0,0 +1,2 @@ +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('Worker','isHr','WRITE','ALLOW','ROLE','hr'); diff --git a/loopback/locale/en.json b/loopback/locale/en.json index fdd39325b..c52da615a 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -245,6 +245,6 @@ "Invalid or expired verification code": "Invalid or expired verification code", "Payment method is required": "Payment method is required", "The raid information is not correct": "The raid information is not correct", - "Sales already moved": "Sales already moved" - + "Sales already moved": "Sales already moved", + "Holidays to past days not available": "Holidays to past days not available" } diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 4e691f375..782b2504a 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -388,5 +388,6 @@ "ticketLostExpedition": "El ticket [{{ticketId}}]({{{ticketUrl}}}) tiene la siguiente expedición perdida:{{ expeditionId }}", "The web user's email already exists": "El correo del usuario web ya existe", "Sales already moved": "Ya han sido transferidas", - "The raid information is not correct": "La información de la redada no es correcta" + "The raid information is not correct": "La información de la redada no es correcta", + "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles" } diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 0397886cf..264de4dd3 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -54,6 +54,7 @@ module.exports = Self => { try { const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions); const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); + const isHr = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isHr', 'WRITE'); if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); @@ -113,6 +114,11 @@ module.exports = Self => { if ((holiday && isFestive) && (workCenter.workcenterFk === holiday.workCenterFk)) throw new UserError(`Cannot add holidays on this day`); + const newDate = new Date(ctx.args.dated).getTime(); + const nowDate = now.getTime(); + if ((nowDate > newDate) && !isHr) + throw new UserError(`Holidays to past days not available`); + const absence = await models.Calendar.create({ businessFk: labour.businessFk, dayOffTypeFk: args.absenceTypeId, diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js index fcb1c1633..1c7efcd28 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -162,4 +162,33 @@ describe('Worker createAbsence()', () => { expect(error.message).toEqual(`The worker has hours recorded that day`); }); + + it(`Should throw an error when adding a "Vacation" absence on a past day`, async() => { + const ctx = { + req: {accessToken: {userId: 19}}, + args: { + id: 1110, + businessFk: 1110, + absenceTypeId: 1, + dated: '2000-12-27T23:00:00.000Z', + } + }; + const workerId = 19; + + const tx = await app.models.Calendar.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + + await app.models.Worker.createAbsence(ctx, workerId, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`Holidays to past days not available`); + }); }); From 4e6b83809b21ba4ab74fe448b4eb7a19998958c7 Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 2 Dec 2024 10:29:12 +0100 Subject: [PATCH 2/5] fix: workerHoliday hr --- db/versions/11372-azureMedeola/00-firstScript.sql | 2 +- modules/worker/back/methods/worker/createAbsence.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/db/versions/11372-azureMedeola/00-firstScript.sql b/db/versions/11372-azureMedeola/00-firstScript.sql index b98701509..53295e9f8 100644 --- a/db/versions/11372-azureMedeola/00-firstScript.sql +++ b/db/versions/11372-azureMedeola/00-firstScript.sql @@ -1,2 +1,2 @@ INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) - VALUES ('Worker','isHr','WRITE','ALLOW','ROLE','hr'); + VALUES ('Worker','canCreateAbsenceInPast','WRITE','ALLOW','ROLE','hr'); diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 264de4dd3..dad02d0dc 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -54,7 +54,7 @@ module.exports = Self => { try { const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions); const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); - const isHr = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isHr', 'WRITE'); + const canCreateAbsenceInPast = await models.ACL.checkAccessAcl(ctx, 'Worker', 'canCreateAbsenceInPast', 'WRITE'); if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); @@ -116,7 +116,7 @@ module.exports = Self => { const newDate = new Date(ctx.args.dated).getTime(); const nowDate = now.getTime(); - if ((nowDate > newDate) && !isHr) + if ((nowDate > newDate) && !canCreateAbsenceInPast) throw new UserError(`Holidays to past days not available`); const absence = await models.Calendar.create({ From fb40e25c7c334ef76aba966ac24dde3f26de3da3 Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 2 Dec 2024 10:47:44 +0100 Subject: [PATCH 3/5] fix: remove ctx --- modules/worker/back/methods/worker/createAbsence.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index dad02d0dc..495997f29 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -54,11 +54,11 @@ module.exports = Self => { try { const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions); const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); - const canCreateAbsenceInPast = await models.ACL.checkAccessAcl(ctx, 'Worker', 'canCreateAbsenceInPast', 'WRITE'); if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); + const canCreateAbsenceInPast = await models.ACL.checkAccessAcl(ctx, 'Worker', 'canCreateAbsenceInPast', 'WRITE'); const labour = await models.WorkerLabour.findById(args.businessFk, { include: {relation: 'department'} }, myOptions); @@ -114,7 +114,7 @@ module.exports = Self => { if ((holiday && isFestive) && (workCenter.workcenterFk === holiday.workCenterFk)) throw new UserError(`Cannot add holidays on this day`); - const newDate = new Date(ctx.args.dated).getTime(); + const newDate = new Date(args.dated).getTime(); const nowDate = now.getTime(); if ((nowDate > newDate) && !canCreateAbsenceInPast) throw new UserError(`Holidays to past days not available`); From f63d850ea3ba7a1bdec4db6fcd13596783c66deb Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 9 Dec 2024 11:37:01 +0100 Subject: [PATCH 4/5] refactor: refs #7366 rename ref to reference --- modules/travel/back/methods/travel/extraCommunityFilter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/travel/back/methods/travel/extraCommunityFilter.js b/modules/travel/back/methods/travel/extraCommunityFilter.js index 164da93af..f1586f804 100644 --- a/modules/travel/back/methods/travel/extraCommunityFilter.js +++ b/modules/travel/back/methods/travel/extraCommunityFilter.js @@ -86,7 +86,7 @@ module.exports = Self => { return /^\d+$/.test(value) ? {'t.id': value} : {'t.ref': {like: `%${value}%`}}; - case 'ref': + case 'reference': return {'t.ref': {like: `%${value}%`}}; case 'shippedFrom': return {'t.shipped': {gte: value}}; @@ -111,7 +111,7 @@ module.exports = Self => { let stmt; stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.travel'); stmt = new ParameterizedSQL( - `CREATE TEMPORARY TABLE tmp.travel + `CREATE TEMPORARY TABLE tmp.travel (INDEX (id)) ENGINE = MEMORY SELECT t.id, From eaa62909ff5a3241f0a7f4ce5143f8adcb84a7f7 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 9 Dec 2024 14:30:45 +0100 Subject: [PATCH 5/5] chore: simplify --- modules/worker/back/methods/worker/createAbsence.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 34dcd1f59..e46ce9577 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -62,9 +62,8 @@ module.exports = Self => { await models.ACL.checkAccessAcl(ctx, 'Worker', 'canCreateAbsenceInPast', 'WRITE'); const now = Date.vnNew(); const newDate = new Date(args.dated).getTime(); - const nowDate = now.getTime(); - if ((nowDate > newDate) && !canCreateAbsenceInPast) + if ((now.getTime() > newDate) && !canCreateAbsenceInPast) throw new UserError(`Holidays to past days not available`); const labour = await models.WorkerLabour.findById(args.businessFk,