Skip to content

Commit b29a4d5

Browse files
committed
Merge branch 'main' of github.com:UK-Export-Finance/dtfs2 into feat/DTFS2-7730/delete-amendment-endpoint-dtfs-central-api
2 parents 9be8dec + fe2faaf commit b29a4d5

File tree

13 files changed

+853
-3
lines changed

13 files changed

+853
-3
lines changed

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ UTILISATION_REPORT_CREATION_FAILURE_EMAIL_ADDRESS=
6767

6868
DEAL_CANCELLATION_SCHEDULE='* 2 * * *'
6969

70+
RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE='0 2 * * *'
71+
7072
# STORAGE
7173
AZURE_PORTAL_STORAGE_ACCOUNT=
7274
AZURE_PORTAL_STORAGE_ACCESS_KEY=

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ env:
167167
ENTRA_ID_CLIENT_SECRET: ${{ vars.ENTRA_ID_CLIENT_SECRET }}
168168
ENTRA_ID_REDIRECT_URL: ${{ vars.ENTRA_ID_REDIRECT_URL }}
169169
DEAL_CANCELLATION_SCHEDULE: ${{ vars.DEAL_CANCELLATION_SCHEDULE }}
170+
RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE: ${{ vars.RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE }}
170171

171172
jobs:
172173
# 1. Setup test infrastructure

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ services:
8989
- FF_TFM_DEAL_CANCELLATION_ENABLED
9090
- DEAL_CANCELLATION_SCHEDULE
9191
- FF_PORTAL_FACILITY_AMENDMENTS_ENABLED
92+
- RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE
9293

