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-3737): clear old record correction transient form data (portal) #4196

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
sw-mattw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { LessThan } from 'typeorm';

Check failure on line 1 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L1

Unable to resolve path to module 'typeorm'.
import { deleteAllOldCorrectionTransientFormData, deleteAllOldCorrectionTransientFormDataJob } from '.';
import { FeeRecordCorrectionRequestTransientFormDataRepo } from '../../repositories/fee-record-correction-request-transient-form-data-repo/fee-record-correction-request-transient-form-data.repo';
import { FeeRecordCorrectionTransientFormDataRepo } from '../../repositories/fee-record-correction-transient-form-data-repo';

console.error = jest.fn();

describe('delete-correction-and-correction-request-transient-form-data', () => {
jest.mock('typeorm', () => ({
LessThan: jest.fn(),
}));

const mockCorrectionDelete = jest.fn();
const mockCorrectionRequestDelete = jest.fn();

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

describe('deleteAllOldCorrectionTransientFormData', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2025-01-02 12:10:00'));

FeeRecordCorrectionTransientFormDataRepo.delete = mockCorrectionDelete;

Check failure on line 28 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L28

Unsafe member access .delete on an `error` typed value.
FeeRecordCorrectionRequestTransientFormDataRepo.delete = mockCorrectionRequestDelete;

Check failure on line 29 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L29

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

afterEach(() => {
jest.resetAllMocks();
});

it('should delete "correction transient data" records older than one day', async () => {
// Act
await deleteAllOldCorrectionTransientFormData();

// Assert
const expectedDeletionCallArgs = {
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')),

Check failure on line 42 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L42

Unsafe assignment of an error typed value.

Check failure on line 42 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L42

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

expect(mockCorrectionDelete).toHaveBeenCalledTimes(1);
expect(mockCorrectionDelete).toHaveBeenCalledWith(expectedDeletionCallArgs);
});

it('should delete "correction request transient data" records older than one day', async () => {
// Act
await deleteAllOldCorrectionTransientFormData();

// Assert
const expectedDeletionCallArgs = {
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')),
};

expect(mockCorrectionRequestDelete).toHaveBeenCalledTimes(1);
expect(mockCorrectionRequestDelete).toHaveBeenCalledWith(expectedDeletionCallArgs);
});

it('should delete "correction transient data" records older than one day if it is the first day of the month', async () => {
// Arrange
jest.useFakeTimers().setSystemTime(new Date('2025-01-01 12:10:00'));

// Act
await deleteAllOldCorrectionTransientFormData();

// Assert
const expectedDeletionCallArgs = {
lastUpdatedAt: LessThan(new Date('2024-12-31:12:10:00')),

Check failure on line 71 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L71

Unsafe assignment of an error typed value.

Check failure on line 71 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L71

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

expect(mockCorrectionDelete).toHaveBeenCalledTimes(1);
expect(mockCorrectionDelete).toHaveBeenCalledWith(expectedDeletionCallArgs);
});

it('should delete "correction request transient data" records older than one day if it is the first day of the month', async () => {
// Arrange
jest.useFakeTimers().setSystemTime(new Date('2025-01-01 12:10:00'));

// Act
await deleteAllOldCorrectionTransientFormData();

// Assert
const expectedDeletionCallArgs = {
lastUpdatedAt: LessThan(new Date('2024-12-31:12:10:00')),
};

expect(mockCorrectionRequestDelete).toHaveBeenCalledTimes(1);
expect(mockCorrectionRequestDelete).toHaveBeenCalledWith(expectedDeletionCallArgs);
});

describe('when record correction transient data deletion fails', () => {
it('should throw an error', async () => {
// Arrange
const errorMessage = 'This is an error';
const error = new Error(errorMessage);

jest.mocked(mockCorrectionDelete).mockRejectedValue(error);

// Act
await deleteAllOldCorrectionTransientFormData();

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

expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
'Error deleting old transient record correction form data - deleteCorrectionRequestTransientFormData CRON job: %o',
error,
);
});

it('should still call record correction request transient data deletion', async () => {
// Act
await deleteAllOldCorrectionTransientFormData();

// Assert
expect(mockCorrectionRequestDelete).toHaveBeenCalledTimes(1);
});
});

describe('when record correction request transient data deletion fails', () => {
it('should throw an error', async () => {
// Arrange
const errorMessage = 'This is an error';
const error = new Error(errorMessage);

jest.mocked(mockCorrectionRequestDelete).mockRejectedValue(error);

// Act
await deleteAllOldCorrectionTransientFormData();

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

expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
'Error deleting old transient record correction request form data - deleteCorrectionRequestTransientFormData CRON job: %o',
error,
);
});

it('should still call record correction transient data deletion', async () => {
// Act
await deleteAllOldCorrectionTransientFormData();

// Assert
expect(mockCorrectionDelete).toHaveBeenCalledTimes(1);
});
});
});

describe('deleteRecordCorrectionRequestTransientFormDataJob', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2025-01-02 12:10:00'));

FeeRecordCorrectionTransientFormDataRepo.delete = mockCorrectionDelete;
FeeRecordCorrectionRequestTransientFormDataRepo.delete = mockCorrectionRequestDelete;
});

