salix/modules/order/back/methods/order/updateBasicData.js

73 lines
2.1 KiB
JavaScript

let UserError = require('vn-loopback/util/user-error');
let pick = require('object.pick');
module.exports = Self => {
Self.remoteMethod('updateBasicData', {
description: 'Updates basic data of an order',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'string',
required: true,
description: 'Model id',
http: {source: 'path'}
}, {
arg: 'data',
type: 'Object',
required: true,
description: 'Params to update',
http: {source: 'body'}
}
],
returns: {
arg: 'order',
type: 'Object',
root: true
},
http: {
path: `/:id/updateBasicData`,
verb: 'PATCH'
}
});
Self.updateBasicData = async(id, params, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const order = await models.Order.findById(id, null, myOptions);
const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions);
if (order.isConfirmed || orderRows.length != 0)
throw new UserError(`You can't make changes on the basic data of an confirmed order or with rows`);
const updateParams = pick(params, [
'clientFk',
'addressFk',
'landed',
'agencyModeFk',
'note',
]);
if (Object.keys(updateParams).length)
await order.updateAttributes(updateParams, myOptions);
if (tx) await tx.commit();
return order;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};