9394
trade-finance-manager-ui:
9495
build:
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { LessThan } from 'typeorm';
2+
import { deleteCorrectionRequestTransientFormData, deleteCorrectionRequestTransientFormDataJob } from '.';
3+
import { FeeRecordCorrectionRequestTransientFormDataRepo } from '../../repositories/fee-record-correction-request-transient-form-data-repo/fee-record-correction-request-transient-form-data.repo';
4+
5+
describe('delete-record-correction-request-transient-form-data', () => {
6+
jest.mock('typeorm', () => ({
7+
LessThan: jest.fn(),
8+
}));
9+
10+
const mockDelete = jest.fn();
11+
console.error = jest.fn();
12+
13+
beforeAll(() => {
14+
jest.useFakeTimers();
15+
});
16+
17+
afterAll(() => {
18+
jest.useRealTimers();
19+
});
20+
21+
describe('deleteRecordCorrectionRequestTransientFormData', () => {
22+
beforeEach(() => {
23+
jest.resetAllMocks();
24+
FeeRecordCorrectionRequestTransientFormDataRepo.delete = mockDelete;
25+
jest.useFakeTimers().setSystemTime(new Date('2025-01-02 12:10:00'));
26+
});
27+
28+
it('should delete records older than one day', async () => {
29+
await deleteCorrectionRequestTransientFormData();
30+
31+
expect(mockDelete).toHaveBeenCalledTimes(1);
32+
33+
expect(mockDelete).toHaveBeenCalledWith({
34+
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')),
35+
});
36+
});
37+
38+
it('should delete records older than one day if it is the first day of the month', async () => {
39+
jest.useFakeTimers().setSystemTime(new Date('2025-01-01 12:10:00'));
40+
41+
await deleteCorrectionRequestTransientFormData();
42+
43+
expect(mockDelete).toHaveBeenCalledTimes(1);
44+
45+
expect(mockDelete).toHaveBeenCalledWith({
46+
lastUpdatedAt: LessThan(new Date('2024-12-31:12:10:00')),
47+
});
48+
});
49+
50+
it('should throw an error if deletion fails', async () => {
51+
const errorMessage = 'This is an error';
52+
const error = new Error(errorMessage);
53+
jest.mocked(mockDelete).mockRejectedValue(error);
54+
55+
await deleteCorrectionRequestTransientFormData();
56+
57+
expect(console.error).toHaveBeenCalledTimes(1);
58+
expect(console.error).toHaveBeenCalledWith(
59+
'Error deleting old transient record correction requests - deleteCorrectionRequestTransientFormDataJob CRON job: %o',
60+
error,
61+
);
62+
});
63+
});
64+
65+
describe('deleteRecordCorrectionRequestTransientFormDataJob', () => {
66+
beforeEach(() => {
67+
jest.resetAllMocks();
68+
FeeRecordCorrectionRequestTransientFormDataRepo.delete = mockDelete;
69+
jest.useFakeTimers().setSystemTime(new Date('2025-01-02 12:10:00'));
70+
});
71+
72+
it('should be scheduled to run', () => {
73+
expect(deleteCorrectionRequestTransientFormDataJob.cronExpression).toEqual(process.env.RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE);
74+
});
75+
76+
it('should have the correct description', () => {
77+
expect(deleteCorrectionRequestTransientFormDataJob.description).toEqual('Delete record correction transient form data older than 1 day');
78+
});
79+
80+
it('should call FeeRecordCorrectionRequestTransientFormDataRepo.delete', async () => {
81+
// Act
82+
await deleteCorrectionRequestTransientFormDataJob.task('manual');
83+
84+
// Assert
85+
expect(mockDelete).toHaveBeenCalledTimes(1);
86+
87+
expect(mockDelete).toHaveBeenCalledWith({
88+
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')),
89+
});
90+
});
91+
});
92+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { asString, CronSchedulerJob } from '@ukef/dtfs2-common';
2+
import { FeeRecordCorrectionRequestTransientFormDataRepo } from '../../repositories/fee-record-correction-request-transient-form-data-repo';
3+
4+
const { RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE } = process.env;
5+
6+
/**
7+
* Deletes record correction request transient form data more than 1 day old
8+
*/
9+
export const deleteCorrectionRequestTransientFormData = async (): Promise<void> => {
10+
try {
11+
console.info('Getting and deleting old transient record correction requests - deleteCorrectionRequestTransientFormDataJob CRON job');
12+
13+
await FeeRecordCorrectionRequestTransientFormDataRepo.deleteByLastUpdatedOlderThanOneDayAgo();
14+
} catch (error) {
15+
console.error('Error deleting old transient record correction requests - deleteCorrectionRequestTransientFormDataJob CRON job: %o', error);
16+
}
17+
};
18+
19+
export const deleteCorrectionRequestTransientFormDataJob: CronSchedulerJob = {
20+
cronExpression: asString(RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE, 'RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE'),
21+
description: 'Delete record correction transient form data older than 1 day',
22+
task: deleteCorrectionRequestTransientFormData,
23+
};

dtfs-central-api/src/cron-scheduler-jobs/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ import { CronSchedulerJob } from '@ukef/dtfs2-common';
22
import { createUtilisationReportForBanksJob } from './create-utilisation-reports';
33
import { deleteCompleteAcbsDurableFunctionLogsJob } from './delete-acbs-durable-function-logs';
44
import { cancelDealJob } from './deal-cancellation/cancel-deal-job';
5+
import { deleteCorrectionRequestTransientFormDataJob } from './delete-correction-request-transient-form-data';
56

6-
export const cronSchedulerJobs: CronSchedulerJob[] = [createUtilisationReportForBanksJob, deleteCompleteAcbsDurableFunctionLogsJob, cancelDealJob];
7+
export const cronSchedulerJobs: CronSchedulerJob[] = [
8+
createUtilisationReportForBanksJob,
9+
deleteCompleteAcbsDurableFunctionLogsJob,
10+
cancelDealJob,
11+
deleteCorrectionRequestTransientFormDataJob,
12+
];

dtfs-central-api/src/repositories/fee-record-correction-request-transient-form-data-repo/fee-record-correction-request-transient-form-data.repo.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SqlDbDataSource } from '@ukef/dtfs2-common/sql-db-connection';
22
import { FeeRecordCorrectionRequestTransientFormDataEntity } from '@ukef/dtfs2-common';
3-
import { EntityManager } from 'typeorm';
3+
import { EntityManager, LessThan } from 'typeorm';
44

55
/**
66
* Repository for managing fee record correction request transient form data.
@@ -34,6 +34,18 @@ export const FeeRecordCorrectionRequestTransientFormDataRepo = SqlDbDataSource.g
3434
});
3535
},
3636

37+
/**
38+
* deletes the transient form data which is older than a day old
39+
*/
40+
async deleteByLastUpdatedOlderThanOneDayAgo(): Promise<void> {
41+
const today = new Date();
42+
const oneDayAgo = today.setDate(today.getDate() - 1);
43+
44+
await this.delete({
45+
lastUpdatedAt: LessThan(new Date(oneDayAgo)),
46+
});
47+
},
48+
3749
withTransaction(transactionEntityManager: EntityManager) {
3850
const transactionRepository = transactionEntityManager.getRepository(FeeRecordCorrectionRequestTransientFormDataEntity);
3951

0 commit comments

Comments
 (0)