From e2c742c8934a41d584dc639d95f4e445ac486b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Mon, 10 Aug 2020 14:29:25 +0200 Subject: [PATCH] #2383 - Changed model --- db/changes/10200-normality/00-ACL.sql | 2 + ..._employee.sql => 00-calendar_employee.sql} | 0 .../00-zoneEstimatedDelivery.sql | 0 .../01-zoneETD.sql | 0 .../10200-normality/02-workerCalendar.sql | 12 ---- front/core/components/calendar/index.js | 5 +- .../{worker-calendar => calendar}/absences.js | 61 ++++++++++--------- .../specs/absences.spec.js | 6 +- .../back/methods/worker/createAbsence.js | 4 +- .../back/methods/worker/deleteAbsence.js | 2 +- .../worker/specs/createAbsence.spec.js | 4 +- .../worker/specs/deleteAbsence.spec.js | 6 +- .../worker/specs/updateAbsence.spec.js | 8 +-- .../back/methods/worker/updateAbsence.js | 4 +- modules/worker/back/model-config.json | 10 +-- modules/worker/back/models/calendar.js | 3 + .../{worker-calendar.json => calendar.json} | 6 +- modules/worker/back/models/worker-calendar.js | 3 - modules/worker/front/calendar/index.js | 2 +- modules/worker/front/calendar/index.spec.js | 6 +- modules/worker/front/time-control/index.js | 2 +- 21 files changed, 72 insertions(+), 74 deletions(-) create mode 100644 db/changes/10200-normality/00-ACL.sql rename db/changes/10200-normality/{01-calendar_employee.sql => 00-calendar_employee.sql} (100%) rename db/changes/{12200-Normalidad => 10200-normality}/00-zoneEstimatedDelivery.sql (100%) rename db/changes/{12200-Normalidad => 10200-normality}/01-zoneETD.sql (100%) delete mode 100644 db/changes/10200-normality/02-workerCalendar.sql rename modules/worker/back/methods/{worker-calendar => calendar}/absences.js (96%) rename modules/worker/back/methods/{worker-calendar => calendar}/specs/absences.spec.js (95%) create mode 100644 modules/worker/back/models/calendar.js rename modules/worker/back/models/{worker-calendar.json => calendar.json} (79%) delete mode 100644 modules/worker/back/models/worker-calendar.js diff --git a/db/changes/10200-normality/00-ACL.sql b/db/changes/10200-normality/00-ACL.sql new file mode 100644 index 000000000..ad9bc79d6 --- /dev/null +++ b/db/changes/10200-normality/00-ACL.sql @@ -0,0 +1,2 @@ +UPDATE `salix`.`ACL` SET `model` = 'Calendar' WHERE (`id` = '155'); +UPDATE `salix`.`ACL` SET `model` = 'Calendar' WHERE (`id` = '157'); diff --git a/db/changes/10200-normality/01-calendar_employee.sql b/db/changes/10200-normality/00-calendar_employee.sql similarity index 100% rename from db/changes/10200-normality/01-calendar_employee.sql rename to db/changes/10200-normality/00-calendar_employee.sql diff --git a/db/changes/12200-Normalidad/00-zoneEstimatedDelivery.sql b/db/changes/10200-normality/00-zoneEstimatedDelivery.sql similarity index 100% rename from db/changes/12200-Normalidad/00-zoneEstimatedDelivery.sql rename to db/changes/10200-normality/00-zoneEstimatedDelivery.sql diff --git a/db/changes/12200-Normalidad/01-zoneETD.sql b/db/changes/10200-normality/01-zoneETD.sql similarity index 100% rename from db/changes/12200-Normalidad/01-zoneETD.sql rename to db/changes/10200-normality/01-zoneETD.sql diff --git a/db/changes/10200-normality/02-workerCalendar.sql b/db/changes/10200-normality/02-workerCalendar.sql deleted file mode 100644 index 479470ac0..000000000 --- a/db/changes/10200-normality/02-workerCalendar.sql +++ /dev/null @@ -1,12 +0,0 @@ -USE `vn`; -CREATE - OR REPLACE ALGORITHM = UNDEFINED - DEFINER = `root`@`%` - SQL SECURITY DEFINER -VIEW `workerCalendar2` AS - SELECT - `ce`.`id` AS `id`, - `ce`.`business_id` AS `businessFk`, - `ce`.`calendar_state_id` AS `absenceTypeFk`, - `ce`.`date` AS `dated` - FROM `postgresql`.`calendar_employee` `ce`; diff --git a/front/core/components/calendar/index.js b/front/core/components/calendar/index.js index 02d2a4798..17bf52941 100644 --- a/front/core/components/calendar/index.js +++ b/front/core/components/calendar/index.js @@ -135,7 +135,10 @@ export default class Calendar extends FormInput { $days: [day], $type: 'day' }); - // this.repaint(); + + // Repaint only if 'selection' event is not listening + if (!this.$events || this.$events && !this.$events['selection']) + this.repaint(); } /* diff --git a/modules/worker/back/methods/worker-calendar/absences.js b/modules/worker/back/methods/calendar/absences.js similarity index 96% rename from modules/worker/back/methods/worker-calendar/absences.js rename to modules/worker/back/methods/calendar/absences.js index 7ea80509b..36d71ea81 100644 --- a/modules/worker/back/methods/worker-calendar/absences.js +++ b/modules/worker/back/methods/calendar/absences.js @@ -45,34 +45,6 @@ module.exports = Self => { const calendar = {totalHolidays: 0, holidaysEnjoyed: 0}; const holidays = []; - // Get absences of year - let absences = await Self.find({ - include: { - relation: 'absenceType' - }, - where: { - workerFk: workerFk, - dated: {between: [yearStarted, yearEnded]} - } - }); - - let entitlementRate = 0; - absences.forEach(absence => { - const absenceType = absence.absenceType(); - const isHoliday = absenceType.code === 'holiday'; - const isHalfHoliday = absenceType.code === 'halfHoliday'; - - if (isHoliday) - calendar.holidaysEnjoyed += 1; - if (isHalfHoliday) - calendar.holidaysEnjoyed += 0.5; - - entitlementRate += absenceType.holidayEntitlementRate; - - absence.dated = new Date(absence.dated); - absence.dated.setHours(0, 0, 0, 0); - }); - // Get active contracts on current year const year = yearStarted.getFullYear(); const contracts = await models.WorkerLabour.find({ @@ -112,6 +84,39 @@ module.exports = Self => { } }); + // Contracts ids + const contractsId = contracts.map(contract => { + return contract.businessFk; + }); + + // Get absences of year + let absences = await Self.find({ + include: { + relation: 'absenceType' + }, + where: { + businessFk: {inq: contractsId}, + dated: {between: [yearStarted, yearEnded]} + } + }); + + let entitlementRate = 0; + absences.forEach(absence => { + const absenceType = absence.absenceType(); + const isHoliday = absenceType.code === 'holiday'; + const isHalfHoliday = absenceType.code === 'halfHoliday'; + + if (isHoliday) + calendar.holidaysEnjoyed += 1; + if (isHalfHoliday) + calendar.holidaysEnjoyed += 0.5; + + entitlementRate += absenceType.holidayEntitlementRate; + + absence.dated = new Date(absence.dated); + absence.dated.setHours(0, 0, 0, 0); + }); + // Get number of worked days let workedDays = 0; contracts.forEach(contract => { diff --git a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js b/modules/worker/back/methods/calendar/specs/absences.spec.js similarity index 95% rename from modules/worker/back/methods/worker-calendar/specs/absences.spec.js rename to modules/worker/back/methods/calendar/specs/absences.spec.js index eb5865a17..5cd27127b 100644 --- a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js +++ b/modules/worker/back/methods/calendar/specs/absences.spec.js @@ -17,7 +17,7 @@ xdescribe('Worker absences()', () => { ended.setMonth(monthIndex + 1); ended.setDate(0); - let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended); + let result = await app.models.Calendar.absences(ctx, workerFk, started, ended); let calendar = result[0]; let absences = result[1]; @@ -54,7 +54,7 @@ xdescribe('Worker absences()', () => { ended.setMonth(monthIndex + 1); ended.setDate(0); - let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended); + let result = await app.models.Calendar.absences(ctx, workerFk, started, ended); let calendar = result[0]; let absences = result[1]; @@ -128,7 +128,7 @@ xdescribe('Worker absences()', () => { let ctx = {req: {accessToken: {userId: 106}}}; let workerFk = 106; - let result = await app.models.WorkerCalendar.absences(ctx, workerFk, yearStart, yearEnd); + let result = await app.models.Calendar.absences(ctx, workerFk, yearStart, yearEnd); let calendar = result[0]; let absences = result[1]; diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 28a2b9d9d..3163e697d 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -49,9 +49,9 @@ module.exports = Self => { } }); - return models.WorkerCalendar.create({ + return models.Calendar.create({ businessFk: labour.businessFk, - absenceTypeFk: absenceTypeId, + dayOffTypeFk: absenceTypeId, dated: dated }); }; diff --git a/modules/worker/back/methods/worker/deleteAbsence.js b/modules/worker/back/methods/worker/deleteAbsence.js index ea156d7eb..0fe8f7dc8 100644 --- a/modules/worker/back/methods/worker/deleteAbsence.js +++ b/modules/worker/back/methods/worker/deleteAbsence.js @@ -30,7 +30,7 @@ module.exports = Self => { if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); - const absence = await models.WorkerCalendar.findById(absenceId); + const absence = await models.Calendar.findById(absenceId); return absence.destroy(); }; diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js index 33bc2a80e..df48cf80b 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -5,7 +5,7 @@ describe('Worker createAbsence()', () => { let createdAbsence; afterAll(async() => { - const absence = await app.models.WorkerCalendar.findById(createdAbsence.id); + const absence = await app.models.Calendar.findById(createdAbsence.id); await absence.destroy(); }); @@ -34,6 +34,6 @@ describe('Worker createAbsence()', () => { const expectedAbsenceTypeId = 1; expect(createdAbsence.businessFk).toEqual(expectedBusinessId); - expect(createdAbsence.absenceTypeFk).toEqual(expectedAbsenceTypeId); + expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId); }); }); diff --git a/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js b/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js index c506ae86d..140edada8 100644 --- a/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js @@ -7,9 +7,9 @@ describe('Worker deleteAbsence()', () => { it('should return an error for a user without enough privileges', async() => { const ctx = {req: {accessToken: {userId: 106}}}; const businessId = 106; - createdAbsence = await app.models.WorkerCalendar.create({ + createdAbsence = await app.models.Calendar.create({ businessFk: businessId, - absenceTypeFk: 1, + dayOffTypeFk: 1, dated: new Date() }); @@ -31,7 +31,7 @@ describe('Worker deleteAbsence()', () => { await app.models.Worker.deleteAbsence(ctx, workerId, createdAbsence.id); - const deletedAbsence = await app.models.WorkerCalendar.findById(createdAbsence.id); + const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id); expect(deletedAbsence).toBeNull(); }); diff --git a/modules/worker/back/methods/worker/specs/updateAbsence.spec.js b/modules/worker/back/methods/worker/specs/updateAbsence.spec.js index 689d36136..1b34cf2e0 100644 --- a/modules/worker/back/methods/worker/specs/updateAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/updateAbsence.spec.js @@ -5,16 +5,16 @@ describe('Worker updateAbsence()', () => { let createdAbsence; afterAll(async() => { - const absence = await app.models.WorkerCalendar.findById(createdAbsence.id); + const absence = await app.models.Calendar.findById(createdAbsence.id); await absence.destroy(); }); it('should return an error for a user without enough privileges', async() => { const ctx = {req: {accessToken: {userId: 106}}}; const expectedAbsenceTypeId = 2; - createdAbsence = await app.models.WorkerCalendar.create({ + createdAbsence = await app.models.Calendar.create({ businessFk: 106, - absenceTypeFk: 1, + dayOffTypeFk: 1, dated: new Date() }); @@ -33,6 +33,6 @@ describe('Worker updateAbsence()', () => { const expectedAbsenceTypeId = 2; const updatedAbsence = await app.models.Worker.updateAbsence(ctx, workerId, createdAbsence.id, expectedAbsenceTypeId); - expect(updatedAbsence.absenceTypeFk).toEqual(expectedAbsenceTypeId); + expect(updatedAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId); }); }); diff --git a/modules/worker/back/methods/worker/updateAbsence.js b/modules/worker/back/methods/worker/updateAbsence.js index 719bca7e4..7ed8992d3 100644 --- a/modules/worker/back/methods/worker/updateAbsence.js +++ b/modules/worker/back/methods/worker/updateAbsence.js @@ -35,8 +35,8 @@ module.exports = Self => { if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); - const absence = await models.WorkerCalendar.findById(absenceId); + const absence = await models.Calendar.findById(absenceId); - return absence.updateAttribute('absenceTypeFk', absenceTypeId); + return absence.updateAttribute('dayOffTypeFk', absenceTypeId); }; }; diff --git a/modules/worker/back/model-config.json b/modules/worker/back/model-config.json index e91e8b0fc..7a498fce7 100644 --- a/modules/worker/back/model-config.json +++ b/modules/worker/back/model-config.json @@ -2,9 +2,9 @@ "AbsenceType": { "dataSource": "vn" }, - "Department": { + "Calendar": { "dataSource": "vn" - }, + }, "CalendarHoliday": { "dataSource": "vn" }, @@ -14,6 +14,9 @@ "CalendarHolidaysType": { "dataSource": "vn" }, + "Department": { + "dataSource": "vn" + }, "WorkCenter": { "dataSource": "vn" }, @@ -44,9 +47,6 @@ "WorkerDepartment": { "dataSource": "vn" }, - "WorkerCalendar": { - "dataSource": "vn" - }, "WorkerTimeControl": { "dataSource": "vn" }, diff --git a/modules/worker/back/models/calendar.js b/modules/worker/back/models/calendar.js new file mode 100644 index 000000000..7b2f593e4 --- /dev/null +++ b/modules/worker/back/models/calendar.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/calendar/absences')(Self); +}; diff --git a/modules/worker/back/models/worker-calendar.json b/modules/worker/back/models/calendar.json similarity index 79% rename from modules/worker/back/models/worker-calendar.json rename to modules/worker/back/models/calendar.json index ca802caa7..417442571 100644 --- a/modules/worker/back/models/worker-calendar.json +++ b/modules/worker/back/models/calendar.json @@ -1,9 +1,9 @@ { - "name": "WorkerCalendar", + "name": "Calendar", "base": "VnModel", "options": { "mysql": { - "table": "workerCalendar2" + "table": "calendar" } }, "properties": { @@ -22,7 +22,7 @@ "absenceType": { "type": "belongsTo", "model": "AbsenceType", - "foreignKey": "absenceTypeFk" + "foreignKey": "dayOffTypeFk" } } } diff --git a/modules/worker/back/models/worker-calendar.js b/modules/worker/back/models/worker-calendar.js deleted file mode 100644 index ea603af41..000000000 --- a/modules/worker/back/models/worker-calendar.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - require('../methods/worker-calendar/absences')(Self); -}; diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index e47335804..54cc94404 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -200,7 +200,7 @@ class Controller extends Section { started: this.started, ended: this.ended }; - return this.$http.get(`WorkerCalendars/absences`, {params}) + return this.$http.get(`Calendars/absences`, {params}) .then(res => this.onData(res.data)); } } diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js index 635bacca2..97ea89787 100644 --- a/modules/worker/front/calendar/index.spec.js +++ b/modules/worker/front/calendar/index.spec.js @@ -59,7 +59,7 @@ describe('Worker', () => { let yesterday = new Date(today.getTime()); yesterday.setDate(yesterday.getDate() - 1); - $httpBackend.whenRoute('GET', 'WorkerCalendars/absences') + $httpBackend.whenRoute('GET', 'Calendars/absences') .respond({ holidays: [ {dated: today, detail: {description: 'New year'}}, @@ -90,7 +90,7 @@ describe('Worker', () => { let today = new Date(); - $httpBackend.whenRoute('GET', 'WorkerCalendars/absences') + $httpBackend.whenRoute('GET', 'Calendars/absences') .respond({ absences: [ {dated: today, absenceType: {name: 'Holiday', rgb: '#000'}} @@ -316,7 +316,7 @@ describe('Worker', () => { const expecteResponse = [{id: 1}]; const expectedParams = {workerFk: 106, started: started, ended: ended}; const serializedParams = $httpParamSerializer(expectedParams); - $httpBackend.expect('GET', `WorkerCalendars/absences?${serializedParams}`).respond(200, expecteResponse); + $httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse); controller.refresh(); $httpBackend.flush(); diff --git a/modules/worker/front/time-control/index.js b/modules/worker/front/time-control/index.js index a9b4184b5..6b1db9e6d 100644 --- a/modules/worker/front/time-control/index.js +++ b/modules/worker/front/time-control/index.js @@ -95,7 +95,7 @@ class Controller extends Section { ended: this.ended }; - return this.$http.get(`WorkerCalendars/absences`, {params}) + return this.$http.get(`Calendars/absences`, {params}) .then(res => this.onData(res.data)); }