Skip to content

Commit

Permalink
throw error settlement received more than x hours ago
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarnardeviden committed Jan 27, 2025
1 parent 0088f5b commit 735e0f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/inbound/return/process-return-settlement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const db = require('../../data')
const saveSettlement = require('./save-settlement')
const saveSchedule = require('./save-schedule')
const getSettlementByInvoiceNumberAndValue = require('./get-settlement-by-invoice-number-and-value')
const config = require('../../config').processingConfig

const processReturnSettlement = async (settlement) => {
const transaction = await db.sequelize.transaction()
Expand All @@ -10,6 +11,17 @@ const processReturnSettlement = async (settlement) => {
const existingSettlement = await getSettlementByInvoiceNumberAndValue(settlement.invoiceNumber, settlement.value, transaction)
if (existingSettlement) {
console.info(`Duplicate settlement received, skipping ${existingSettlement.reference}`)

const hoursLimit = config.hoursLimit
const receivedDate = new Date(existingSettlement.received)
const currentDate = new Date()
const hoursDifference = Math.abs(currentDate - receivedDate) / 36e5

if (hoursDifference > hoursLimit) {
console.error(`Settlement ${existingSettlement.invoiceNumber} was received more than ${hoursLimit} hours ago.`)
throw new Error(`Settlement ${existingSettlement.invoiceNumber} was received more than ${hoursLimit} hours ago.`)
}

await transaction.rollback()
} else {
const savedSettlement = await saveSettlement(settlement, transaction)
Expand Down
27 changes: 27 additions & 0 deletions test/unit/inbound/process-return-settlement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ describe('process return settlement request', () => {
getSettlementByInvoiceNumberAndValue.mockResolvedValue(undefined)
saveSettlement.mockResolvedValue({ ...settlement, settlementId: 1 })
saveSchedule.mockResolvedValue(undefined)

jest.useFakeTimers('modern')
jest.setSystemTime(new Date(2022, 0, 15))
})

afterEach(() => {
Expand Down Expand Up @@ -180,4 +183,28 @@ describe('process return settlement request', () => {
}
expect(wrapper).rejects.toThrow(Error('Database save down issue'))
})

test('should throw an error if existing settlement was received more than 6 hours ago', async () => {
const settlementRequest = { invoiceNumber: 'INV123', invoiceLines: [] }
const receivedDate = new Date(2022, 0, 14, 17) // 2022-01-14T17:00:00.000Z (7 hours ago from mocked date)
const existingSettlementRequest = { invoiceNumber: 'INV123', received: receivedDate.toISOString() }

getSettlementByInvoiceNumberAndValue.mockResolvedValue(existingSettlementRequest)

await expect(processReturnSettlement(settlementRequest)).rejects.toThrow(`Settlement ${existingSettlementRequest.invoiceNumber} was received more than 6 hours ago.`)
expect(mockRollback).toHaveBeenCalled()
expect(mockCommit).not.toHaveBeenCalled()
})

test('should not throw an error if existing settlement was received less than 6 hours ago', async () => {
const settlementRequest = { invoiceNumber: 'INV123', invoiceLines: [] }
const receivedDate = new Date(2022, 0, 14, 19) // 2022-01-14T19:00:00.000Z (5 hours ago from mocked date)
const existingSettlementRequest = { invoiceNumber: 'INV123', received: receivedDate.toISOString() }

getSettlementByInvoiceNumberAndValue.mockResolvedValue(existingSettlementRequest)

await expect(processReturnSettlement(settlementRequest)).resolves.not.toThrow(`Settlement ${existingSettlementRequest.invoiceNumber} was received more than 6 hours ago.`)
expect(mockRollback).toHaveBeenCalled()
expect(mockCommit).not.toHaveBeenCalled()
})
})

0 comments on commit 735e0f8

Please sign in to comment.