diff --git a/app/services/clients/ledger.client.js b/app/services/clients/ledger.client.js index 4b2137244d..6de87e2912 100644 --- a/app/services/clients/ledger.client.js +++ b/app/services/clients/ledger.client.js @@ -1,6 +1,8 @@ 'use strict' -const baseClient = require('./base-client/base.client') +const { Client } = require('@govuk-pay/pay-js-commons/lib/utils/axios-base-client/axios-base-client') +const { configureClient } = require('./base/config') +const urlJoin = require('url-join') const { legacyConnectorTransactionParity, legacyConnectorEventsParity, @@ -18,53 +20,50 @@ const defaultOptions = { limit_total: true } -const transaction = function transaction (id, gatewayAccountId, options = {}) { - const configuration = Object.assign({ - url: `/v1/transaction/${id}`, - qs: { - account_id: gatewayAccountId, - ...options.transaction_type && { transaction_type: options.transaction_type } - }, - description: 'Get individual transaction details', - transform: legacyConnectorTransactionParity - }, defaultOptions, options) - return baseClient.get(configuration) +const transaction = async function transaction (id, gatewayAccountId, options = {}) { + this.client = new Client(defaultOptions.service) + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + let url = `${baseUrl}/v1/transaction/${id}?account_id=${gatewayAccountId}` + if ( options.transaction_type ) { + url = `${url}&transaction_type=${options.transaction_type}` + } + configureClient(this.client, url) + const response = await this.client.get(url, 'Get individual transaction details') + const body = legacyConnectorTransactionParity(response.data) + return body } -const transactionWithAccountOverride = function transactionWithAccountOverride (id, options = {}) { - const configuration = Object.assign({ - url: `/v1/transaction/${id}`, - qs: { - override_account_id_restriction: true - }, - description: 'Get individual transaction details with no accountId restriction' - }, defaultOptions, options) - return baseClient.get(configuration) +const transactionWithAccountOverride = async function transactionWithAccountOverride (id, options = {}) { + const url = urlJoin(defaultOptions.baseUrl,'/v1/transaction', id) + this.client = new Client(defaultOptions.service) + const fullUrl = `${url}?override_account_id_restriction=true` + configureClient(this.client, fullUrl) + const response = await this.client.get(fullUrl, 'Get individual transaction details with no accountId restriction') + return response.data } -function getDisputesForTransaction (id, gatewayAccountId, options = {}) { - const configuration = Object.assign({ - url: `/v1/transaction/${id}/transaction`, - qs: { - gateway_account_id: gatewayAccountId, - transaction_type: 'DISPUTE' - }, - description: 'Get disputes for payment' - }, defaultOptions, options) - return baseClient.get(configuration) +async function getDisputesForTransaction (id, gatewayAccountId, options = {}) { + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + const url = urlJoin(baseUrl,'/v1/transaction', id, 'transaction') + this.client = new Client(defaultOptions.service) + const fullUrl = `${url}?gateway_account_id=${gatewayAccountId}&transaction_type=DISPUTE` + configureClient(this.client, fullUrl) + const response = await this.client.get(fullUrl, 'Get disputes for payment') + return response.data } -const events = function events (transactionId, gatewayAccountId, options = {}) { - const configuration = Object.assign({ - url: `/v1/transaction/${transactionId}/event`, - qs: { gateway_account_id: gatewayAccountId }, - description: 'List events for a given transaction', - transform: legacyConnectorEventsParity - }, defaultOptions, options) - return baseClient.get(configuration) +const events = async function events (transactionId, gatewayAccountId, options = {}) { + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + const url = urlJoin(baseUrl,'/v1/transaction', transactionId, 'event') + this.client = new Client(defaultOptions.service) + const fullUrl = `${url}?gateway_account_id=${gatewayAccountId}` + configureClient(this.client, fullUrl) + const response = await this.client.get(fullUrl, 'List events for a given transaction') + const body = legacyConnectorEventsParity(response.data) + return body } -const transactions = function transactions (gatewayAccountIds = [], filters = {}, options = {}) { +const transactions = async function transactions (gatewayAccountIds = [], filters = {}, options = {}) { const formatOptions = { arrayFormat: 'comma' } const path = '/v1/transaction' const params = { @@ -75,50 +74,46 @@ const transactions = function transactions (gatewayAccountIds = [], filters = {} const formattedParams = qs.stringify(params, formatOptions) const formattedFilterParams = getQueryStringForParams(filters, true, true) - const configuration = Object.assign({ - url: `${path}?${formattedParams}&${formattedFilterParams}`, - description: 'List transactions for a given gateway account ID', - transform: legacyConnectorTransactionsParity, - additionalLoggingFields: { - gateway_account_ids: gatewayAccountIds, - multiple_accounts: gatewayAccountIds.length > 1, - filters: Object.keys(filters).sort().join(', ') + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + const url = `${baseUrl}${path}?${formattedParams}&${formattedFilterParams}` + this.client = new Client(defaultOptions.service) + configureClient(this.client, url) + const response = await this.client.get( + url, + 'List transactions for a given gateway account ID', + { + data : { + gateway_account_ids: gatewayAccountIds, + multiple_accounts: gatewayAccountIds.length > 1, + filters: Object.keys(filters).sort().join(', ') + } } - }, defaultOptions, options) - - return baseClient.get(configuration) + ) + const body = legacyConnectorTransactionsParity(response.data) + return body } -const transactionSummary = function transactionSummary (gatewayAccountId, fromDate, toDate, options = {}) { +const transactionSummary = async function transactionSummary (gatewayAccountId, fromDate, toDate, options = {}) { const path = '/v1/report/transactions-summary' - const configuration = Object.assign({ - url: path, - qs: { - account_id: gatewayAccountId, - from_date: fromDate, - to_date: toDate - }, - description: 'Transaction summary statistics for a given gateway account ID', - transform: legacyConnectorTransactionSummaryParity - }, defaultOptions, options) - - return baseClient.get(configuration) + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + const url = `${baseUrl}${path}?account_id=${gatewayAccountId}&from_date=${fromDate}&to_date=${toDate}` + this.client = new Client(defaultOptions.service) + configureClient(this.client, url) + const response = await this.client.get(url,'Transaction summary statistics for a given gateway account ID') + const body = legacyConnectorTransactionSummaryParity(response.data) + return body } -const payouts = function payouts (gatewayAccountIds = [], page = 1, displaySize, options = {}) { - const configuration = Object.assign({ - url: '/v1/payout', - qs: { - // qsStringifyOptions doesn't seem to be accepted here and the request library is deprecated for upstream changes - gateway_account_id: gatewayAccountIds.join(','), - state: 'paidout', - ...displaySize && { display_size: displaySize }, - page - }, - description: 'List payouts for a given gateway account ID' - }, defaultOptions, options) - - return baseClient.get(configuration) +const payouts = async function payouts (gatewayAccountIds = [], page = 1, displaySize, options = {}) { + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + let url = `${baseUrl}/v1/payout?gateway_account_id=${gatewayAccountIds.join(',')}&state=paidout&page=${page}` + if ( displaySize ) { + url = `${url}&display_size=${displaySize}` + } + this.client = new Client(defaultOptions.service) + configureClient(this.client, url) + const response = await this.client.get(url,'List payouts for a given gateway account ID') + return response.data } /** @@ -127,39 +122,26 @@ const payouts = function payouts (gatewayAccountIds = [], page = 1, displaySize, * we haven't realised yet. For now, we additionally send the gateway account ID but the intention is to remove the need * to send this. */ -const agreements = function agreements (serviceId, live, accountId, page = 1, options = {}) { - const config = { - url: '/v1/agreement', - qs: { - service_id: serviceId, - live, - account_id: accountId, - page, - ...options.filters - }, - description: 'List agreements for a given service and environment', - baseUrl: process.env.LEDGER_URL, - json: true, - service: 'ledger', - ...options +const agreements = async function agreements (serviceId, live, accountId, page = 1, options = {}) { + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + let url = `${baseUrl}/v1/agreement?service_id=${serviceId}&account_id=${accountId}&live=${live}&page=${page}` + if ( options.filters ) { + const filterParams = new URLSearchParams(options.filters).toString() + url = `${url}&${filterParams}` } - - return baseClient.get(config) + this.client = new Client(defaultOptions.service) + configureClient(this.client, url) + const response = await this.client.get(url,'List agreements for a given service and environment') + return response.data } -const agreement = function agreement (id, serviceId, options = {}) { - const config = { - url: `/v1/agreement/${id}`, - qs: { - service_id: serviceId - }, - description: 'Get agreement by ID', - baseUrl: process.env.LEDGER_URL, - json: true, - service: 'ledger', - ...options - } - return baseClient.get(config) +const agreement = async function agreement (id, serviceId, options = {}) { + const baseUrl = options.baseUrl ? options.baseUrl : defaultOptions.baseUrl + let url = `${baseUrl}/v1/agreement/${id}?service_id=${serviceId}` + this.client = new Client(defaultOptions.service) + configureClient(this.client, url) + const response = await this.client.get(url,'Get agreement by ID') + return response.data } module.exports = {