52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
let UserError = require('../../helpers').UserError;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('updateBasicData', {
|
|
description: 'Updates basic data of an order',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'data',
|
|
type: 'Object',
|
|
required: true,
|
|
description: 'Params to update',
|
|
http: {source: 'body'}
|
|
}, {
|
|
arg: 'id',
|
|
type: 'string',
|
|
required: true,
|
|
description: 'Model id',
|
|
http: {source: 'path'}
|
|
}],
|
|
returns: {
|
|
arg: 'order',
|
|
type: 'Object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/updateBasicData`,
|
|
verb: 'PATCH'
|
|
}
|
|
});
|
|
|
|
Self.updateBasicData = async (params, id) => {
|
|
let order = await Self.app.models.Order.findById(id);
|
|
let orderRows = await Self.app.models.OrderRow.find({where: {orderFk: id}});
|
|
|
|
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`);
|
|
|
|
let validUpdateParams = [
|
|
'clientFk',
|
|
'companyFk',
|
|
'landed',
|
|
'note',
|
|
];
|
|
|
|
for (const key in params) {
|
|
if (validUpdateParams.indexOf(key) === -1)
|
|
throw new UserError(`You don't have enough privileges to do that`);
|
|
}
|
|
return await order.updateAttributes(params);
|
|
};
|
|
};
|