Skip to content

Commit

Permalink
chore(EMS-4013): update get application middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbarnes committed Jan 29, 2025
1 parent 8e1bfac commit 9754747
Show file tree
Hide file tree
Showing 251 changed files with 1,117 additions and 3,202 deletions.
24 changes: 5 additions & 19 deletions src/ui/server/controllers/insurance/all-sections/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { TEMPLATE, get } from '.';
import { PAGES } from '../../../content-strings';
import { ROUTES, TEMPLATES } from '../../../constants';
import { TEMPLATES } from '../../../constants';
import insuranceCorePageVariables from '../../../helpers/page-variables/core/insurance';
import getUserNameFromSession from '../../../helpers/get-user-name-from-session';
import generateGroupsAndTasks from '../../../helpers/task-list/generate-groups-and-tasks';
import generateTaskList from '../../../helpers/task-list';
import flattenApplicationData from '../../../helpers/flatten-application-data';
import mapApplicationToFormFields from '../../../helpers/mappings/map-application-to-form-fields';
import { Request, Response } from '../../../../types';
import { mockReq, mockRes, mockApplication } from '../../../test-mocks';

const { PROBLEM_WITH_SERVICE } = ROUTES.INSURANCE;
import { Request, ResponseInsurance } from '../../../../types';
import { mockReq, mockResInsurance, mockApplication } from '../../../test-mocks';

describe('controllers/insurance/all-sections', () => {
let req: Request;
let res: Response;
let res: ResponseInsurance;

beforeEach(() => {
req = mockReq();
res = mockRes();
res = mockResInsurance();
});

describe('TEMPLATE', () => {
Expand Down Expand Up @@ -102,17 +100,5 @@ describe('controllers/insurance/all-sections', () => {

expect(res.render).toHaveBeenCalledWith(TEMPLATES.INSURANCE.ALL_SECTIONS, expectedVariables);
});

describe('when there is no application', () => {
beforeEach(() => {
delete res.locals.application;
});

it(`should redirect to ${PROBLEM_WITH_SERVICE}`, () => {
get(req, res);

expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE);
});
});
});
});
12 changes: 4 additions & 8 deletions src/ui/server/controllers/insurance/all-sections/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { PAGES } from '../../../content-strings';
import { ROUTES, TEMPLATES } from '../../../constants';
import { TEMPLATES } from '../../../constants';
import insuranceCorePageVariables from '../../../helpers/page-variables/core/insurance';
import getUserNameFromSession from '../../../helpers/get-user-name-from-session';
import generateGroupsAndTasks from '../../../helpers/task-list/generate-groups-and-tasks';
import generateTaskList from '../../../helpers/task-list';
import flattenApplicationData from '../../../helpers/flatten-application-data';
import mapApplicationToFormFields from '../../../helpers/mappings/map-application-to-form-fields';
import { Request, Response } from '../../../../types';
import { Request, ResponseInsurance } from '../../../../types';

export const TEMPLATE = TEMPLATES.INSURANCE.ALL_SECTIONS;

