Skip to content

Commit

Permalink
Merge pull request #173 from companieshouse/spike/refactor-transactio…
Browse files Browse the repository at this point in the history
…n-creation

Refactor transaction and submission creation
  • Loading branch information
AlisonGriffiths authored Nov 27, 2024
2 parents 2f5f655 + 0139531 commit d6b7dbf
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 110 deletions.
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export const Urls = {
START: "/start",
COMPANY_NUMBER: "/company-number",
CONFIRM_COMPANY: "/confirm-company",
INDIVIDUAL_PSC_LIST: "/individual/psc-list",
NEW_SUBMISSION: "/new-submission",
PSC_TYPE: `${urlWithTransactionIdAndSubmissionId}/psc-type`,
INDIVIDUAL_PSC_LIST: `${urlWithTransactionIdAndSubmissionId}/individual/psc-list`,
PERSONAL_CODE: `${urlWithTransactionIdAndSubmissionId}/individual/personal-code`,
INDIVIDUAL_STATEMENT: `${urlWithTransactionIdAndSubmissionId}/individual/psc-statement`,
PSC_VERIFIED: `${urlWithTransactionIdAndSubmissionId}/psc-verified`,
PSC_TYPE: `${urlWithTransactionIdAndSubmissionId}/psc-type`,
RLE_LIST: `${urlWithTransactionIdAndSubmissionId}/rle/rle-list`
} as const;

Expand All @@ -22,13 +22,13 @@ export const PrefixedUrls = {
HEALTHCHECK: servicePathPrefix + Urls.HEALTHCHECK,
COMPANY_NUMBER: servicePathPrefix + Urls.COMPANY_NUMBER,
CONFIRM_COMPANY: servicePathPrefix + Urls.CONFIRM_COMPANY,
NEW_SUBMISSION: servicePathPrefix + Urls.NEW_SUBMISSION,
INDIVIDUAL_PSC_LIST: servicePathPrefix + Urls.INDIVIDUAL_PSC_LIST,
PSC_TYPE: servicePathPrefix + Urls.PSC_TYPE,
NEW_SUBMISSION: servicePathPrefix + Urls.NEW_SUBMISSION,
PERSONAL_CODE: servicePathPrefix + Urls.PERSONAL_CODE,
INDIVIDUAL_STATEMENT: servicePathPrefix + Urls.INDIVIDUAL_STATEMENT,
PSC_VERIFIED: servicePathPrefix + Urls.PSC_VERIFIED,
RLE_LIST: servicePathPrefix + Urls.RLE_LIST,
PSC_TYPE: servicePathPrefix + Urls.PSC_TYPE,
COOKIES: "/help/cookies"
} as const;

