const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('updateThermograph', { description: 'Updates a travel thermograph', accessType: 'WRITE', accepts: [{ arg: 'id', type: 'Number', description: 'The travel id', http: {source: 'path'} }, { arg: 'thermographId', type: 'String', description: 'The thermograph id', required: true }, { arg: 'state', type: 'String', required: true }, { arg: 'warehouseId', type: 'Number', description: 'The warehouse id' }, { arg: 'companyId', type: 'Number', description: 'The company id' }, { arg: 'dmsTypeId', type: 'Number', description: 'The dms type id' }, { arg: 'reference', type: 'String' }, { arg: 'description', type: 'String' }, { arg: 'hasFileAttached', type: 'Boolean', description: 'True if has an attached file' }], returns: { type: 'Object', root: true }, http: { path: `/:id/updateThermograph`, verb: 'POST' } }); Self.updateThermograph = async(ctx, id, thermographId, state) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); try { const options = {transaction: tx}; const travelThermograph = await models.TravelThermograph.findOne({ where: { thermographFk: thermographId, travelFk: id } }, options); if (!travelThermograph) throw new UserError('No valid travel thermograph found'); const dmsFk = travelThermograph.dmsFk; await models.Dms.updateFile(ctx, dmsFk, options); await travelThermograph.updateAttributes({ result: state }, options); await tx.commit(); return travelThermograph; } catch (e) { await tx.rollback(); throw e; } }; };