63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('updateVolume', {
|
|
description: 'Update the volume in a route',
|
|
accessType: 'WRITE',
|
|
accepts: {
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'Route id',
|
|
http: {source: 'path'}
|
|
},
|
|
returns: {
|
|
type: 'number',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/updateVolume`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.updateVolume = async(ctx, id, options) => {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const models = Self.app.models;
|
|
|
|
let tx;
|
|
let myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const originalRoute = await models.Route.findById(id, null, myOptions);
|
|
|
|
await Self.rawSql(`CALL vn.routeUpdateM3(?)`, [id], myOptions);
|
|
|
|
const updatedRoute = await models.Route.findById(id, null, myOptions);
|
|
|
|
await models.RouteLog.create({
|
|
originFk: id,
|
|
userFk: userId,
|
|
action: 'update',
|
|
changedModel: 'Route',
|
|
changedModelId: id,
|
|
oldInstance: {m3: originalRoute.m3},
|
|
newInstance: {m3: updatedRoute.m3}
|
|
}, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedRoute;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|