From dd8e03dd0fa8fc6ee986a3d86cc70d75669370b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Tue, 7 Jul 2020 14:29:57 +0200 Subject: [PATCH 1/8] Added absence selection --- .../10200-normality/00-workerCalendar.sql | 11 ++ .../back/methods/worker/createAbsences.js | 101 ++++++++++++++++++ .../worker/back/models/worker-calendar.json | 11 +- modules/worker/back/models/worker.js | 1 + modules/worker/front/calendar/index.html | 69 +++++++++++- modules/worker/front/calendar/index.js | 89 +++++++++++++-- modules/worker/front/calendar/style.scss | 13 +++ 7 files changed, 274 insertions(+), 21 deletions(-) create mode 100644 db/changes/10200-normality/00-workerCalendar.sql create mode 100644 modules/worker/back/methods/worker/createAbsences.js diff --git a/db/changes/10200-normality/00-workerCalendar.sql b/db/changes/10200-normality/00-workerCalendar.sql new file mode 100644 index 000000000..04b30394f --- /dev/null +++ b/db/changes/10200-normality/00-workerCalendar.sql @@ -0,0 +1,11 @@ +USE `vn`; +CREATE + OR REPLACE ALGORITHM = UNDEFINED + DEFINER = `root`@`%` + SQL SECURITY DEFINER +VIEW `workerCalendar2` AS + SELECT + `ce`.`business_id` AS `businessFk`, + `ce`.`calendar_state_id` AS `absenceTypeFk`, + `ce`.`date` AS `dated` + FROM `postgresql`.`calendar_employee` `ce`; diff --git a/modules/worker/back/methods/worker/createAbsences.js b/modules/worker/back/methods/worker/createAbsences.js new file mode 100644 index 000000000..e744d4796 --- /dev/null +++ b/modules/worker/back/methods/worker/createAbsences.js @@ -0,0 +1,101 @@ +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/models/worker-calendar.json b/modules/worker/back/models/worker-calendar.json index 569d4d1ba..109b93c77 100644 --- a/modules/worker/back/models/worker-calendar.json +++ b/modules/worker/back/models/worker-calendar.json @@ -3,7 +3,7 @@ "base": "VnModel", "options": { "mysql": { - "table": "workerCalendar" + "table": "workerCalendar2" } }, "properties": { @@ -11,21 +11,12 @@ "id": 1, "type": "Number" }, - "workerFk": { - "id": 2, - "type": "Number" - }, "dated": { "id": 3, "type": "Date" } }, "relations": { - "worker": { - "type": "belongsTo", - "model": "Worker", - "foreignKey": "workerFk" - }, "absenceType": { "type": "belongsTo", "model": "AbsenceType", diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index 692c8c735..5f97c3e10 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -4,4 +4,5 @@ module.exports = Self => { require('../methods/worker/isSubordinate')(Self); require('../methods/worker/getWorkedHours')(Self); require('../methods/worker/uploadFile')(Self); + require('../methods/worker/createAbsences')(Self); }; diff --git a/modules/worker/front/calendar/index.html b/modules/worker/front/calendar/index.html index 08b3f469c..859da81b5 100644 --- a/modules/worker/front/calendar/index.html +++ b/modules/worker/front/calendar/index.html @@ -12,7 +12,8 @@ format-day="$ctrl.formatDay($day, $element)" display-controls="false" hide-contiguous="true" - hide-year="true"> + hide-year="true" + on-selection="$ctrl.onSelection($days, $type, $weekday)"> @@ -26,12 +27,74 @@
- + + {{absenceType.name}}
- \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index 6b849d19f..0a7f591db 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -43,15 +43,8 @@ class Controller extends Section { set worker(value) { this._worker = value; - if (!value) return; - let params = { - workerFk: this.worker.id, - started: this.started, - ended: this.ended - }; - this.$http.get(`WorkerCalendars/absences`, {params}) - .then(res => this.onData(res.data)); + if (value) this.refresh(); } onData(data) { @@ -102,6 +95,86 @@ class Controller extends Section { dayNumber.style.backgroundColor = event.color; dayNumber.style.color = 'rgba(0, 0, 0, 0.7)'; } + + pick(absenceType) { + if (absenceType == this.absenceType) + absenceType = null; + + this.absenceType = absenceType; + } + + onSelection($days, $type, $weekday) { + if (!this.absenceType) + return this.vnApp.showMessage(this.$t('Choose an absence type')); + + let $events = []; + for (let day of $days) { + const stamp = day.getTime(); + $events = $events.concat(this.events[stamp] || []); + } + + if ($events.length) + this.edit($events[0]); + else + this.create($type, $days, $weekday); + } + + create(type, days, weekday) { + this.isNew = true; + this.selected = { + type: 'day', + dated: days[0] + }; + + this.$.dialog.show(); + } + + edit() { + this.isNew = false; + } + + onAbsenceResponse(response) { + switch (response) { + case 'accept': { + const selected = this.selected; + const params = Object.assign(selected, { + absenceTypeId: this.absenceType.id + }); + + /* const selected = { + businessFk: 106, + absenceTypeFk: this.absenceType.id, + dated: $days[0] + }; */ + + let req; + if (this.isNew) { + const path = `Workers/${this.$params.id}/createAbsences`; + req = this.$http.post(path, params); + } else + req = this.$http.put(`${this.path}/${selected.id}`, selected); + + return req.then(() => { + this.selected = null; + this.isNew = null; + this.refresh(); + }); + } + case 'delete': + return this.onDelete(this.selected.id) + .then(response => response == 'accept'); + } + } + + refresh() { + const params = { + workerFk: this.worker.id, + started: this.started, + ended: this.ended + }; + this.$http.get(`WorkerCalendars/absences`, {params}) + .then(res => this.onData(res.data)); + } } ngModule.component('vnWorkerCalendar', { diff --git a/modules/worker/front/calendar/style.scss b/modules/worker/front/calendar/style.scss index 9b3fc749b..1c22fdfb0 100644 --- a/modules/worker/front/calendar/style.scss +++ b/modules/worker/front/calendar/style.scss @@ -16,4 +16,17 @@ vn-worker-calendar { max-width: 288px; } } + + vn-chip.selectable { + cursor: pointer + } + + vn-chip.selectable:hover { + opacity: 0.8 + } + + vn-chip vn-avatar { + text-align: center; + color: white + } } 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 2/8] 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() { From 91e739547bdf96bb85ff296a4b153649bec78104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Sat, 11 Jul 2020 18:30:27 +0200 Subject: [PATCH 3/8] Changes --- modules/worker/front/calendar/index.html | 25 +++--- modules/worker/front/calendar/index.js | 87 +++++++++++---------- modules/worker/front/calendar/locale/es.yml | 3 +- 3 files changed, 61 insertions(+), 54 deletions(-) diff --git a/modules/worker/front/calendar/index.html b/modules/worker/front/calendar/index.html index b77366328..55f22a931 100644 --- a/modules/worker/front/calendar/index.html +++ b/modules/worker/front/calendar/index.html @@ -4,17 +4,20 @@ auto-load="true">
- - - + + + + +
diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index 0b43eefc0..5e83e561a 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -44,7 +44,8 @@ class Controller extends Section { set worker(value) { this._worker = value; - if (value) this.refresh(); + if (value) + this.refresh().then(() => this.repaint()); } onData(data) { @@ -78,8 +79,6 @@ class Controller extends Section { }); }); } - - this.repaint(); } repaint() { @@ -105,24 +104,25 @@ class Controller extends Section { this.absenceType = absenceType; } - onSelection($days) { + onSelection($event, $days) { if (!this.absenceType) - return this.vnApp.showMessage(this.$t('Choose an absence type')); + return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu')); const day = $days[0]; const stamp = day.getTime(); const event = this.events[stamp]; + const calendar = $event.target.closest('vn-calendar').$ctrl; if (event) { if (event.type == this.absenceType.code) - this.delete(day, event); + this.delete(calendar, day, event); else - this.edit(event); + this.edit(calendar, event); } else - this.create(day); + this.create(calendar, day); } - create(dated) { + create(calendar, dated) { const absenceType = this.absenceType; const params = { dated: dated, @@ -131,55 +131,58 @@ class Controller extends Section { 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(); - }); + const newEvent = res.data; + this.events[dated.getTime()] = { + name: absenceType.name, + color: absenceType.rgb, + type: absenceType.code, + absenceId: newEvent.id + }; + + this.repaintCanceller(() => + this.refresh().then(calendar.repaint()) + ); }); } - edit(event) { + edit(calendar, 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(); - }) - ); + this.$http.patch(path, params).then(() => { + event.color = absenceType.rgb; + event.name = absenceType.name; + event.code = absenceType.code; + + this.repaintCanceller(() => + this.refresh().then(calendar.repaint()) + ); + }); } - delete(day, event) { + delete(calendar, 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(); - }) - ); + this.$http.delete(path, {params}).then(() => { + delete this.events[day.getTime()]; + + this.repaintCanceller(() => + this.refresh().then(calendar.repaint()) + ); + }); } - responseHandler(cb) { - if (this.repaintCanceller) { - clearTimeout(this.repaintCanceller); - this.repaintCanceller = null; + repaintCanceller(cb) { + if (this.canceller) { + clearTimeout(this.canceller); + this.canceller = null; } - this.repaintCanceller = setTimeout( - () => cb(), 650); + this.canceller = setTimeout( + () => cb(), 500); } refresh() { @@ -188,7 +191,7 @@ class Controller extends Section { started: this.started, ended: this.ended }; - this.$http.get(`WorkerCalendars/absences`, {params}) + return this.$http.get(`WorkerCalendars/absences`, {params}) .then(res => this.onData(res.data)); } } diff --git a/modules/worker/front/calendar/locale/es.yml b/modules/worker/front/calendar/locale/es.yml index 82939ce91..85a2c5431 100644 --- a/modules/worker/front/calendar/locale/es.yml +++ b/modules/worker/front/calendar/locale/es.yml @@ -2,4 +2,5 @@ Calendar: Calendario Holidays: Vacaciones Used: Utilizados of: de -days: días \ No newline at end of file +days: días +Choose an absence type from the right menu: Elige un tipo de ausencia desde el menú de la derecha \ No newline at end of file From 2f9d80be7a8269f43d51720070f94b1a6cfa34e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Wed, 15 Jul 2020 11:35:34 +0200 Subject: [PATCH 4/8] Added ACL + unit tests --- modules/worker/front/calendar/index.html | 6 +-- modules/worker/front/calendar/index.js | 11 +++- modules/worker/front/calendar/index.spec.js | 57 ++++++++++++++++++++- modules/worker/front/calendar/locale/es.yml | 3 +- modules/worker/front/calendar/style.scss | 7 +++ 5 files changed, 78 insertions(+), 6 deletions(-) diff --git a/modules/worker/front/calendar/index.html b/modules/worker/front/calendar/index.html index 55f22a931..197fb0797 100644 --- a/modules/worker/front/calendar/index.html +++ b/modules/worker/front/calendar/index.html @@ -4,8 +4,8 @@ auto-load="true">
- - +
- diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index 5e83e561a..b1fce0adb 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -44,8 +44,16 @@ class Controller extends Section { set worker(value) { this._worker = value; - if (value) + if (value) { this.refresh().then(() => this.repaint()); + this.getIsSubordinate(); + } + } + + getIsSubordinate() { + this.$http.get(`Workers/${this.worker.id}/isSubordinate`).then(res => + this.isSubordinate = res.data + ); } onData(data) { @@ -98,6 +106,7 @@ class Controller extends Section { } pick(absenceType) { + if (!this.isSubordinate) return; if (absenceType == this.absenceType) absenceType = null; diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js index da49b8f0f..a0589b632 100644 --- a/modules/worker/front/calendar/index.spec.js +++ b/modules/worker/front/calendar/index.spec.js @@ -12,8 +12,9 @@ describe('Worker', () => { beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; - const $element = angular.element(''); + const $element = angular.element(''); controller = $componentController('vnWorkerCalendar', {$element, $scope}); + controller.isSubordinate = true; })); describe('started property', () => { @@ -44,6 +45,8 @@ describe('Worker', () => { describe('worker() setter', () => { it(`should perform a get query and set the reponse data on the model`, () => { + jest.spyOn(controller, 'getIsSubordinate').mockReturnThis(); + let today = new Date(); let tomorrow = new Date(today.getTime()); @@ -73,11 +76,14 @@ describe('Worker', () => { expect(events[tomorrow.getTime()].name).toEqual('Easter'); expect(events[yesterday.getTime()].name).toEqual('Leave'); expect(events[yesterday.getTime()].color).toEqual('#bbb'); + expect(controller.getIsSubordinate).toHaveBeenCalledWith(); }); }); describe('formatDay()', () => { it(`should set the day element style`, () => { + jest.spyOn(controller, 'getIsSubordinate').mockReturnThis(); + let today = new Date(); $httpBackend.whenRoute('GET', 'WorkerCalendars/absences') @@ -99,5 +105,54 @@ describe('Worker', () => { expect(dayNumber.style.backgroundColor).toEqual('rgb(0, 0, 0)'); }); }); + + describe('pick()', () => { + it(`should set the absenceType property to null if they match with the current one`, () => { + const absenceType = {id: 1, name: 'Holiday'}; + controller.absenceType = absenceType; + controller.pick(absenceType); + + expect(controller.absenceType).toBeNull(); + }); + + it(`should set the absenceType property`, () => { + const absenceType = {id: 1, name: 'Holiday'}; + const expectedAbsence = {id: 2, name: 'Leave of absence'}; + controller.absenceType = absenceType; + controller.pick(expectedAbsence); + + expect(controller.absenceType).toEqual(expectedAbsence); + }); + }); + + describe('onSelection()', () => { + it(`should show an snackbar message if no absence type is selected`, () => { + jest.spyOn(controller.vnApp, 'showMessage').mockReturnThis(); + + const $event = {}; + const $days = []; + controller.onSelection($event, $days); + + expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu'); + }); + + it(`should call to the create() method`, () => { + jest.spyOn(controller, 'create').mockReturnThis(); + + const selectedDay = new Date(); + const $event = { + target: { + closest: () => { + return {$ctrl: {}}; + } + } + }; + const $days = [selectedDay]; + controller.absenceType = {id: 1}; + controller.onSelection($event, $days); + + expect(controller.create).toHaveBeenCalledWith(jasmine.any(Object), selectedDay); + }); + }); }); }); diff --git a/modules/worker/front/calendar/locale/es.yml b/modules/worker/front/calendar/locale/es.yml index 85a2c5431..6681f730f 100644 --- a/modules/worker/front/calendar/locale/es.yml +++ b/modules/worker/front/calendar/locale/es.yml @@ -3,4 +3,5 @@ Holidays: Vacaciones Used: Utilizados of: de days: días -Choose an absence type from the right menu: Elige un tipo de ausencia desde el menú de la derecha \ No newline at end of file +Choose an absence type from the right menu: Elige un tipo de ausencia desde el menú de la derecha +To start adding absences, click an absence type from the right menu and then on the day you want to add an absence: Para empezar a añadir ausencias, haz clic en un tipo de ausencia desde el menu de la derecha y después en el día que quieres añadir la ausencia \ No newline at end of file diff --git a/modules/worker/front/calendar/style.scss b/modules/worker/front/calendar/style.scss index 1c22fdfb0..1934a6ca0 100644 --- a/modules/worker/front/calendar/style.scss +++ b/modules/worker/front/calendar/style.scss @@ -2,6 +2,7 @@ vn-worker-calendar { .calendars { + position: relative; display: flex; flex-wrap: wrap; justify-content: center; @@ -29,4 +30,10 @@ vn-worker-calendar { text-align: center; color: white } + + vn-icon[icon="info"] { + position: absolute; + top: 16px; + right: 16px + } } From bbf3b7200142a350e993839b3be1d3830aabd553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 16 Jul 2020 10:04:16 +0200 Subject: [PATCH 5/8] Added front & back tests --- .../back/methods/worker/createAbsence.js | 4 +- .../back/methods/worker/deleteAbsence.js | 4 +- .../worker/specs/createAbsence.spec.js | 39 ++++ .../worker/specs/deleteAbsence.spec.js | 38 ++++ .../worker/specs/updateAbsence.spec.js | 38 ++++ .../back/methods/worker/updateAbsence.js | 4 +- modules/worker/front/calendar/index.js | 2 +- modules/worker/front/calendar/index.spec.js | 186 +++++++++++++++++- 8 files changed, 310 insertions(+), 5 deletions(-) create mode 100644 modules/worker/back/methods/worker/specs/createAbsence.spec.js create mode 100644 modules/worker/back/methods/worker/specs/deleteAbsence.spec.js create mode 100644 modules/worker/back/methods/worker/specs/updateAbsence.spec.js diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 3f15b19c1..9c65aa66a 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -31,9 +31,11 @@ module.exports = Self => { Self.createAbsence = async(ctx, id, absenceTypeId, dated) => { const models = Self.app.models; + const userId = ctx.req.accessToken.userId; const isSubordinate = await models.Worker.isSubordinate(ctx, id); + const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); - if (!isSubordinate) + if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); const labour = await models.WorkerLabour.findOne({ diff --git a/modules/worker/back/methods/worker/deleteAbsence.js b/modules/worker/back/methods/worker/deleteAbsence.js index a5bdd2014..d5dbfa00d 100644 --- a/modules/worker/back/methods/worker/deleteAbsence.js +++ b/modules/worker/back/methods/worker/deleteAbsence.js @@ -23,9 +23,11 @@ module.exports = Self => { Self.deleteAbsence = async(ctx, id, absenceId) => { const models = Self.app.models; + const userId = ctx.req.accessToken.userId; const isSubordinate = await models.Worker.isSubordinate(ctx, id); + const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); - if (!isSubordinate) + if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); const absence = await models.WorkerCalendar.findById(absenceId); diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js new file mode 100644 index 000000000..33bc2a80e --- /dev/null +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -0,0 +1,39 @@ +const app = require('vn-loopback/server/server'); + +describe('Worker createAbsence()', () => { + const workerId = 106; + let createdAbsence; + + afterAll(async() => { + const absence = await app.models.WorkerCalendar.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 absenceTypeId = 1; + const dated = new Date(); + + let error; + await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`You don't have enough privileges`); + }); + + expect(error).toBeDefined(); + }); + + it('should create a new absence', async() => { + const ctx = {req: {accessToken: {userId: 37}}}; + const absenceTypeId = 1; + const dated = new Date(); + createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated); + + const expectedBusinessId = 106; + const expectedAbsenceTypeId = 1; + + expect(createdAbsence.businessFk).toEqual(expectedBusinessId); + expect(createdAbsence.absenceTypeFk).toEqual(expectedAbsenceTypeId); + }); +}); diff --git a/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js b/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js new file mode 100644 index 000000000..c506ae86d --- /dev/null +++ b/modules/worker/back/methods/worker/specs/deleteAbsence.spec.js @@ -0,0 +1,38 @@ +const app = require('vn-loopback/server/server'); + +describe('Worker deleteAbsence()', () => { + const workerId = 106; + let createdAbsence; + + 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({ + businessFk: businessId, + absenceTypeFk: 1, + dated: new Date() + }); + + let error; + await app.models.Worker.deleteAbsence(ctx, workerId, createdAbsence.id).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`You don't have enough privileges`); + }); + + expect(error).toBeDefined(); + }); + + it('should create a new absence', async() => { + const ctx = {req: {accessToken: {userId: 37}}}; + const businessId = 106; + + expect(createdAbsence.businessFk).toEqual(businessId); + + await app.models.Worker.deleteAbsence(ctx, workerId, createdAbsence.id); + + const deletedAbsence = await app.models.WorkerCalendar.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 new file mode 100644 index 000000000..689d36136 --- /dev/null +++ b/modules/worker/back/methods/worker/specs/updateAbsence.spec.js @@ -0,0 +1,38 @@ +const app = require('vn-loopback/server/server'); + +describe('Worker updateAbsence()', () => { + const workerId = 106; + let createdAbsence; + + afterAll(async() => { + const absence = await app.models.WorkerCalendar.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({ + businessFk: 106, + absenceTypeFk: 1, + dated: new Date() + }); + + let error; + await app.models.Worker.updateAbsence(ctx, workerId, createdAbsence.id, expectedAbsenceTypeId).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`You don't have enough privileges`); + }); + + expect(error).toBeDefined(); + }); + + it('should create a new absence', async() => { + const ctx = {req: {accessToken: {userId: 37}}}; + const expectedAbsenceTypeId = 2; + const updatedAbsence = await app.models.Worker.updateAbsence(ctx, workerId, createdAbsence.id, expectedAbsenceTypeId); + + expect(updatedAbsence.absenceTypeFk).toEqual(expectedAbsenceTypeId); + }); +}); diff --git a/modules/worker/back/methods/worker/updateAbsence.js b/modules/worker/back/methods/worker/updateAbsence.js index e129d228f..e35eafa0c 100644 --- a/modules/worker/back/methods/worker/updateAbsence.js +++ b/modules/worker/back/methods/worker/updateAbsence.js @@ -28,9 +28,11 @@ module.exports = Self => { Self.updateAbsence = async(ctx, id, absenceId, absenceTypeId) => { const models = Self.app.models; + const userId = ctx.req.accessToken.userId; const isSubordinate = await models.Worker.isSubordinate(ctx, id); + const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); - if (!isSubordinate) + if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) throw new UserError(`You don't have enough privileges`); const absence = await models.WorkerCalendar.findById(absenceId); diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index b1fce0adb..32adc272d 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -164,7 +164,7 @@ class Controller extends Section { this.$http.patch(path, params).then(() => { event.color = absenceType.rgb; event.name = absenceType.name; - event.code = absenceType.code; + event.type = absenceType.code; this.repaintCanceller(() => this.refresh().then(calendar.repaint()) diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js index a0589b632..9ec4166d9 100644 --- a/modules/worker/front/calendar/index.spec.js +++ b/modules/worker/front/calendar/index.spec.js @@ -3,20 +3,39 @@ import './index'; describe('Worker', () => { describe('Component vnWorkerCalendar', () => { let $httpBackend; + let $httpParamSerializer; let $scope; let controller; let year = new Date().getFullYear(); beforeEach(ngModule('worker')); - beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpParamSerializer_, _$httpBackend_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; const $element = angular.element(''); controller = $componentController('vnWorkerCalendar', {$element, $scope}); controller.isSubordinate = true; + controller.absenceType = {id: 1, name: 'Holiday', code: 'holiday', rgb: 'red'}; + controller.$params.id = 106; + controller._worker = {id: 106}; })); + describe('worker() setter', () => { + it(`should set the data an then call the refresh() and getIsSubordinate() methods`, () => { + jest.spyOn(controller, 'refresh').mockReturnValue(new Promise(resolve => { + return resolve(); + })); + jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true); + + controller.worker = {id: 107}; + + expect(controller.refresh).toHaveBeenCalledWith(); + expect(controller.getIsSubordinate).toHaveBeenCalledWith(); + }); + }); + describe('started property', () => { it(`should return first day and month of current year`, () => { let started = new Date(year, 0, 1); @@ -131,6 +150,7 @@ describe('Worker', () => { const $event = {}; const $days = []; + controller.absenceType = null; controller.onSelection($event, $days); expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu'); @@ -153,6 +173,170 @@ describe('Worker', () => { expect(controller.create).toHaveBeenCalledWith(jasmine.any(Object), selectedDay); }); + + it(`should call to the delete() method`, () => { + jest.spyOn(controller, 'delete').mockReturnThis(); + + const selectedDay = new Date(); + const expectedEvent = { + dated: selectedDay, + type: 'holiday' + }; + const $event = { + target: { + closest: () => { + return {$ctrl: {}}; + } + } + }; + const $days = [selectedDay]; + controller.events[selectedDay.getTime()] = expectedEvent; + controller.absenceType = {id: 1, code: 'holiday'}; + controller.onSelection($event, $days); + + expect(controller.delete).toHaveBeenCalledWith(jasmine.any(Object), selectedDay, expectedEvent); + }); + + it(`should call to the edit() method`, () => { + jest.spyOn(controller, 'edit').mockReturnThis(); + + const selectedDay = new Date(); + const expectedEvent = { + dated: selectedDay, + type: 'leaveOfAbsence' + }; + const $event = { + target: { + closest: () => { + return {$ctrl: {}}; + } + } + }; + const $days = [selectedDay]; + controller.events[selectedDay.getTime()] = expectedEvent; + controller.absenceType = {id: 1, code: 'holiday'}; + controller.onSelection($event, $days); + + expect(controller.edit).toHaveBeenCalledWith(jasmine.any(Object), expectedEvent); + }); + }); + + describe('create()', () => { + it(`should make a HTTP POST query and then call to the repaintCanceller() method`, () => { + jest.spyOn(controller, 'repaintCanceller').mockReturnThis(); + + const dated = new Date(); + const calendarElement = {}; + const expectedResponse = {id: 10}; + + $httpBackend.expect('POST', `Workers/106/createAbsence`).respond(200, expectedResponse); + controller.create(calendarElement, dated); + $httpBackend.flush(); + + const createdEvent = controller.events[dated.getTime()]; + const absenceType = controller.absenceType; + + expect(createdEvent.absenceId).toEqual(expectedResponse.id); + expect(createdEvent.color).toEqual(absenceType.rgb); + expect(controller.repaintCanceller).toHaveBeenCalled(); + }); + }); + + describe('edit()', () => { + it(`should make a HTTP PATCH query and then call to the repaintCanceller() method`, () => { + jest.spyOn(controller, 'repaintCanceller').mockReturnThis(); + + const event = {absenceId: 10}; + const calendarElement = {}; + const newAbsenceType = { + id: 2, + name: 'Leave of absence', + code: 'leaveOfAbsence', + rgb: 'purple' + }; + controller.absenceType = newAbsenceType; + + const expectedParams = {absenceId: 10, absenceTypeId: 2}; + $httpBackend.expect('PATCH', `Workers/106/updateAbsence`, expectedParams).respond(200); + controller.edit(calendarElement, event); + $httpBackend.flush(); + + expect(event.name).toEqual(newAbsenceType.name); + expect(event.color).toEqual(newAbsenceType.rgb); + expect(event.type).toEqual(newAbsenceType.code); + expect(controller.repaintCanceller).toHaveBeenCalled(); + }); + }); + + describe('delete()', () => { + it(`should make a HTTP DELETE query and then call to the repaintCanceller() method`, () => { + jest.spyOn(controller, 'repaintCanceller').mockReturnThis(); + + const expectedParams = {absenceId: 10}; + const calendarElement = {}; + const selectedDay = new Date(); + const expectedEvent = { + dated: selectedDay, + type: 'leaveOfAbsence', + absenceId: 10 + }; + + controller.events[selectedDay.getTime()] = expectedEvent; + + const serializedParams = $httpParamSerializer(expectedParams); + $httpBackend.expect('DELETE', `Workers/106/deleteAbsence?${serializedParams}`).respond(200); + controller.delete(calendarElement, selectedDay, expectedEvent); + $httpBackend.flush(); + + const event = controller.events[selectedDay.getTime()]; + + expect(event).toBeUndefined(); + expect(controller.repaintCanceller).toHaveBeenCalled(); + }); + }); + + describe('repaintCanceller()', () => { + it(`should cancell the callback execution timer`, () => { + jest.spyOn(window, 'clearTimeout'); + jest.spyOn(window, 'setTimeout'); + + const timeoutId = 90; + controller.canceller = timeoutId; + + controller.repaintCanceller(() => { + return 'My callback'; + }); + + expect(window.clearTimeout).toHaveBeenCalledWith(timeoutId); + expect(window.setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 500); + }); + }); + + describe('refresh()', () => { + it(`should make a HTTP GET query and then call to the onData() method`, () => { + jest.spyOn(controller, 'onData').mockReturnThis(); + + const dated = controller.date; + const started = new Date(dated.getTime()); + started.setMonth(0); + started.setDate(1); + + const ended = new Date(dated.getTime()); + ended.setMonth(12); + ended.setDate(0); + + controller.started = started; + controller.ended = ended; + + 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); + controller.refresh(); + $httpBackend.flush(); + + expect(controller.onData).toHaveBeenCalledWith(expecteResponse); + }); }); }); }); From 87fa66fe8c8949031e8f2f726e3233ece5e9bdb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 16 Jul 2020 11:45:44 +0200 Subject: [PATCH 6/8] Updated methods description --- modules/worker/back/methods/worker/createAbsence.js | 2 +- modules/worker/back/methods/worker/deleteAbsence.js | 2 +- modules/worker/back/methods/worker/updateAbsence.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 9c65aa66a..28a2b9d9d 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('createAbsence', { - description: 'Returns an array of absences from an specified worker', + description: 'Creates a new worker absence', accepts: [{ arg: 'id', type: 'Number', diff --git a/modules/worker/back/methods/worker/deleteAbsence.js b/modules/worker/back/methods/worker/deleteAbsence.js index d5dbfa00d..ea156d7eb 100644 --- a/modules/worker/back/methods/worker/deleteAbsence.js +++ b/modules/worker/back/methods/worker/deleteAbsence.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('deleteAbsence', { - description: 'Returns an array of absences from an specified worker', + description: 'Deletes a worker absence', accepts: [{ arg: 'id', type: 'Number', diff --git a/modules/worker/back/methods/worker/updateAbsence.js b/modules/worker/back/methods/worker/updateAbsence.js index e35eafa0c..719bca7e4 100644 --- a/modules/worker/back/methods/worker/updateAbsence.js +++ b/modules/worker/back/methods/worker/updateAbsence.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('updateAbsence', { - description: 'Returns an array of absences from an specified worker', + description: 'Updates a worker absence', accepts: [{ arg: 'id', type: 'Number', From cbb2ad7f44032cbf96f5b190736dc527c0c8b7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 16 Jul 2020 11:59:16 +0200 Subject: [PATCH 7/8] Removed duplicated test --- modules/worker/front/calendar/index.spec.js | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js index 9ec4166d9..07e574d09 100644 --- a/modules/worker/front/calendar/index.spec.js +++ b/modules/worker/front/calendar/index.spec.js @@ -22,20 +22,6 @@ describe('Worker', () => { controller._worker = {id: 106}; })); - describe('worker() setter', () => { - it(`should set the data an then call the refresh() and getIsSubordinate() methods`, () => { - jest.spyOn(controller, 'refresh').mockReturnValue(new Promise(resolve => { - return resolve(); - })); - jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true); - - controller.worker = {id: 107}; - - expect(controller.refresh).toHaveBeenCalledWith(); - expect(controller.getIsSubordinate).toHaveBeenCalledWith(); - }); - }); - describe('started property', () => { it(`should return first day and month of current year`, () => { let started = new Date(year, 0, 1); @@ -64,7 +50,10 @@ describe('Worker', () => { describe('worker() setter', () => { it(`should perform a get query and set the reponse data on the model`, () => { - jest.spyOn(controller, 'getIsSubordinate').mockReturnThis(); + jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true); + jest.spyOn(controller, 'refresh').mockReturnValue(new Promise(resolve => { + return resolve(); + })); let today = new Date(); @@ -86,7 +75,7 @@ describe('Worker', () => { ] }); - controller.worker = {id: 1}; + controller.worker = {id: 107}; $httpBackend.flush(); let events = controller.events; From a07d960a2e9be86ceb133cc4138de8b2972a3f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 16 Jul 2020 12:09:50 +0200 Subject: [PATCH 8/8] Fixed test --- modules/worker/front/calendar/index.spec.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js index 07e574d09..9d14cca20 100644 --- a/modules/worker/front/calendar/index.spec.js +++ b/modules/worker/front/calendar/index.spec.js @@ -51,12 +51,8 @@ describe('Worker', () => { describe('worker() setter', () => { it(`should perform a get query and set the reponse data on the model`, () => { jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true); - jest.spyOn(controller, 'refresh').mockReturnValue(new Promise(resolve => { - return resolve(); - })); let today = new Date(); - let tomorrow = new Date(today.getTime()); tomorrow.setDate(tomorrow.getDate() + 1);