-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FN-3691): cron job to delete old record correction transient for…
…m data (#4168)
- Loading branch information
Showing
7 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...l-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { LessThan } from 'typeorm'; | ||
import { deleteCorrectionRequestTransientFormData, deleteCorrectionRequestTransientFormDataJob } from '.'; | ||
import { FeeRecordCorrectionRequestTransientFormDataRepo } from '../../repositories/fee-record-correction-request-transient-form-data-repo/fee-record-correction-request-transient-form-data.repo'; | ||
|
||
describe('delete-record-correction-request-transient-form-data', () => { | ||
jest.mock('typeorm', () => ({ | ||
LessThan: jest.fn(), | ||
})); | ||
|
||
const mockDelete = jest.fn(); | ||
console.error = jest.fn(); | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
describe('deleteRecordCorrectionRequestTransientFormData', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
FeeRecordCorrectionRequestTransientFormDataRepo.delete = mockDelete; | ||
jest.useFakeTimers().setSystemTime(new Date('2025-01-02 12:10:00')); | ||
}); | ||
|
||
it('should delete records older than one day', async () => { | ||
await deleteCorrectionRequestTransientFormData(); | ||
|
||
expect(mockDelete).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockDelete).toHaveBeenCalledWith({ | ||
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')), | ||
}); | ||
}); | ||
|
||
it('should delete records older than one day if it is the first day of the month', async () => { | ||
jest.useFakeTimers().setSystemTime(new Date('2025-01-01 12:10:00')); | ||
|
||
await deleteCorrectionRequestTransientFormData(); | ||
|
||
expect(mockDelete).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockDelete).toHaveBeenCalledWith({ | ||
lastUpdatedAt: LessThan(new Date('2024-12-31:12:10:00')), | ||
}); | ||
}); | ||
|
||
it('should throw an error if deletion fails', async () => { | ||
const errorMessage = 'This is an error'; | ||
const error = new Error(errorMessage); | ||
jest.mocked(mockDelete).mockRejectedValue(error); | ||
|
||
await deleteCorrectionRequestTransientFormData(); | ||
|
||
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'Error deleting old transient record correction requests - deleteCorrectionRequestTransientFormDataJob CRON job: %o', | ||
error, | ||
); | ||
}); | ||
}); | ||
|
||
describe('deleteRecordCorrectionRequestTransientFormDataJob', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
FeeRecordCorrectionRequestTransientFormDataRepo.delete = mockDelete; | ||
jest.useFakeTimers().setSystemTime(new Date('2025-01-02 12:10:00')); | ||
}); | ||
|
||
it('should be scheduled to run', () => { | ||
expect(deleteCorrectionRequestTransientFormDataJob.cronExpression).toEqual(process.env.RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE); | ||
}); | ||
|
||
it('should have the correct description', () => { | ||
expect(deleteCorrectionRequestTransientFormDataJob.description).toEqual('Delete record correction transient form data older than 1 day'); | ||
}); | ||
|
||
it('should call FeeRecordCorrectionRequestTransientFormDataRepo.delete', async () => { | ||
// Act | ||
await deleteCorrectionRequestTransientFormDataJob.task('manual'); | ||
|
||
// Assert | ||
expect(mockDelete).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockDelete).toHaveBeenCalledWith({ | ||
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')), | ||
}); | ||
}); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
...entral-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { asString, CronSchedulerJob } from '@ukef/dtfs2-common'; | ||
import { FeeRecordCorrectionRequestTransientFormDataRepo } from '../../repositories/fee-record-correction-request-transient-form-data-repo'; | ||
|
||
const { RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE } = process.env; | ||
|
||
/** | ||
* Deletes record correction request transient form data more than 1 day old | ||
*/ | ||
export const deleteCorrectionRequestTransientFormData = async (): Promise<void> => { | ||
try { | ||
console.info('Getting and deleting old transient record correction requests - deleteCorrectionRequestTransientFormDataJob CRON job'); | ||
|
||
await FeeRecordCorrectionRequestTransientFormDataRepo.deleteByLastUpdatedOlderThanOneDayAgo(); | ||
} catch (error) { | ||
console.error('Error deleting old transient record correction requests - deleteCorrectionRequestTransientFormDataJob CRON job: %o', error); | ||
} | ||
}; | ||
|
||
export const deleteCorrectionRequestTransientFormDataJob: CronSchedulerJob = { | ||
cronExpression: asString(RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE, 'RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE'), | ||
description: 'Delete record correction transient form data older than 1 day', | ||
task: deleteCorrectionRequestTransientFormData, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters