feat: update commissionWorkCenterFk when create a route

This commit is contained in:
Vicent Llopis 2022-07-05 10:19:06 +02:00
parent 5f2f5b9b25
commit 51bd18ac37
4 changed files with 62 additions and 1 deletions

View File

@ -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;
}
};
};

View File

@ -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'

View File

@ -47,6 +47,9 @@
},
"description": {
"type": "string"
},
"commissionWorkCenterFk": {
"type": "number"
}
},
"relations": {

View File

@ -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});
}
);
}
}