const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = Self => { Self.remoteMethod('getSerial', { description: 'Return invoiceIn serial', accessType: 'READ', accepts: [{ arg: 'daysAgo', type: 'number', required: true }, { arg: 'serial', type: 'string' }], returns: { type: 'object', root: true }, http: { path: '/getSerial', verb: 'GET' } }); Self.getSerial = async(daysAgo, serial) => { const conn = Self.dataSource.connector; const stmt = []; const issued = Date.vnNew(); issued.setDate(issued.getDate() - daysAgo); stmt.push(new ParameterizedSQL(` SELECT i.serial, SUM(IF(i.isBooked, 0,1)) pending, COUNT(*) total FROM vn.invoiceIn i WHERE i.issued >= ? `, [issued])); if (serial) stmt.push(new ParameterizedSQL(`AND i.serial LIKE ? `, [serial])); stmt.push(`GROUP BY i.serial`); const sql = ParameterizedSQL.join(stmt); const result = await conn.executeStmt(sql); return result; }; };