Skip to content

Commit

Permalink
chore(EMS-3633): data migration - required fields for in progress app…
Browse files Browse the repository at this point in the history
…lications (#2931)

* chore(EMS-3633): data migration - required fields for in progress application

* chore(EMS-3633): data migration - required fields for in progress application

* chore(EMS-3633): various code updates/simplifications

* chore(EMS-3633): add missing params

* chore(EMS-3633): simplify summary list call

* chore(EMS-3633): fix/update unit test
  • Loading branch information
ttbarnes authored Aug 9, 2024
1 parent bcd74de commit a64af4e
Show file tree
Hide file tree
Showing 29 changed files with 392 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('api/generate-xlsx/map-application-to-xlsx/map-buyer', () => {
it('should return an array of mapped buyer fields', () => {
const result = mapBuyer(mockApplication);

const { eligibility, buyer } = mockApplication;
const { eligibility, buyer, migratedV1toV2 } = mockApplication;
const { buyerTradingHistory, relationship } = buyer;

const expected = [
Expand All @@ -44,7 +44,7 @@ describe('api/generate-xlsx/map-application-to-xlsx/map-buyer', () => {

...mapBuyerTradingHistory(buyerTradingHistory),

...mapPreviousCoverWithBuyer(eligibility, relationship),
...mapPreviousCoverWithBuyer(eligibility, relationship, migratedV1toV2),

xlsxRow(String(FIELDS[HAS_BUYER_FINANCIAL_ACCOUNTS]), mapYesNoField({ answer: relationship[HAS_BUYER_FINANCIAL_ACCOUNTS] })),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const { FIELDS } = XLSX;
* @returns {Array<object>} Array of objects for XLSX generation
*/
const mapBuyer = (application: Application) => {
const { buyer, eligibility } = application;
const { buyer, eligibility, migratedV1toV2 } = application;
const { buyerTradingHistory, relationship } = buyer;

const mapped = [
Expand All @@ -46,7 +46,7 @@ const mapBuyer = (application: Application) => {

...mapBuyerTradingHistory(buyerTradingHistory),

...mapPreviousCoverWithBuyer(eligibility, relationship),
...mapPreviousCoverWithBuyer(eligibility, relationship, migratedV1toV2),

xlsxRow(String(FIELDS[HAS_BUYER_FINANCIAL_ACCOUNTS]), mapYesNoField({ answer: relationship[HAS_BUYER_FINANCIAL_ACCOUNTS] })),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import mapPreviousCoverWithBuyer from '.';
import { TOTAL_CONTRACT_VALUE } from '../../../../constants/total-contract-value';
import FIELD_IDS from '../../../../constants/field-ids/insurance/your-buyer';
import FIELD_IDS from '../../../../constants/field-ids/insurance';
import { XLSX } from '../../../../content-strings';
import mapYesNoField from '../../helpers/map-yes-no-field';
import xlsxRow from '../../helpers/xlsx-row';
import { mockApplication, mockApplicationSinglePolicyTotalContractValueOverThreshold } from '../../../../test-mocks';
import { mockApplicationSinglePolicyTotalContractValueOverThreshold, mockApplicationEligibilityTotalContractValueBelowThreshold } from '../../../../test-mocks';

const { HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER, PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER } = FIELD_IDS;
const {
MIGRATED_FROM_V1_TO_V2,
YOUR_BUYER: { HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER, PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER },
} = FIELD_IDS;

const { FIELDS } = XLSX;

Expand All @@ -15,57 +18,94 @@ const {
eligibility,
} = mockApplicationSinglePolicyTotalContractValueOverThreshold;

const mockRelationshipTrue = {
...relationship,
[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]: true,
};

const mockRelationshipFalse = {
...relationship,
[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]: false,
};

describe('api/generate-xlsx/map-application-to-xlsx/map-buyer/map-previous-cover-with-buyer', () => {
describe(`when the total contract value is ${TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE}`, () => {
describe(`${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER} is true`, () => {
describe(`when ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER} is true`, () => {
it(`should return an array of mapped fields with ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER}`, () => {
const mockRelationship = {
...relationship,
[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]: true,
};

const result = mapPreviousCoverWithBuyer(eligibility, mockRelationship);
const result = mapPreviousCoverWithBuyer(eligibility, mockRelationshipTrue, false);

const expected = [
xlsxRow(
String(FIELDS[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
mapYesNoField({
answer: mockRelationship[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER],
answer: mockRelationshipTrue[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER],
}),
),
xlsxRow(String(FIELDS[PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]), mockRelationship[PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
xlsxRow(String(FIELDS[PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]), mockRelationshipTrue[PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
];

expect(result).toEqual(expected);
});
});

describe(`${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER} is false`, () => {
it(`should return an array of mapped fields without ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER}`, () => {
const mockRelationship = {
...relationship,
exporterHasPreviousCreditInsuranceWithBuyer: false,
};
describe(`when ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER} is false`, () => {
it('should return an array with one field', () => {
const result = mapPreviousCoverWithBuyer(eligibility, mockRelationshipFalse, false);

const expected = [
xlsxRow(
String(FIELDS[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
mapYesNoField({
answer: mockRelationshipFalse[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER],
}),
),
];

expect(result).toEqual(expected);
});
});
});

const result = mapPreviousCoverWithBuyer(eligibility, mockRelationship);
describe(`when ${MIGRATED_FROM_V1_TO_V2} is true`, () => {
describe(`when ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER} is true`, () => {
it(`should return an array of mapped fields with ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER}`, () => {
const result = mapPreviousCoverWithBuyer(mockApplicationEligibilityTotalContractValueBelowThreshold, mockRelationshipTrue, true);

const expected = [
xlsxRow(
String(FIELDS[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
mapYesNoField({
answer: mockRelationship[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER],
answer: mockRelationshipTrue[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER],
}),
),
xlsxRow(String(FIELDS[PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]), mockRelationshipTrue[PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
];

expect(result).toEqual(expected);
});

describe(`when ${HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER} is false`, () => {
it('should return an array with one field', () => {
const result = mapPreviousCoverWithBuyer(mockApplicationEligibilityTotalContractValueBelowThreshold, mockRelationshipFalse, true);

const expected = [
xlsxRow(
String(FIELDS[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]),
mapYesNoField({
answer: mockRelationshipFalse[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER],
}),
),
];

expect(result).toEqual(expected);
});
});
});
});

describe(`when the total contract value is NOT ${TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE}`, () => {
describe(`when the total contract value is NOT ${TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE} and ${MIGRATED_FROM_V1_TO_V2} is false`, () => {
it('should return an empty array', () => {
const result = mapPreviousCoverWithBuyer(mockApplication.eligibility, relationship);
const result = mapPreviousCoverWithBuyer(mockApplicationEligibilityTotalContractValueBelowThreshold, relationship, false);

expect(result).toEqual([]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ const { FIELDS } = XLSX;
* Generate an XLSX row if an exporter has "previous cover" with the buyer.
* @param {ApplicationEligibility} eligibility: Application eligibility
* @param {ApplicationBuyerRelationship} relationship: Application buyer relationship
* @param {Boolean} migratedV1toV2: Application has been migrated from V1 to V2
* @returns {Array<object>} Array of objects for XLSX generation
*/
const mapPreviousCoverWithBuyer = (eligibility: ApplicationEligibility, relationship: ApplicationBuyerRelationship) => {
const mapPreviousCoverWithBuyer = (eligibility: ApplicationEligibility, relationship: ApplicationBuyerRelationship, migratedV1toV2?: boolean) => {
// TODO: EMS-3467: move to getPopulatedApplication.
const totalContractValueOverThreshold = eligibility.totalContractValue.value === TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE;

if (totalContractValueOverThreshold) {
if (totalContractValueOverThreshold || migratedV1toV2) {
const answer = relationship[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER];

const mapped = [xlsxRow(String(FIELDS[HAS_PREVIOUS_CREDIT_INSURANCE_COVER_WITH_BUYER]), mapYesNoField({ answer }))];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mapPrivateMarket from '.';
import { TOTAL_CONTRACT_VALUE } from '../../../../constants/total-contract-value';
import FIELD_IDS from '../../../../constants/field-ids/insurance/export-contract';
import FIELD_IDS from '../../../../constants/field-ids/insurance';
import { XLSX } from '../../../../content-strings';
import xlsxRow from '../../helpers/xlsx-row';
import mapYesNoField from '../../helpers/map-yes-no-field';
Expand All @@ -9,33 +9,46 @@ import { mockApplication } from '../../../../test-mocks';
const { FIELDS } = XLSX;

const {
PRIVATE_MARKET: { ATTEMPTED, DECLINED_DESCRIPTION },
EXPORT_CONTRACT: {
PRIVATE_MARKET: { ATTEMPTED, DECLINED_DESCRIPTION },
},
MIGRATED_FROM_V1_TO_V2,
} = FIELD_IDS;

const {
eligibility,
exportContract: { privateMarket },
} = mockApplication;

const mockTotalContractValueOverThreshold = {
...eligibility.totalContractValue,
value: TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE,
};

const mockTotalContractValueUnderThreshold = {
...eligibility.totalContractValue,
value: TOTAL_CONTRACT_VALUE.LESS_THAN_250K.VALUE,
};

const mockPrivateMarketAttemptedTrue = {
...privateMarket,
[ATTEMPTED]: true,
};

const mockPrivateMarketAttemptedFalse = {
...privateMarket,
[ATTEMPTED]: false,
};

describe('api/generate-xlsx/map-application-to-xlsx/map-private-market', () => {
describe(`when the total contract value is ${TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE}`, () => {
const mockTotalContractValue = {
...eligibility.totalContractValue,
value: TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE,
};

describe(`when ${ATTEMPTED} is true`, () => {
it('should return an array of mapped fields', () => {
const mockPrivateMarket = {
...privateMarket,
[ATTEMPTED]: true,
};

const result = mapPrivateMarket(mockPrivateMarket, mockTotalContractValue);
const result = mapPrivateMarket(mockPrivateMarketAttemptedTrue, mockTotalContractValueOverThreshold, false);

const expected = [
xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: mockPrivateMarket[ATTEMPTED] })),
xlsxRow(String(FIELDS.EXPORT_CONTRACT[DECLINED_DESCRIPTION]), mockPrivateMarket[DECLINED_DESCRIPTION]),
xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: mockPrivateMarketAttemptedTrue[ATTEMPTED] })),
xlsxRow(String(FIELDS.EXPORT_CONTRACT[DECLINED_DESCRIPTION]), mockPrivateMarketAttemptedTrue[DECLINED_DESCRIPTION]),
];

expect(result).toEqual(expected);
Expand All @@ -44,28 +57,43 @@ describe('api/generate-xlsx/map-application-to-xlsx/map-private-market', () => {

describe(`when ${ATTEMPTED} is false`, () => {
it('should return an array with one field', () => {
const mockPrivateMarket = {
...privateMarket,
[ATTEMPTED]: false,
};
const result = mapPrivateMarket(mockPrivateMarketAttemptedFalse, mockTotalContractValueOverThreshold, false);

const result = mapPrivateMarket(mockPrivateMarket, mockTotalContractValue);
const expected = [xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: mockPrivateMarketAttemptedFalse[ATTEMPTED] }))];

const expected = [xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: mockPrivateMarket[ATTEMPTED] }))];
expect(result).toEqual(expected);
});
});
});

describe(`when ${MIGRATED_FROM_V1_TO_V2} is true`, () => {
describe(`when ${ATTEMPTED} is true`, () => {
it('should return an array of mapped fields', () => {
const result = mapPrivateMarket(mockPrivateMarketAttemptedTrue, mockTotalContractValueUnderThreshold, true);

const expected = [
xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: mockPrivateMarketAttemptedTrue[ATTEMPTED] })),
xlsxRow(String(FIELDS.EXPORT_CONTRACT[DECLINED_DESCRIPTION]), mockPrivateMarketAttemptedTrue[DECLINED_DESCRIPTION]),
];

expect(result).toEqual(expected);
});
});

describe(`when ${ATTEMPTED} is false`, () => {
it('should return an array with one field', () => {
const result = mapPrivateMarket(mockPrivateMarketAttemptedFalse, mockTotalContractValueUnderThreshold, true);

const expected = [xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: mockPrivateMarketAttemptedFalse[ATTEMPTED] }))];

expect(result).toEqual(expected);
});
});
});

describe(`when the total contract value is NOT ${TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE}`, () => {
describe(`when the total contract value is NOT ${TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE} and ${MIGRATED_FROM_V1_TO_V2} is false`, () => {
it('should return an empty array', () => {
const mockTotalContractValue = {
...eligibility.totalContractValue,
value: TOTAL_CONTRACT_VALUE.LESS_THAN_250K.VALUE,
};

const result = mapPrivateMarket(privateMarket, mockTotalContractValue);
const result = mapPrivateMarket(privateMarket, mockTotalContractValueUnderThreshold, false);

expect(result).toEqual([]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ const {
* Map an application's "export contract - private market" fields into an array of objects for XLSX generation
* @param {Application} application: Application
* @param {TotalContractValue} totalContractValue: Total contract value
* @param {Boolean} migratedV1toV2: Application has been migrated from V1 to V2
* @returns {Array<object>} Array of objects for XLSX generation
*/
const mapPrivateMarket = (privateMarket: ApplicationPrivateMarket, totalContractValue: TotalContractValue) => {
const mapPrivateMarket = (privateMarket: ApplicationPrivateMarket, totalContractValue: TotalContractValue, migratedV1toV2?: boolean) => {
// TODO: EMS-3467: move to getPopulatedApplication.
const totalContractValueOverThreshold = totalContractValue.value === TOTAL_CONTRACT_VALUE.MORE_THAN_250K.VALUE;

if (totalContractValueOverThreshold) {
if (totalContractValueOverThreshold || migratedV1toV2) {
const attempedPrivateMarketAnswer = privateMarket[ATTEMPTED];

const mapped = [xlsxRow(String(FIELDS.EXPORT_CONTRACT[ATTEMPTED]), mapYesNoField({ answer: attempedPrivateMarketAnswer }))];
Expand Down
7 changes: 7 additions & 0 deletions src/api/test-mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export const mockApplicationSinglePolicyTotalContractValueOverThreshold = {
},
};

export const mockApplicationEligibilityTotalContractValueBelowThreshold = {
...mockApplicationSinglePolicyTotalContractValueOverThreshold.eligibility,
[TOTAL_CONTRACT_VALUE_FIELD_ID]: {
value: TOTAL_CONTRACT_VALUE.LESS_THAN_250K.VALUE,
},
};

export const mockApplicationMultiplePolicyTotalContractValueOverThreshold = {
...mockApplicationMinimalBrokerBuyerAndCompany,
policy: {
Expand Down
1 change: 1 addition & 0 deletions src/api/types/application-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export interface Application {
companySicCodes: Array<ApplicationCompanySicCode>;
companyAddress: ApplicationCompanyAddress;
declaration: ApplicationDeclaration;
migratedV1toV2?: boolean;
nominatedLossPayee: ApplicationNominatedLossPayee;
owner: ApplicationOwner;
policy: ApplicationPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
INSURANCE: { INSURANCE_ROOT, ALL_SECTIONS, PROBLEM_WITH_SERVICE },
} = ROUTES;

const { exportContract, totalContractValueOverThreshold } = mockApplication;
const { exportContract, migratedV1toV2, totalContractValueOverThreshold } = mockApplication;

const {
finalDestinationKnown,
Expand Down Expand Up @@ -94,7 +94,14 @@ describe('controllers/insurance/check-your-answers/export-contract', () => {

const checkAndChange = true;

const summaryList = exportContractSummaryLists(exportContract, totalContractValueOverThreshold, referenceNumber, mockCountries, checkAndChange);
const summaryList = exportContractSummaryLists(
exportContract,
totalContractValueOverThreshold,
migratedV1toV2,
referenceNumber,
mockCountries,
checkAndChange,
);

const exportContractFields = requiredFields({
totalContractValueOverThreshold,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const get = async (req: Request, res: Response) => {
return res.redirect(PROBLEM_WITH_SERVICE);
}

const { referenceNumber, exportContract, totalContractValueOverThreshold } = application;
const { referenceNumber, exportContract, totalContractValueOverThreshold, migratedV1toV2 } = application;

const {
finalDestinationKnown,
Expand All @@ -67,7 +67,7 @@ export const get = async (req: Request, res: Response) => {

const countries = await api.keystone.countries.getAll();

const summaryList = exportContractSummaryLists(exportContract, totalContractValueOverThreshold, referenceNumber, countries, checkAndChange);
const summaryList = exportContractSummaryLists(exportContract, totalContractValueOverThreshold, migratedV1toV2, referenceNumber, countries, checkAndChange);

const exportContractFields = requiredFields({
totalContractValueOverThreshold,
Expand Down
Loading

0 comments on commit a64af4e

Please sign in to comment.