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 => { let existsAddress = await Self.app.models.Address.findOne({where: {id: params.addressFk, clientFk: params.clientFk}}); if (!existsAddress) throw new Error(`This address doesn't exist`); let query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @pe); SELECT @pe newTicketId;`; let result = await Self.rawSql(query, [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk | 442, params.addressFk, params.agencyModeFk, params.routeFk | null, params.landed, params.userId ]); return result[1][0].newTicketId; }; };