salix/back/methods/campaign/latest.js

44 lines
1.3 KiB
JavaScript

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 = Date.vnNew();
minDate.setFullYear(minDate.getFullYear() - 1);
const where = {dated: {gte: minDate}};
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('LIMIT 10000000000000000000');
stmt.merge(') sub');
stmt.merge('GROUP BY code');
stmt.merge(conn.makePagination(filter));
return conn.executeStmt(stmt);
};
};