diff --git a/db/versions/11372-azureMedeola/00-firstScript.sql b/db/versions/11372-azureMedeola/00-firstScript.sql new file mode 100644 index 000000000..53295e9f8 --- /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','canCreateAbsenceInPast','WRITE','ALLOW','ROLE','hr'); diff --git a/loopback/locale/en.json b/loopback/locale/en.json index ff0281d4a..7372ac9a6 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -247,5 +247,5 @@ "The raid information is not correct": "The raid information is not correct", "Payment method is required": "Payment method is required", "Sales already moved": "Sales already moved", - "There are tickets to be invoiced": "There are tickets to be invoiced for this zone, please delete them first" + "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 e4bdd035e..0dc8e53a8 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -391,6 +391,7 @@ "Sales already moved": "Ya han sido transferidas", "The raid information is not correct": "La información de la redada no es correcta", "There are tickets to be invoiced": "Hay tickets para esta zona, borralos primero", - "An item type with the same code already exists": "Un tipo con el mismo código ya existe" + "An item type with the same code already exists": "Un tipo con el mismo código ya existe", + "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles" } 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, diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 419659083..e46ce9577 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -58,17 +58,24 @@ module.exports = Self => { 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 now = Date.vnNew(); + const newDate = new Date(args.dated).getTime(); + + if ((now.getTime() > newDate) && !canCreateAbsenceInPast) + throw new UserError(`Holidays to past days not available`); + const labour = await models.WorkerLabour.findById(args.businessFk, {fields: ['started', 'ended', 'businessFk']}, myOptions); if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended)) throw new UserError(`The contract was not active during the selected date`); - query = `SELECT * - FROM vn.workerTimeControl - WHERE userFk = ? AND timed BETWEEN DATE(?) AND CONCAT(DATE(?), ' 23:59:59') - LIMIT 1;`; - const [hasHoursRecorded] = await Self.rawSql(query, [id, args.dated, args.dated]); + const [hasHoursRecorded] = await Self.rawSql(`SELECT * + FROM vn.workerTimeControl + WHERE userFk = ? AND timed BETWEEN DATE(?) AND CONCAT(DATE(?), ' 23:59:59') + LIMIT 1;`, [id, args.dated, args.dated]); const absenceType = await models.AbsenceType.findById(args.absenceTypeId, null, myOptions); @@ -80,7 +87,6 @@ module.exports = Self => { throw new UserError(`The worker has hours recorded that day`); const date = Date.vnNew(); - const now = Date.vnNew(); date.setHours(0, 0, 0, 0); const [result] = await Self.rawSql( `SELECT COUNT(*) halfHolidayCounter 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`); + }); });