let UserError = require('../../helpers').UserError; module.exports = Self => { Self.remoteMethod('new', { description: 'Create a newticket and returns the new ID', accessType: 'WRITE', accepts: [{ arg: 'params', type: 'object', required: true, description: 'ClientFk, Shipped, WharehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed, userId', http: {source: 'body'} }], returns: { type: 'number', root: true }, http: { path: `/new`, verb: 'post' } }); Self.new = async(params, transaction) => { let existsAddress = await Self.app.models.Address.findOne({ where: { id: params.addressFk, clientFk: params.clientFk} }); if (!existsAddress) throw new UserError(`This address doesn't exist`); let query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result); SELECT @result newTicketId;`; let result = await Self.rawSql(query, [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk | 442, params.addressFk, params.agencyModeFk | null, params.routeFk | null, params.landed, params.userId ], transaction); return await Self.findOne({ where: {id: result[1][0].newTicketId} }, transaction); }; };