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

Address bug issue #213

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ACSPData } from "../../../model/ACSPData";
import { Session } from "@companieshouse/node-session-handler";
import { CORRESPONDENCE_ADDRESS, USER_DATA } from "../../../common/__utils/constants";
import { saveDataInSession } from "../../../common/__utils/sessionHelper";
import { CorrespondenceAddressDetailsService } from "../../../services/correspondence-address/address-details";

export const get = async (req: Request, res: Response, next: NextFunction) => {
const session: Session = req.session as any as Session;
Expand Down Expand Up @@ -49,24 +50,10 @@ export const post = async (req: Request, res: Response, next: NextFunction) => {
addresses: acspData?.addresses
});
} else {
const addressList = acspData.addresses!;
const selectPremise = req.body.correspondenceAddress;
for (const ukAddress of addressList) {
if (ukAddress.propertyDetails!.toUpperCase() === selectPremise.toUpperCase()) {
// Save the correspondence address to session
const correspondenceAddress = {
propertyDetails: ukAddress.propertyDetails,
line1: ukAddress.line1,
line2: ukAddress.line2,
town: ukAddress.town,
country: ukAddress.country,
postcode: ukAddress.postcode
};
saveDataInSession(req, CORRESPONDENCE_ADDRESS, correspondenceAddress);

}

}
const correspondenceAddress = req.body.correspondenceAddress;
const addressDetailsService = new CorrespondenceAddressDetailsService();
addressDetailsService.saveCorrespondenceDetailsAddress(req, acspData, correspondenceAddress);
const nextPageUrl = addLangToUrl(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, lang);
res.redirect(nextPageUrl);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { NextFunction, Request, Response } from "express";
import { ValidationError, validationResult } from "express-validator";
import { FormattedValidationErrors, formatValidationError } from "../../../validation/validation";
Expand All @@ -11,6 +12,7 @@ import { ACSPData } from "../../../model/ACSPData";
import { Session } from "@companieshouse/node-session-handler";
import { USER_DATA } from "../../../common/__utils/constants";
import { saveDataInSession } from "../../../common/__utils/sessionHelper";
import CorrespondenceAddressAutoLookService from "../../../services/correspondence-address/address-autolookup";

export const get = async (req: Request, res: Response, next: NextFunction) => {
const session: Session = req.session as any as Session;
Expand Down Expand Up @@ -56,60 +58,13 @@ export const post = async (req: Request, res: Response, next: NextFunction) => {
const correspondencePremise = req.body.premise;
getAddressFromPostcode(postcode).then((ukAddresses) => {
if (correspondencePremise !== "" && ukAddresses.find((address) => address.premise === correspondencePremise)) {
let address = {
premise: "",
addressLine1: "",
addressLine2: "",
postTown: "",
postalCode: "",
country: ""
};
for (const ukAddress of ukAddresses) {
if (ukAddress.premise === correspondencePremise) {
address = {
premise: ukAddress.premise,
addressLine1: ukAddress.addressLine1,
addressLine2: ukAddress.addressLine2!,
postTown: ukAddress.postTown,
postalCode: ukAddress.postcode,
country: getCountryFromKey(ukAddress.country)
};
}
}
// Save the correspondence address to session
const correspondenceAddress : Address = {
propertyDetails: address.premise,
line1: address.addressLine1,
line2: address.addressLine2,
town: address.postTown,
country: address.country,
postcode: address.postalCode
};
const userAddresses : Array<Address> = acspData?.addresses ? acspData.addresses : [];
userAddresses.push(correspondenceAddress);
acspData.addresses = userAddresses;
saveDataInSession(req, USER_DATA, acspData);
res.redirect(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM);
const autoLookupSessionservice = new CorrespondenceAddressAutoLookService();
autoLookupSessionservice.saveCorrespondenceAddressToSession(acspData, req, ukAddresses, correspondencePremise);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saveCorrespondenceAddress is enough no need to add "ToSession" in the name

res.redirect(addLangToUrl(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, lang));

} else {

const addressList : Array<Address> = [];
for (const ukAddress of ukAddresses) {
const address = {
propertyDetails: ukAddress.premise,
line1: ukAddress.addressLine1,
line2: ukAddress.addressLine2,
town: ukAddress.postTown,
country: getCountryFromKey(ukAddress.country),
postcode: ukAddress.postcode,
formattedAddress: ukAddress.premise + ", " + ukAddress.addressLine1 + ", " + ukAddress.postTown + ", " + getCountryFromKey(ukAddress.country) + ", " + ukAddress.postcode
};

addressList.push(address);

}
acspData.addresses = addressList;
saveDataInSession(req, USER_DATA, acspData);
const autoLookupListservice = new CorrespondenceAddressAutoLookService();
autoLookupListservice.saveAddressListToSession(acspData, req, ukAddresses);
const nextPageUrl = addLangToUrl(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST, lang);
res.redirect(nextPageUrl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const get = async (req: Request, res: Response, next: NextFunction) => {
const lang = selectLang(req.query.lang);
const locales = getLocalesService();
const session: Session = req.session as any as Session;
const acspData : ACSPData = session?.getExtraData(USER_DATA)!;

const acspData : ACSPData = session?.getExtraData(USER_DATA)!;
const { firstName, lastName, addresses } = acspData;

res.render(config.SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, {
Expand All @@ -22,7 +22,7 @@ export const get = async (req: Request, res: Response, next: NextFunction) => {
currentUrl: BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM,
firstName,
lastName,
correspondenceAddress: addresses ? [0] : ""
correspondenceAddress: addresses ? addresses[0] : ""
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ACSPData } from "../../../model/ACSPData";
import { Session } from "@companieshouse/node-session-handler";
import { USER_DATA } from "../../../common/__utils/constants";
import { saveDataInSession } from "../../../common/__utils/sessionHelper";
import { CorrespondenceAddressManualService } from "../../../services/correspondence-address/address-manual";

export const get = async (req: Request, res: Response, next: NextFunction) => {
const lang = selectLang(req.query.lang);
Expand Down Expand Up @@ -46,21 +47,10 @@ export const post = async (req: Request, res: Response, next: NextFunction) => {
lastName: acspData?.lastName
});
} else {
// Save the correspondence address to session
const correspondenceAddress : Address = {
propertyDetails: req.body.addressPropertyDetails,
line1: req.body.addressLine1,
line2: req.body.addressLine2,
town: req.body.addressTown,
county: req.body.addressCounty,
country: req.body.addressCountry,
postcode: req.body.addressPostcode
};
const userAddress : Array<Address> = acspData?.addresses ? acspData.addresses : [];
userAddress.push(correspondenceAddress);
acspData.addresses = userAddress;
saveDataInSession(req, USER_DATA, acspData);
res.redirect(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM);
const AddressManualservice = new CorrespondenceAddressManualService();
AddressManualservice.saveCorrespondenceManualAddress(req, acspData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable name needs to follow camel casing

res.redirect(addLangToUrl(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, lang));

}
} catch (error) {
next(error);
Expand Down
75 changes: 75 additions & 0 deletions src/main/services/correspondence-address/address-autolookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { UKAddress } from "@companieshouse/api-sdk-node/dist/services/postcode-lookup/types";
import { Session } from "@companieshouse/node-session-handler";
import { Request } from "express";
import { ADDRESS_LIST, USER_DATA } from "../../common/__utils/constants";
import { saveDataInSession } from "../../common/__utils/sessionHelper";
import { ACSPData } from "../../model/ACSPData";
import { Address } from "../../model/Address";
import { getCountryFromKey } from "../../utils/web";

export class CorrespondenceAddressAutoLookService {
saveCorrespondenceAddressToSession (acspData :ACSPData, req: Request, ukAddresses: UKAddress[], inputPremise: string) {

sampankumar marked this conversation as resolved.
Show resolved Hide resolved
let address = {
premise: "",
propertyDetails: "",
line1: "",
line2: "",
town: "",
country: "",
postcode: ""
};
for (const ukAddress of ukAddresses) {
if (ukAddress.premise === inputPremise) {
address = {
propertyDetails: ukAddress.premise,
premise: ukAddress.premise,
line1: ukAddress.addressLine1,
line2: ukAddress.addressLine2!,
town: ukAddress.postTown,
country: getCountryFromKey(ukAddress.country),
postcode: ukAddress.postcode
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can break out of the loop once the match is found

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no difference between address and correspondenceAddress. Only one can be used

}

const correspondenceAddress: Address = {
propertyDetails: address.premise,
line1: address.line1,
line2: address.line2,
town: address.town,
country: address.country,
postcode: address.postcode
};
const userAddresses: Array<Address> = acspData?.addresses ? acspData.addresses : [];
userAddresses.push(correspondenceAddress);
acspData.addresses = userAddresses;
// saveDataInSession(req, USER_DATA, acspData);
return acspData;
}

saveAddressListToSession (acspData: ACSPData, req: Request, ukAddresses: UKAddress[]) {

const addressList: Array<Address> = [];
for (const ukAddress of ukAddresses) {
const address = {
propertyDetails: ukAddress.premise,
line1: ukAddress.addressLine1,
line2: ukAddress.addressLine2,
town: ukAddress.postTown,
country: getCountryFromKey(ukAddress.country),
postcode: ukAddress.postcode,
formattedAddress: `${ukAddress.premise}, ${ukAddress.addressLine1}, ${ukAddress.postTown}, ${getCountryFromKey(ukAddress.country)}, ${ukAddress.postcode}`
};

addressList.push(address);

}
// Save the list of addresses to the session
acspData.addresses = addressList;
// saveDataInSession(req, ADDRESS_LIST, addressList);
return acspData;
}
}

export default CorrespondenceAddressAutoLookService;
24 changes: 24 additions & 0 deletions src/main/services/correspondence-address/address-details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Request } from "express";
import { saveDataInSession } from "../../common/__utils/sessionHelper";
import { ACSPData } from "../../model/ACSPData";
import { CORRESPONDENCE_ADDRESS } from "../../common/__utils/constants";

export class CorrespondenceAddressDetailsService {
saveCorrespondenceDetailsAddress (req: Request, acspData: ACSPData, selectPremise: string): void {
const addressList = acspData.addresses!;
for (const ukAddress of addressList) {
if (ukAddress.propertyDetails!.toUpperCase() === selectPremise.toUpperCase()) {
const correspondenceAddress = {
propertyDetails: ukAddress.propertyDetails,
line1: ukAddress.line1,
line2: ukAddress.line2,
town: ukAddress.town,
country: ukAddress.country,
postcode: ukAddress.postcode
};
saveDataInSession(req, CORRESPONDENCE_ADDRESS, correspondenceAddress);
break;
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/services/correspondence-address/address-manual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Request } from "express";
import { Address } from "../../model/Address";
import { ACSPData } from "../../model/ACSPData";
import { saveDataInSession } from "../../common/__utils/sessionHelper";
import { USER_DATA } from "../../common/__utils/constants";

export class CorrespondenceAddressManualService {
saveCorrespondenceManualAddress (req: Request, acspData: ACSPData): void {
const correspondenceAddress: Address = {
propertyDetails: req.body.addressPropertyDetails,
line1: req.body.addressLine1,
line2: req.body.addressLine2,
town: req.body.addressTown,
county: req.body.addressCounty,
country: req.body.addressCountry,
postcode: req.body.addressPostcode
};

const userAddress: Array<Address> = acspData?.addresses ? acspData.addresses : [];
userAddress.push(correspondenceAddress);
acspData.addresses = userAddress;
saveDataInSession(req, USER_DATA, acspData);
}
}