Skip to content

Commit

Permalink
use generateRecurringPaymentRecord to remap transaction so that it ca…
Browse files Browse the repository at this point in the history
…n be used by processRecurringPayment
  • Loading branch information
jaucourt committed Dec 4, 2024
1 parent 8805739 commit 25609db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { TRANSACTION_STAGING_TABLE, TRANSACTION_STAGING_HISTORY_TABLE } from '..
import AwsMock from 'aws-sdk'
import { POCL_DATA_SOURCE, DDE_DATA_SOURCE } from '@defra-fish/business-rules-lib'
import moment from 'moment'
import { processRecurringPayment, generateRecurringPaymentRecord } from '../../recurring-payments.service.js'

jest.mock('../../reference-data.service.js', () => ({
...jest.requireActual('../../reference-data.service.js'),
Expand Down Expand Up @@ -64,9 +65,12 @@ jest.mock('@defra-fish/business-rules-lib', () => ({
START_AFTER_PAYMENT_MINUTES: 30
}))

jest.mock('../../recurring-payments.service.js')

describe('transaction service', () => {
beforeAll(() => {
TRANSACTION_STAGING_TABLE.TableName = 'TestTable'
processRecurringPayment.mockResolvedValue({})
})

beforeEach(jest.clearAllMocks)
Expand Down Expand Up @@ -125,6 +129,7 @@ describe('transaction service', () => {
[
'licences with a recurring payment',
() => {
processRecurringPayment.mockResolvedValueOnce({ recurringPayment: new RecurringPayment() })
const mockRecord = mockFinalisedTransactionRecord()
mockRecord.payment.recurring = {
name: 'Test name',
Expand Down Expand Up @@ -369,6 +374,29 @@ describe('transaction service', () => {
expect(paymentJournal.total).toBe(cost)
})
})

describe('recurring payment processing', () => {
it('passes transaction record to generateRecurringPaymentRecord', async () => {
let grprArg
generateRecurringPaymentRecord.mockImplementationOnce(transaction => {
grprArg = JSON.parse(JSON.stringify(transaction))
})
const mockRecord = mockFinalisedTransactionRecord()
AwsMock.DynamoDB.DocumentClient.__setResponse('get', { Item: mockRecord })
await processQueue({ id: mockRecord.id })
// jest.fn args aren't immutable and transaction is changed in processQueue, so we use our clone that _is_ immutable
expect(grprArg).toEqual(mockRecord)
})

it('passes return value of generateRecurringPaymentRecord to processRecurringPayment', async () => {
const rprSymbol = Symbol('rpr')
const finalisedTransaction = mockFinalisedTransactionRecord()
generateRecurringPaymentRecord.mockReturnValueOnce(rprSymbol)
AwsMock.DynamoDB.DocumentClient.__setResponse('get', { Item: finalisedTransaction })
await processQueue({ id: finalisedTransaction.id })
expect(processRecurringPayment).toHaveBeenCalledWith(rprSymbol, expect.any(Contact))
})
})
})

describe('.getTransactionJournalRefNumber', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@defra-fish/dynamics-lib'
import { DDE_DATA_SOURCE, FULFILMENT_SWITCHOVER_DATE, POCL_TRANSACTION_SOURCES } from '@defra-fish/business-rules-lib'
import { getReferenceDataForEntityAndId, getGlobalOptionSetValue, getReferenceDataForEntity } from '../reference-data.service.js'
import { processRecurringPayment } from '../recurring-payments.service.js'
import { generateRecurringPaymentRecord, processRecurringPayment } from '../recurring-payments.service.js'
import { resolveContactPayload } from '../contacts.service.js'
import { retrieveStagedTransaction } from './retrieve-transaction.js'
import { TRANSACTION_STAGING_TABLE, TRANSACTION_STAGING_HISTORY_TABLE } from '../../config.js'
Expand Down Expand Up @@ -65,7 +65,7 @@ export async function processQueue ({ id }) {
isRenewal
)

const { recurringPayment } = await processRecurringPayment(transactionRecord, contact)
const { recurringPayment } = await processRecurringPayment(generateRecurringPaymentRecord(transactionRecord), contact)
if (recurringPayment) {
entities.push(recurringPayment)
}
Expand Down

0 comments on commit 25609db

Please sign in to comment.