Skip to content

Commit 7c51a07

Browse files
author
Zain Kassam
committed
feat(FN-3615): review markups
1 parent 0a6a45e commit 7c51a07

File tree

23 files changed

+567
-133
lines changed

23 files changed

+567
-133
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { Response } from 'supertest';
2+
import { format } from 'date-fns';
3+
import {
4+
Bank,
5+
FeeRecordEntityMockBuilder,
6+
RECONCILIATION_IN_PROGRESS,
7+
FeeRecordCorrectionEntityMockBuilder,
8+
UtilisationReportEntityMockBuilder,
9+
ReportPeriod,
10+
GetRecordCorrectionLogDetailsResponseBody,
11+
DATE_FORMATS,
12+
} from '@ukef/dtfs2-common';
13+
import { HttpStatusCode, getUri } from 'axios';
14+
import { withSqlIdPathParameterValidationTests } from '@ukef/dtfs2-common/test-cases-backend';
15+
import { testApi } from '../../test-api';
16+
import { SqlDbHelper } from '../../sql-db-helper';
17+
import { wipe } from '../../wipeDB';
18+
import { mongoDbClient } from '../../../src/drivers/db-client';
19+
import { aBank } from '../../../test-helpers';
20+
21+
const BASE_URL = '/v1/utilisation-reports/record-correction-log-details/:correctionId';
22+
23+
interface CustomResponse extends Response {
24+
body: GetRecordCorrectionLogDetailsResponseBody;
25+
}
26+
27+
console.error = jest.fn();
28+
29+
describe(`GET ${BASE_URL}`, () => {
30+
const getUrl = (correctionId: number | string) =>
31+
getUri({
32+
url: BASE_URL.replace(':correctionId', correctionId.toString()),
33+
});
34+
35+
const today = new Date();
36+
37+
const bankId = '123';
38+
const bankName = 'Test bank';
39+
const bank: Bank = {
40+
...aBank(),
41+
id: bankId,
42+
name: bankName,
43+
};
44+
45+
const reportPeriod: ReportPeriod = {
46+
start: { month: 1, year: 2024 },
47+
end: { month: 1, year: 2024 },
48+
};
49+
50+
const reportId = 1;
51+
const feeRecordId = 2;
52+
53+
const reconciliationInProgressReport = UtilisationReportEntityMockBuilder.forStatus(RECONCILIATION_IN_PROGRESS)
54+
.withId(reportId)
55+
.withBankId(bankId)
56+
.withReportPeriod(reportPeriod)
57+
.build();
58+
59+
const feeRecord = FeeRecordEntityMockBuilder.forReport(reconciliationInProgressReport).withId(feeRecordId).build();
60+
61+
const recordCorrection = FeeRecordCorrectionEntityMockBuilder.forFeeRecordAndIsCompleted(feeRecord, false).withDateRequested(today).build();
62+
63+
beforeAll(async () => {
64+
await SqlDbHelper.initialize();
65+
await SqlDbHelper.deleteAllEntries('UtilisationReport');
66+
await SqlDbHelper.deleteAllEntries('FeeRecordCorrection');
67+
68+
await wipe(['banks']);
69+
70+
const banksCollection = await mongoDbClient.getCollection('banks');
71+
await banksCollection.insertOne(bank);
72+
});
73+
74+
beforeEach(async () => {
75+
await SqlDbHelper.saveNewEntry('UtilisationReport', reconciliationInProgressReport);
76+
await SqlDbHelper.saveNewEntry('FeeRecord', feeRecord);
77+
await SqlDbHelper.saveNewEntry('FeeRecordCorrection', recordCorrection);
78+
});
79+
80+
afterEach(async () => {
81+
await SqlDbHelper.deleteAllEntries('FeeRecord');
82+
await SqlDbHelper.deleteAllEntries('UtilisationReport');
83+
await SqlDbHelper.deleteAllEntries('FeeRecordCorrection');
84+
});
85+
86+
afterAll(async () => {
87+
await SqlDbHelper.deleteAllEntries('FeeRecord');
88+
await SqlDbHelper.deleteAllEntries('UtilisationReport');
89+
await SqlDbHelper.deleteAllEntries('FeeRecordCorrection');
90+
});
91+
92+
withSqlIdPathParameterValidationTests({
93+
baseUrl: BASE_URL,
94+
makeRequest: (url) => testApi.get(url),
95+
});
96+
97+
it(`returns a ${HttpStatusCode.NotFound} when the record correction cannot be found by id`, async () => {
98+
// Act
99+
const response: CustomResponse = await testApi.get(getUrl(recordCorrection.id + 1));
100+
101+
// Assert
102+
expect(response.status).toEqual(HttpStatusCode.NotFound);
103+
});
104+
105+
it(`returns a ${HttpStatusCode.Ok} when a correction with the provided id is found`, async () => {
106+
// Act
107+
const response: CustomResponse = await testApi.get(getUrl(recordCorrection.id));
108+
109+
const expected = {
110+
correctionDetails: {
111+
facilityId: feeRecord.facilityId,
112+
exporter: feeRecord.exporter,
113+
formattedReasons: 'Utilisation is incorrect',
114+
formattedDateSent: format(today, DATE_FORMATS.DD_MMM_YYYY),
115+
formattedOldRecords: '100.00',
116+
formattedCorrectRecords: '-',
117+
isCompleted: false,
118+
bankTeamName: recordCorrection.bankTeamName,
119+
formattedBankTeamEmails: 'test1@ukexportfinance.gov.uk, test2@ukexportfinance.gov.uk',
120+
additionalInfo: recordCorrection.additionalInfo,
121+
formattedBankCommentary: '-',
122+
formattedDateReceived: '-',
123+
formattedRequestedByUser: `${recordCorrection.requestedByUser.firstName} ${recordCorrection.requestedByUser.lastName}`,
124+
},
125+
bankName,
126+
reportPeriod,
127+
};
128+
129+
// Assert
130+
expect(response.status).toEqual(HttpStatusCode.Ok);
131+
expect(response.body).toEqual(expected);
132+
});
133+
});

