From 75612175a9be6cca5c59100e7a137e375d140ae9 Mon Sep 17 00:00:00 2001 From: Dave Sag Date: Wed, 5 Jun 2024 06:34:36 +1000 Subject: [PATCH 1/2] #775 add includeTotals param to getTransactions --- src/api/private/getTransactions.js | 10 +++++++--- src/utils/fixKeys.js | 10 +++++----- src/utils/payloadBuilder.js | 4 ++-- src/validation/isArrayOf.js | 4 ++-- src/validation/isPositiveNumber.js | 4 ++-- src/validation/isRequired.js | 8 ++++---- src/validation/isString.js | 4 ++-- test/helpers/privateHandlerTest.js | 4 ++-- test/unit/api/private/getTransactions.test.js | 7 +++++-- 9 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/api/private/getTransactions.js b/src/api/private/getTransactions.js index 52d03f4..408f1df 100644 --- a/src/api/private/getTransactions.js +++ b/src/api/private/getTransactions.js @@ -4,11 +4,13 @@ const { defaultParams } = require('../../defaults') const { validateFields } = require('../../validation') const isPositiveNumber = require('../../validation/isPositiveNumber') const isTime = require('../../validation/isTime') +const isOneOf = require('../../validation/isOneOf') const validation = { accountGuid: ['isRequired', 'isGuid'], pageIndex: ['isPositiveNumber'], - pageSize: [isPositiveNumber(5000)] + pageSize: [isPositiveNumber(5000)], + includeTotals: [isOneOf(['true', 'false'])] } // https://www.independentreserve.com/products/api#GetTransactions @@ -21,7 +23,8 @@ const getTransactions = (apiKey, apiSecret) => { toTimestampUtc, txTypes, pageIndex = defaultParams.pageIndex, - pageSize = defaultParams.pageSize + pageSize = defaultParams.pageSize, + includeTotals = 'true' }) => { const payload = { accountGuid, @@ -29,7 +32,8 @@ const getTransactions = (apiKey, apiSecret) => { toTimestampUtc, txTypes, pageIndex, - pageSize + pageSize, + includeTotals } // eslint-disable-next-line fp/no-unused-expression validateFields(payload, { diff --git a/src/utils/fixKeys.js b/src/utils/fixKeys.js index ce0462d..6756686 100644 --- a/src/utils/fixKeys.js +++ b/src/utils/fixKeys.js @@ -5,11 +5,11 @@ const fixKeys = input => ? Array.isArray(input) ? input.map(fixKeys) : typeof input === 'object' - ? Object.keys(input).reduce( - (acc, elem) => ({ ...acc, [downcase(elem)]: fixKeys(input[elem]) }), - {} - ) - : input + ? Object.keys(input).reduce( + (acc, elem) => ({ ...acc, [downcase(elem)]: fixKeys(input[elem]) }), + {} + ) + : input : input module.exports = fixKeys diff --git a/src/utils/payloadBuilder.js b/src/utils/payloadBuilder.js index 3520d1e..df7c590 100644 --- a/src/utils/payloadBuilder.js +++ b/src/utils/payloadBuilder.js @@ -24,8 +24,8 @@ const payloadBuilder = elem === 'signature' ? acc : Array.isArray(data[elem]) - ? [...acc, `${elem}=${data[elem].join(',')}`] - : [...acc, `${elem}=${data[elem]}`], + ? [...acc, `${elem}=${data[elem].join(',')}`] + : [...acc, `${elem}=${data[elem]}`], [url] ) .join(',') diff --git a/src/validation/isArrayOf.js b/src/validation/isArrayOf.js index 15ec917..3f5890d 100644 --- a/src/validation/isArrayOf.js +++ b/src/validation/isArrayOf.js @@ -12,7 +12,7 @@ const isArrayOf = (allowed.length === 0 || (allowed.length !== 0 && value.every(val => allowed.includes(val))))) ? null : allowed.length === 0 - ? 'Expected an array' - : `Expected an array containing ${allowed.join(',')}` + ? 'Expected an array' + : `Expected an array containing ${allowed.join(',')}` module.exports = isArrayOf diff --git a/src/validation/isPositiveNumber.js b/src/validation/isPositiveNumber.js index f45b97a..86806a4 100644 --- a/src/validation/isPositiveNumber.js +++ b/src/validation/isPositiveNumber.js @@ -6,7 +6,7 @@ const isPositiveNumber = max => value => (typeof value === 'number' && (isNaN(value) || (value > 0 && (!max || (max && !(value > max)))))) ? null : max - ? `Expected a positive number no greater than ${max}` - : 'Expected a positive number' + ? `Expected a positive number no greater than ${max}` + : 'Expected a positive number' module.exports = isPositiveNumber diff --git a/src/validation/isRequired.js b/src/validation/isRequired.js index 15723b0..2e8cd29 100644 --- a/src/validation/isRequired.js +++ b/src/validation/isRequired.js @@ -3,10 +3,10 @@ const checkRequired = value => typeof value === 'number' ? !isNaN(value) : Array.isArray(value) || typeof value === 'string' - ? value.length !== 0 - : value === undefined || value === null - ? false - : typeof value === 'object' && Object.keys(value).length !== 0 + ? value.length !== 0 + : value === undefined || value === null + ? false + : typeof value === 'object' && Object.keys(value).length !== 0 const isRequired = value => (checkRequired(value) ? null : 'Field is required') diff --git a/src/validation/isString.js b/src/validation/isString.js index 9512d74..6c31dcb 100644 --- a/src/validation/isString.js +++ b/src/validation/isString.js @@ -4,7 +4,7 @@ const isString = max => value => !value || (typeof value === 'string' && (!max || (max && !(value.length > max)))) ? null : max - ? `Expected a string no longer than ${max} characters` - : 'Expected a string' + ? `Expected a string no longer than ${max} characters` + : 'Expected a string' module.exports = isString diff --git a/test/helpers/privateHandlerTest.js b/test/helpers/privateHandlerTest.js index 16b8b86..dd72022 100644 --- a/test/helpers/privateHandlerTest.js +++ b/test/helpers/privateHandlerTest.js @@ -69,8 +69,8 @@ const doTest = ({ handler, params, useDefaults, validation }) => { } : params : useDefaults - ? defaultParams - : undefined + ? defaultParams + : undefined let result diff --git a/test/unit/api/private/getTransactions.test.js b/test/unit/api/private/getTransactions.test.js index 839499c..64ebdf5 100644 --- a/test/unit/api/private/getTransactions.test.js +++ b/test/unit/api/private/getTransactions.test.js @@ -1,6 +1,7 @@ const doTest = require('../../../helpers/privateHandlerTest') const isPositiveNumber = require('../../../../src/validation/isPositiveNumber') const isTime = require('../../../../src/validation/isTime') +const isOneOf = require('../../../../src/validation/isOneOf') const fromTimestampUtc = '2014-08-01T08:00:00Z' const toTimestampUtc = '2016-08-01T08:00:00Z' @@ -11,7 +12,8 @@ const config = { accountGuid: 'dd015a29-8f73-4469-a5fa-ea91544dfcda', fromTimestampUtc, toTimestampUtc, - txTypes: ['Brokerage', 'Trade'] + txTypes: ['Brokerage', 'Trade'], + includeTotals: 'false' }, useDefaults: true, validation: { @@ -19,7 +21,8 @@ const config = { fromTimestampUtc: ['isRequired', isTime({ before: toTimestampUtc })], toTimestampUtc: ['isRequired', isTime({ after: fromTimestampUtc })], pageIndex: ['isPositiveNumber'], - pageSize: [isPositiveNumber(5000)] + pageSize: [isPositiveNumber(5000)], + includeTotals: [isOneOf(['true', 'false'])] } } From d36a406dd8797d42b05cec7d4c2db03a0ed045b8 Mon Sep 17 00:00:00 2001 From: Dave Sag Date: Wed, 5 Jun 2024 06:52:09 +1000 Subject: [PATCH 2/2] #775 fix test --- test/unit/api/private/getTransactions.test.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/unit/api/private/getTransactions.test.js b/test/unit/api/private/getTransactions.test.js index 64ebdf5..466ca7a 100644 --- a/test/unit/api/private/getTransactions.test.js +++ b/test/unit/api/private/getTransactions.test.js @@ -6,14 +6,13 @@ const isOneOf = require('../../../../src/validation/isOneOf') const fromTimestampUtc = '2014-08-01T08:00:00Z' const toTimestampUtc = '2016-08-01T08:00:00Z' -const config = { +const config1 = { handler: 'getTransactions', params: { accountGuid: 'dd015a29-8f73-4469-a5fa-ea91544dfcda', fromTimestampUtc, toTimestampUtc, - txTypes: ['Brokerage', 'Trade'], - includeTotals: 'false' + txTypes: ['Brokerage', 'Trade'] }, useDefaults: true, validation: { @@ -26,4 +25,14 @@ const config = { } } -doTest(config) +doTest(config1) + +const config2 = { + ...config1, + params: { + ...config1.params, + includeTotals: 'false' + } +} + +doTest(config2)