diff --git a/.vscode/settings.json b/.vscode/settings.json index 82815d588..159cecdc9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,5 +5,6 @@ "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, - "search.useIgnoreFiles": false + "search.useIgnoreFiles": false, + "editor.defaultFormatter": "dbaeumer.vscode-eslint" } diff --git a/modules/client/back/methods/client/getTransactions.js b/modules/client/back/methods/client/getTransactions.js deleted file mode 100644 index 45183bbdc..000000000 --- a/modules/client/back/methods/client/getTransactions.js +++ /dev/null @@ -1,49 +0,0 @@ - -const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; - -module.exports = Self => { - Self.remoteMethod('getTransactions', { - description: 'Returns last entries', - accessType: 'READ', - accepts: [{ - arg: 'filter', - type: 'object', - description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', - http: {source: 'query'} - }], - returns: { - type: ['object'], - root: true - }, - http: { - path: `/getTransactions`, - verb: 'GET' - } - }); - - Self.getTransactions = async(filter, options) => { - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - const conn = Self.dataSource.connector; - const stmt = new ParameterizedSQL(` - SELECT - t.id, - t.clientFk, - t.created, - t.amount / 100 amount, - t.receiptFk IS NOT NULL AS isConfirmed, - tt.message responseMessage, - te.message errorMessage - FROM hedera.tpvTransaction t - 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`); - - stmt.merge(conn.makeSuffix(filter, 't')); - - return Self.rawStmt(stmt, myOptions); - }; -}; diff --git a/modules/client/back/methods/client/specs/getTransactions.spec.js b/modules/client/back/methods/client/specs/getTransactions.spec.js deleted file mode 100644 index 0387eb59a..000000000 --- a/modules/client/back/methods/client/specs/getTransactions.spec.js +++ /dev/null @@ -1,21 +0,0 @@ -const models = require('vn-loopback/server/server').models; - -describe('Client getTransations', () => { - it('should call getTransations() method to receive a list of Web Payments from Bruce Wayne', async() => { - const tx = await models.Client.beginTransaction({}); - - try { - const options = {transaction: tx}; - - const filter = {where: {clientFk: 1101}}; - const result = await models.Client.getTransactions(filter, options); - - expect(result[1].id).toBeTruthy(); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); -}); diff --git a/modules/client/back/methods/client/specs/transactions.spec.js b/modules/client/back/methods/client/specs/transactions.spec.js new file mode 100644 index 000000000..b4af40195 --- /dev/null +++ b/modules/client/back/methods/client/specs/transactions.spec.js @@ -0,0 +1,66 @@ +const models = require('vn-loopback/server/server').models; + +describe('Client transactions', () => { + it('should call transactions() method to receive a list of Web Payments from Bruce Wayne', async() => { + const tx = await models.Client.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ctx = {}; + const filter = {where: {clientFk: 1101}}; + const result = await models.Client.transactions(ctx, filter, options); + + expect(result[1].id).toBeTruthy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should call transactions() method filtering by orderFk', async() => { + const tx = await models.Client.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ctx = {args: {orderFk: 6}}; + const filter = {}; + const result = await models.Client.transactions(ctx, filter, options); + + const firstRow = result[0]; + + expect(result.length).toEqual(1); + expect(firstRow.id).toEqual(6); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should call transactions() method filtering by amount', async() => { + const tx = await models.Client.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ctx = {args: {amount: 40}}; + const filter = {}; + const result = await models.Client.transactions(ctx, filter, options); + + const randomIndex = Math.floor(Math.random() * result.length); + const transaction = result[randomIndex]; + + expect(transaction.amount).toEqual(40); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/client/back/methods/client/transactions.js b/modules/client/back/methods/client/transactions.js new file mode 100644 index 000000000..691910721 --- /dev/null +++ b/modules/client/back/methods/client/transactions.js @@ -0,0 +1,84 @@ + +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('transactions', { + description: 'Returns customer transactions', + accessType: 'READ', + accepts: [ + { + arg: 'filter', + type: 'object', + description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', + http: {source: 'query'} + }, + { + arg: 'orderFk', + type: 'number', + http: {source: 'query'} + }, + { + arg: 'clientFk', + type: 'number', + http: {source: 'query'} + }, + { + arg: 'amount', + type: 'number', + http: {source: 'query'} + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/transactions`, + verb: 'GET' + } + }); + + Self.transactions = async(ctx, filter, options) => { + const args = ctx.args; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const where = buildFilter(args, (param, value) => { + switch (param) { + case 'orderFk': + return {'t.id': value}; + case 'clientFk': + return {'t.clientFk': value}; + case 'amount': + return {'t.amount': (value * 100)}; + } + }); + + filter = mergeFilters(filter, {where}); + + const conn = Self.dataSource.connector; + const stmt = new ParameterizedSQL(` + 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`); + + stmt.merge(conn.makeSuffix(filter, 't')); + + return Self.rawStmt(stmt, myOptions); + }; +}; diff --git a/modules/client/back/models/client-methods.js b/modules/client/back/models/client-methods.js index 9241d80cf..fc77fc090 100644 --- a/modules/client/back/models/client-methods.js +++ b/modules/client/back/models/client-methods.js @@ -13,7 +13,7 @@ module.exports = Self => { require('../methods/client/getCard')(Self); require('../methods/client/getDebt')(Self); require('../methods/client/getMana')(Self); - require('../methods/client/getTransactions')(Self); + require('../methods/client/transactions')(Self); require('../methods/client/hasCustomerRole')(Self); require('../methods/client/isValidClient')(Self); require('../methods/client/lastActiveTickets')(Self); diff --git a/modules/client/front/web-payment/index.html b/modules/client/front/web-payment/index.html index 203d4bb60..2ecfc950b 100644 --- a/modules/client/front/web-payment/index.html +++ b/modules/client/front/web-payment/index.html @@ -1,55 +1,39 @@ - + - + - - - - State - Id - Date - Amount - - - - - - - - - - - - {{::transaction.id}} - {{::transaction.created | date:'dd/MM/yyyy HH:mm'}} - {{::transaction.amount | currency: 'EUR':2}} - - - - - - - + + + + State + Id + Date + Amount + + + + + + + + + + + + {{::transaction.id}} + {{::transaction.created | date:'dd/MM/yyyy HH:mm'}} + {{::transaction.amount | currency: 'EUR':2}} + + + + + + + \ No newline at end of file