From 01b32d2b895b69bba84606141949e63b4a19c2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Wed, 8 Jul 2020 15:05:56 +0200 Subject: [PATCH] Calendar changes --- .../10200-normality/01-calendar_employee.sql | 5 + ...rkerCalendar.sql => 02-workerCalendar.sql} | 1 + front/core/components/calendar/index.js | 2 +- .../back/methods/worker/createAbsence.js | 56 +++++++++ .../back/methods/worker/createAbsences.js | 101 --------------- .../back/methods/worker/deleteAbsence.js | 35 ++++++ .../back/methods/worker/updateAbsence.js | 40 ++++++ .../worker/back/models/worker-calendar.json | 6 +- modules/worker/back/models/worker.js | 4 +- modules/worker/front/calendar/index.html | 57 +-------- modules/worker/front/calendar/index.js | 118 ++++++++++-------- 11 files changed, 213 insertions(+), 212 deletions(-) create mode 100644 db/changes/10200-normality/01-calendar_employee.sql rename db/changes/10200-normality/{00-workerCalendar.sql => 02-workerCalendar.sql} (92%) create mode 100644 modules/worker/back/methods/worker/createAbsence.js delete mode 100644 modules/worker/back/methods/worker/createAbsences.js create mode 100644 modules/worker/back/methods/worker/deleteAbsence.js create mode 100644 modules/worker/back/methods/worker/updateAbsence.js diff --git a/db/changes/10200-normality/01-calendar_employee.sql b/db/changes/10200-normality/01-calendar_employee.sql new file mode 100644 index 000000000..c5db9da2c --- /dev/null +++ b/db/changes/10200-normality/01-calendar_employee.sql @@ -0,0 +1,5 @@ +ALTER TABLE `postgresql`.`calendar_employee` +ADD COLUMN `id` INT NULL AUTO_INCREMENT FIRST, +ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE, +ADD INDEX `id_index` (`id` ASC) VISIBLE; +; diff --git a/db/changes/10200-normality/00-workerCalendar.sql b/db/changes/10200-normality/02-workerCalendar.sql similarity index 92% rename from db/changes/10200-normality/00-workerCalendar.sql rename to db/changes/10200-normality/02-workerCalendar.sql index 04b30394f..479470ac0 100644 --- a/db/changes/10200-normality/00-workerCalendar.sql +++ b/db/changes/10200-normality/02-workerCalendar.sql @@ -5,6 +5,7 @@ CREATE 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` diff --git a/front/core/components/calendar/index.js b/front/core/components/calendar/index.js index 96bc435db..02d2a4798 100644 --- a/front/core/components/calendar/index.js +++ b/front/core/components/calendar/index.js @@ -135,7 +135,7 @@ export default class Calendar extends FormInput { $days: [day], $type: 'day' }); - this.repaint(); + // this.repaint(); } /* diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js new file mode 100644 index 000000000..3f15b19c1 --- /dev/null +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -0,0 +1,56 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('createAbsence', { + description: 'Returns an array of absences from an specified worker', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The worker id', + http: {source: 'path'} + }, + { + arg: 'absenceTypeId', + type: 'Number', + required: true + }, + { + arg: 'dated', + type: 'Date', + required: false + }], + returns: { + type: 'Object', + root: true + }, + http: { + path: `/:id/createAbsence`, + verb: 'POST' + } + }); + + Self.createAbsence = async(ctx, id, absenceTypeId, dated) => { + const models = Self.app.models; + const isSubordinate = await models.Worker.isSubordinate(ctx, id); + + if (!isSubordinate) + throw new UserError(`You don't have enough privileges`); + + const labour = await models.WorkerLabour.findOne({ + where: { + and: [ + {workerFk: id}, + {or: [{ + ended: {gte: [dated]} + }, {ended: null}]} + ] + } + }); + + return models.WorkerCalendar.create({ + businessFk: labour.businessFk, + absenceTypeFk: absenceTypeId, + dated: dated + }); + }; +}; diff --git a/modules/worker/back/methods/worker/createAbsences.js b/modules/worker/back/methods/worker/createAbsences.js deleted file mode 100644 index e744d4796..000000000 --- a/modules/worker/back/methods/worker/createAbsences.js +++ /dev/null @@ -1,101 +0,0 @@ -const UserError = require('vn-loopback/util/user-error'); - -module.exports = Self => { - Self.remoteMethodCtx('createAbsences', { - description: 'Returns an array of absences from an specified worker', - accepts: [{ - arg: 'id', - type: 'Number', - description: 'The worker id', - http: {source: 'path'} - }, - { - arg: 'absenceTypeId', - type: 'Number', - required: true - }, - { - arg: 'dated', - type: 'Date', - required: false - }, - { - arg: 'started', - type: 'Date', - required: false - }, - { - arg: 'ended', - type: 'Date', - required: false - }, - { - arg: 'type', - type: 'String', - required: true - }], - returns: 'Object', - http: { - path: `/:id/createAbsences`, - verb: 'POST' - } - }); - - Self.createAbsences = async(ctx, id, absenceTypeId, dated, started, ended, type) => { - const models = Self.app.models; - const isSubordinate = await models.Worker.isSubordinate(ctx, id); - - if (!isSubordinate) - throw new UserError(`You don't have enough privileges`); - - const absences = []; - - if (type == 'day') { - const contracts = await models.WorkerLabour.find({ - where: { - and: [ - {workerFk: id}, - {or: [{ - ended: {gte: [yearStarted]} - }, {ended: null}]} - ] - } - }); - - absences.push({ - businessFk: 106, - absenceTypeFk: absenceTypeId, - dated: dated - }); - } else if (type == 'range') - console.log(true); - - return models.WorkerCalendar.create(absences); - }; -}; - -/* include: [{ - relation: 'holidays', - scope: { - where: {year} - } -}, -{ - relation: 'workCenter', - scope: { - include: { - relation: 'holidays', - scope: { - include: [{ - relation: 'detail' - }, - { - relation: 'type' - }], - where: { - dated: {between: [yearStarted, yearEnded]} - } - } - } - } -}], */ diff --git a/modules/worker/back/methods/worker/deleteAbsence.js b/modules/worker/back/methods/worker/deleteAbsence.js new file mode 100644 index 000000000..a5bdd2014 --- /dev/null +++ b/modules/worker/back/methods/worker/deleteAbsence.js @@ -0,0 +1,35 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('deleteAbsence', { + description: 'Returns an array of absences from an specified worker', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The worker id', + http: {source: 'path'} + }, + { + arg: 'absenceId', + type: 'Number', + required: true + }], + returns: 'Object', + http: { + path: `/:id/deleteAbsence`, + verb: 'DELETE' + } + }); + + Self.deleteAbsence = async(ctx, id, absenceId) => { + const models = Self.app.models; + const isSubordinate = await models.Worker.isSubordinate(ctx, id); + + if (!isSubordinate) + throw new UserError(`You don't have enough privileges`); + + const absence = await models.WorkerCalendar.findById(absenceId); + + return absence.destroy(); + }; +}; diff --git a/modules/worker/back/methods/worker/updateAbsence.js b/modules/worker/back/methods/worker/updateAbsence.js new file mode 100644 index 000000000..e129d228f --- /dev/null +++ b/modules/worker/back/methods/worker/updateAbsence.js @@ -0,0 +1,40 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('updateAbsence', { + description: 'Returns an array of absences from an specified worker', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The worker id', + http: {source: 'path'} + }, + { + arg: 'absenceId', + type: 'Number', + required: true + }, + { + arg: 'absenceTypeId', + type: 'Number', + required: true + }], + returns: 'Object', + http: { + path: `/:id/updateAbsence`, + verb: 'PATCH' + } + }); + + Self.updateAbsence = async(ctx, id, absenceId, absenceTypeId) => { + const models = Self.app.models; + const isSubordinate = await models.Worker.isSubordinate(ctx, id); + + if (!isSubordinate) + throw new UserError(`You don't have enough privileges`); + + const absence = await models.WorkerCalendar.findById(absenceId); + + return absence.updateAttribute('absenceTypeFk', absenceTypeId); + }; +}; diff --git a/modules/worker/back/models/worker-calendar.json b/modules/worker/back/models/worker-calendar.json index 109b93c77..ca802caa7 100644 --- a/modules/worker/back/models/worker-calendar.json +++ b/modules/worker/back/models/worker-calendar.json @@ -7,12 +7,14 @@ } }, "properties": { + "id": { + "id": true, + "type": "Number" + }, "businessFk": { - "id": 1, "type": "Number" }, "dated": { - "id": 3, "type": "Date" } }, diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index 5f97c3e10..0d94c788e 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -4,5 +4,7 @@ module.exports = Self => { require('../methods/worker/isSubordinate')(Self); require('../methods/worker/getWorkedHours')(Self); require('../methods/worker/uploadFile')(Self); - require('../methods/worker/createAbsences')(Self); + require('../methods/worker/createAbsence')(Self); + require('../methods/worker/deleteAbsence')(Self); + require('../methods/worker/updateAbsence')(Self); }; diff --git a/modules/worker/front/calendar/index.html b/modules/worker/front/calendar/index.html index 859da81b5..b77366328 100644 --- a/modules/worker/front/calendar/index.html +++ b/modules/worker/front/calendar/index.html @@ -13,7 +13,7 @@ display-controls="false" hide-contiguous="true" hide-year="true" - on-selection="$ctrl.onSelection($days, $type, $weekday)"> + on-selection="$ctrl.onSelection($days)"> @@ -38,61 +38,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - this.selected = null; - this.isNew = null; + const path = `Workers/${this.$params.id}/createAbsence`; + this.$http.post(path, params).then(res => { + this.responseHandler(() => { + const newEvent = res.data; + this.events[dated.getTime()] = { + name: absenceType.name, + color: absenceType.rgb, + type: absenceType.code, + absenceId: newEvent.id + }; this.refresh(); }); + }); + } + + edit(event) { + const absenceType = this.absenceType; + const params = { + absenceId: event.absenceId, + absenceTypeId: absenceType.id + }; + const path = `Workers/${this.$params.id}/updateAbsence`; + this.$http.patch(path, params).then( + this.responseHandler(() => { + event.color = absenceType.rgb; + event.name = absenceType.name; + event.code = absenceType.code; + this.refresh(); + }) + ); + } + + delete(day, event) { + const params = {absenceId: event.absenceId}; + const path = `Workers/${this.$params.id}/deleteAbsence`; + this.$http.delete(path, {params}).then( + this.responseHandler(() => { + delete this.events[day.getTime()]; + this.refresh(); + }) + ); + } + + responseHandler(cb) { + if (this.repaintCanceller) { + clearTimeout(this.repaintCanceller); + this.repaintCanceller = null; } - case 'delete': - return this.onDelete(this.selected.id) - .then(response => response == 'accept'); - } + + this.repaintCanceller = setTimeout( + () => cb(), 650); } refresh() {