Skip to content

Commit

Permalink
Amended generateRecurringPaymentRecord to take a Permission object ra…
Browse files Browse the repository at this point in the history
…ther than using the permission embedded in the transaction, altered transaction queue processor to save RecurringPayment after Permission and Contact, removed contactId and activePermission fields as these were extraneous
  • Loading branch information
jaucourt committed Dec 6, 2024
1 parent 25609db commit a0362b6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ describe('recurring payment entity', () => {
recurringPayment.agreementId = 'c9267c6e-573d-488b-99ab-ea18431fc472'
recurringPayment.publicId = '649-213'
recurringPayment.status = 1
recurringPayment.contactId = 'b3d33cln-2e83-ea11-a811-000d3a649213'
recurringPayment.activePermission = 'a5b24adf-2e83-ea11-a811-000d3a649213'

recurringPayment.bindToEntity(RecurringPayment.definition.relationships.contact, contact)
recurringPayment.bindToEntity(RecurringPayment.definition.relationships.activePermission, permission)
Expand Down Expand Up @@ -66,9 +64,7 @@ describe('recurring payment entity', () => {
endDate: '2019-12-15T00:00:00Z',
agreementId: 'c9267c6e-573d-488b-99ab-ea18431fc472',
publicId: '649-213',
status: 1,
activePermission: 'a5b24adf-2e83-ea11-a811-000d3a649213',
contactId: 'b3d33cln-2e83-ea11-a811-000d3a649213'
status: 1
})
)
})
Expand All @@ -93,8 +89,6 @@ describe('recurring payment entity', () => {
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'[email protected]': `$${contact.uniqueContentId}`,
'[email protected]': `$${permission.uniqueContentId}`
})
Expand All @@ -121,9 +115,7 @@ describe('recurring payment entity', () => {
endDate: '2019-12-15T00:00:00Z',
agreementId: 'c9267c6e-573d-488b-99ab-ea18431fc472',
publicId: '649-213',
status: 1,
activePermission: 'a5b24adf-2e83-ea11-a811-000d3a649213',
contactId: 'b3d33cln-2e83-ea11-a811-000d3a649213'
status: 1
})
)
})
Expand All @@ -143,8 +135,6 @@ describe('recurring payment entity', () => {
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'[email protected]': `$${contact.uniqueContentId}`,
'[email protected]': `$${permission.uniqueContentId}`
})
Expand Down
24 changes: 0 additions & 24 deletions packages/dynamics-lib/src/entities/recurring-payment.entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,4 @@ export class RecurringPayment extends BaseEntity {
set status (status) {
super._setState('status', status)
}

/**
* The ID of the associated contact
* @type {string}
*/
get contactId () {
return super._getState('contactId')
}

set contactId (contactId) {
super._setState('contactId', contactId)
}

/**
* The ID of the associated active permission
* @type {string}
*/
get activePermission () {
return super._getState('activePermission')
}

set activePermission (activePermission) {
super._setState('activePermission', activePermission)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findDueRecurringPayments } from '@defra-fish/dynamics-lib'
import { findDueRecurringPayments, Permission } from '@defra-fish/dynamics-lib'
import { getRecurringPayments, processRecurringPayment, generateRecurringPaymentRecord } from '../recurring-payments.service.js'
import { createHash } from 'node:crypto'

Expand Down Expand Up @@ -89,7 +89,21 @@ const getMockPermission = () => ({
})

describe('recurring payments service', () => {
const createSampleTransactionRecord = () => ({ payment: { recurring: true }, permissions: [{}] })
const createSimpleSampleTransactionRecord = () => ({ payment: { recurring: true }, permissions: [{}] })
const createSamplePermission = overrides => {
const p = new Permission()
p.referenceNumber = 'ABC123'
p.issueDate = '2024-12-04T11:15:12Z'
p.startDate = '2024-12-04T11:45:12Z'
p.endDate = '2025-12-03T23:59:59.999Z'
p.stagingId = 'aaa-111-bbb-222'
p.isRenewal = false
p.isLicenseForYou = 1
for (const key in overrides) {
p[key] = overrides[key]
}
return p
}

beforeEach(jest.clearAllMocks)
describe('getRecurringPayments', () => {
Expand Down Expand Up @@ -148,7 +162,7 @@ describe('recurring payments service', () => {
update: () => {},
digest: () => samplePublicId
})
const result = await processRecurringPayment(createSampleTransactionRecord(), getMockContact())
const result = await processRecurringPayment(createSimpleSampleTransactionRecord(), getMockContact())
expect(result.recurringPayment.publicId).toBe(samplePublicId)
})

Expand All @@ -158,12 +172,12 @@ describe('recurring payments service', () => {
update,
digest: () => {}
})
const { recurringPayment } = await processRecurringPayment(createSampleTransactionRecord(), getMockContact())
const { recurringPayment } = await processRecurringPayment(createSimpleSampleTransactionRecord(), getMockContact())
expect(update).toHaveBeenCalledWith(recurringPayment.uniqueContentId)
})

it('hashes using sha256', async () => {
await processRecurringPayment(createSampleTransactionRecord(), getMockContact())
await processRecurringPayment(createSimpleSampleTransactionRecord(), getMockContact())
expect(createHash).toHaveBeenCalledWith('sha256')
})

Expand All @@ -173,13 +187,13 @@ describe('recurring payments service', () => {
update: () => {},
digest
})
await processRecurringPayment(createSampleTransactionRecord(), getMockContact())
await processRecurringPayment(createSimpleSampleTransactionRecord(), getMockContact())
expect(digest).toHaveBeenCalledWith('base64')
})
})