Expand Down
4 changes: 2 additions & 2 deletions src/routerDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const routerDispatch = (app: Application) => {
router.use(Urls.HEALTHCHECK, HealthCheckRouter);
router.use(Urls.COMPANY_NUMBER, authenticate, CompanyNumberRouter);
router.use(Urls.CONFIRM_COMPANY, authenticate, ConfirmCompanyRouter);
router.use(Urls.INDIVIDUAL_PSC_LIST, authenticate, fetchCompany, IndividualPscListRouter);
router.use(Urls.NEW_SUBMISSION, authenticate, NewSubmissionRouter);
router.use(Urls.PSC_TYPE, authenticate, fetchVerification, PscTypeRouter);
router.use(Urls.INDIVIDUAL_PSC_LIST, authenticate, fetchVerification, fetchCompany, IndividualPscListRouter);
router.use(Urls.PERSONAL_CODE, authenticate, fetchVerification, PersonalCodeRouter);
router.use(Urls.INDIVIDUAL_STATEMENT, authenticate, fetchVerification, IndividualStatementRouter);
router.use(Urls.PSC_VERIFIED, authenticate, fetchVerification, fetchCompany, PscVerifiedRouter);
router.use(Urls.RLE_LIST, authenticate, RlePscListRouter);
router.use(Urls.PSC_TYPE, authenticate, fetchVerification, PscTypeRouter);

router.use("*", (req: Request, res: Response) => {
res.status(404).render("partials/error_400");
Expand Down
3 changes: 1 addition & 2 deletions src/routers/handlers/confirm-company/confirmCompany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class ConfirmCompanyHandler extends GenericHandler<ConfirmCompanyViewData
logger.info(`${ConfirmCompanyHandler.name} - ${this.executePost.name} called`);
const companyNumber = req.body.companyNumber as string;
const lang = selectLang(req.body.lang);
// return the url for createNewSubmission route
return addSearchParams(PrefixedUrls.NEW_SUBMISSION, { companyNumber, lang });
return addSearchParams(PrefixedUrls.INDIVIDUAL_PSC_LIST, { companyNumber, lang });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { PrefixedUrls, Urls } from "../../../constants";
import { logger } from "../../../lib/logger";
import { getLocaleInfo, getLocalesService, selectLang } from "../../../utils/localise";
import { addSearchParams } from "../../../utils/queryParams";
import { getUrlWithTransactionIdAndSubmissionId } from "../../../utils/url";
import { BaseViewData, GenericHandler, ViewModel } from "../generic";
import { CompanyPersonWithSignificantControl } from "@companieshouse/api-sdk-node/dist/services/company-psc/types";
import { getCompanyIndividualPscList } from "../../../services/companyPscService";
import { patchPscVerification } from "../../../services/pscVerificationService";
import { formatDateBorn, internationaliseDate } from "../../utils";
import { env } from "../../../config";

Expand Down Expand Up @@ -37,8 +35,7 @@ export class IndividualPscListHandler extends GenericHandler<IndividualPscListVi
const baseViewData = await super.getViewData(req, res);
const lang = selectLang(req.query.lang);
const locales = getLocalesService();
const verification = res.locals.submission;
const companyNumber = verification?.data?.companyNumber as string;
const companyNumber = req.query.companyNumber as string;
const companyProfile = res.locals.companyProfile;
const dsrEmailAddress = env.DSR_EMAIL_ADDRESS;
const dsrPhoneNumber = env.DSR_PHONE_NUMBER;
Expand All @@ -58,32 +55,28 @@ export class IndividualPscListHandler extends GenericHandler<IndividualPscListVi
individualPscList = await getCompanyIndividualPscList(req, companyNumber);
}

const selectedPscId = verification?.data?.pscAppointmentId as string;
const pscType = "individual";

return {
...baseViewData,
...getLocaleInfo(locales, lang),
currentUrl: resolveUrlTemplate(PrefixedUrls.INDIVIDUAL_PSC_LIST),
backURL: resolveUrlTemplate(PrefixedUrls.PSC_TYPE),
nextPageUrl: resolveUrlTemplate(PrefixedUrls.PERSONAL_CODE) + "&selectedPscId=",
backURL: resolveUrlTemplate(PrefixedUrls.CONFIRM_COMPANY),
nextPageUrl: resolveUrlTemplate(PrefixedUrls.NEW_SUBMISSION) + "&selectedPscId=",
companyName,
confirmationStatementDate,
dsrEmailAddress,
dsrPhoneNumber,
pscDetails: this.getPscDetails(individualPscList, lang),
selectedPscId,
pscDetails: this.getViewPscDetails(individualPscList, lang),
templateName: Urls.INDIVIDUAL_PSC_LIST,
backLinkDataEvent: "psc-list-back-link"
};

function resolveUrlTemplate (prefixedUrl: string): string | null {
return addSearchParams(getUrlWithTransactionIdAndSubmissionId(prefixedUrl, req.params.transactionId, req.params.submissionId), { lang, pscType });
return addSearchParams(prefixedUrl, { companyNumber, lang });
}
}

public async executeGet (req: Request, res: Response): Promise<ViewModel<IndividualPscListViewData>> {
logger.info(`${IndividualPscListHandler.name} - ${this.executeGet.name} called for transaction: ${req.params?.transactionId} and submissionId: ${req.params?.submissionId}`);
logger.info(`${IndividualPscListHandler.name} - ${this.executeGet.name} `);
const viewData = await this.getViewData(req, res);

return {
Expand All @@ -92,22 +85,7 @@ export class IndividualPscListHandler extends GenericHandler<IndividualPscListVi
};
}

public async executePost (req: Request, _response: Response) {
logger.info(`${IndividualPscListHandler.name} - ${this.executePost.name} called for transaction: ${req.params?.transactionId} and submissionId: ${req.params?.submissionId}`);

const pscSelected = req.body.pscSelect;
logger.debug(`${IndividualPscListHandler.name} - ${this.executePost.name} - patching submission resource for transaction: ${req.params.transactionId} and submissionId: ${req.params.submissionId} with PSC ID: ${pscSelected}`);
await patchPscVerification(req, req.params.transactionId, req.params.submissionId, { pscAppointmentId: pscSelected });

const lang = selectLang(req.query.lang);
const queryParams = new URLSearchParams(req.url.split("?")[1]);
queryParams.set("lang", lang);

const nextPageUrl = getUrlWithTransactionIdAndSubmissionId(PrefixedUrls.PERSONAL_CODE, req.params.transactionId, req.params.submissionId);
return (`${nextPageUrl}?${queryParams}`);
}

private getPscDetails (individualPscList: CompanyPersonWithSignificantControl[], lang: string): PscListData[] {
private getViewPscDetails (individualPscList: CompanyPersonWithSignificantControl[], lang: string): PscListData[] {
return individualPscList.map(psc => {
const pscFormattedDob = `${formatDateBorn(psc.dateOfBirth, lang)}`;

Expand Down
8 changes: 5 additions & 3 deletions src/routers/handlers/new-submission/newSubmissionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ export class NewSubmissionHandler extends GenericHandler<BaseViewData> {
const resource = await this.createNewSubmission(req, transaction);
logger.info(`${NewSubmissionHandler.name} - ${this.execute.name} - CREATED New Resource ${resource?.resource?.links.self}`);

// set up redirect to psc_type screen
// set up redirect to psc_code screen
const lang = selectLang(req.query.lang);
const regex = "significant-control-verification/(.*)$";
const resourceId = resource.resource?.links.self.match(regex);
const nextPageUrl = getUrlWithTransactionIdAndSubmissionId(PrefixedUrls.PSC_TYPE, transaction.id!, resourceId![1]);
const nextPageUrl = getUrlWithTransactionIdAndSubmissionId(PrefixedUrls.PERSONAL_CODE, transaction.id!, resourceId![1]);

// send the redirect
return addSearchParams(nextPageUrl, { lang });
}

public async createNewSubmission (request: Request, transaction: Transaction): Promise<Resource<PscVerification>> {
const companyNumber = request.query.companyNumber as string;
const pscAppointmentId = request.query.selectedPscId as string;
const verification: PscVerificationData = {
companyNumber: companyNumber
companyNumber: companyNumber,
pscAppointmentId: pscAppointmentId
};
return createPscVerification(request, transaction, verification);
}
Expand Down
8 changes: 4 additions & 4 deletions src/routers/handlers/personal-code/personalCodeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class PersonalCodeHandler extends GenericHandler<PersonalCodeViewData> {

const baseViewData = await super.getViewData(req, res);
const verification: PscVerification = res.locals.submission;
const selectedPscId = req.query?.selectedPscId as string;
const pscIndividual = await getPscIndividual(req, verification.data.companyNumber as string, selectedPscId);
const companyNumber = verification.data.companyNumber as string;
const pscIndividual = await getPscIndividual(req, companyNumber, verification.data.pscAppointmentId as string);
const lang = selectLang(req.query.lang);
const locales = getLocalesService();

Expand All @@ -37,13 +37,13 @@ export class PersonalCodeHandler extends GenericHandler<PersonalCodeViewData> {
monthBorn: formatDateBorn(pscIndividual.resource?.dateOfBirth, selectLang(req.query.lang)),
personalCode: verification?.data?.verificationDetails?.uvid || "",
currentUrl: resolveUrlTemplate(PrefixedUrls.PERSONAL_CODE),
backURL: resolveUrlTemplate(PrefixedUrls.INDIVIDUAL_PSC_LIST),
backURL: addSearchParams(PrefixedUrls.INDIVIDUAL_PSC_LIST, { companyNumber, lang }),
templateName: Urls.PERSONAL_CODE,
backLinkDataEvent: "personal-code-back-link"
};

function resolveUrlTemplate (prefixedUrl: string): string | null {
return addSearchParams(getUrlWithTransactionIdAndSubmissionId(prefixedUrl, req.params.transactionId, req.params.submissionId), { lang, selectedPscId });
return addSearchParams(getUrlWithTransactionIdAndSubmissionId(prefixedUrl, req.params.transactionId, req.params.submissionId), { lang });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/routers/handlers/psc-verified/pscVerifiedHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface PscVerifiedViewData extends BaseViewData {
companyNumber: string;
pscName: string;
companyLookupUrl: string;
createNewSubmissionUrl: string;
pscListUrl: string;
}

export class PscVerifiedHandler extends GenericHandler<PscVerifiedViewData> {
Expand Down Expand Up @@ -44,7 +44,7 @@ export class PscVerifiedHandler extends GenericHandler<PscVerifiedViewData> {
companyNumber: companyNumber,
pscName: pscDetailsResponse.resource?.name!,
referenceNumber: transactionId,
createNewSubmissionUrl: addSearchParams(PrefixedUrls.NEW_SUBMISSION, { companyNumber, lang }),
pscListUrl: addSearchParams(PrefixedUrls.INDIVIDUAL_PSC_LIST, { companyNumber, lang }),
companyLookupUrl: addSearchParams(ExternalUrls.COMPANY_LOOKUP, { forward }),
templateName: Urls.PSC_VERIFIED
};
Expand Down
7 changes: 1 addition & 6 deletions src/routers/individualPscListRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import { IndividualPscListHandler } from "./handlers/individual-psc-list/individ

const individualPscListRouter: Router = Router({ mergeParams: true });

individualPscListRouter.get("/", handleExceptions(async (req: Request, res: Response, _next: NextFunction) => {
individualPscListRouter.all("/", handleExceptions(async (req: Request, res: Response, _next: NextFunction) => {
const handler = new IndividualPscListHandler();
const { templatePath, viewData } = await handler.executeGet(req, res);
res.render(templatePath, viewData);
}));

individualPscListRouter.post("/", handleExceptions(async (req: Request, res: Response, _next: NextFunction) => {
const handler = new IndividualPscListHandler();
res.redirect(await handler.executePost(req, res));
}));

export default individualPscListRouter;
2 changes: 1 addition & 1 deletion src/views/router_views/pscVerified/pscVerified.njk
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<h1 class="govuk-heading-m">{{i18n.psc_verified_next_heading}}</h1>

<p class="govuk-body">
<a href="{{ createNewSubmissionUrl }} class="govuk-link" data-event-id="same-company-verification-link">{{ i18n.psc_verified_verification_details_link }} {{ companyName }}</a>.
<a href="{{ pscListUrl }} class="govuk-link" data-event-id="same-company-verification-link">{{ i18n.psc_verified_verification_details_link }} {{ companyName }}</a>.
</p>
<p class="govuk-body">
Expand Down
4 changes: 2 additions & 2 deletions test/routers/confirmCompany.int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ describe("ConfirmCompany router/handler integration tests", () => {

describe("POST method", () => {

it("Should redirect to the new submission router with a temporary redirect status code", async () => {
it("Should redirect to the psc-list router with a temporary redirect status code", async () => {
const lang = "en";
const expectedRedirectUrl = `/persons-with-significant-control-verification/new-submission?companyNumber=${COMPANY_NUMBER}&lang=${lang}`;
const expectedRedirectUrl = `/persons-with-significant-control-verification/individual/psc-list?companyNumber=${COMPANY_NUMBER}&lang=${lang}`;

await request(app)
.post(PrefixedUrls.CONFIRM_COMPANY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("confirm company handler tests", () => {
const response = httpMocks.createResponse();
const handler = new ConfirmCompanyHandler();
const redirectUrl = await handler.executePost(request, response);
expect(redirectUrl).toBe(`${PrefixedUrls.NEW_SUBMISSION}?companyNumber=${COMPANY_NUMBER}&lang=en`);
expect(redirectUrl).toBe(`${PrefixedUrls.INDIVIDUAL_PSC_LIST}?companyNumber=${COMPANY_NUMBER}&lang=en`);

});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { HttpStatusCode } from "axios";
import { COMPANY_NUMBER, INDIVIDUAL_VERIFICATION_CREATED, PSC_VERIFICATION_ID, TRANSACTION_ID } from "../../../mocks/pscVerification.mock";
import { COMPANY_NUMBER } from "../../../mocks/pscVerification.mock";
import { VALID_COMPANY_PSC_ITEMS } from "../../../mocks/companyPsc.mock";
import { getCompanyProfile } from "../../../../src/services/companyProfileService";
import { getPscVerification } from "../../../../src/services/pscVerificationService";
import { validCompanyProfile } from "../../../mocks/companyProfile.mock";
import { getCompanyIndividualPscList } from "../../../../src/services/companyPscService";
import * as httpMocks from "node-mocks-http";
import { Urls } from "../../../../src/constants";
import { IndividualPscListHandler } from "../../../../src/routers/handlers/individual-psc-list/individualPscListHandler";

jest.mock("../../../../src/services/pscVerificationService");
const mockGetPscVerification = getPscVerification as jest.Mock;
mockGetPscVerification.mockResolvedValueOnce({
httpStatusCode: HttpStatusCode.Ok,
resource: INDIVIDUAL_VERIFICATION_CREATED
});

jest.mock("../../../../src/services/companyProfileService");
const mockGetCompanyProfile = getCompanyProfile as jest.Mock;
mockGetCompanyProfile.mockResolvedValue(validCompanyProfile);
Expand All @@ -24,7 +15,7 @@ jest.mock("../../../../src/services/companyPscService");
const mockGetCompanyIndividualPscList = getCompanyIndividualPscList as jest.Mock;
mockGetCompanyIndividualPscList.mockResolvedValueOnce(VALID_COMPANY_PSC_ITEMS.filter(psc => /^individual/.test(psc.kind)));

describe("psc type handler", () => {
describe("psc list handler", () => {

afterEach(() => {
jest.clearAllMocks();
Expand All @@ -34,54 +25,24 @@ describe("psc type handler", () => {
const req = httpMocks.createRequest({
method: "GET",
url: Urls.INDIVIDUAL_PSC_LIST,
params: {
transactionId: TRANSACTION_ID,
submissionId: PSC_VERIFICATION_ID
},
query: {
pscType: "individual",
companyNumber: COMPANY_NUMBER,
lang: "en"
}
});

const res = httpMocks.createResponse({ locals: { submission: INDIVIDUAL_VERIFICATION_CREATED } });
const res = httpMocks.createResponse({});
const handler = new IndividualPscListHandler();

const { templatePath, viewData } = await handler.executeGet(req, res);

expect(templatePath).toBe("router_views/individualPscList/individualPscList");
expect(viewData).toMatchObject({
backURL: `/persons-with-significant-control-verification/transaction/${TRANSACTION_ID}/submission/${PSC_VERIFICATION_ID}/psc-type?lang=en&pscType=individual`,
currentUrl: `/persons-with-significant-control-verification/transaction/${TRANSACTION_ID}/submission/${PSC_VERIFICATION_ID}/individual/psc-list?lang=en&pscType=individual`
backURL: `/persons-with-significant-control-verification/confirm-company?companyNumber=${COMPANY_NUMBER}&lang=en`,
currentUrl: `/persons-with-significant-control-verification/individual/psc-list?companyNumber=${COMPANY_NUMBER}&lang=en`,
nextPageUrl: `/persons-with-significant-control-verification/new-submission?companyNumber=${COMPANY_NUMBER}&lang=en&selectedPscId=`
});

});
});
describe("executePost", () => {
it("should patch the submission data", async () => {
const req = httpMocks.createRequest({
method: "POST",
url: Urls.INDIVIDUAL_PSC_LIST,
params: {
transactionId: TRANSACTION_ID,
submissionId: PSC_VERIFICATION_ID
},
query: {
lang: "en",
companyNumber: COMPANY_NUMBER
},
body: {
pscType: "individual"
}
});

const res = httpMocks.createResponse();
const handler = new IndividualPscListHandler();

const redirectUrl = await handler.executePost(req, res);

expect(redirectUrl).toBe(`/persons-with-significant-control-verification/transaction/${TRANSACTION_ID}/submission/${PSC_VERIFICATION_ID}/individual/personal-code?lang=en`);
});
});
});
Loading

0 comments on commit d6b7dbf

Please sign in to comment.