dtfs-central-api/src/v1/controllers/utilisation-report-service/get-record-correction-log-details.controller/index.test.ts

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getRecordCorrectionLogDetails } from '.';
55
import { FeeRecordCorrectionRepo } from '../../../../repositories/fee-record-correction-repo';
66
import { getRecordCorrectionFields } from '../helpers/get-record-correction-fields';
77
import { getBankNameById } from '../../../../repositories/banks-repo';
8+
import { NotFoundError } from '../../../../errors';
89

910
console.error = jest.fn();
1011

@@ -38,82 +39,27 @@ describe('get-record-correction-log-details.controller', () => {
3839
findRecordCorrectionSpy.mockResolvedValue(null);
3940
});
4041

41-
it(`should respond with ${HttpStatusCode.NotFound}`, async () => {
42+
it('should throw a NotFoundError', async () => {
4243
// Arrange
4344
const { req, res } = getHttpMocks();
4445

45-
// Act
46-
await getRecordCorrectionLogDetails(req, res);
47-
48-
// Assert
49-
expect(res._getStatusCode()).toEqual(HttpStatusCode.NotFound);
50-
expect(res._getData()).toEqual('Record correction not found');
51-
});
52-
53-
it('should call FeeRecordCorrectionRepo.findOneByIdWithFeeRecordAndReport once with the correctionId', async () => {
54-
// Arrange
55-
const { req, res } = getHttpMocks();
56-
57-
// Act
58-
await getRecordCorrectionLogDetails(req, res);
59-
6046
// Assert
61-
expect(findRecordCorrectionSpy).toHaveBeenCalledTimes(1);
62-
expect(findRecordCorrectionSpy).toHaveBeenCalledWith(correctionId);
63-
});
64-
65-
it('should NOT call getBankNameById', async () => {
66-
// Arrange
67-
const { req, res } = getHttpMocks();
68-
69-
// Act
70-
await getRecordCorrectionLogDetails(req, res);
71-
72-
// Assert
73-
expect(getBankNameById).toHaveBeenCalledTimes(0);
47+
await expect(getRecordCorrectionLogDetails(req, res)).rejects.toThrow(new NotFoundError('Record correction not found'));
7448
});
7549
});
7650

