salix/modules/client/back/methods/client/consumption.js

127 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-06-12 07:18:19 +00:00
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('consumption', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'Object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
}, {
arg: 'search',
type: 'String',
description: `If it's and integer searchs by id, otherwise it searchs by name`
}, {
2020-08-12 06:26:00 +00:00
arg: 'itemId',
2020-08-12 09:38:39 +00:00
type: 'Number',
2020-06-12 07:18:19 +00:00
description: 'Item id'
}, {
2020-08-12 06:26:00 +00:00
arg: 'categoryId',
2020-08-12 09:38:39 +00:00
type: 'Number',
2020-06-12 07:18:19 +00:00
description: 'Category id'
}, {
2020-08-12 06:26:00 +00:00
arg: 'typeId',
2020-08-12 09:38:39 +00:00
type: 'Number',
2020-06-12 07:18:19 +00:00
description: 'Item type id',
}, {
2020-08-12 06:26:00 +00:00
arg: 'buyerId',
2020-08-12 09:38:39 +00:00
type: 'Number',
2020-06-12 07:18:19 +00:00
description: 'Buyer id'
}, {
arg: 'from',
type: 'Date',
description: `The from date filter`
}, {
arg: 'to',
type: 'Date',
description: `The to date filter`
}, {
arg: 'grouped',
type: 'Boolean',
description: 'Group by item'
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/consumption`,
verb: 'GET'
}
});
Self.consumption = async(ctx, filter) => {
const conn = Self.dataSource.connector;
const args = ctx.args;
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
return /^\d+$/.test(value)
? {'i.id': value}
: {'i.name': {like: `%${value}%`}};
case 'itemId':
return {'i.id': value};
case 'description':
return {'i.description': {like: `%${value}%`}};
case 'categoryId':
return {'it.categoryFk': value};
case 'typeId':
return {'it.id': value};
case 'buyerId':
return {'it.workerFk': value};
2020-08-12 06:26:00 +00:00
case 'from':
return {'t.shipped': {gte: value}};
case 'to':
return {'t.shipped': {lte: value}};
2020-06-12 07:18:19 +00:00
}
});
filter = mergeFilters(filter, {where});
let stmt = new ParameterizedSQL('SELECT');
if (args.grouped)
stmt.merge(`SUM(s.quantity) AS quantity,`);
else
stmt.merge(`s.quantity,`);
stmt.merge(`s.itemFk,
s.concept,
s.ticketFk,
t.shipped,
i.name AS itemName,
i.size AS itemSize,
i.typeFk AS itemTypeFk,
i.subName,
i.tag5,
i.value5,
i.tag6,
i.value6,
i.tag7,
i.value7,
i.tag8,
i.value8,
i.tag9,
i.value9,
i.tag10,
i.value10
FROM sale s
JOIN ticket t ON t.id = s.ticketFk
JOIN item i ON i.id = s.itemFk
JOIN itemType it ON it.id = i.typeFk`, [args.grouped]);
stmt.merge(conn.makeWhere(filter.where));
if (args.grouped)
stmt.merge(`GROUP BY s.itemFk`);
stmt.merge(conn.makePagination(filter));
2020-08-12 10:00:46 +00:00
return conn.executeStmt(stmt);
2020-06-12 07:18:19 +00:00
};
};