module.exports = Self => { Self.remoteMethod('hasUncheckedTicket', { description: 'Get boolean if the collection of ticket has a ticket not checked', accessType: 'READ', accepts: [{ arg: 'ticketFk', type: 'integer', required: true, description: 'Ticket id' }], returns: { type: 'boolean', root: true }, http: { path: `/hasUncheckedTicket`, verb: 'GET' } }); Self.hasUncheckedTicket = async(ticketFk, options) => { const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const result = await Self.rawSql(` SELECT tc2.ticketFk FROM vn.ticketCollection tc JOIN vn.ticketCollection tc2 ON tc2.collectionFk = tc.collectionFk LEFT JOIN vn.ticketState ts ON ts.ticketFk = tc2.ticketFk JOIN vn.state s ON s.id = ts.stateFk JOIN vn.state s2 ON s2.code = 'CHECKED' WHERE tc.ticketFk = ? AND s.order < s2.id LIMIT 1;`, [ticketFk], myOptions); return result[0]?.ticketFk > 0 && result[0].ticketFk; }; };