7751
describe('when a bank is not found', () => {
7852
beforeEach(() => {
7953
findRecordCorrectionSpy.mockResolvedValue(feeRecordCorrectionEntity);
80-
jest.mocked(getBankNameById).mockResolvedValue('');
81-
});
82-
83-
it(`should respond with ${HttpStatusCode.NotFound}`, async () => {
84-
// Arrange
85-
const { req, res } = getHttpMocks();
86-
87-
// Act
88-
await getRecordCorrectionLogDetails(req, res);
89-
90-
// Assert
91-
expect(res._getStatusCode()).toEqual(HttpStatusCode.NotFound);
92-
expect(res._getData()).toEqual('Bank not found');
93-
});
94-
95-
it('should call FeeRecordCorrectionRepo.findOneByIdWithFeeRecordAndReport once with the correctionId', async () => {
96-
// Arrange
97-
const { req, res } = getHttpMocks();
98-
99-
// Act
100-
await getRecordCorrectionLogDetails(req, res);
101-
102-
// Assert
103-
expect(findRecordCorrectionSpy).toHaveBeenCalledTimes(1);
104-
expect(findRecordCorrectionSpy).toHaveBeenCalledWith(correctionId);
54+
jest.mocked(getBankNameById).mockResolvedValue(undefined);
10555
});
10656

107-
it('should call getBankNameById once with the bankId', async () => {
57+
it('should throw a NotFoundError', async () => {
10858
// Arrange
10959
const { req, res } = getHttpMocks();
11060

111-
// Act
112-
await getRecordCorrectionLogDetails(req, res);
113-
11461
// Assert
115-
expect(getBankNameById).toHaveBeenCalledTimes(1);
116-
expect(getBankNameById).toHaveBeenCalledWith(feeRecordCorrectionEntity.feeRecord.report.bankId);
62+
await expect(getRecordCorrectionLogDetails(req, res)).rejects.toThrow(new NotFoundError('Bank not found'));
11763
});
11864
});
11965

@@ -130,8 +76,38 @@ describe('get-record-correction-log-details.controller', () => {
13076
// Act
13177
await getRecordCorrectionLogDetails(req, res);
13278

79+
const {
80+
facilityId,
81+
exporter,
82+
formattedReasons,
83+
formattedDateSent,
84+
formattedOldRecords,
85+
formattedCorrectRecords,
86+
bankTeamName,
87+
isCompleted,
88+
formattedBankTeamEmails,
89+
additionalInfo,
90+
formattedBankCommentary,
91+
formattedDateReceived,
92+
formattedRequestedByUser,
93+
} = getRecordCorrectionFields(feeRecordCorrectionEntity.feeRecord, feeRecordCorrectionEntity);
94+
13395
const expected = {
134-
fields: getRecordCorrectionFields(feeRecordCorrectionEntity.feeRecord, feeRecordCorrectionEntity),
96+
correctionDetails: {
97+
facilityId,
98+
exporter,
99+
formattedReasons,
100+
formattedDateSent,
101+
formattedOldRecords,
102+
formattedCorrectRecords,
103+
bankTeamName,
104+
isCompleted,
105+
formattedBankTeamEmails,
106+
additionalInfo,
107+
formattedBankCommentary,
108+
formattedDateReceived,
109+
formattedRequestedByUser,
110+
},
135111
bankName,
136112
reportPeriod: feeRecordCorrectionEntity.feeRecord.report.reportPeriod,
137113
};

dtfs-central-api/src/v1/controllers/utilisation-report-service/get-record-correction-log-details.controller/index.ts

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GetRecordCorrectionLogDetailsResponse } from '@ukef/dtfs2-common';
44
import { FeeRecordCorrectionRepo } from '../../../../repositories/fee-record-correction-repo';
55
import { getRecordCorrectionFields } from '../helpers/get-record-correction-fields';
66
import { getBankNameById } from '../../../../repositories/banks-repo';
7+
import { ApiError, NotFoundError } from '../../../../errors';
78

89
/**
910
* gets record correction log details by id
@@ -14,29 +15,66 @@ import { getBankNameById } from '../../../../repositories/banks-repo';
1415
* @returns formatted fields for the record correction and fee record
1516
*/
1617
export const getRecordCorrectionLogDetails = async (req: Request, res: GetRecordCorrectionLogDetailsResponse) => {
17-
const { correctionId: correctionIdString } = req.params;
18+
try {
19+
const { correctionId: correctionIdString } = req.params;
1820

19-
const correctionId = Number(correctionIdString);
21+
const correctionId = Number(correctionIdString);
2022

21-
const response = await FeeRecordCorrectionRepo.findOneByIdWithFeeRecordAndReport(correctionId);
23+
const correction = await FeeRecordCorrectionRepo.findOneByIdWithFeeRecordAndReport(correctionId);
2224

23-
if (!response || !response?.feeRecord || !response?.feeRecord?.report) {
24-
return res.status(HttpStatusCode.NotFound).send('Record correction not found');
25-
}
25+
if (!correction || !correction?.feeRecord || !correction?.feeRecord?.report) {
26+
throw new NotFoundError('Record correction not found');
27+
}
2628

27-
const bankName = await getBankNameById(response.feeRecord.report.bankId);
29+
const bankName = await getBankNameById(correction.feeRecord.report.bankId);
2830

29-
if (!bankName?.length) {
30-
return res.status(HttpStatusCode.NotFound).send('Bank not found');
31-
}
31+
if (!bankName) {
32+
throw new NotFoundError('Bank not found');
33+
}
3234

33-
const fields = getRecordCorrectionFields(response.feeRecord, response);
35+
const {
36+
facilityId,
37+
exporter,
38+
formattedReasons,
39+
formattedDateSent,
40+
formattedOldRecords,
41+
formattedCorrectRecords,
42+
bankTeamName,
43+
isCompleted,
44+
formattedBankTeamEmails,
45+
additionalInfo,
46+
formattedBankCommentary,
47+
formattedDateReceived,
48+
formattedRequestedByUser,
49+
} = getRecordCorrectionFields(correction.feeRecord, correction);
3450

35-
const responseObject = {
36-
fields,
37-
bankName,
38-
reportPeriod: response.feeRecord.report.reportPeriod,
39-
};
51+
const responseObject = {
52+
correctionDetails: {
53+
facilityId,
54+
exporter,
55+
formattedReasons,
56+
formattedDateSent,
57+
formattedOldRecords,
58+
formattedCorrectRecords,
59+
bankTeamName,
60+
isCompleted,
61+
formattedBankTeamEmails,
62+
additionalInfo,
63+
formattedBankCommentary,
64+
formattedDateReceived,
65+
formattedRequestedByUser,
66+
},
67+
bankName,
68+
reportPeriod: correction.feeRecord.report.reportPeriod,
69+
};
4070

41-
return res.status(HttpStatusCode.Ok).send(responseObject);
71+
return res.status(HttpStatusCode.Ok).send(responseObject);
72+
} catch (error) {
73+
const errorMessage = `Failed to get record correction log details`;
74+
console.error('%s %o', errorMessage, error);
75+
if (error instanceof ApiError) {
76+
return res.status(error.status).send(`${errorMessage}: ${error.message}`);
77+
}
78+
return res.status(HttpStatusCode.InternalServerError).send(errorMessage);
79+
}
4280
};

0 commit comments

Comments
 (0)