From 7efb19ab8373bad4b5ebf8b00cb5b62f93d54e56 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 15 Nov 2021 14:49:14 +0100 Subject: [PATCH 1/5] =?UTF-8?q?feat(worker=5Fcalendar):=20add=20restrictio?= =?UTF-8?q?n=20to=20choose=20"Vacaciones=201/2=20d=C3=ADa"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- loopback/locale/es.json | 1 + modules/worker/back/methods/worker/createAbsence.js | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index afa349684..f7377300a 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -193,6 +193,7 @@ "Client assignment has changed": "He cambiado el comercial ~*\"<{{previousWorkerName}}>\"*~ por *\"<{{currentWorkerName}}>\"* del cliente [{{clientName}} ({{clientId}})]({{{url}}})", "None": "Ninguno", "The contract was not active during the selected date": "El contrato no estaba activo durante la fecha seleccionada", + "Cannot add more than one '1/2 day vacation'": "No puedes añadir más de un 'Vacaciones 1/2 dia'", "This document already exists on this ticket": "Este documento ya existe en el ticket", "Some of the selected tickets are not billable": "Algunos de los tickets seleccionados no son facturables", "You can't invoice tickets from multiple clients": "No puedes facturar tickets de multiples clientes", diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 549ba7fd1..3045a3169 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -65,6 +65,14 @@ module.exports = Self => { if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended)) throw new UserError(`The contract was not active during the selected date`); + let result = await Self.rawSql( + `SELECT COUNT(*) halfDayHolidayCounter + FROM vn.calendar c + WHERE c.dayOffTypeFk = 6 AND c.businessFk = ?`, [args.businessFk]); + + if (args.absenceTypeId == 6 && result[0].halfDayHolidayCounter > 0) + throw new UserError(`Cannot add more than one '1/2 day vacation'`); + const absence = await models.Calendar.create({ businessFk: labour.businessFk, dayOffTypeFk: args.absenceTypeId, From 180fb95a0004dd360c8af130f93e30a39b8d3370 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 16 Nov 2021 10:09:42 +0100 Subject: [PATCH 2/5] feat(createAbsence): add backTest --- db/dump/fixtures.sql | 1 + .../back/methods/worker/createAbsence.js | 9 ++++-- .../worker/specs/createAbsence.spec.js | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 49ff08db9..745a2ee07 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1882,6 +1882,7 @@ INSERT INTO `postgresql`.`calendar_state` (`calendar_state_id`, `type`, `rgb`, ` INSERT INTO `postgresql`.`calendar_employee` (`business_id`, `calendar_state_id`, `date`) VALUES + (1, 6, IF(MONTH(CURDATE()) = 12 AND DAY(CURDATE()) > 10, DATE_ADD(CURDATE(), INTERVAL -10 DAY), DATE_ADD(CURDATE(), INTERVAL 10 DAY))), (1106, 1, IF(MONTH(CURDATE()) = 12 AND DAY(CURDATE()) > 10, DATE_ADD(CURDATE(), INTERVAL -10 DAY), DATE_ADD(CURDATE(), INTERVAL 10 DAY))), (1106, 1, IF(MONTH(CURDATE()) = 12 AND DAY(CURDATE()) > 10, DATE_ADD(CURDATE(), INTERVAL -11 DAY), DATE_ADD(CURDATE(), INTERVAL 11 DAY))), (1106, 1, IF(MONTH(CURDATE()) = 12 AND DAY(CURDATE()) > 10, DATE_ADD(CURDATE(), INTERVAL -12 DAY), DATE_ADD(CURDATE(), INTERVAL 12 DAY))), diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 3045a3169..217191a30 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -65,12 +65,15 @@ module.exports = Self => { if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended)) throw new UserError(`The contract was not active during the selected date`); - let result = await Self.rawSql( - `SELECT COUNT(*) halfDayHolidayCounter + const result = await Self.rawSql( + `SELECT COUNT(*) halfHolidayCounter FROM vn.calendar c WHERE c.dayOffTypeFk = 6 AND c.businessFk = ?`, [args.businessFk]); - if (args.absenceTypeId == 6 && result[0].halfDayHolidayCounter > 0) + const hasHalfHoliday = result[0].halfHolidayCounter > 0; + const isHalfHoliday = args.absenceTypeId == 6; + + if (isHalfHoliday && hasHalfHoliday) throw new UserError(`Cannot add more than one '1/2 day vacation'`); const absence = await models.Calendar.create({ diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js index f2c00e804..e5387c379 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -74,4 +74,35 @@ describe('Worker createAbsence()', () => { throw e; } }); + + it('should return an error trying to create a "Half holiday" absence', async() => { + const ctx = { + req: {accessToken: {userId: 19}}, + args: { + businessFk: 1, + absenceTypeId: 6, + dated: new Date() + } + }; + + const tx = await app.models.Calendar.beginTransaction({}); + + try { + const options = {transaction: tx}; + + let error; + await app.models.Worker.createAbsence(ctx, workerId, options).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`); + }); + + expect(error).toBeDefined(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); From 651a9257085c0970087f541114d5a1634a24c0b5 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 17 Nov 2021 14:20:26 +0100 Subject: [PATCH 3/5] feat(worker_calendar): only can choose one halfHoliday by year --- modules/worker/back/methods/worker/createAbsence.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 217191a30..ba13a8a10 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -68,7 +68,13 @@ module.exports = Self => { const result = await Self.rawSql( `SELECT COUNT(*) halfHolidayCounter FROM vn.calendar c - WHERE c.dayOffTypeFk = 6 AND c.businessFk = ?`, [args.businessFk]); + JOIN postgresql.business b ON b.business_id = c.businessFk + JOIN postgresql.profile p ON p.profile_id = b.client_id + JOIN vn.person pe ON pe.id = p.person_id + WHERE c.dayOffTypeFk = 6 + AND pe.workerFk = ? + AND c.dated BETWEEN util.firstDayOfYear(CURDATE()) + AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 12-MONTH(NOW()) MONTH))`, [args.id]); const hasHalfHoliday = result[0].halfHolidayCounter > 0; const isHalfHoliday = args.absenceTypeId == 6; From 00a33fabb39775176addec37f82a5815a2305051 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 17 Nov 2021 14:38:12 +0100 Subject: [PATCH 4/5] update backTest --- modules/worker/back/methods/worker/specs/createAbsence.spec.js | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js index e5387c379..03467347a 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -79,6 +79,7 @@ describe('Worker createAbsence()', () => { const ctx = { req: {accessToken: {userId: 19}}, args: { + id: 1, businessFk: 1, absenceTypeId: 6, dated: new Date() From cdf7c20b163132a0942758999cd3ca50f2a7818a Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 22 Nov 2021 13:34:42 +0100 Subject: [PATCH 5/5] feat(worker_createAbsense): update backTest --- .../methods/worker/specs/createAbsence.spec.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js index 03467347a..0d6ebfc80 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -75,7 +75,7 @@ describe('Worker createAbsence()', () => { } }); - it('should return an error trying to create a "Half holiday" absence', async() => { + it(`should throw an error when adding a "Half holiday" absence if there's already one`, async() => { const ctx = { req: {accessToken: {userId: 19}}, args: { @@ -88,22 +88,18 @@ describe('Worker createAbsence()', () => { const tx = await app.models.Calendar.beginTransaction({}); + let error; try { const options = {transaction: tx}; - let error; - await app.models.Worker.createAbsence(ctx, workerId, options).catch(e => { - error = e; - }).finally(() => { - expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`); - }); - - expect(error).toBeDefined(); + await app.models.Worker.createAbsence(ctx, workerId, options); await tx.rollback(); } catch (e) { await tx.rollback(); - throw e; + error = e; } + + expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`); }); });