const closure = require('./closure'); module.exports = Self => { Self.remoteMethodCtx('closeByTicket', { description: 'Makes the closure process by ticket', accessType: 'WRITE', accepts: [ { arg: 'id', type: 'number', required: true, description: 'The ticket id', http: {source: 'path'} } ], returns: { type: 'object', root: true }, http: { path: `/:id/close-by-ticket`, verb: 'POST' } }); Self.closeByTicket = async ctx => { const args = ctx.args; const tickets = await Self.rawSql(` SELECT t.id, t.clientFk, t.companyFk, c.name clientName, c.email recipient, c.salesPersonFk, c.isToBeMailed, c.hasToInvoice, co.hasDailyInvoice, eu.email salesPersonEmail FROM expedition e JOIN ticket t ON t.id = e.ticketFk JOIN ticketState ts ON ts.ticketFk = t.id JOIN alertLevel al ON al.id = ts.alertLevel JOIN client c ON c.id = t.clientFk JOIN province p ON p.id = c.provinceFk JOIN country co ON co.id = p.countryFk LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk WHERE al.code = 'PACKED' AND t.id = ? AND t.refFk IS NULL GROUP BY e.ticketFk`, [args.id]); await closure(Self, tickets); return { message: 'Success' }; }; };