Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FN-3691): cron job to delete old record correction transient form data #4168

Merged
merged 13 commits into from
Jan 27, 2025
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ UTILISATION_REPORT_CREATION_FAILURE_EMAIL_ADDRESS=

DEAL_CANCELLATION_SCHEDULE='* 2 * * *'

RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE='0 2 * * *'

# STORAGE
AZURE_PORTAL_STORAGE_ACCOUNT=
AZURE_PORTAL_STORAGE_ACCESS_KEY=
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ env:
ENTRA_ID_CLIENT_SECRET: ${{ vars.ENTRA_ID_CLIENT_SECRET }}
ENTRA_ID_REDIRECT_URL: ${{ vars.ENTRA_ID_REDIRECT_URL }}
DEAL_CANCELLATION_SCHEDULE: ${{ vars.DEAL_CANCELLATION_SCHEDULE }}
RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE: ${{ vars.RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE }}

jobs:
# 1. Setup test infrastructure
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ services:
- FF_TFM_DEAL_CANCELLATION_ENABLED
- DEAL_CANCELLATION_SCHEDULE
- FF_PORTAL_FACILITY_AMENDMENTS_ENABLED
- RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE

trade-finance-manager-ui:
build:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { LessThan } from 'typeorm';

Check failure on line 1 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L1

Unable to resolve path to module '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;

Check failure on line 24 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L24

Unsafe member access .delete on an `error` typed value.
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')),

Check failure on line 34 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L34

Unsafe assignment of an error typed value.

Check failure on line 34 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L34

Unsafe call of an `error` type typed value.
});
});

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')),

Check failure on line 46 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L46

Unsafe assignment of an error typed value.

Check failure on line 46 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L46

Unsafe call of an `error` type typed value.
});
});

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);

Check failure on line 73 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L73

Unsafe member access .cronExpression on an `error` typed value.
});

it('should have the correct description', () => {
expect(deleteCorrectionRequestTransientFormDataJob.description).toEqual('Delete record correction transient form data older than 1 day');

Check failure on line 77 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L77

Unsafe member access .description on an `error` typed value.
});

it('should call FeeRecordCorrectionRequestTransientFormDataRepo.delete', async () => {
// Act
await deleteCorrectionRequestTransientFormDataJob.task('manual');

Check failure on line 82 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L82

Unsafe call of an `error` type typed value.

Check failure on line 82 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.test.ts#L82

Unsafe member access .task on an `error` typed value.

// Assert
expect(mockDelete).toHaveBeenCalledTimes(1);

expect(mockDelete).toHaveBeenCalledWith({
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')),
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { asString, CronSchedulerJob } from '@ukef/dtfs2-common';

Check failure on line 1 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts#L1

Unable to resolve path to module '@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();

Check failure on line 13 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts#L13

Unsafe call of an `error` type typed value.

Check failure on line 13 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts#L13

Unsafe member access .deleteByLastUpdatedOlderThanOneDayAgo on an `error` typed value.
} 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'),

Check failure on line 20 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts#L20

Unsafe assignment of an error typed value.

Check failure on line 20 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

dtfs-central-api/src/cron-scheduler-jobs/delete-correction-request-transient-form-data/index.ts#L20

Unsafe call of an `error` type typed value.
description: 'Delete record correction transient form data older than 1 day',
task: deleteCorrectionRequestTransientFormData,
};
8 changes: 7 additions & 1 deletion dtfs-central-api/src/cron-scheduler-jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ import { CronSchedulerJob } from '@ukef/dtfs2-common';
import { createUtilisationReportForBanksJob } from './create-utilisation-reports';
import { deleteCompleteAcbsDurableFunctionLogsJob } from './delete-acbs-durable-function-logs';
import { cancelDealJob } from './deal-cancellation/cancel-deal-job';
import { deleteCorrectionRequestTransientFormDataJob } from './delete-correction-request-transient-form-data';

export const cronSchedulerJobs: CronSchedulerJob[] = [createUtilisationReportForBanksJob, deleteCompleteAcbsDurableFunctionLogsJob, cancelDealJob];
export const cronSchedulerJobs: CronSchedulerJob[] = [
createUtilisationReportForBanksJob,
deleteCompleteAcbsDurableFunctionLogsJob,
cancelDealJob,
deleteCorrectionRequestTransientFormDataJob,
];
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SqlDbDataSource } from '@ukef/dtfs2-common/sql-db-connection';
import { FeeRecordCorrectionRequestTransientFormDataEntity } from '@ukef/dtfs2-common';
import { EntityManager } from 'typeorm';
import { EntityManager, LessThan } from 'typeorm';

/**
* Repository for managing fee record correction request transient form data.
Expand Down Expand Up @@ -34,6 +34,18 @@
});
},

/**
* deletes the transient form data which is older than a day old
*/
async deleteByLastUpdatedOlderThanOneDayAgo(): Promise<void> {
const today = new Date();
const oneDayAgo = today.setDate(today.getDate() - 1);

await this.delete({
lastUpdatedAt: LessThan(new Date(oneDayAgo)),

Check failure on line 45 in dtfs-central-api/src/repositories/fee-record-correction-request-transient-form-data-repo/fee-record-correction-request-transient-form-data.repo.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

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

Unsafe assignment of an error typed value.

Check failure on line 45 in dtfs-central-api/src/repositories/fee-record-correction-request-transient-form-data-repo/fee-record-correction-request-transient-form-data.repo.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

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

Unsafe call of an `error` type typed value.
});
},

withTransaction(transactionEntityManager: EntityManager) {
const transactionRepository = transactionEntityManager.getRepository(FeeRecordCorrectionRequestTransientFormDataEntity);

Expand Down
Loading