salix/back/methods/campaign/latest.js

44 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;
2023-01-16 14:18:24 +00:00
const minDate = Date.vnNew();
2020-10-05 05:53:03 +00:00
minDate.setFullYear(minDate.getFullYear() - 1);
2023-01-03 11:04:38 +00:00
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 10:00:01 +00:00
stmt.merge('ORDER BY dated ASC');
2023-01-03 10:53:47 +00:00
stmt.merge('LIMIT 10000000000000000000');
2023-01-03 10:00:01 +00:00
stmt.merge(') sub');
2020-10-05 05:53:03 +00:00
stmt.merge('GROUP BY code');
stmt.merge(conn.makePagination(filter));
2023-01-03 10:39:50 +00:00
2020-10-05 05:53:03 +00:00
return conn.executeStmt(stmt);
};
};