Skip to content

Commit 5f149fd

Browse files
committed
feat(FN-3504): apply cover percentage to initial utilisation
1 parent ec78533 commit 5f149fd

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

dtfs-central-api/src/services/state-machines/utilisation-report/event-handlers/helpers/calculate-initial-utilisation-and-fixed-fee.test.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { calculateInitialUtilisation } from '@ukef/dtfs2-common';
1+
import * as dtfsCommon from '@ukef/dtfs2-common';
22
import { calculateInitialUtilisationAndFixedFee, parseDate, hasRequiredValues, RequiredParams } from './calculate-initial-utilisation-and-fixed-fee';
33
import { TfmFacilitiesRepo } from '../../../../../repositories/tfm-facilities-repo';
44
import { aTfmFacility } from '../../../../../../test-helpers';
5-
import { calculateInitialFixedFee } from './calculate-initial-fixed-fee';
6-
import { calculateUkefShareOfUtilisation } from '../../../../../helpers';
5+
import * as fixedFeeHelpers from './calculate-initial-fixed-fee';
6+
7+
jest.mock('./calculate-initial-fixed-fee');
8+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
9+
jest.mock('@ukef/dtfs2-common', () => ({
10+
...jest.requireActual('@ukef/dtfs2-common'),
11+
calculateDrawnAmount: jest.fn(),
12+
}));
713

814
describe('helpers/calculate-initial-utilisation-and-fixed-fee', () => {
915
describe('parseDate', () => {
@@ -145,26 +151,40 @@ describe('helpers/calculate-initial-utilisation-and-fixed-fee', () => {
145151
findOneByUkefFacilityIdSpy.mockResolvedValue(facility);
146152
});
147153

148-
it('should return a value for utilisation and fixed fee', async () => {
154+
it('should set initial utilisation to drawn amount rounded to 2 decimal places', async () => {
155+
// Arrange
156+
const drawnAmount = 12345.678;
157+
const drawnAmountRoundedToTwoDecimalPlaces = 12345.68;
158+
const calculateDrawnAmountSpy = jest.spyOn(dtfsCommon, 'calculateDrawnAmount').mockReturnValue(drawnAmount);
159+
jest.mocked(fixedFeeHelpers.calculateInitialFixedFee).mockReturnValue(999.99);
160+
161+
// Act
149162
const result = await calculateInitialUtilisationAndFixedFee(facilityId);
150163

151-
const { value, coverStartDate, coverEndDate, interestPercentage, dayCountBasis } = facility.facilitySnapshot;
164+
// Assert
165+
expect(calculateDrawnAmountSpy).toHaveBeenCalledWith(facility.facilitySnapshot.value, facility.facilitySnapshot.coverPercentage);
166+
expect(result.utilisation).toEqual(drawnAmountRoundedToTwoDecimalPlaces);
167+
});
152168

153-
const utilisation = calculateInitialUtilisation(value);
154-
const ukefShareOfUtilisation = calculateUkefShareOfUtilisation(utilisation, facility.facilitySnapshot.coverPercentage);
169+
it('should calculate and return the initial fixed fee', async () => {
170+
// Arrange
171+
const drawnAmount = 12345.678;
172+
const drawnAmountRoundedToTwoDecimalPlaces = 12345.68;
173+
jest.mocked(dtfsCommon.calculateDrawnAmount).mockReturnValue(drawnAmount);
174+
const calculateInitialFixedFeeSpy = jest.spyOn(fixedFeeHelpers, 'calculateInitialFixedFee').mockReturnValue(999.99);
155175

156-
const expected = {
157-
fixedFee: calculateInitialFixedFee({
158-
ukefShareOfUtilisation,
159-
coverStartDate: parseDate(coverStartDate),
160-
coverEndDate: parseDate(coverEndDate),
161-
interestPercentage,
162-
dayCountBasis,
163-
}),
164-
utilisation,
165-
};
176+
// Act
177+
const result = await calculateInitialUtilisationAndFixedFee(facilityId);
166178

167-
expect(result).toEqual(expected);
179+
// Assert
180+
expect(calculateInitialFixedFeeSpy).toHaveBeenCalledWith({
181+
ukefShareOfUtilisation: drawnAmountRoundedToTwoDecimalPlaces,
182+
coverStartDate: facility.facilitySnapshot.coverStartDate,
183+
coverEndDate: facility.facilitySnapshot.coverEndDate,
184+
interestPercentage: facility.facilitySnapshot.interestPercentage,
185+
dayCountBasis: facility.facilitySnapshot.dayCountBasis,
186+
});
187+
expect(result.fixedFee).toEqual(999.99);
168188
});
169189
});
170190
});

dtfs-central-api/src/services/state-machines/utilisation-report/event-handlers/helpers/calculate-initial-utilisation-and-fixed-fee.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { calculateInitialUtilisation, isValidDate } from '@ukef/dtfs2-common';
1+
import { calculateDrawnAmount, isValidDate } from '@ukef/dtfs2-common';
2+
import Big from 'big.js';
23
import { TfmFacilitiesRepo } from '../../../../../repositories/tfm-facilities-repo';
34
import { calculateInitialFixedFee } from './calculate-initial-fixed-fee';
4-
import { calculateUkefShareOfUtilisation } from '../../../../../helpers';
55

66
export type RequiredParams = {
77
value?: number | null;
@@ -70,12 +70,10 @@ export const calculateInitialUtilisationAndFixedFee = async (facilityId: string)
7070
throw new Error(`TFM facility values for ${facilityId} are missing`);
7171
}
7272

73-
const utilisation = calculateInitialUtilisation(value);
74-
75-
const ukefShareOfUtilisation = calculateUkefShareOfUtilisation(utilisation, coverPercentage);
73+
const ukefShareOfInitialUtilisation = new Big(calculateDrawnAmount(value, coverPercentage)).round(2).toNumber();
7674

7775
const fixedFee = calculateInitialFixedFee({
78-
ukefShareOfUtilisation,
76+
ukefShareOfUtilisation: ukefShareOfInitialUtilisation,
7977
coverStartDate: parseDate(coverStartDate),
8078
coverEndDate: parseDate(coverEndDate),
8179
interestPercentage,
@@ -84,6 +82,6 @@ export const calculateInitialUtilisationAndFixedFee = async (facilityId: string)
8482

8583
return {
8684
fixedFee,
87-
utilisation,
85+
utilisation: ukefShareOfInitialUtilisation,
8886
};
8987
};

0 commit comments

Comments
 (0)