Skip to content

Commit

Permalink
chore(EMS-1807-1808): Your Business - Construct payload (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainzzkk authored Jun 26, 2023
1 parent 1126912 commit 98c9e51
Show file tree
Hide file tree
Showing 24 changed files with 431 additions and 211 deletions.
30 changes: 20 additions & 10 deletions src/ui/server/controllers/insurance/business/broker/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PAGES } from '../../../../content-strings';
import { pageVariables, get, post, TEMPLATE } from '.';
import { pageVariables, get, post, TEMPLATE, FIELD_IDS } from '.';
import { TEMPLATES, ROUTES } from '../../../../constants';
import FIELD_IDS from '../../../../constants/field-ids/insurance/business';
import BUSINESS_FIELD_IDS from '../../../../constants/field-ids/insurance/business';
import insuranceCorePageVariables from '../../../../helpers/page-variables/core/insurance';
import constructPayload from '../../../../helpers/construct-payload';
import getUserNameFromSession from '../../../../helpers/get-user-name-from-session';
import mapApplicationToFormFields from '../../../../helpers/mappings/map-application-to-form-fields';
import generateValidationErrors from './validation';
Expand All @@ -13,7 +14,7 @@ import { mockReq, mockRes, mockApplication, mockBroker } from '../../../../test-

const { BROKER: BROKER_FIELDS } = FIELDS;

const { USING_BROKER, HEADING, NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, COUNTY, TOWN, POSTCODE, EMAIL, DETAILS } = FIELD_IDS.BROKER;
const { USING_BROKER, HEADING, NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, COUNTY, TOWN, POSTCODE, EMAIL, DETAILS } = BUSINESS_FIELD_IDS.BROKER;

const { BROKER } = PAGES.INSURANCE.EXPORTER_BUSINESS;
const { BROKER: BROKER_TEMPLATE } = TEMPLATES.INSURANCE.EXPORTER_BUSINESS;
Expand Down Expand Up @@ -48,6 +49,12 @@ describe('controllers/insurance/business/broker', () => {
});
});

describe('FIELD_IDS', () => {
it('should have the correct FIELD_IDS', () => {
expect(FIELD_IDS).toEqual([USING_BROKER, NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, TOWN, COUNTY, POSTCODE, EMAIL]);
});
});

