From 8d5cc93ddba0f966854ce05a1ecccf8343a76bac Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Tue, 23 Apr 2019 13:29:52 +0200 Subject: [PATCH] #1359 refactor absences --- db/dump/fixtures.sql | 2 +- .../05-ticket-module/12_descriptor.spec.js | 4 +- .../back/methods/worker-calendar/absences.js | 18 ++- .../worker-calendar/specs/absences.spec.js | 129 +++++++++++++++++- 4 files changed, 145 insertions(+), 8 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 1914310738..1d075939fa 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1273,7 +1273,7 @@ INSERT INTO `postgresql`.`profile`(`profile_id`, `person_id`, `profile_type_id`) FROM `postgresql`.`person` `p`; INSERT INTO `postgresql`.`business`(`business_id`, `client_id`, `provider_id`, `date_start`, `date_end`, `workerBusiness`, `reasonEndFk`) - SELECT p.profile_id, p.profile_id, 1000, DATE_ADD(CURDATE(), INTERVAL -15 DAY), DATE_ADD(CURDATE(), INTERVAL +6 MONTH), CONCAT('E-46-',RPAD(CONCAT(p.profile_id,9),8,p.profile_id)), NULL + SELECT p.profile_id, p.profile_id, 1000, CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL -1 YEAR)), '-12-31'), CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL +1 YEAR)), '-01-01'), CONCAT('E-46-',RPAD(CONCAT(p.profile_id,9),8,p.profile_id)), NULL FROM `postgresql`.`profile` `p`; INSERT INTO `postgresql`.`business_labour`(`business_id`, `notes`, `department_id`, `professional_category_id`, `incentivo`, `calendar_labour_type_id`, `porhoras`, `labour_agreement_id`, `workcenter_id`) diff --git a/e2e/paths/05-ticket-module/12_descriptor.spec.js b/e2e/paths/05-ticket-module/12_descriptor.spec.js index 1044fbd7ec..5d47d504d9 100644 --- a/e2e/paths/05-ticket-module/12_descriptor.spec.js +++ b/e2e/paths/05-ticket-module/12_descriptor.spec.js @@ -150,11 +150,11 @@ describe('Ticket descriptor path', () => { }); describe('Make invoice', () => { - it('should login as Invoicing role then search for a ticket', async() => { + it('should login as adminBoss role then search for a ticket', async() => { const invoiceableTicketId = 11; const url = await nightmare - .loginAndModule('developer', 'ticket') + .loginAndModule('adminBoss', 'ticket') .accessToSearchResult(invoiceableTicketId) .waitForURL('/summary') .parsedUrl(); diff --git a/modules/worker/back/methods/worker-calendar/absences.js b/modules/worker/back/methods/worker-calendar/absences.js index 1ca2128638..b6eae8a131 100644 --- a/modules/worker/back/methods/worker-calendar/absences.js +++ b/modules/worker/back/methods/worker-calendar/absences.js @@ -25,10 +25,12 @@ module.exports = Self => { arg: 'calendar' }, { - arg: 'absences' + arg: 'absences', + type: 'Number' }, { - arg: 'holidays' + arg: 'holidays', + type: 'Number' }], http: { path: `/absences`, @@ -147,8 +149,16 @@ module.exports = Self => { const startedTime = started.getTime(); const contractDays = Math.floor((endedTime - startedTime) / dayTimestamp); - if (contractDays < 365) - return Math.floor(contract.holidays().days * (contractDays + 1) / 365); + if (contractDays < 365) { + let holidays = contract.holidays().days * (contractDays + 1) / 365; + let integerPart = parseInt(holidays); + let decimalPart = holidays - integerPart; + let decimal = decimalPart >= 0.5 ? 0.5 : 0; + + holidays = integerPart + decimal; + + return holidays; + } return contract.holidays().days; } diff --git a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js index 526366d6ff..336f556361 100644 --- a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js +++ b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js @@ -1,6 +1,19 @@ const app = require('vn-loopback/server/server'); describe('Worker absences()', () => { + afterAll(async done => { + const hiredWorker = await app.models.WorkerLabour.findById(106); + + const endedDate = new Date(); + endedDate.setFullYear(endedDate.getFullYear() + 1); + endedDate.setHours(0, 0, 0, 0); + endedDate.setMonth(0); + endedDate.setDate(1); + + await hiredWorker.updateAttributes({ended: endedDate}); + done(); + }); + it('should get the absence calendar for the given dates then evaluate the type of absences', async() => { let ctx = {req: {accessToken: {userId: 106}}}; let workerFk = 106; @@ -20,7 +33,121 @@ describe('Worker absences()', () => { let calendar = result[0]; let absences = result[1]; - expect(calendar.totalHolidays).toEqual(21); + expect(calendar.totalHolidays).toEqual(27.5); + expect(calendar.holidaysEnjoyed).toEqual(5); + + let firstType = absences[0].absenceType().name; + let sixthType = absences[5].absenceType().name; + + expect(firstType).toEqual('Leave of absence'); + expect(sixthType).toEqual('Holidays'); + }); + + it(`should fire the worker 106 on July and see he/she has 13.75`, async() => { + const firedWorker = await app.models.WorkerLabour.findById(106); + + const endedDate = new Date(); + endedDate.setHours(0, 0, 0, 0); + endedDate.setMonth(5); + endedDate.setDate(31); + + await firedWorker.updateAttributes({ended: endedDate}); + + let ctx = {req: {accessToken: {userId: 106}}}; + let workerFk = 106; + + const started = new Date(); + started.setHours(0, 0, 0, 0); + started.setMonth(0); + started.setDate(1); + + const monthIndex = 11; + const ended = new Date(); + ended.setHours(0, 0, 0, 0); + ended.setMonth(monthIndex + 1); + ended.setDate(0); + + let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended); + let calendar = result[0]; + let absences = result[1]; + + expect(calendar.totalHolidays).toEqual(13.5); + expect(calendar.holidaysEnjoyed).toEqual(5); + + let firstType = absences[0].absenceType().name; + let sixthType = absences[5].absenceType().name; + + expect(firstType).toEqual('Leave of absence'); + expect(sixthType).toEqual('Holidays'); + }); + + it(`should fire the worker 106 on March and see he/she has 6.5`, async() => { + const firedWorker = await app.models.WorkerLabour.findById(106); + + const endedDate = new Date(); + endedDate.setHours(0, 0, 0, 0); + endedDate.setMonth(2); + endedDate.setDate(31); + + await firedWorker.updateAttributes({ended: endedDate}); + + let ctx = {req: {accessToken: {userId: 106}}}; + let workerFk = 106; + + const started = new Date(); + started.setHours(0, 0, 0, 0); + started.setMonth(0); + started.setDate(1); + + const monthIndex = 11; + const ended = new Date(); + ended.setHours(0, 0, 0, 0); + ended.setMonth(monthIndex + 1); + ended.setDate(0); + + let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended); + let calendar = result[0]; + let absences = result[1]; + + expect(calendar.totalHolidays).toEqual(6.5); + expect(calendar.holidaysEnjoyed).toEqual(5); + + let firstType = absences[0].absenceType().name; + let sixthType = absences[5].absenceType().name; + + expect(firstType).toEqual('Leave of absence'); + expect(sixthType).toEqual('Holidays'); + }); + + it(`should fire the worker 106 on january and see he/she has x`, async() => { + const firedWorker = await app.models.WorkerLabour.findById(106); + + const endedDate = new Date(); + endedDate.setHours(0, 0, 0, 0); + endedDate.setMonth(0); + endedDate.setDate(28); + + await firedWorker.updateAttributes({ended: endedDate}); + + let ctx = {req: {accessToken: {userId: 106}}}; + let workerFk = 106; + + const started = new Date(); + started.setHours(0, 0, 0, 0); + started.setMonth(0); + started.setDate(1); + + const monthIndex = 11; + const ended = new Date(); + ended.setHours(0, 0, 0, 0); + ended.setMonth(monthIndex + 1); + ended.setDate(0); + + let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended); + let calendar = result[0]; + let absences = result[1]; + + expect(calendar.totalHolidays).toEqual(2); expect(calendar.holidaysEnjoyed).toEqual(5); let firstType = absences[0].absenceType().name;