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); minDate.setMonth(0); minDate.setDate(1); const where = {dated: {gte: minDate}}; filter = mergeFilters(filter, {where}); const stmt = new ParameterizedSQL( `SELECT * FROM campaign`); stmt.merge(conn.makeWhere(filter.where)); stmt.merge('GROUP BY code'); stmt.merge(conn.makePagination(filter)); return conn.executeStmt(stmt); }; };