salix/back/methods/campaign/latest.js

45 lines
1.4 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 10:00:01 +00:00
stmt.merge('ORDER BY dated ASC');
stmt.merge(') 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));
2023-01-03 10:00:01 +00:00
console.log(stmt);
console.log(minDate);
2020-10-05 05:53:03 +00:00
return conn.executeStmt(stmt);
};
};