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(EMS-4015): application submission - xlsx - uk broker address #3468

Merged
merged 3 commits into from
Jan 13, 2025
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
195 changes: 121 additions & 74 deletions src/api/.keystone/config.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/api/content-strings/XLSX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const {
YOUR_COMPANY: { HAS_DIFFERENT_TRADING_NAME, DIFFERENT_TRADING_NAME, PHONE_NUMBER, HAS_DIFFERENT_TRADING_ADDRESS, WEBSITE },
},
POLICY: {
BROKER_DETAILS: { NAME: BROKER_NAME, EMAIL: BROKER_EMAIL, FULL_ADDRESS: BROKER_ADDRESS },
BROKER_DETAILS: { NAME: BROKER_NAME, EMAIL: BROKER_EMAIL },
BROKER_MANUAL_ADDRESS: { FULL_ADDRESS: BROKER_ADDRESS },
CONTRACT_POLICY: {
REQUESTED_START_DATE,
SINGLE: { CONTRACT_COMPLETION_DATE, TOTAL_CONTRACT_VALUE: TOTAL_CONTRACT_VALUE_ID },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { POLICY as FIELD_IDS } from '../../../../constants/field-ids/insurance/p
import { XLSX } from '../../../../content-strings';
import xlsxRow from '../../helpers/xlsx-row';
import mapYesNoField from '../../helpers/map-yes-no-field';
import mapBrokerAddress from './map-broker-address';
import { mockApplication } from '../../../../test-mocks';

const {
USING_BROKER,
BROKER_DETAILS: { NAME: BROKER_NAME, EMAIL, FULL_ADDRESS },
BROKER_DETAILS: { NAME: BROKER_NAME, EMAIL },
} = FIELD_IDS;

const { FIELDS } = XLSX;
Expand All @@ -23,7 +24,7 @@ describe('api/generate-xlsx/map-application-to-xlsx/map-policy/map-broker', () =
xlsxRow(String(FIELDS[USING_BROKER]), mapYesNoField({ answer: broker[USING_BROKER] })),
xlsxRow(String(FIELDS[BROKER_NAME]), broker[BROKER_NAME]),
xlsxRow(String(FIELDS[EMAIL]), broker[EMAIL]),
xlsxRow(String(FIELDS[FULL_ADDRESS]), broker[FULL_ADDRESS]),
mapBrokerAddress(broker),
];

expect(result).toEqual(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { POLICY as FIELD_IDS } from '../../../../constants/field-ids/insurance/p
import { XLSX } from '../../../../content-strings';
import xlsxRow from '../../helpers/xlsx-row';
import mapYesNoField from '../../helpers/map-yes-no-field';
import mapBrokerAddress from './map-broker-address';
import { Application } from '../../../../types';

const {
USING_BROKER,
BROKER_DETAILS: { NAME: BROKER_NAME, EMAIL, FULL_ADDRESS },
BROKER_DETAILS: { NAME: BROKER_NAME, EMAIL },
} = FIELD_IDS;

const { FIELDS } = XLSX;
Expand All @@ -23,12 +24,7 @@ const mapBroker = (application: Application) => {
let mapped = [xlsxRow(String(FIELDS[USING_BROKER]), mapYesNoField({ answer: broker[USING_BROKER] }))];

if (broker[USING_BROKER]) {
mapped = [
...mapped,
xlsxRow(String(FIELDS[BROKER_NAME]), broker[BROKER_NAME]),
xlsxRow(String(FIELDS[EMAIL]), broker[EMAIL]),
xlsxRow(String(FIELDS[FULL_ADDRESS]), broker[FULL_ADDRESS]),
];
mapped = [...mapped, xlsxRow(String(FIELDS[BROKER_NAME]), broker[BROKER_NAME]), xlsxRow(String(FIELDS[EMAIL]), broker[EMAIL]), mapBrokerAddress(broker)];
}

return mapped;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import mapBrokerAddressBasedInTheUk from '.';
import { POLICY as FIELD_IDS } from '../../../../../../constants/field-ids/insurance/policy';
import { ApplicationBroker } from '../../../../../../types';
import { mockApplication } from '../../../../../../test-mocks';

const {
BROKER_DETAILS: { BUILDING_NUMBER_OR_NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, TOWN, COUNTY, POSTCODE },
} = FIELD_IDS;

const { broker } = mockApplication;

const { buildingNumberOrName, addressLine1, addressLine2, town, county, postcode } = broker;

describe('api/generate-xlsx/map-application-to-xlsx/map-policy/map-broker/map-broker-address/based-in-the-uk', () => {
abhi-markan marked this conversation as resolved.
Show resolved Hide resolved
describe(`when a broker only has ${BUILDING_NUMBER_OR_NAME}, ${ADDRESS_LINE_1} and ${POSTCODE} generic fields`, () => {
it('should return a single string containing only said fields', () => {
const mockBroker: ApplicationBroker = {
...broker,
[ADDRESS_LINE_2]: '',
[TOWN]: '',
[COUNTY]: '',
};

const result = mapBrokerAddressBasedInTheUk(mockBroker);

const expected = `${buildingNumberOrName}\n${addressLine1}\n${postcode}`;

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

describe(`when a broker has generic fields and ${ADDRESS_LINE_2}`, () => {
it('should return a single string containing only said fields', () => {
const mockBroker: ApplicationBroker = {
...broker,
[TOWN]: '',
[COUNTY]: '',
};

const result = mapBrokerAddressBasedInTheUk(mockBroker);

const expected = `${buildingNumberOrName}\n${addressLine1}\n${addressLine2}\n${postcode}`;

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

describe(`when a broker has generic fields and ${TOWN}`, () => {
it('should return a single string containing only said fields', () => {
const mockBroker: ApplicationBroker = {
...broker,
[ADDRESS_LINE_2]: '',
[COUNTY]: '',
};

const result = mapBrokerAddressBasedInTheUk(mockBroker);

const expected = `${buildingNumberOrName}\n${addressLine1}\n${town}\n${postcode}`;

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

describe(`when a broker has generic fields and ${COUNTY}`, () => {
it('should return a single string containing only said fields', () => {
const mockBroker: ApplicationBroker = {
...broker,
[ADDRESS_LINE_2]: '',
[TOWN]: '',
};

const result = mapBrokerAddressBasedInTheUk(mockBroker);

const expected = `${buildingNumberOrName}\n${addressLine1}\n${county}\n${postcode}`;

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

describe('when a broker has all possible fields', () => {
it('should return a single string with all fields', () => {
const result = mapBrokerAddressBasedInTheUk(broker);

const expected = `${buildingNumberOrName}\n${addressLine1}\n${addressLine2}\n${town}\n${county}\n${postcode}`;

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

describe('when all broker fields are empty string', () => {
it('should return a single string with all fields', () => {
const mockBroker: ApplicationBroker = {
...broker,
[BUILDING_NUMBER_OR_NAME]: '',
[ADDRESS_LINE_1]: '',
[ADDRESS_LINE_2]: '',
[TOWN]: '',
[COUNTY]: '',
[POSTCODE]: '',
};

const result = mapBrokerAddressBasedInTheUk(mockBroker);

expect(result).toEqual('');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ApplicationBroker } from '../../../../../../types';

/**
* mapBrokerAddressBasedInTheUk
* Map an application's broker "based in the UK" address fields into a string for an XLSX object/column
* A broker's UK address is saved in multiple fields.
* The XLSX requires this data to be rendered in a single string field/column.
* @param {ApplicationBroker} broker
* @returns {String} Broker UK address string
*/
const mapBrokerAddressBasedInTheUk = (broker: ApplicationBroker) => {
const { buildingNumberOrName, addressLine1, addressLine2, town, county, postcode } = broker;

let addressString = '';

if (buildingNumberOrName) {
addressString += `${buildingNumberOrName}\n`;
}

if (addressLine1) {
addressString += `${addressLine1}\n`;
}

if (addressLine2) {
addressString += `${addressLine2}\n`;
}

if (town) {
addressString += `${town}\n`;
}

if (county) {
addressString += `${county}\n`;
}

addressString += postcode;

return addressString;
};

export default mapBrokerAddressBasedInTheUk;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import mapBrokerAddress from '.';
import { POLICY as FIELD_IDS } from '../../../../../constants/field-ids/insurance/policy';
import { XLSX } from '../../../../../content-strings';
import mapBrokerAddressBasedInTheUk from './based-in-the-uk';
import xlsxRow from '../../../helpers/xlsx-row';
import { mockApplication } from '../../../../../test-mocks';

const {
BROKER_DETAILS: { IS_BASED_IN_UK },
BROKER_MANUAL_ADDRESS: { FULL_ADDRESS },
} = FIELD_IDS;

const { FIELDS } = XLSX;

describe('api/generate-xlsx/map-application-to-xlsx/map-policy/map-broker/map-broker-address', () => {
describe(`when ${IS_BASED_IN_UK} is true`, () => {
const mockBroker = {
...mockApplication.broker,
[IS_BASED_IN_UK]: true,
};

it('should return xlsxRow with the value via mapBrokerAddressBasedInTheUk', () => {
const result = mapBrokerAddress(mockBroker);

const expectedValue = mapBrokerAddressBasedInTheUk(mockBroker);

const expected = xlsxRow(String(FIELDS[FULL_ADDRESS]), expectedValue);

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

describe(`when ${IS_BASED_IN_UK} is false`, () => {
const mockBroker = {
...mockApplication.broker,
[IS_BASED_IN_UK]: false,
};

it(`should return xlsxRow with the value as ${FULL_ADDRESS}`, () => {
const result = mapBrokerAddress(mockBroker);

const expectedValue = mockBroker[FULL_ADDRESS];

const expected = xlsxRow(String(FIELDS[FULL_ADDRESS]), expectedValue);

expect(result).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { POLICY as FIELD_IDS } from '../../../../../constants/field-ids/insurance/policy';
import { XLSX } from '../../../../../content-strings';
import mapBrokerAddressBasedInTheUk from './based-in-the-uk';
import xlsxRow from '../../../helpers/xlsx-row';
import { ApplicationBroker } from '../../../../../types';

const {
BROKER_DETAILS: { IS_BASED_IN_UK },
BROKER_MANUAL_ADDRESS: { FULL_ADDRESS },
} = FIELD_IDS;

const { FIELDS } = XLSX;

/**
* mapBrokerAddress
* Map an application's broker address fields into an object for XLSX generation
* - If the broker is based in the UK, consume mapBrokerAddressBasedInTheUk.
* - Otherwise, simply consume the FULL_ADDRESS field.
* @param {ApplicationBroker} broker
* @returns {object} Object for XLSX generation
*/
const mapBrokerAddress = (broker: ApplicationBroker) => {
let fieldValue = '';

if (broker[IS_BASED_IN_UK]) {
fieldValue = mapBrokerAddressBasedInTheUk(broker);
} else {
fieldValue = broker[FULL_ADDRESS];
}

return xlsxRow(String(FIELDS[FULL_ADDRESS]), fieldValue);
};

export default mapBrokerAddress;
21 changes: 21 additions & 0 deletions src/api/helpers/string/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isEmptyString } from '.';

describe('api/helpers/string', () => {
describe('isEmptyString', () => {
describe('when a string is empty', () => {
it('should return true', () => {
abhi-markan marked this conversation as resolved.
Show resolved Hide resolved
const result = isEmptyString('');

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

describe('when a string is not empty', () => {
it('should return false', () => {
const result = isEmptyString('Mock');

expect(result).toEqual(false);
});
});
});
});
7 changes: 7 additions & 0 deletions src/api/helpers/string/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* isEmptyString
* Check if a string is empty
* @param {String}
* @returns {Boolean}
*/
export const isEmptyString = (str: string) => str === '';
abhi-markan marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions src/api/test-mocks/mock-broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const mockBroker = {
name: 'Mock broker name',
email: process.env.GOV_NOTIFY_EMAIL_RECIPIENT_1,
fullAddress: 'Mock broker address',
buildingNumberOrName: 'Mock broker building name/number',
addressLine1: 'Mock broker address line 1',
addressLine2: 'Mock broker address line 2',
town: 'Mock broker town',
county: 'Mock broker county',
postcode: 'Mock broker postcode',
};

export default mockBroker;
10 changes: 8 additions & 2 deletions src/api/types/application-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import { Relationship } from '../relationship';
export interface ApplicationBroker extends Relationship {
isUsingBroker?: boolean;
name?: string;
fullAddress?: string;
email?: string;
fullAddress?: string;
buildingNumberOrName?: string;
addressLine1?: string;
addressLine2?: string;
town?: string;
county?: string;
postcode?: string;
}

export interface ApplicationBusiness extends Relationship {
Expand Down Expand Up @@ -290,7 +296,7 @@ export interface Application {
previousStatus?: string;
eligibility: ApplicationEligibility;
exportContract: ApplicationExportContract;
broker: Relationship;
broker: ApplicationBroker;
business: ApplicationBusiness;
buyer: ApplicationBuyer;
company: ApplicationCompany;
Expand Down
Loading