From a1701ca9affc36433343d7b08f570d03120f8e86 Mon Sep 17 00:00:00 2001 From: "ola.tomoloju" Date: Thu, 14 Dec 2023 14:27:16 +0000 Subject: [PATCH] PP-11877 adding filter persistence logic, preventing submission to ledger api, applying validation logic to all services transaction UI page. --- .../get.controller.js | 2 +- .../transaction-list.controller.js | 24 +++++++++++-------- app/utils/filters.js | 13 +++++----- app/utils/transaction-view.js | 5 ++-- app/views/transactions/index.njk | 12 +++++----- app/views/transactions/list.njk | 5 +++- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/app/controllers/all-service-transactions/get.controller.js b/app/controllers/all-service-transactions/get.controller.js index b59c123404..731ce820de 100644 --- a/app/controllers/all-service-transactions/get.controller.js +++ b/app/controllers/all-service-transactions/get.controller.js @@ -40,7 +40,7 @@ module.exports = async function getTransactionsForAllServices (req, res, next) { const searchResultOutput = await transactionService.search(userPermittedAccountsSummary.gatewayAccountIds, filters.result) const cardTypes = await client.getAllCardTypes() const downloadRoute = filterLiveAccounts ? paths.allServiceTransactions.download : paths.formattedPathFor(paths.allServiceTransactions.downloadStatusFilter, 'test') - const model = buildPaymentList(searchResultOutput, cardTypes, null, filters.result, downloadRoute, req.session.backPath) + const model = buildPaymentList(searchResultOutput, cardTypes, null, filters.result,filters.dateRangeState, downloadRoute, req.session.backPath) delete req.session.backPath model.search_path = filterLiveAccounts ? paths.allServiceTransactions.index : paths.formattedPathFor(paths.allServiceTransactions.indexStatusFilter, 'test') model.filtersDescription = describeFilters(filters.result) diff --git a/app/controllers/transactions/transaction-list.controller.js b/app/controllers/transactions/transaction-list.controller.js index 7f4728ef8f..acee1b47bd 100644 --- a/app/controllers/transactions/transaction-list.controller.js +++ b/app/controllers/transactions/transaction-list.controller.js @@ -21,22 +21,26 @@ module.exports = async function showTransactionList (req, res, next) { req.session.filters = url.parse(req.url).query - // This ssort of error handling may not be appropriate - //if (!filters.dateRangeState.validDateRange) { - // console.log('You have entered an invalid date range') - // return next(new Error('You have entered an invalid date range')) - //} - if (!filters.valid) { return next(new Error('Invalid search')) } + let noTransactionSearchResults = { + total: 0, + count: 0, + page: 1, + results: [], + _links: {}, + filters: {}, + } let result + try { - result = await Promise.all([ - transactionService.search([accountId], filters.result), - client.getAllCardTypes() - ]) + result = await Promise.all([ + (filters.dateRangeState.isInvalidDateRange) ? + noTransactionSearchResults : transactionService.search([accountId], filters.result) , + client.getAllCardTypes() + ]) } catch (error) { return next(error) } diff --git a/app/utils/filters.js b/app/utils/filters.js index 0ce51657fa..daea161f8b 100644 --- a/app/utils/filters.js +++ b/app/utils/filters.js @@ -1,6 +1,7 @@ 'use strict' const qs = require('qs') +const moment = require('moment-timezone') const check = require('check-types') const Paginator = require('../utils/paginator.js') const states = require('../utils/states') @@ -25,16 +26,14 @@ function trimFilterValues (filters) { } function validateDateRange(filters){ - var fromDate = new Date(filters.fromDate).getTime() - var toDate = new Date(filters.toDate).getTime() - var isValid = false + let result = moment(filters.fromDate, 'DD/MM/YYYY').isAfter(moment(filters.toDate, 'DD/MM/YYYY'))? 1: -1; + var isInvalid = false - if (fromDate < toDate) { - isValid = true + if (result == 1) { + isInvalid = true } - return { - validDateRange: isValid, + isInvalidDateRange: isInvalid, fromDateParam: filters.fromDate, toDateParam: filters.toDate } diff --git a/app/utils/transaction-view.js b/app/utils/transaction-view.js index 8703ebef37..bc42968792 100644 --- a/app/utils/transaction-view.js +++ b/app/utils/transaction-view.js @@ -27,12 +27,13 @@ module.exports = { connectorData.total = connectorData.total || (connectorData.results && connectorData.results.length) connectorData.totalOverLimit = connectorData.total > LEDGER_TRANSACTION_COUNT_LIMIT connectorData.showCsvDownload = showCsvDownload(connectorData, filtersResult) - connectorData.filtersDateRangeState = filtersDateRangeState + connectorData.isInvalidDateRange = filtersDateRangeState.isInvalidDateRange === true + connectorData.fromDateParam = filtersDateRangeState.fromDateParam + connectorData.toDateParam = filtersDateRangeState.toDateParam connectorData.totalFormatted = connectorData.total.toLocaleString() connectorData.maxLimitFormatted = parseInt(LEDGER_TRANSACTION_COUNT_LIMIT).toLocaleString() connectorData.paginationLinks = getPaginationLinks(connectorData) connectorData.hasPaginationLinks = !!getPaginationLinks(connectorData) - connectorData.hasPageSizeLinks = hasPageSizeLinks(connectorData) connectorData.pageSizeLinks = getPageSizeLinks(connectorData) diff --git a/app/views/transactions/index.njk b/app/views/transactions/index.njk index 923a38b1b7..a388211083 100644 --- a/app/views/transactions/index.njk +++ b/app/views/transactions/index.njk @@ -65,7 +65,11 @@ {% include "transactions/display-size.njk" %} -

+ {% if (isInvalidDateRange) %} +

End date must be after start date

+

The from date entered is {{fromDateParam}} and the to date entered is {{toDateParam}}

+ {% else %} +

{% if totalOverLimit %} Over {{maxLimitFormatted}} transactions {% else %} @@ -73,6 +77,7 @@ {% endif %} {{ filtersDescription | safe }}

+ {% endif %} {% if allServiceTransactions or permissions.transactions_download_read %} @@ -92,11 +97,6 @@ {% include "transactions/paginator.njk" %} {% include "transactions/list.njk" %} - - {% if (!filtersDateRangeState.isValid) %} -

You have entered an invalid date range, the from date must be earlier than the to date.

-

The from date entered is {{showInvalidDateRangeError.fromDateParam}} and the to date entered is {{showInvalidDateRangeError.toDateParam}}

- {% endif %} {% include "transactions/paginator.njk" %} diff --git a/app/views/transactions/list.njk b/app/views/transactions/list.njk index 07a38c40bc..a22aae0ee7 100644 --- a/app/views/transactions/list.njk +++ b/app/views/transactions/list.njk @@ -51,5 +51,8 @@ {% else %} -

There are no results for the filters you used.

+ {% if (isInvalidDateRange) %} + {% else %} +

There are no results for the filters you used.

+ {% endif %} {% endif %}