70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
const buildFilter = require('vn-loopback/util/filter').buildFilter;
|
|
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('getSerial', {
|
|
description: 'Return invoiceIn serial',
|
|
accessType: 'READ',
|
|
accepts: [{
|
|
arg: 'filter',
|
|
type: 'object'
|
|
}, {
|
|
arg: 'daysAgo',
|
|
type: 'number',
|
|
required: true
|
|
}, {
|
|
arg: 'serial',
|
|
type: 'string'
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: '/getSerial',
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.getSerial = async(ctx, options) => {
|
|
const conn = Self.dataSource.connector;
|
|
const args = ctx.args;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const where = buildFilter(args, (param, value) => {
|
|
switch (param) {
|
|
case 'serial':
|
|
return {'f.serial': {like: `%${value}%`}};
|
|
}
|
|
});
|
|
|
|
filter = mergeFilters(args.filter, {where});
|
|
const issued = Date.vnNew();
|
|
issued.setDate(issued.getDate() - args.daysAgo);
|
|
|
|
const stmts = [];
|
|
const stmt = new ParameterizedSQL(
|
|
`SELECT *
|
|
FROM (
|
|
SELECT i.serial, SUM(IF(i.isBooked, 0,1)) pending, COUNT(*) total
|
|
FROM vn.invoiceIn i
|
|
WHERE i.issued >= ?
|
|
GROUP BY i.serial) f`
|
|
, [issued]);
|
|
|
|
stmt.merge(conn.makeWhere(filter.where));
|
|
stmt.merge(conn.makeOrderBy(filter.order));
|
|
stmt.merge(conn.makeLimit(filter));
|
|
|
|
const invoiceInIndex = stmts.push(stmt) - 1;
|
|
const sql = ParameterizedSQL.join(stmts, ';');
|
|
const result = await conn.executeStmt(sql, myOptions);
|
|
|
|
return invoiceInIndex === 0 ? result : result[invoiceInIndex];
|
|
};
|
|
};
|