54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('replaceItem', {
|
|
description: 'Replace item from sale',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'saleFk',
|
|
type: 'number',
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'substitutionFk',
|
|
type: 'number',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'quantity',
|
|
type: 'number',
|
|
required: true
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/replaceItem`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.replaceItem = async(ctx, saleFk, substitutionFk, quantity, options) => {
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
let tx;
|
|
const {_saleFk, _substitutionFk, _quantity} = ctx.args;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const result = await Self.rawSql('CALL sale_replaceItem(?,?,?)', [saleFk, substitutionFk, quantity], myOptions);
|
|
return result;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|