From 5f2f5b9b250ddb8b1b4ee0794166870e2d927dfa Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 5 Jul 2022 10:18:26 +0200 Subject: [PATCH 1/8] feat: add sql changes --- db/changes/10480-june/00-route.sql | 2 ++ db/dump/fixtures.sql | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/changes/10480-june/00-route.sql diff --git a/db/changes/10480-june/00-route.sql b/db/changes/10480-june/00-route.sql new file mode 100644 index 000000000..d48d9475b --- /dev/null +++ b/db/changes/10480-june/00-route.sql @@ -0,0 +1,2 @@ +ALTER TABLE `vn`.`route` ADD commissionWorkCenterFk INT NULL; +ALTER TABLE `vn`.`route` ADD CONSTRAINT route_FK FOREIGN KEY (commissionWorkCenterFk) REFERENCES vn.workCenter(id) ON DELETE RESTRICT ON UPDATE CASCADE; \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 5a2188cb5..ebbe9e2de 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2606,4 +2606,9 @@ INSERT INTO `vn`.`sectorCollectionSaleGroup` (`sectorCollectionFk`, `saleGroupFk INSERT INTO `vn`.`workerTimeControlConfig` (`id`, `dayBreak`, `dayBreakDriver`, `shortWeekBreak`, `longWeekBreak`, `weekScope`, `mailPass`, `mailHost`, `mailSuccessFolder`, `mailErrorFolder`, `mailUser`, `minHoursToBreak`, `breakHours`, `hoursCompleteWeek`, `startNightlyHours`, `endNightlyHours`, `maxTimePerDay`, `breakTime`, `timeToBreakTime`, `dayMaxTime`, `shortWeekDays`, `longWeekDays`) VALUES - (1, 43200, 32400, 129600, 259200, 604800, '', '', 'Leidos.exito', 'Leidos.error', 'timeControl', 5.33, 0.33, 40, '22:00:00', '06:00:00', 57600, 1200, 18000, 57600, 6, 13); \ No newline at end of file + (1, 43200, 32400, 129600, 259200, 604800, '', '', 'Leidos.exito', 'Leidos.error', 'timeControl', 5.33, 0.33, 40, '22:00:00', '06:00:00', 57600, 1200, 18000, 57600, 6, 13); + +ALTER TABLE `vn`.`routeConfig` ADD defaultWorkCenterFk INT NULL; +INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) + VALUES + (1, 9); \ No newline at end of file -- 2.40.1 From 51bd18ac375d7786522978100e23e388602dc786 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 5 Jul 2022 10:19:06 +0200 Subject: [PATCH 2/8] feat: update commissionWorkCenterFk when create a route --- .../back/methods/route/updateWorkCenter.js | 54 +++++++++++++++++++ modules/route/back/models/route.js | 1 + modules/route/back/models/route.json | 3 ++ modules/route/front/create/index.js | 5 +- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 modules/route/back/methods/route/updateWorkCenter.js diff --git a/modules/route/back/methods/route/updateWorkCenter.js b/modules/route/back/methods/route/updateWorkCenter.js new file mode 100644 index 000000000..10ecbe8ab --- /dev/null +++ b/modules/route/back/methods/route/updateWorkCenter.js @@ -0,0 +1,54 @@ +module.exports = Self => { + Self.remoteMethodCtx('updateWorkCenter', { + description: 'Update the commission work center through user salix connected', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'number', + description: 'Route Id', + http: {source: 'path'} + }, + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/updateWorkCenter`, + verb: 'GET' + } + }); + + Self.updateWorkCenter = async(ctx, id, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + const userId = ctx.req.accessToken.userId; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const [result] = await Self.rawSql(` + SELECT IFNULL(wl.workCenterFK, r.defaultWorkCenterFk) AS commissionWorkCenter + FROM vn.routeConfig r + LEFT JOIN vn.workerLabour wl ON wl.workerFk = ? + AND CURDATE() BETWEEN wl.started AND IFNULL(wl.ended, CURDATE()); + `, [userId], myOptions); + + const route = await models.Route.findById(id, null, myOptions); + await route.updateAttribute('commissionWorkCenterFk', result.commissionWorkCenter, myOptions); + + if (tx) await tx.commit(); + + return route; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index c82d1722e..4050e62fe 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -9,6 +9,7 @@ module.exports = Self => { require('../methods/route/clone')(Self); require('../methods/route/getSuggestedTickets')(Self); require('../methods/route/unlink')(Self); + require('../methods/route/updateWorkCenter')(Self); Self.validate('kmStart', validateDistance, { message: 'Distance must be lesser than 1000' diff --git a/modules/route/back/models/route.json b/modules/route/back/models/route.json index 46fb6b76f..3b33cc028 100644 --- a/modules/route/back/models/route.json +++ b/modules/route/back/models/route.json @@ -47,6 +47,9 @@ }, "description": { "type": "string" + }, + "commissionWorkCenterFk": { + "type": "number" } }, "relations": { diff --git a/modules/route/front/create/index.js b/modules/route/front/create/index.js index 56c8cc25a..63497fe69 100644 --- a/modules/route/front/create/index.js +++ b/modules/route/front/create/index.js @@ -4,7 +4,10 @@ import Section from 'salix/components/section'; export default class Controller extends Section { onSubmit() { this.$.watcher.submit().then( - res => this.$state.go('route.card.summary', {id: res.data.id}) + res => { + this.$http.get(`Routes/${res.data.id}/updateWorkCenter`); + this.$state.go('route.card.summary', {id: res.data.id}); + } ); } } -- 2.40.1 From 979ee1d9568767f2069fb9164eb1af3b336937e1 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 5 Jul 2022 10:19:22 +0200 Subject: [PATCH 3/8] feat: add backTest --- .../route/specs/updateWorkCenter.spec.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 modules/route/back/methods/route/specs/updateWorkCenter.spec.js diff --git a/modules/route/back/methods/route/specs/updateWorkCenter.spec.js b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js new file mode 100644 index 000000000..a7d0f0caa --- /dev/null +++ b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js @@ -0,0 +1,51 @@ +const models = require('vn-loopback/server/server').models; + +describe('route updateWorkCenter()', () => { + const routeId = 1; + + it('should update the commissionWorkCenterFk from workerLabour.workCenterFK', async() => { + const tx = await models.Defaulter.beginTransaction({}); + try { + const developerId = 9; + const ctx = { + req: { + accessToken: {userId: developerId} + } + }; + const options = {transaction: tx}; + + const expectedResult = 1; + const updatedRoute = await models.Route.updateWorkCenter(ctx, routeId, options); + + expect(updatedRoute.commissionWorkCenterFk).toEqual(expectedResult); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should update the commissionWorkCenterFk from defaultWorkCenterFk if workerLabour.workCenterFK is null', async() => { + const tx = await models.Defaulter.beginTransaction({}); + try { + const noExistentId = 2; + const ctx = { + req: { + accessToken: {userId: noExistentId} + } + }; + const options = {transaction: tx}; + + const expectedResult = 9; + const updatedRoute = await models.Route.updateWorkCenter(ctx, routeId, options); + + expect(updatedRoute.commissionWorkCenterFk).toEqual(expectedResult); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); -- 2.40.1 From 189d0a27bc6202690c67b7b1bc8eeb53f236e71b Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 5 Jul 2022 11:44:03 +0200 Subject: [PATCH 4/8] refactor: delete unnecessary sql --- db/changes/10480-june/00-route.sql | 2 -- db/dump/fixtures.sql | 1 - db/dump/structure.sql | 4 ++++ 3 files changed, 4 insertions(+), 3 deletions(-) delete mode 100644 db/changes/10480-june/00-route.sql diff --git a/db/changes/10480-june/00-route.sql b/db/changes/10480-june/00-route.sql deleted file mode 100644 index d48d9475b..000000000 --- a/db/changes/10480-june/00-route.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE `vn`.`route` ADD commissionWorkCenterFk INT NULL; -ALTER TABLE `vn`.`route` ADD CONSTRAINT route_FK FOREIGN KEY (commissionWorkCenterFk) REFERENCES vn.workCenter(id) ON DELETE RESTRICT ON UPDATE CASCADE; \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index ebbe9e2de..01708ca69 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2608,7 +2608,6 @@ INSERT INTO `vn`.`workerTimeControlConfig` (`id`, `dayBreak`, `dayBreakDriver`, VALUES (1, 43200, 32400, 129600, 259200, 604800, '', '', 'Leidos.exito', 'Leidos.error', 'timeControl', 5.33, 0.33, 40, '22:00:00', '06:00:00', 57600, 1200, 18000, 57600, 6, 13); -ALTER TABLE `vn`.`routeConfig` ADD defaultWorkCenterFk INT NULL; INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) VALUES (1, 9); \ No newline at end of file diff --git a/db/dump/structure.sql b/db/dump/structure.sql index 2c0e8231a..7b9de25e6 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -34348,6 +34348,7 @@ CREATE TABLE `route` ( `priority` int(11) NOT NULL DEFAULT '0', `invoiceInFk` mediumint(8) unsigned DEFAULT NULL, `beachFk` int(11) DEFAULT NULL, + `commissionWorkCenterFk` int(11) DEFAULT NULL COMMENT 'WorkerCenter que gestiona la ruta', PRIMARY KEY (`id`), KEY `Id_Agencia` (`agencyModeFk`), KEY `Fecha` (`created`), @@ -34356,7 +34357,9 @@ CREATE TABLE `route` ( KEY `fk_route_1_idx` (`zoneFk`), KEY `asdfasdf_idx` (`invoiceInFk`), KEY `route_idxIsOk` (`isOk`), + KEY `route_WorkCenterFk_idx` (`commissionWorkCenterFk`), CONSTRAINT `fk_route_1` FOREIGN KEY (`zoneFk`) REFERENCES `zone` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `route_WorkCenterFk` FOREIGN KEY (`commissionWorkCenterFk`) REFERENCES `workCenter` (`id`) ON UPDATE CASCADE, CONSTRAINT `route_fk5` FOREIGN KEY (`agencyModeFk`) REFERENCES `agencyMode` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `route_ibfk_1` FOREIGN KEY (`gestdocFk`) REFERENCES `dms` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `route_ibfk_2` FOREIGN KEY (`workerFk`) REFERENCES `worker` (`id`) ON UPDATE CASCADE, @@ -34513,6 +34516,7 @@ CREATE TABLE `routeConfig` ( `plusCategory1Concept` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `plusCategory2Concept` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `defaultCompanyFk` smallint(5) unsigned DEFAULT '442', + `defaultWorkCenterFk` int(11) DEFAULT 9 COMMENT 'Para el cálculo de las comisiones, en caso de el creador de la ruta no tenga workCenter', PRIMARY KEY (`id`), KEY `routeConfig_FK` (`defaultCompanyFk`), CONSTRAINT `routeConfig_FK` FOREIGN KEY (`defaultCompanyFk`) REFERENCES `company` (`id`) -- 2.40.1 From 6f7a4fc5fff8e25b6d90d60513eca50487b7edc5 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 5 Jul 2022 12:08:16 +0200 Subject: [PATCH 5/8] refactor title of test --- .../route/back/methods/route/specs/updateWorkCenter.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/route/back/methods/route/specs/updateWorkCenter.spec.js b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js index a7d0f0caa..4973a3afc 100644 --- a/modules/route/back/methods/route/specs/updateWorkCenter.spec.js +++ b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js @@ -3,7 +3,7 @@ const models = require('vn-loopback/server/server').models; describe('route updateWorkCenter()', () => { const routeId = 1; - it('should update the commissionWorkCenterFk from workerLabour.workCenterFK', async() => { + it('should set the commission work center if the worker has workCenter', async() => { const tx = await models.Defaulter.beginTransaction({}); try { const developerId = 9; @@ -26,7 +26,7 @@ describe('route updateWorkCenter()', () => { } }); - it('should update the commissionWorkCenterFk from defaultWorkCenterFk if workerLabour.workCenterFK is null', async() => { + it(`should set the commission work center equals default work center if the worker hasn't workCenter`, async() => { const tx = await models.Defaulter.beginTransaction({}); try { const noExistentId = 2; -- 2.40.1 From d9a12fd20379b13dcbd31069a97d3c2781c1e033 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 7 Jul 2022 07:39:46 +0200 Subject: [PATCH 6/8] feat: update commissionWorkCenterFk where is not calculated --- db/changes/10480-june/00-route.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 db/changes/10480-june/00-route.sql diff --git a/db/changes/10480-june/00-route.sql b/db/changes/10480-june/00-route.sql new file mode 100644 index 000000000..beb7d5e41 --- /dev/null +++ b/db/changes/10480-june/00-route.sql @@ -0,0 +1,10 @@ +UPDATE `vn`.`route` r + JOIN(SELECT r.id, wl.workcenterFk + FROM `vn`.`route` r + JOIN `vn`.`routeLog` rl ON rl.originFk = r.id + JOIN `vn`.`workerLabour` wl ON wl.workerFk = rl.userFk + AND r.created BETWEEN wl.started AND IFNULL(wl.ended, r.created) + WHERE r.created BETWEEN '2021-12-01' AND CURDATE() + AND rl.action = 'insert' + )sub ON sub.id = r.id + SET r.commissionWorkCenterFk = sub.workcenterFk; \ No newline at end of file -- 2.40.1 From 28deb34047e7970d2877ac251f5cfae892d0a684 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 19 Jul 2022 14:40:23 +0200 Subject: [PATCH 7/8] refactor: update translation --- .../back/methods/route/specs/updateWorkCenter.spec.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/route/back/methods/route/specs/updateWorkCenter.spec.js b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js index 4973a3afc..5328dc240 100644 --- a/modules/route/back/methods/route/specs/updateWorkCenter.spec.js +++ b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js @@ -4,7 +4,7 @@ describe('route updateWorkCenter()', () => { const routeId = 1; it('should set the commission work center if the worker has workCenter', async() => { - const tx = await models.Defaulter.beginTransaction({}); + const tx = await models.Route.beginTransaction({}); try { const developerId = 9; const ctx = { @@ -26,13 +26,13 @@ describe('route updateWorkCenter()', () => { } }); - it(`should set the commission work center equals default work center if the worker hasn't workCenter`, async() => { - const tx = await models.Defaulter.beginTransaction({}); + it(`shoul set the default commision work center if that worker didn't have one yet`, async() => { + const tx = await models.Route.beginTransaction({}); try { - const noExistentId = 2; + const userWithoutWorkCenter = 2; const ctx = { req: { - accessToken: {userId: noExistentId} + accessToken: {userId: userWithoutWorkCenter} } }; const options = {transaction: tx}; -- 2.40.1 From 318b688b67b4557c2529e68b027beecd497830a8 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 19 Jul 2022 14:40:41 +0200 Subject: [PATCH 8/8] fix: typo errors --- modules/route/back/methods/route/updateWorkCenter.js | 4 ++-- modules/route/front/create/index.js | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/route/back/methods/route/updateWorkCenter.js b/modules/route/back/methods/route/updateWorkCenter.js index 10ecbe8ab..7796fba41 100644 --- a/modules/route/back/methods/route/updateWorkCenter.js +++ b/modules/route/back/methods/route/updateWorkCenter.js @@ -14,7 +14,7 @@ module.exports = Self => { }, http: { path: `/:id/updateWorkCenter`, - verb: 'GET' + verb: 'POST' } }); @@ -34,7 +34,7 @@ module.exports = Self => { try { const [result] = await Self.rawSql(` - SELECT IFNULL(wl.workCenterFK, r.defaultWorkCenterFk) AS commissionWorkCenter + SELECT IFNULL(wl.workCenterFk, r.defaultWorkCenterFk) AS commissionWorkCenter FROM vn.routeConfig r LEFT JOIN vn.workerLabour wl ON wl.workerFk = ? AND CURDATE() BETWEEN wl.started AND IFNULL(wl.ended, CURDATE()); diff --git a/modules/route/front/create/index.js b/modules/route/front/create/index.js index 63497fe69..c81394c10 100644 --- a/modules/route/front/create/index.js +++ b/modules/route/front/create/index.js @@ -5,8 +5,10 @@ export default class Controller extends Section { onSubmit() { this.$.watcher.submit().then( res => { - this.$http.get(`Routes/${res.data.id}/updateWorkCenter`); - this.$state.go('route.card.summary', {id: res.data.id}); + this.$http.post(`Routes/${res.data.id}/updateWorkCenter`, null) + .then(() => { + this.$state.go('route.card.summary', {id: res.data.id}); + }); } ); } -- 2.40.1