const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { Self.remoteMethod('latest', { description: 'Returns the lastest campaigns', accessType: 'READ', accepts: [{ arg: 'filter', type: 'Object', description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string` }], returns: { type: ['object'], root: true }, http: { path: `/latest`, verb: 'GET' } }); Self.latest = async filter => { const conn = Self.dataSource.connector; const minDate = new Date(); minDate.setFullYear(minDate.getFullYear() - 1); const where = {dated: {gte: minDate.toJSON().split('T')[0]}}; filter = mergeFilters(filter, {where}); const stmt = new ParameterizedSQL( `SELECT * FROM (`); stmt.merge('SELECT * FROM campaign'); stmt.merge(conn.makeWhere(filter.where)); stmt.merge('ORDER BY dated ASC'); stmt.merge(') sub'); stmt.merge('GROUP BY code'); stmt.merge('LIMIT 10000000000000000000'); stmt.merge(conn.makePagination(filter)); return conn.executeStmt(stmt); }; };