2019-09-06 09:43:15 +00:00
|
|
|
module.exports = Self => {
|
2021-05-26 07:14:18 +00:00
|
|
|
Self.remoteMethodCtx('updateConcept', {
|
2019-09-06 09:43:15 +00:00
|
|
|
description: 'Updates the concept of a sale',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'sale ID',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}, {
|
|
|
|
arg: 'newConcept',
|
|
|
|
type: 'any',
|
|
|
|
required: true,
|
|
|
|
description: 'new concept'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateConcept`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
Self.updateConcept = async(ctx, id, newConcept, options) => {
|
2021-05-26 07:14:18 +00:00
|
|
|
const models = Self.app.models;
|
2021-09-29 06:27:18 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const currentLine = await models.Sale.findById(id, null, myOptions);
|
|
|
|
|
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, [id], myOptions);
|
|
|
|
|
|
|
|
if (!canEditSale)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
2019-09-06 09:43:15 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const line = await currentLine.updateAttributes({concept: newConcept}, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return line;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-09-06 09:43:15 +00:00
|
|
|
};
|
|
|
|
};
|