afterEach(() => {
jest.resetAllMocks();
});

it('should be scheduled to run', () => {
// Assert
expect(deleteAllOldCorrectionTransientFormDataJob.cronExpression).toEqual(process.env.RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE);

Check failure on line 169 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L169

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

it('should have the correct description', () => {
// Assert
expect(deleteAllOldCorrectionTransientFormDataJob.description).toEqual(

Check failure on line 174 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L174

Unsafe member access .description on an `error` typed value.
'Deletes record correction and record correction request transient form data older than 1 day',
);
});

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

Check failure on line 181 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L181

Unsafe call of an `error` type typed value.

Check failure on line 181 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.test.ts#L181

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

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

expect(mockCorrectionDelete).toHaveBeenCalledWith({
lastUpdatedAt: LessThan(new Date('2025-01-01:12:10:00')),
});
});

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

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

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

Check failure on line 1 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-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';
import { FeeRecordCorrectionTransientFormDataRepo } from '../../repositories/fee-record-correction-transient-form-data-repo';

const { RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE } = process.env;

/**
* Deletes record correction and correction request transient form data more than 1 day old.
*/
export const deleteAllOldCorrectionTransientFormData = async (): Promise<void> => {
console.info('Deleting old transient record corrections and correction requests - deleteCorrectionRequestTransientFormData CRON job');

try {
await FeeRecordCorrectionTransientFormDataRepo.deleteByLastUpdatedOlderThanOneDayAgo();

Check failure on line 14 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.ts#L14

Unsafe call of an `error` type typed value.

Check failure on line 14 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.ts#L14

Unsafe member access .deleteByLastUpdatedOlderThanOneDayAgo on an `error` typed value.
} catch (error) {
console.error('Error deleting old transient record correction form data - deleteCorrectionRequestTransientFormData CRON job: %o', error);
}

try {
await FeeRecordCorrectionRequestTransientFormDataRepo.deleteByLastUpdatedOlderThanOneDayAgo();

Check failure on line 20 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.ts#L20

Unsafe call of an `error` type typed value.

Check failure on line 20 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.ts#L20

Unsafe member access .deleteByLastUpdatedOlderThanOneDayAgo on an `error` typed value.
} catch (error) {
console.error('Error deleting old transient record correction request form data - deleteCorrectionRequestTransientFormData CRON job: %o', error);
}
};

export const deleteAllOldCorrectionTransientFormDataJob: CronSchedulerJob = {
cronExpression: asString(RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE, 'RECORD_CORRECTION_TRANSIENT_FORM_DATA_DELETE_SCHEDULE'),

Check failure on line 27 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.ts#L27

Unsafe assignment of an error typed value.

Check failure on line 27 in dtfs-central-api/src/cron-scheduler-jobs/delete-correction-and-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-and-correction-request-transient-form-data/index.ts#L27

Unsafe call of an `error` type typed value.
description: 'Deletes record correction and record correction request transient form data older than 1 day',
task: deleteAllOldCorrectionTransientFormData,
};

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions dtfs-central-api/src/cron-scheduler-jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +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';
import { deleteAllOldCorrectionTransientFormDataJob } from './delete-correction-and-correction-request-transient-form-data';

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

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

/**
* Deletes transient fee record correction data that was last updated more than one day ago.
*/
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 44 in dtfs-central-api/src/repositories/fee-record-correction-transient-form-data-repo/fee-record-correction-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-transient-form-data-repo/fee-record-correction-transient-form-data.repo.ts#L44

Unsafe assignment of an error typed value.

Check failure on line 44 in dtfs-central-api/src/repositories/fee-record-correction-transient-form-data-repo/fee-record-correction-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-transient-form-data-repo/fee-record-correction-transient-form-data.repo.ts#L44

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

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

Expand Down
Loading