/**
* get
* Render the All sections page
* @param {Express.Request} Express request
* @param {Express.Response} Express response
* @param {ResponseInsurance} Express response for "insurance" routes
* @returns {Express.Response.render} All sections page
*/
export const get = (req: Request, res: Response) => {
export const get = (req: Request, res: ResponseInsurance) => {
const { application } = res.locals;

if (!application) {
return res.redirect(ROUTES.INSURANCE.PROBLEM_WITH_SERVICE);
}

const { broker, buyer, company, declaration, exportContract, nominatedLossPayee, policy, referenceNumber, totalContractValueOverThreshold } = application;

const { policyType, jointlyInsuredParty } = policy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { ROUTES, TEMPLATES, APPLICATION } from '../../../constants';
import insuranceCorePageVariables from '../../../helpers/page-variables/core/insurance';
import getUserNameFromSession from '../../../helpers/get-user-name-from-session';
import mapApplicationToFormFields from '../../../helpers/mappings/map-application-to-form-fields';
import { Request, Response } from '../../../../types';
import { mockReq, mockRes, mockApplication, referenceNumber } from '../../../test-mocks';
import { Request, ResponseInsurance } from '../../../../types';
import { mockReq, mockResInsurance, mockApplication, referenceNumber } from '../../../test-mocks';

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

describe('controllers/insurance/application-submitted', () => {
let req: Request;
let res: Response;
let res: ResponseInsurance;

beforeEach(() => {
req = mockReq();
res = mockRes();
res = mockResInsurance();

res.locals.application = {
...mockApplication,
Expand Down Expand Up @@ -47,18 +47,6 @@ describe('controllers/insurance/application-submitted', () => {
expect(res.render).toHaveBeenCalledWith(TEMPLATE, expectedVariables);
});

describe('when there is no application', () => {
beforeEach(() => {
delete res.locals.application;
});

it(`should redirect to ${PROBLEM_WITH_SERVICE}`, () => {
get(req, res);

expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE);
});
});

describe(`when the application does not have a status of ${APPLICATION.STATUS.SUBMITTED}`, () => {
beforeEach(() => {
res.locals.application = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PAGES } from '../../../content-strings';
import insuranceCorePageVariables from '../../../helpers/page-variables/core/insurance';
import getUserNameFromSession from '../../../helpers/get-user-name-from-session';
import mapApplicationToFormFields from '../../../helpers/mappings/map-application-to-form-fields';
import { Request, Response } from '../../../../types';
import { Request, ResponseInsurance } from '../../../../types';

const {
INSURANCE: { INSURANCE_ROOT, ALL_SECTIONS },
Expand All @@ -15,16 +15,12 @@ export const TEMPLATE = TEMPLATES.INSURANCE.APPLICATION_SUBMITTED;
* get
* Get the application and render the Application submitted page
* @param {Express.Request} Express request
* @param {Express.Response} Express response
* @param {ResponseInsurance} Express response for "insurance" routes
* @returns {Express.Response.render} Application submitted page
*/
export const get = (req: Request, res: Response) => {
export const get = (req: Request, res: ResponseInsurance) => {
const { application } = res.locals;

if (!application) {
return res.redirect(ROUTES.INSURANCE.PROBLEM_WITH_SERVICE);
}

const { referenceNumber } = req.params;

if (application.status !== APPLICATION.STATUS.SUBMITTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import INSURANCE_FIELD_IDS from '../../../../constants/field-ids/insurance';
import { EXPORTER_BUSINESS_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 { Request, Response } from '../../../../../types';
import { mockReq, mockRes, mockApplication, referenceNumber } from '../../../../test-mocks';
import { Request, ResponseInsurance } from '../../../../../types';
import { mockReq, mockResInsurance, mockApplication, referenceNumber } from '../../../../test-mocks';
import constructPayload from '../../../../helpers/construct-payload';
import generateValidationErrors from './validation';
import mapApplicationToFormFields from '../../../../helpers/mappings/map-application-to-form-fields';
Expand All @@ -25,7 +25,6 @@ const {
ALTERNATIVE_TRADING_ADDRESS_CHECK_AND_CHANGE,
},
CHECK_YOUR_ANSWERS: { YOUR_BUSINESS: CHECK_AND_CHANGE_ROUTE },
PROBLEM_WITH_SERVICE,
} = INSURANCE_ROUTES;

const {
Expand All @@ -38,11 +37,11 @@ const {

describe('controllers/insurance/business/alternative-trading-address', () => {
let req: Request;
let res: Response;
let res: ResponseInsurance;

beforeEach(() => {
req = mockReq();
res = mockRes();
res = mockResInsurance();
});

describe('TEMPLATE', () => {
Expand Down Expand Up @@ -91,18 +90,6 @@ describe('controllers/insurance/business/alternative-trading-address', () => {
SAVE_AND_BACK_URL: `${INSURANCE_ROOT}/${referenceNumber}${SAVE_AND_BACK}`,
});
});

describe('when there is no application', () => {
beforeEach(() => {
delete res.locals.application;
});

it(`should redirect to ${PROBLEM_WITH_SERVICE}`, () => {
get(req, res);

expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE);
});
});
});

describe('post', () => {
Expand Down Expand Up @@ -185,17 +172,5 @@ describe('controllers/insurance/business/alternative-trading-address', () => {
});
});
});

describe('when there is no application', () => {
beforeEach(() => {
delete res.locals.application;
});

it(`should redirect to ${PROBLEM_WITH_SERVICE}`, () => {
post(req, res);

expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import INSURANCE_FIELD_IDS from '../../../../constants/field-ids/insurance';
import { EXPORTER_BUSINESS_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 { Request, Response } from '../../../../../types';
import constructPayload from '../../../../helpers/construct-payload';
import generateValidationErrors from './validation';
import mapApplicationToFormFields from '../../../../helpers/mappings/map-application-to-form-fields';
import generateMultipleFieldHtml from '../../../../helpers/generate-multiple-field-html';
import mapAndSave from '../map-and-save/company-different-trading-address';
import isChangeRoute from '../../../../helpers/is-change-route';
import isCheckAndChangeRoute from '../../../../helpers/is-check-and-change-route';
import { Request, ResponseInsurance } from '../../../../../types';

const {
INSURANCE_ROOT,
Expand Down Expand Up @@ -57,17 +57,13 @@ const pageVariables = {
* get
* Render the alternative trading address page
* @param {Express.Request} Express request
* @param {Express.Response} Express response
* @param {ResponseInsurance} Express response for "insurance" routes
* @returns {Express.Response.render} Alternative trading address page
*/
const get = (req: Request, res: Response) => {
const get = (req: Request, res: ResponseInsurance) => {
try {
const { application } = res.locals;

if (!application) {
return res.redirect(PROBLEM_WITH_SERVICE);
}

const { company, referenceNumber } = application;

// generates address as HTML with line breaks
Expand Down Expand Up @@ -95,17 +91,13 @@ const get = (req: Request, res: Response) => {
* post
* Check Alternative trading address validation errors and if successful, redirect to the next part of the flow.
* @param {Express.Request} Express request
* @param {Express.Response} Express response
* @param {ResponseInsurance} Express response for "insurance" routes
* @returns {Express.Response.redirect} Next part of the flow or error page
*/
const post = async (req: Request, res: Response) => {
const post = async (req: Request, res: ResponseInsurance) => {
try {
const { application } = res.locals;

if (!application) {
return res.redirect(PROBLEM_WITH_SERVICE);
}

const { referenceNumber } = application;

const { body } = req;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FIELD_IDS } from '..';
import { ROUTES } from '../../../../../constants';
import BUSINESS_FIELD_IDS from '../../../../../constants/field-ids/insurance/business';
import mapAndSave from '../../map-and-save/company-different-trading-address';
import { mockReq, mockRes, mockApplication, mockSpyPromiseRejection, referenceNumber } from '../../../../../test-mocks';
import { Request, Response } from '../../../../../../types';
import { mockReq, mockResInsurance, mockApplication, mockSpyPromiseRejection, referenceNumber } from '../../../../../test-mocks';
import { Request, ResponseInsurance } from '../../../../../../types';

const {
ALTERNATIVE_TRADING_ADDRESS: { FULL_ADDRESS },
Expand All @@ -15,13 +15,13 @@ const { INSURANCE_ROOT, ALL_SECTIONS, PROBLEM_WITH_SERVICE } = ROUTES.INSURANCE;

describe('controllers/insurance/business/alternative-trading-address/save-and-back', () => {
let req: Request;
let res: Response;
let res: ResponseInsurance;

let updateMapAndSave = jest.fn(() => Promise.resolve(true));

beforeEach(() => {
req = mockReq();
res = mockRes();
res = mockResInsurance();

mapAndSave.companyDifferentTradingAddress = updateMapAndSave;
});
Expand Down Expand Up @@ -78,24 +78,10 @@ describe('controllers/insurance/business/alternative-trading-address/save-and-ba
});
});

describe('when there is no application', () => {
beforeEach(() => {
req.body = validBody;
delete res.locals.application;
});

it(`should redirect to ${PROBLEM_WITH_SERVICE}`, () => {
post(req, res);

expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE);
});
});

describe('api error handling', () => {
describe('when mapAndSave.companyDifferentTradingAddress returns false', () => {
beforeEach(() => {
req.body = validBody;
res.locals = mockRes().locals;
mapAndSave.companyDifferentTradingAddress = jest.fn(() => Promise.resolve(false));
});

Expand All @@ -108,7 +94,6 @@ describe('controllers/insurance/business/alternative-trading-address/save-and-ba

describe('when mapAndSave.companyDifferentTradingAddress fails', () => {
beforeEach(() => {
res.locals = mockRes().locals;
req.body = validBody;
updateMapAndSave = mockSpyPromiseRejection;
mapAndSave.companyDifferentTradingAddress = updateMapAndSave;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ import { FIELD_IDS } from '..';
import generateValidationErrors from '../validation';
import mapAndSave from '../../map-and-save/company-different-trading-address';
import constructPayload from '../../../../../helpers/construct-payload';
import { Request, Response } from '../../../../../../types';
import { Request, ResponseInsurance } from '../../../../../../types';

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

/**
* saves and goes back to all sections from alternative trading address page unless there are API errors
* @param {Express.Request} Express request
* @param {Express.Response} Express response
* @param {ResponseInsurance} Express response for "insurance" routes
* @returns {Express.Response.redirect} redirects to all sections page on success
*/
const post = async (req: Request, res: Response) => {
const post = async (req: Request, res: ResponseInsurance) => {
try {
const { application } = res.locals;

const { referenceNumber } = req.params;

if (!application) {
return res.redirect(PROBLEM_WITH_SERVICE);
}

const { body } = req;

const payload = constructPayload(body, FIELD_IDS);
Expand Down
Loading

0 comments on commit 9754747

Please sign in to comment.