describe('pageVariables', () => {
it('should have correct properties', () => {
const result = pageVariables(mockApplication.referenceNumber);
Expand Down Expand Up @@ -140,9 +147,7 @@ describe('controllers/insurance/business/broker', () => {
it('should render template with validation errors', async () => {
req.body = {};

const submittedValues = {
[USING_BROKER]: req.body[USING_BROKER],
};
const payload = constructPayload(req.body, FIELD_IDS);

await post(req, res);

Expand All @@ -157,7 +162,7 @@ describe('controllers/insurance/business/broker', () => {
...pageVariables(mockApplication.referenceNumber),
validationErrors,
application: mapApplicationToFormFields(mockApplication),
submittedValues,
submittedValues: payload,
});
});
});
Expand All @@ -172,14 +177,19 @@ describe('controllers/insurance/business/broker', () => {
expect(res.redirect).toHaveBeenCalledWith(expected);
});

it('should call mapAndSave.broker once with broker and application', async () => {
req.body = mockBroker;
it('should call mapAndSave.broker once with data from constructPayload function', async () => {
req.body = {
...mockBroker,
injection: 1,
};

await post(req, res);

const payload = constructPayload(req.body, FIELD_IDS);

expect(mapAndSave.broker).toHaveBeenCalledTimes(1);

expect(mapAndSave.broker).toHaveBeenCalledWith(req.body, mockApplication);
expect(mapAndSave.broker).toHaveBeenCalledWith(payload, mockApplication);
});

describe("when the url's last substring is `check-and-change`", () => {
Expand Down
13 changes: 9 additions & 4 deletions src/ui/server/controllers/insurance/business/broker/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { PAGES } from '../../../../content-strings';
import { TEMPLATES, ROUTES } from '../../../../constants';
import FIELD_IDS from '../../../../constants/field-ids/insurance/business';
import BUSINESS_FIELD_IDS from '../../../../constants/field-ids/insurance/business';
import { FIELDS } from '../../../../content-strings/fields/insurance/your-business';
import insuranceCorePageVariables from '../../../../helpers/page-variables/core/insurance';
import getUserNameFromSession from '../../../../helpers/get-user-name-from-session';
import constructPayload from '../../../../helpers/construct-payload';
import mapApplicationToFormFields from '../../../../helpers/mappings/map-application-to-form-fields';
import generateValidationErrors from './validation';
import mapAndSave from '../map-and-save/broker';
import isCheckAndChangeRoute from '../../../../helpers/is-check-and-change-route';
import { Request, Response } from '../../../../../types';

const { USING_BROKER, HEADING, NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, TOWN, COUNTY, POSTCODE, EMAIL, DETAILS } = FIELD_IDS.BROKER;
const { USING_BROKER, HEADING, NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, TOWN, COUNTY, POSTCODE, EMAIL, DETAILS } = BUSINESS_FIELD_IDS.BROKER;

const { BROKER } = PAGES.INSURANCE.EXPORTER_BUSINESS;
const { BROKER: BROKER_TEMPLATE } = TEMPLATES.INSURANCE.EXPORTER_BUSINESS;

export const TEMPLATE = BROKER_TEMPLATE;

export const FIELD_IDS = [USING_BROKER, NAME, ADDRESS_LINE_1, ADDRESS_LINE_2, TOWN, COUNTY, POSTCODE, EMAIL];

const {
INSURANCE_ROOT,
EXPORTER_BUSINESS: EXPORTER_BUSINESS_ROUTES,
Expand Down Expand Up @@ -120,6 +123,8 @@ const post = async (req: Request, res: Response) => {

const { body } = req;

const payload = constructPayload(body, FIELD_IDS);

// run validation on inputs
const validationErrors = generateValidationErrors(body);

Expand All @@ -134,12 +139,12 @@ const post = async (req: Request, res: Response) => {
...pageVariables(application.referenceNumber),
validationErrors,
application: mapApplicationToFormFields(application),
submittedValues: body,
submittedValues: payload,
});
}

// if no errors, then runs save api call to db
const saveResponse = await mapAndSave.broker(body, application);
const saveResponse = await mapAndSave.broker(payload, application);

if (!saveResponse) {
return res.redirect(PROBLEM_WITH_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Request, Response } from '../../../../../../types';
import { post } from '.';
import { FIELD_IDS, ROUTES } from '../../../../../constants';
import { mockReq, mockRes, mockApplication, mockBroker } from '../../../../../test-mocks';
import { FIELD_IDS } from '..';
import { ROUTES } from '../../../../../constants';
import BUSINESS_FIELD_IDS from '../../../../../constants/field-ids/insurance/business';
import constructPayload from '../../../../../helpers/construct-payload';
import mapAndSave from '../../map-and-save/broker';
import { mockReq, mockRes, mockApplication, mockBroker } from '../../../../../test-mocks';
import { Request, Response } from '../../../../../../types';

const {
EXPORTER_BUSINESS: {
BROKER: { NAME, POSTCODE },
},
} = FIELD_IDS.INSURANCE;
BROKER: { NAME, POSTCODE },
} = BUSINESS_FIELD_IDS;

const { INSURANCE_ROOT, ALL_SECTIONS, PROBLEM_WITH_SERVICE } = ROUTES.INSURANCE;

Expand Down Expand Up @@ -43,12 +44,19 @@ describe('controllers/insurance/business/broker/save-and-back', () => {
expect(res.redirect).toHaveBeenCalledWith(`${INSURANCE_ROOT}/${req.params.referenceNumber}${ALL_SECTIONS}`);
});

it('should call mapAndSave.broker once', async () => {
req.body = validBody;
it('should call mapAndSave.broker once with data from constructPayload function', async () => {
req.body = {
...validBody,
injection: 1,
};

await post(req, res);

const payload = constructPayload(req.body, FIELD_IDS);

expect(updateMapAndSave).toHaveBeenCalledTimes(1);

expect(updateMapAndSave).toHaveBeenCalledWith(payload, mockApplication, false);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Request, Response } from '../../../../../../types';
import { TEMPLATES, ROUTES } from '../../../../../constants';
import constructPayload from '../../../../../helpers/construct-payload';
import generateValidationErrors from '../validation';
import mapAndSave from '../../map-and-save/broker';
import { FIELD_IDS } from '..';

const { BROKER: BROKER_TEMPLATE } = TEMPLATES.INSURANCE.EXPORTER_BUSINESS;

Expand All @@ -26,11 +28,14 @@ const post = async (req: Request, res: Response) => {
}

const { body } = req;

const payload = constructPayload(body, FIELD_IDS);

// run validation on inputs
const validationErrors = generateValidationErrors(body);

// runs save and go back commmand
const saveResponse = await mapAndSave.broker(body, application, validationErrors);
const saveResponse = await mapAndSave.broker(payload, application, validationErrors);

if (!saveResponse) {
return res.redirect(PROBLEM_WITH_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from '../../../../../types';
import { pageVariables, post } from '.';
import { FIELD_IDS, ROUTES, TEMPLATES } from '../../../../constants';
import { pageVariables, post, FIELD_IDS } from '.';
import { ROUTES, TEMPLATES } from '../../../../constants';
import BUSINESS_FIELD_IDS from '../../../../constants/field-ids/insurance/business';
import insuranceCorePageVariables from '../../../../helpers/page-variables/core/insurance';
import getUserNameFromSession from '../../../../helpers/get-user-name-from-session';
import { PAGES } from '../../../../content-strings';
Expand All @@ -10,13 +11,12 @@ import { mockReq, mockRes, mockApplication, mockPhoneNumbers, mockCompany } from
import { sanitiseValue } from '../../../../helpers/sanitise-data';
import mapAndSave from '../map-and-save/company-details';
import api from '../../../../api';
import constructPayload from '../../../../helpers/construct-payload';

const {
EXPORTER_BUSINESS: {
COMPANY_HOUSE: { INPUT },
YOUR_COMPANY: { TRADING_NAME, TRADING_ADDRESS, WEBSITE, PHONE_NUMBER },
},
} = FIELD_IDS.INSURANCE;
COMPANY_HOUSE: { INPUT },
YOUR_COMPANY: { TRADING_NAME, TRADING_ADDRESS, WEBSITE, PHONE_NUMBER },
} = BUSINESS_FIELD_IDS;

const { COMPANY_DETAILS } = PAGES.INSURANCE.EXPORTER_BUSINESS;
const { COMPANY_DETAILS: companyDetailsTemplate } = TEMPLATES.INSURANCE.EXPORTER_BUSINESS;
Expand Down Expand Up @@ -108,15 +108,20 @@ describe('controllers/insurance/business/companies-details', () => {
expect(res.redirect).toHaveBeenCalledWith(expected);
});

it('should call mapAndSave.companyDetails once with updateBody and application', async () => {
req.body = body;
it('should call mapAndSave.companyDetails once with data from constructPayload function and application', async () => {
req.body = {
...body,
injection: 1,
};

await post(req, res);

expect(mapAndSave.companyDetails).toHaveBeenCalledTimes(1);

const payload = constructPayload(req.body, FIELD_IDS);

const updateBody = {
...req.body,
...payload,
...mockCompany,
};
expect(mapAndSave.companyDetails).toHaveBeenCalledWith(updateBody, mockApplication);
Expand Down
Loading

0 comments on commit 98c9e51

Please sign in to comment.