diff --git a/db/versions/11030-salmonCyca/00-firstScript.sql b/db/versions/11030-salmonCyca/00-firstScript.sql new file mode 100644 index 000000000..72ccc9ae2 --- /dev/null +++ b/db/versions/11030-salmonCyca/00-firstScript.sql @@ -0,0 +1,10 @@ +-- Place your SQL code here +ALTER TABLE vn.absenceType ADD isFestiveEligible BOOL DEFAULT 1 NOT NULL COMMENT 'Para marcar un tipo de absence'; + +UPDATE vn.absenceType +SET isFestiveEligible = 0 +WHERE code = 'holiday'; + +UPDATE vn.absenceType + SET isFestiveEligible=0 + WHERE code = 'halfHoliday'; diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 93a54393d..ca76eae42 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -226,4 +226,4 @@ "This password can only be changed by the user themselves": "This password can only be changed by the user themselves", "They're not your subordinate": "They're not your subordinate", "InvoiceIn is already booked": "InvoiceIn is already booked" -} \ No newline at end of file +} diff --git a/modules/client/back/models/business.json b/modules/client/back/models/business.json index 7ad2d307f..58e989ae0 100644 --- a/modules/client/back/models/business.json +++ b/modules/client/back/models/business.json @@ -10,6 +10,9 @@ "id": { "type": "number", "id": true + }, + "workcenterFk" : { + "type": "number" } }, "relations": { @@ -24,4 +27,4 @@ "foreignKey": "departmentFk" } } -} \ No newline at end of file +} diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index d628d0a2b..2de1d6e4d 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -98,6 +98,22 @@ module.exports = Self => { if (isHalfHoliday && hasHalfHoliday) throw new UserError(`Cannot add more than one '1/2 day vacation'`); + const isFestive = absenceType.isFestiveEligible; + + const workCenter = await models.Business.findOne({ + where: {id: args.businessFk} + },); + + const [holiday] = await models.CalendarHoliday.find({ + where: { + dated: args.dated, + workCenterFk: workCenter.workCenterFk + } + }); + + if (holiday && isFestive) + throw new UserError(`Cannot add holidays on this day`); + 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 346e43c51..aadaca99b 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -104,6 +104,35 @@ describe('Worker createAbsence()', () => { expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`); }); + it(`should throw an error when adding a "Holiday" absence if there's a festivity`, async() => { + const ctx = { + req: {accessToken: {userId: 9}}, + args: { + id: 3, + businessFk: 3, + absenceTypeId: 1, + dated: '2001-12-08T23:00:00.000Z' + } + }; + const workerId = 1; + + 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(`Cannot add holidays on this day`); + }); + it(`should throw an error when adding a absence if the worker has hours recorded that day and not is a half absence`, async() => { const ctx = { req: {accessToken: {userId: 19}}, diff --git a/modules/worker/back/models/absence-type.json b/modules/worker/back/models/absence-type.json index 7fc62f780..2f6103367 100644 --- a/modules/worker/back/models/absence-type.json +++ b/modules/worker/back/models/absence-type.json @@ -22,7 +22,10 @@ }, "holidayEntitlementRate": { "type": "number" - } + }, + "isFestiveEligible": { + "type": "boolean" + } }, "acls": [ { @@ -32,4 +35,4 @@ "permission": "ALLOW" } ] -} \ No newline at end of file +}