Skip to content

Commit

Permalink
Merge pull request #525 from companieshouse/IDVA5-1413-Start-New-Tran…
Browse files Browse the repository at this point in the history
…saction-Change-TOB

Started new transaction when user changes type of business
  • Loading branch information
ttingle-ch authored Oct 31, 2024
2 parents 136c3d7 + d6665f2 commit a12b988
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 185 deletions.
26 changes: 16 additions & 10 deletions src/controllers/features/common/otherTypeOfBusinessController.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { AcspData } from "@companieshouse/api-sdk-node/dist/services/acsp";
import { Session } from "@companieshouse/node-session-handler";
import { NextFunction, Request, Response } from "express";
import { validationResult } from "express-validator";
import * as config from "../../../config";
import { formatValidationError, getPageProperties } from "../../../validation/validation";
import { selectLang, addLangToUrl, getLocalesService, getLocaleInfo } from "../../../utils/localise";
import { TYPE_OF_BUSINESS, OTHER_TYPE_OF_BUSINESS, UNINCORPORATED_NAME_REGISTERED_WITH_AML, BASE_URL, LIMITED_WHAT_IS_THE_COMPANY_NUMBER } from "../../../types/pageURL";
import { GET_ACSP_REGISTRATION_DETAILS_ERROR, POST_ACSP_REGISTRATION_DETAILS_ERROR, SUBMISSION_ID, USER_DATA } from "../../../common/__utils/constants";
import { saveDataInSession } from "../../../common/__utils/sessionHelper";
import { USER_DATA, SUBMISSION_ID, GET_ACSP_REGISTRATION_DETAILS_ERROR, POST_ACSP_REGISTRATION_DETAILS_ERROR } from "../../../common/__utils/constants";
import { Session } from "@companieshouse/node-session-handler";
import * as config from "../../../config";
import { AcspDataService } from "../../../services/acspDataService";
import { getAcspRegistration } from "../../../services/acspRegistrationService";
import logger from "../../../utils/logger";
import { ErrorService } from "../../../services/errorService";
import { AcspDataService } from "../../../services/acspDataService";
import { AcspData } from "@companieshouse/api-sdk-node/dist/services/acsp";
import { BASE_URL, LIMITED_WHAT_IS_THE_COMPANY_NUMBER, OTHER_TYPE_OF_BUSINESS, TYPE_OF_BUSINESS, UNINCORPORATED_NAME_REGISTERED_WITH_AML } from "../../../types/pageURL";
import { addLangToUrl, getLocaleInfo, getLocalesService, selectLang } from "../../../utils/localise";
import logger from "../../../utils/logger";
import { formatValidationError, getPageProperties } from "../../../validation/validation";

