From 51bd18ac375d7786522978100e23e388602dc786 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 5 Jul 2022 10:19:06 +0200 Subject: [PATCH] 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}); + } ); } }