From a1df1bcb9d39c7e0c6faa818adb9ba1f34987de7 Mon Sep 17 00:00:00 2001 From: Matt Carroll Date: Fri, 13 Sep 2024 10:02:54 +1000 Subject: [PATCH] ON-43523 # Added formatDate to getDisplayDetailsFromFormSubmissionPayment --- src/paymentService.ts | 38 +++++------ src/replaceCustomValues.ts | 5 ++ .../__snapshots__/paymentService.test.ts.snap | 2 +- tests/paymentService.test.ts | 63 +++++++------------ 4 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/paymentService.ts b/src/paymentService.ts index a9610b1..e8dfb3a 100644 --- a/src/paymentService.ts +++ b/src/paymentService.ts @@ -116,30 +116,32 @@ type PaymentDisplayDetail = { * * ```js * const detailItems = - * paymentService.getDisplayDetailsFromFormSubmissionPayment({ + * paymentService.getDisplayDetailsFromFormSubmissionPayment( * formSubmissionPayment, - * formatCurrency, - * }) + * { + * formatCurrency, + * formatDateTime, + * formatDate, + * }, + * ) * ``` * + * @param formSubmissionPayment * @param options * @returns */ -export const getDisplayDetailsFromFormSubmissionPayment = ({ - formSubmissionPayment, - formatCurrency, - formatDateTime, -}: { +export const getDisplayDetailsFromFormSubmissionPayment = ( /** The form submission payment to get the details from */ - formSubmissionPayment: SubmissionTypes.FormSubmissionPayment - /** A function to format any curreny values */ - formatCurrency: ReplaceInjectablesFormatters['formatCurrency'] - /** - * A function to format any dates. If this is not passed, datetimes will be - * returned as iso strings. - */ - formatDateTime: ReplaceInjectablesFormatters['formatDateTime'] -}): PaymentDisplayDetail[] => { + formSubmissionPayment: SubmissionTypes.FormSubmissionPayment, + { + formatCurrency, + formatDateTime, + formatDate, + }: Pick< + ReplaceInjectablesFormatters, + 'formatCurrency' | 'formatDate' | 'formatDateTime' + >, +): PaymentDisplayDetail[] => { switch (formSubmissionPayment.type) { case 'NSW_GOV_PAY': { const { paymentTransaction } = formSubmissionPayment @@ -417,7 +419,7 @@ export const getDisplayDetailsFromFormSubmissionPayment = ({ { key: 'settlementDate', label: 'Settlement Date', - value: formatDateTime(paymentTransaction.settlementDate), + value: formatDate(paymentTransaction.settlementDate), }, ] } diff --git a/src/replaceCustomValues.ts b/src/replaceCustomValues.ts index 14142ff..1907c0d 100644 --- a/src/replaceCustomValues.ts +++ b/src/replaceCustomValues.ts @@ -18,10 +18,15 @@ import { import { findFormElement, flattenFormElements } from './formElementsService' export type ReplaceInjectablesFormatters = { + /** A function to format any date/times. */ formatDateTime: (value: string) => string + /** A function to format any dates. */ formatDate: (value: string) => string + /** A function to format any times. */ formatTime: (value: string) => string + /** A function to format any numbers. */ formatNumber: (value: number) => string + /** A function to format any numbers as currency. */ formatCurrency: (value: number) => string } diff --git a/tests/__snapshots__/paymentService.test.ts.snap b/tests/__snapshots__/paymentService.test.ts.snap index 4ca79de..5f022c2 100644 --- a/tests/__snapshots__/paymentService.test.ts.snap +++ b/tests/__snapshots__/paymentService.test.ts.snap @@ -200,7 +200,7 @@ exports[`getDisplayDetailsFromFormSubmissionPayment Westpac gets the correct det { "key": "settlementDate", "label": "Settlement Date", - "value": "11/09/2024 12:00:00 PM", + "value": "11/09/2024", }, ] `; diff --git a/tests/paymentService.test.ts b/tests/paymentService.test.ts index 59d4edd..95c7875 100644 --- a/tests/paymentService.test.ts +++ b/tests/paymentService.test.ts @@ -2,8 +2,11 @@ import { SubmissionTypes } from '@oneblink/types' import { paymentService } from '../src' describe('getDisplayDetailsFromFormSubmissionPayment', () => { - const formatCurrency = (amount: number) => `$${amount.toFixed(2)}` - const formatDateTime = () => '11/09/2024 12:00:00 PM' + const formatters = { + formatCurrency: (amount: number) => `$${amount.toFixed(2)}`, + formatDateTime: () => '11/09/2024 12:00:00 PM', + formatDate: () => '11/09/2024', + } describe('NSW Gov Pay', () => { const formSubmissionPayment: SubmissionTypes.FormSubmissionPayment = { @@ -43,11 +46,8 @@ describe('getDisplayDetailsFromFormSubmissionPayment', () => { it('gets the correct details', () => { const details = paymentService.getDisplayDetailsFromFormSubmissionPayment( - { - formSubmissionPayment, - formatCurrency, - formatDateTime, - }, + formSubmissionPayment, + formatters, ) expect(details).toMatchSnapshot() }) @@ -55,21 +55,18 @@ describe('getDisplayDetailsFromFormSubmissionPayment', () => { it('using BPAY', () => { const details = paymentService.getDisplayDetailsFromFormSubmissionPayment( { - formSubmissionPayment: { - ...formSubmissionPayment, - paymentTransaction: { - ...formSubmissionPayment.paymentTransaction, - // @ts-expect-error Wrong - agencyCompletionPayment: { - ...formSubmissionPayment.paymentTransaction - .agencyCompletionPayment, - paymentMethod: 'BPAY', - }, + ...formSubmissionPayment, + paymentTransaction: { + ...formSubmissionPayment.paymentTransaction, + // @ts-expect-error Wrong + agencyCompletionPayment: { + ...formSubmissionPayment.paymentTransaction + .agencyCompletionPayment, + paymentMethod: 'BPAY', }, }, - formatCurrency, - formatDateTime, }, + formatters, ) expect(details.find((d) => d.key === 'billerCode')?.value).toBe( @@ -105,11 +102,8 @@ describe('getDisplayDetailsFromFormSubmissionPayment', () => { } it('gets the correct details', () => { const details = paymentService.getDisplayDetailsFromFormSubmissionPayment( - { - formSubmissionPayment, - formatCurrency, - formatDateTime, - }, + formSubmissionPayment, + formatters, ) expect(details).toMatchSnapshot() }) @@ -158,22 +152,16 @@ describe('getDisplayDetailsFromFormSubmissionPayment', () => { it('gets the correct details - v1', () => { const details = paymentService.getDisplayDetailsFromFormSubmissionPayment( - { - formSubmissionPayment: formSubmissionPaymentv1, - formatCurrency, - formatDateTime, - }, + formSubmissionPaymentv1, + formatters, ) expect(details).toMatchSnapshot() }) it('gets the correct details - v2', () => { const details = paymentService.getDisplayDetailsFromFormSubmissionPayment( - { - formSubmissionPayment: formSubmissionPaymentv2, - formatCurrency, - formatDateTime, - }, + formSubmissionPaymentv2, + formatters, ) expect(details).toMatchSnapshot() }) @@ -209,11 +197,8 @@ describe('getDisplayDetailsFromFormSubmissionPayment', () => { it('gets the correct details', () => { const details = paymentService.getDisplayDetailsFromFormSubmissionPayment( - { - formSubmissionPayment, - formatCurrency, - formatDateTime, - }, + formSubmissionPayment, + formatters, ) expect(details).toMatchSnapshot() })