describe('generateRecurringPaymentRecord', () => {
const generateSampleTransaction = (agreementId, permission) => ({
const createFinalisedSampleTransaction = (agreementId, permission) => ({
expires: 1732892402,
cost: 35.8,
isRecurringPaymentSupported: true,
Expand Down Expand Up @@ -246,10 +260,11 @@ describe('recurring payments service', () => {
},
'2025-11-12T00:00:00.000Z'
]
])('creates record from transaction with %s', (_d, agreementId, permission, expectedNextDueDate) => {
const sampleTransaction = generateSampleTransaction(agreementId, permission)
])('creates record from transaction with %s', (_d, agreementId, permissionData, expectedNextDueDate) => {
const sampleTransaction = createFinalisedSampleTransaction(agreementId, permissionData)
const permission = createSamplePermission(permissionData)

const rpRecord = generateRecurringPaymentRecord(sampleTransaction)
const rpRecord = generateRecurringPaymentRecord(sampleTransaction, permission)

expect(rpRecord).toEqual(
expect.objectContaining({
Expand All @@ -259,12 +274,12 @@ describe('recurring payments service', () => {
nextDueDate: expectedNextDueDate,
cancelledDate: null,
cancelledReason: null,
endDate: permission.endDate,
endDate: permissionData.endDate,
agreementId,
status: 1
})
}),
permissions: expect.arrayContaining([expect.objectContaining(permission)])
permissions: expect.arrayContaining([permission])
})
)
})
Expand All @@ -287,7 +302,7 @@ describe('recurring payments service', () => {
}
]
])('throws an error for invalid dates when %s', (_d, permission) => {
const sampleTransaction = generateSampleTransaction('hyu78ijhyu78ijuhyu78ij9iu6', permission)
const sampleTransaction = createFinalisedSampleTransaction('hyu78ijhyu78ijuhyu78ij9iu6', permission)

expect(() => generateRecurringPaymentRecord(sampleTransaction)).toThrow('Invalid dates provided for permission')
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const getNextDueDate = (startDate, issueDate, endDate) => {
throw new Error('Invalid dates provided for permission')
}

export const generateRecurringPaymentRecord = transactionRecord => {
export const generateRecurringPaymentRecord = (transactionRecord, permission) => {
const [{ startDate, issueDate, endDate }] = transactionRecord.permissions
return {
payment: {
Expand All @@ -32,7 +32,7 @@ export const generateRecurringPaymentRecord = transactionRecord => {
status: 1
}
},
permissions: transactionRecord.permissions
permissions: [permission]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ describe('transaction service', () => {
expect.any(Transaction),
expect.any(TransactionJournal),
expect.any(TransactionJournal),
expect.any(RecurringPayment),
expect.any(Contact),
expect.any(Permission),
expect.any(RecurringPayment),
expect.any(RecurringPaymentInstruction),
expect.any(ConcessionProof)
]
Expand Down Expand Up @@ -377,15 +377,29 @@ describe('transaction service', () => {

describe('recurring payment processing', () => {
it('passes transaction record to generateRecurringPaymentRecord', async () => {
let grprArg
const callingArgs = {}
generateRecurringPaymentRecord.mockImplementationOnce(transaction => {
grprArg = JSON.parse(JSON.stringify(transaction))
callingArgs.transaction = 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)
// jest.fn args aren't immutable and transaction is changed in processQueue, so we use our clone that hasn't changed
expect(callingArgs.transaction).toEqual(mockRecord)
})

it('passes permission to generateRecurringPaymentRecord', async () => {
const mockRecord = mockFinalisedTransactionRecord()
const expectedPermissionData = {}
const keysToCopy = ['referenceNumber', 'issueDate', 'startDate', 'endDate', 'isRenewal']
for (const key of keysToCopy) {
expectedPermissionData[key] = mockRecord.permissions[0][key]
}
AwsMock.DynamoDB.DocumentClient.__setResponse('get', { Item: mockRecord })

await processQueue({ id: mockRecord.id })

expect(generateRecurringPaymentRecord).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining(expectedPermissionData))
})

it('passes return value of generateRecurringPaymentRecord to processRecurringPayment', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ export async function processQueue ({ id }) {
isRenewal
)

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

permission.bindToEntity(Permission.definition.relationships.licensee, contact)
permission.bindToEntity(Permission.definition.relationships.permit, permit)
permission.bindToEntity(Permission.definition.relationships.transaction, transaction)
Expand All @@ -78,7 +73,10 @@ export async function processQueue ({ id }) {

entities.push(contact, permission)

const { recurringPayment } = await processRecurringPayment(generateRecurringPaymentRecord(transactionRecord, permission), contact)

if (recurringPayment && permit.isRecurringPaymentSupported) {
entities.push(recurringPayment)
const paymentInstruction = new RecurringPaymentInstruction()
paymentInstruction.bindToEntity(RecurringPaymentInstruction.definition.relationships.licensee, contact)
paymentInstruction.bindToEntity(RecurringPaymentInstruction.definition.relationships.permit, permit)
Expand Down

0 comments on commit a0362b6

Please sign in to comment.