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

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-09-28 13:00:12 +00:00
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
2018-09-28 13:00:12 +00:00
module.exports = Self => {
Self.remoteMethodCtx('transactions', {
description: 'Returns customer transactions',
2018-09-28 13:00:12 +00:00
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
},
{
arg: 'orderFk',
type: 'number',
},
{
arg: 'clientFk',
type: 'number',
},
{
arg: 'amount',
type: 'number',
2023-09-29 12:42:47 +00:00
},
{
arg: 'from',
type: 'date',
},
{
arg: 'to',
type: 'date',
}
],
2018-09-28 13:00:12 +00:00
returns: {
2021-07-08 13:53:30 +00:00
type: ['object'],
2018-09-28 13:00:12 +00:00
root: true
},
http: {
path: `/transactions`,
2018-09-28 13:00:12 +00:00
verb: 'GET'
}
});
2023-10-18 10:05:33 +00:00
Self.transactions = async(ctx, filter, orderFk, clientFk, amount, from, to, options) => {
2021-07-08 13:53:30 +00:00
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2023-10-18 10:05:33 +00:00
if (to) to.setHours(23, 59, 59, 999);
2023-09-29 12:42:47 +00:00
2023-10-18 10:05:33 +00:00
const where = buildFilter({orderFk, clientFk, amount, from, to}, (param, value) => {
switch (param) {
case 'orderFk':
return {'t.id': value};
case 'clientFk':
return {'t.clientFk': value};
case 'amount':
return {'t.amount': (value * 100)};
2023-09-29 12:42:47 +00:00
case 'from':
return {'t.created': {gte: value}};
case 'to':
return {'t.created': {lte: value}};
}
});
2023-03-21 09:05:27 +00:00
filter = mergeFilters(filter, {where});
2021-07-08 13:53:30 +00:00
const conn = Self.dataSource.connector;
const stmt = new ParameterizedSQL(`
2018-09-28 13:00:12 +00:00
SELECT
t.id,
t.clientFk,
c.name AS customerName,
t.created,
t.amount / 100 amount,
t.receiptFk IS NOT NULL AS isConfirmed,
tt.message responseMessage,
te.message errorMessage
FROM hedera.tpvTransaction t
JOIN client c ON c.id = t.clientFk
JOIN hedera.tpvMerchant m ON m.id = t.merchantFk
LEFT JOIN hedera.tpvResponse tt ON tt.id = t.response
LEFT JOIN hedera.tpvError te ON te.code = errorCode`);
2018-09-28 13:00:12 +00:00
2018-11-07 13:20:58 +00:00
stmt.merge(conn.makeSuffix(filter, 't'));
2018-09-28 13:00:12 +00:00
2021-07-08 13:53:30 +00:00
return Self.rawStmt(stmt, myOptions);
2018-09-28 13:00:12 +00:00
};
};