salix/back/methods/campaign/latest.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-10-05 05:53:03 +00:00
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}};
2020-10-05 05:53:03 +00:00
filter = mergeFilters(filter, {where});
const stmt = new ParameterizedSQL(
2023-01-03 09:36:31 +00:00
`SELECT * FROM (`);
stmt.merge('SELECT * FROM campaign');
2020-10-05 05:53:03 +00:00
stmt.merge(conn.makeWhere(filter.where));
2023-01-03 09:36:31 +00:00
stmt.merge('ORDER BY dated ASC) sub');
2020-10-05 05:53:03 +00:00
stmt.merge('GROUP BY code');
2023-01-03 09:20:44 +00:00
stmt.merge('LIMIT 10000000000000000000');
2020-10-05 05:53:03 +00:00
stmt.merge(conn.makePagination(filter));
return conn.executeStmt(stmt);
};
};