diff --git a/modules/ticket/back/methods/sale/replaceItem.js b/modules/ticket/back/methods/sale/replaceItem.js new file mode 100644 index 000000000..529d429eb --- /dev/null +++ b/modules/ticket/back/methods/sale/replaceItem.js @@ -0,0 +1,53 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('replaceItem', { + description: 'Replace item from sale', + accessType: 'WRITE', + accepts: [{ + arg: 'saleFk', + type: 'number', + required: true, + }, + { + arg: 'newItemFk', + type: 'number', + required: true + }, + { + arg: 'quantity', + type: 'number', + required: true + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/recalculatePrice`, + verb: 'POST' + } + }); + + Self.recalculatePrice = async(ctx, saleFk, itemFk, quantity, options) => { + const myOptions = {userId: ctx.req.accessToken.userId}; + let tx; + + 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, itemFk, quantity], myOptions); + return result; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/models/sale.js b/modules/ticket/back/models/sale.js index 1b4d8e31c..6aa542050 100644 --- a/modules/ticket/back/models/sale.js +++ b/modules/ticket/back/models/sale.js @@ -13,6 +13,7 @@ module.exports = Self => { require('../methods/sale/usesMana')(Self); require('../methods/sale/clone')(Self); require('../methods/sale/getFromSectorCollection')(Self); + require('../methods/sale/replaceItem')(Self); Self.validatesPresenceOf('concept', { message: `Concept cannot be blank`