salix/modules/route/back/methods/route/guessPriority.js

51 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('guessPriority', {
description: 'Changes automatically the priority of the tickets in a route',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'Route Id ',
http: {source: 'path'}
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/guessPriority`,
verb: 'GET'
}
});
Self.guessPriority = async(ctx, id) => {
const userId = ctx.req.accessToken.userId;
const $t = ctx.req.__; // $translate
const query = `CALL vn.routeGuessPriority(?)`;
const tx = await Self.beginTransaction({});
try {
let options = {transaction: tx};
const priority = await Self.rawSql(query, [id], options);
let logRecord = {
originFk: id,
userFk: userId,
action: 'update',
changedModel: 'Route',
description: $t('Sorts whole route')
};
await Self.app.models.RouteLog.create(logRecord, options);
await tx.commit();
return priority;
} catch (e) {
await tx.rollback();
throw e;
}
};
};