export const get = async (req: Request, res: Response, next: NextFunction) => {
const lang = selectLang(req.query.lang);
Expand Down Expand Up @@ -65,7 +65,13 @@ export const post = async (req: Request, res: Response, next: NextFunction) => {
});
} else {
const acspDataService = new AcspDataService();
await acspDataService.saveAcspData(session, acspData, selectedOption);

if (acspData != null && acspData.typeOfBusiness !== selectedOption) {
await acspDataService.createNewApplication(session, selectedOption);
} else {
await acspDataService.saveAcspData(session, acspData, selectedOption);
}

saveDataInSession(req, "resume_application", true);

if (selectedOption === "CORPORATE_BODY") {
Expand Down
19 changes: 6 additions & 13 deletions src/controllers/features/common/typeOfBusinessController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as config from "../../../config";
import { formatValidationError, getPageProperties } from "../../../validation/validation";
import { selectLang, addLangToUrl, getLocalesService, getLocaleInfo } from "../../../utils/localise";
import { TYPE_OF_BUSINESS, OTHER_TYPE_OF_BUSINESS, LIMITED_BUSINESS_MUSTBE_AML_REGISTERED_KICKOUT, SOLE_TRADER_WHAT_IS_YOUR_ROLE, BASE_URL, LIMITED_WHAT_IS_THE_COMPANY_NUMBER, UNINCORPORATED_NAME_REGISTERED_WITH_AML } from "../../../types/pageURL";
import { TypeOfBusinessService } from "../../../services/typeOfBusinessService";
import { SUBMISSION_ID, POST_ACSP_REGISTRATION_DETAILS_ERROR, GET_ACSP_REGISTRATION_DETAILS_ERROR, USER_DATA } from "../../../common/__utils/constants";
import logger from "../../../utils/logger";
import { Session } from "@companieshouse/node-session-handler";
Expand All @@ -23,16 +22,7 @@ export const get = async (req: Request, res: Response, next: NextFunction) => {
const session: Session = req.session as any as Session;
const previousPage: string = addLangToUrl(BASE_URL, lang);
const currentUrl: string = BASE_URL + TYPE_OF_BUSINESS;
const typeOfBusinessService = new TypeOfBusinessService();
const existingTransactionId = session?.getExtraData(SUBMISSION_ID);
try {
// create transaction record
if (existingTransactionId === undefined || JSON.stringify(existingTransactionId) === "{}") {
await typeOfBusinessService.createTransaction(req, res).then((transactionId) => {
// get transaction record data
saveDataInSession(req, SUBMISSION_ID, transactionId);
});
}

let typeOfBusiness = "";
if (session?.getExtraData("resume_application")) {
Expand Down Expand Up @@ -89,9 +79,13 @@ export const post = async (req: Request, res: Response, next: NextFunction) => {
} else if (selectedOption !== "OTHER") {

const acspDataService = new AcspDataService();
await acspDataService.saveAcspData(session, acspData, selectedOption);
saveDataInSession(req, "resume_application", true);

if (acspData != null && acspData.typeOfBusiness !== selectedOption) {
await acspDataService.createNewApplication(session, selectedOption);
} else {
await acspDataService.saveAcspData(session, acspData, selectedOption);
}
saveDataInSession(req, "resume_application", true);
switch (selectedOption) {
case "LC":
case "LLP":
Expand All @@ -105,7 +99,6 @@ export const post = async (req: Request, res: Response, next: NextFunction) => {
res.redirect(addLangToUrl(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_ROLE, lang));
break;
}

} else {
res.redirect(addLangToUrl(BASE_URL + OTHER_TYPE_OF_BUSINESS, lang));
}
Expand Down
49 changes: 26 additions & 23 deletions src/services/acspDataService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { AcspData, AcspDataDto } from "@companieshouse/api-sdk-node/dist/services/acsp";
import { Session } from "@companieshouse/node-session-handler";
import { APPLICATION_ID, SUBMISSION_ID, USER_DATA } from "../common/__utils/constants";
import { postAcspRegistration, putAcspRegistration } from "./acspRegistrationService";
import { APPLICATION_ID, RESUME_APPLICATION_ID, SUBMISSION_ID, USER_DATA } from "../common/__utils/constants";
import { deleteAcspApplication, postAcspRegistration, putAcspRegistration } from "./acspRegistrationService";
import logger from "../utils/logger";
import { TypeOfBusinessService } from "./typeOfBusinessService";

export class AcspDataService {
async saveAcspData (session: Session, acspData: AcspData, selectedOption?: string): Promise<void> {
try {
// create transaction record if one does not already exist
const existingTransactionId = session?.getExtraData(SUBMISSION_ID);
if (existingTransactionId === undefined || JSON.stringify(existingTransactionId) === "{}") {
const typeOfBusinessService = new TypeOfBusinessService();
await typeOfBusinessService.createTransaction(session).then((transactionId) => {
// get transaction record data
session.setExtraData(SUBMISSION_ID, transactionId);
});
}
if (acspData === undefined) {
acspData = {
typeOfBusiness: selectedOption
Expand All @@ -16,9 +26,6 @@ export class AcspDataService {
const response = resp as AcspDataDto;
session.setExtraData(APPLICATION_ID, response.id);
} else {
if (selectedOption !== undefined) {
acspData = this.typeOfBusinessChange(session, acspData, selectedOption);
}
// save data to mongodb
await putAcspRegistration(session, session.getExtraData(SUBMISSION_ID)!, acspData);
}
Expand All @@ -28,25 +35,21 @@ export class AcspDataService {
}
}

typeOfBusinessChange (session:Session, acspData: AcspData, selectedOption: string): AcspData {
if (acspData.typeOfBusiness !== selectedOption) {
async createNewApplication (session: Session, selectedOption: string): Promise<void> {
try {
// delete the old application
await deleteAcspApplication(session, session.getExtraData(SUBMISSION_ID)!, session.getExtraData(RESUME_APPLICATION_ID)!);
session.deleteExtraData(USER_DATA);
const clearedAcspData: AcspData = {
...acspData,
typeOfBusiness: selectedOption,
registeredOfficeAddress: undefined,
roleType: undefined,
verified: undefined,
businessName: undefined,
workSector: undefined,
amlSupervisoryBodies: undefined,
companyDetails: undefined,
companyAuthCodeProvided: undefined,
howAreYouRegisteredWithAml: undefined,
applicantDetails: undefined
};
acspData = clearedAcspData;
session.deleteExtraData(SUBMISSION_ID);
session.deleteExtraData(RESUME_APPLICATION_ID);
session.deleteExtraData(APPLICATION_ID);

// Save the data to MongoDB
const acspData = session.getExtraData(USER_DATA)!;
await this.saveAcspData(session, acspData, selectedOption);
} catch (error) {
logger.error("Error creating new application: " + error);
return Promise.reject(error);
}
return acspData;
}
}
18 changes: 9 additions & 9 deletions src/services/acspRegistrationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import { HttpResponse } from "@companieshouse/api-sdk-node/dist/http";
* GET an acsp registration object with the given transaction ID and acspApplicationId.
* @param session The current session to connect to the api
* @param transactionId The acsp registration associated transaction ID
* @param emailId The Id set on the acsp registration object
* @param applicationId: The ID set on the acsp registration object
* @returns The acsp registration details
*/

export const getAcspRegistration = async (session: Session, transactionId:string, acspApplicationId: string): Promise<AcspData> => {
const apiClient: ApiClient = createPublicOAuthApiClient(session);

logger.debug(`Retrieving acsp registration details for emailId ${acspApplicationId}`);
logger.debug(`Retrieving acsp registration details for application ID: ${acspApplicationId}`);
const sdkResponse: Resource<AcspData> | ApiErrorResponse = await apiClient.acsp.getAcsp(transactionId, acspApplicationId);

if (!sdkResponse) {
logger.error(`acsp registration GET request returned no response for emailId ${acspApplicationId}`);
logger.error(`acsp registration GET request returned no response for application ID: ${acspApplicationId}`);
return Promise.reject(sdkResponse);
}
if (!sdkResponse.httpStatusCode || sdkResponse.httpStatusCode >= 400) {
logger.error(`Http status code ${sdkResponse.httpStatusCode} and error ${JSON.stringify(sdkResponse)} - Failed to get acsp registration for emailId ${acspApplicationId}`);
logger.error(`Http status code ${sdkResponse.httpStatusCode} and error ${JSON.stringify(sdkResponse)} - Failed to get acsp registration for application ID: ${acspApplicationId}`);
return Promise.reject(sdkResponse);
}

const castedSdkResponse: Resource<AcspData> = sdkResponse as Resource<AcspData>;
if (castedSdkResponse.resource === undefined) {
logger.error(`acsp registration API GET request returned no resource for emailId ${acspApplicationId}`);
logger.error(`acsp registration API GET request returned no resource for application ID: ${acspApplicationId}`);
return Promise.reject(sdkResponse);
}

Expand Down Expand Up @@ -117,18 +117,18 @@ export const putAcspRegistration = async (session: Session, transactionId: strin
export const deleteAcspApplication = async (session: Session, transactionId: string, acspApplicationId: string): Promise<HttpResponse> => {
const apiClient: ApiClient = createPublicOAuthApiClient(session);

logger.debug(`Deleting acsp registration for user ${acspApplicationId}`);
logger.debug(`Deleting acsp registration for application ID: ${acspApplicationId}`);
const sdkResponse: HttpResponse = await apiClient.acsp.deleteSavedApplication(transactionId, acspApplicationId);

if (!sdkResponse) {
logger.error(`acsp registration DELETE request returned no response for user ${acspApplicationId}`);
logger.error(`acsp registration DELETE request returned no response for application ID: ${acspApplicationId}`);
return Promise.reject(sdkResponse);
}
if (!sdkResponse.status || sdkResponse.status >= 400) {
logger.error(`Http status code ${sdkResponse.status} - Failed to DELETE acsp registration for user ${acspApplicationId}`);
logger.error(`Http status code ${sdkResponse.status} - Failed to DELETE acsp registration for application ID: ${acspApplicationId}`);
return Promise.reject(sdkResponse);
}

logger.debug(`acsp registration for user ${acspApplicationId} has been deleted`);
logger.debug(`acsp registration for application ID: ${acspApplicationId} has been deleted`);
return Promise.resolve(sdkResponse);
};
3 changes: 1 addition & 2 deletions src/services/typeOfBusinessService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import logger from "../utils/logger";

export class TypeOfBusinessService {

async createTransaction (req: Request, res: Response): Promise<string> {
const session = req.session as Session;
async createTransaction (session: Session): Promise<string> {
let transactionId: string = "";
try {
await postTransaction(session, CREATE_DESCRIPTION, REFERENCE).then((transaction) => {
Expand Down
Loading

0 comments on commit a12b988

Please sign in to comment.