const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('recalculateComponents', { description: 'Calculates the price of a sale and its components', accessType: 'WRITE', accepts: [{ arg: 'id', description: 'The ticket id', type: 'number', required: true, http: {source: 'path'} }], returns: { type: 'number', root: true }, http: { path: `/:id/recalculateComponents`, verb: 'POST' } }); Self.recalculateComponents = async(ctx, id, options) => { const myOptions = {}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const isEditable = await Self.isEditable(ctx, id, myOptions); if (!isEditable) throw new UserError(`The current ticket can't be modified`); const recalculation = await Self.rawSql('CALL vn.ticket_recalcComponents(?, NULL)', [id], myOptions); if (tx) await tx.commit(); return recalculation; } catch (e) { if (tx) await tx.rollback(); throw e; } }; };