Skip to content

Commit

Permalink
Added negative tests for generateRecurringPaymentRecord, amended it t…
Browse files Browse the repository at this point in the history
…o throw errors for invalid dates and refactored
  • Loading branch information
jaucourt committed Dec 2, 2024
1 parent 6b0b1df commit 8805739
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ describe('recurring payments service', () => {
})

describe('generateRecurringPaymentRecord', () => {
const generateSampleTransaction = (agreementId, permission) => ({
expires: 1732892402,
cost: 35.8,
isRecurringPaymentSupported: true,
permissions: [
{
permitId: 'permit-id-1',
licensee: {},
referenceNumber: '23211125-2WC3FBP-ABNDT8',
isLicenceForYou: true,
...permission
}
],
agreementId,
payment: {
amount: 35.8,
source: 'Gov Pay',
method: 'Debit card',
timestamp: '2024-11-22T15:00:45.922Z'
},
id: 'd26d646f-ed0f-4cf1-b6c1-ccfbbd611757',
dataSource: 'Web Sales',
transactionId: 'd26d646f-ed0f-4cf1-b6c1-ccfbbd611757',
status: { id: 'FINALISED' }
})

it.each([
[
'same day start - next due on issue date plus one year minus ten days',
Expand All @@ -204,38 +230,24 @@ describe('recurring payments service', () => {
'starts ten days after issue - next due on issue date plus one year',
'9o8u7yhui89u8i9oiu8i8u7yhu',
{
startDate: '2024-12-23T00:00:00.000Z',
startDate: '2024-11-22T00:00:00.000Z',
issueDate: '2024-11-12T15:00:45.922Z',
endDate: '2025-11-21T23:59:59.999Z'
},
'2025-11-12T00:00:00.000Z'
],
[
'starts twenty days after issue - next due on issue date plus one year',
'9o8u7yhui89u8i9oiu8i8u7yhu',
{
startDate: '2024-12-01T00:00:00.000Z',
issueDate: '2024-11-12T15:00:45.922Z',
endDate: '2025-12-22T23:59:59.999Z'
endDate: '2025-01-30T23:59:59.999Z'
},
'2025-11-12T00:00:00.000Z'
]
])('creates record from transaction with %s', (_d, agreementId, permission, expectedNextDueDate) => {
const sampleTransaction = {
expires: 1732892402,
cost: 35.8,
isRecurringPaymentSupported: true,
permissions: [
{
permitId: 'permit-id-1',
licensee: {},
referenceNumber: '23211125-2WC3FBP-ABNDT8',
isLicenceForYou: true,
...permission
}
],
agreementId,
payment: {
amount: 35.8,
source: 'Gov Pay',
method: 'Debit card',
timestamp: '2024-11-22T15:00:45.922Z'
},
id: 'd26d646f-ed0f-4cf1-b6c1-ccfbbd611757',
dataSource: 'Web Sales',
transactionId: 'd26d646f-ed0f-4cf1-b6c1-ccfbbd611757',
status: { id: 'FINALISED' }
}
const sampleTransaction = generateSampleTransaction(agreementId, permission)

const rpRecord = generateRecurringPaymentRecord(sampleTransaction)

Expand All @@ -256,5 +268,28 @@ describe('recurring payments service', () => {
})
)
})

it.each([
[
'start date is thirty one days after issue date',
{
startDate: '2024-12-14T00:00:00.000Z',
issueDate: '2024-11-12T15:00:45.922Z',
endDate: '2025-12-13T23:59:59.999Z'
}
],
[
'start date precedes issue date',
{
startDate: '2024-11-11T00:00:00.000Z',
issueDate: '2024-11-12T15:00:45.922Z',
endDate: '2025-11-10T23:59:59.999Z'
}
]
])('throws an error for invalid dates when %s', (_d, permission) => {
const sampleTransaction = generateSampleTransaction('hyu78ijhyu78ijuhyu78ij9iu6', permission)

expect(() => generateRecurringPaymentRecord(sampleTransaction)).toThrow('Invalid dates provided for permission')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import moment from 'moment'
export const getRecurringPayments = date => executeQuery(findDueRecurringPayments(date))

const getNextDueDate = (startDate, issueDate, endDate) => {
if (moment(startDate).isSame(moment(issueDate), 'day')) {
return moment(startDate).add(1, 'year').subtract(10, 'days').startOf('day').toISOString()
}
if (moment(startDate).isBefore(moment(issueDate).add(10, 'days'), 'day')) {
return moment(endDate).subtract(10, 'days').startOf('day').toISOString()
}
if (moment(startDate).isSameOrAfter(moment(issueDate).add(10, 'days'), 'day')) {
const mStart = moment(startDate)
if (mStart.isAfter(moment(issueDate)) && mStart.isSameOrBefore(moment(issueDate).add(30, 'days'), 'day')) {
if (mStart.isSame(moment(issueDate), 'day')) {
return moment(startDate).add(1, 'year').subtract(10, 'days').startOf('day').toISOString()
}
if (mStart.isBefore(moment(issueDate).add(10, 'days'), 'day')) {
return moment(endDate).subtract(10, 'days').startOf('day').toISOString()
}
return moment(issueDate).add(1, 'year').startOf('day').toISOString()
}
throw new Error('Invalid dates provided for permission')
}

export const generateRecurringPaymentRecord = transactionRecord => {
Expand Down

0 comments on commit 8805739

Please sign in to comment.