Skip to content

Commit

Permalink
Merge pull request #113 from companieshouse/feature/lp-321-get-data
Browse files Browse the repository at this point in the history
Feature/lp 321 get data
  • Loading branch information
lduranteau authored Dec 24, 2024
2 parents 6b175d0 + 4795280 commit b28b1ed
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 32 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"license": "MIT",
"homepage": "https://github.com/companieshouse/limited-partnerships-web#readme",
"dependencies": {
"@companieshouse/api-sdk-node": "^2.0.218",
"@companieshouse/api-sdk-node": "^2.0.219",
"@companieshouse/ch-node-utils": "^1.3.13",
"@companieshouse/node-session-handler": "^5.0.1",
"@companieshouse/structured-logging-node": "^2.0.1",
Expand Down
18 changes: 16 additions & 2 deletions src/application/registration/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@ class RegistrationService {
this.registrationGateway = registrationGateway;
}

getSubmissionById(id: string): Promise<LimitedPartnership> {
return this.registrationGateway.getSubmissionById(id);
async getLimitedPartnership(
opt: { access_token: string; refresh_token: string },
transactionId: string,
submissionId: string
): Promise<LimitedPartnership> {
try {
return await this.registrationGateway.getLimitedPartnership(
opt,
transactionId,
submissionId
);
} catch (error: any) {
logger.error(`Error getting LimitedPartnership ${JSON.stringify(error)}`);

throw error;
}
}

async createTransactionAndFirstSubmission(
Expand Down
6 changes: 5 additions & 1 deletion src/domain/IRegistrationGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ interface IRegistrationGateway {
transactionId: string,
data: Record<string, any>
): Promise<string>;
getSubmissionById(id: string): Promise<LimitedPartnership>;
getLimitedPartnership(
opt: { access_token: string; refresh_token: string },
transactionId: string,
submissionId: string
): Promise<LimitedPartnership>;
sendPageData(
opt: { access_token: string },
transactionId: string,
Expand Down
22 changes: 20 additions & 2 deletions src/infrastructure/gateway/registration/RegistrationGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,26 @@ class RegistrationGateway implements IRegistrationGateway {
}
}

async getSubmissionById(id: string): Promise<LimitedPartnership> {
throw new Error("Method not implemented.");
async getLimitedPartnership(
opt: { access_token: string; refresh_token: string },
transactionId: string,
submissionId: string
): Promise<LimitedPartnership> {
const apiCall = {
service: "limitedPartnershipsService",
method: "getLimitedPartnership",
args: [transactionId, submissionId],
};

const response = await makeApiCallWithRetry<
Resource<LimitedPartnership> | ApiErrorResponse
>(opt, apiCall);

if (response.httpStatusCode !== 200) {
throw response;
}

return (response as Resource<LimitedPartnership>)?.resource ?? {};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,29 @@ class RegistrationInMemoryGateway implements IRegistrationGateway {
this.limitedPartnerships[index] = limitedPartnershipBuilder.build();
}

async getSubmissionById(id: string): Promise<LimitedPartnership> {
async getLimitedPartnership(
opt: { access_token: string; refresh_token: string },
transactionId: string,
submissionId: string
): Promise<LimitedPartnership> {
if (
transactionId === ":transactionId" &&
submissionId === ":submissionId"
) {
return new LimitedPartnershipGatewayBuilder(
this.limitedPartnerships[0]
).build();
}

const limitedPartnerShip = this.limitedPartnerships.find(
(lp) => lp._id === id
(lp) => lp._id === submissionId
);

if (!limitedPartnerShip) {
throw new CustomError("Limited partnership", `Not found: ${id}`);
throw new CustomError(
"Limited partnership",
`Not found: ${submissionId}`
);
}

return new LimitedPartnershipGatewayBuilder(limitedPartnerShip).build();
Expand Down
12 changes: 12 additions & 0 deletions src/presentation/controller/registration/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class RegistrationController extends AbstractController {
return async (request: Request, response: Response, next: NextFunction) => {
try {
const session = request.session as Session;
const tokens = this.extractTokens(request);
const pageType = super.pageType(request.path);
const { transactionId, submissionId } = this.extractIds(request);

Expand All @@ -41,10 +42,21 @@ class RegistrationController extends AbstractController {
submissionId
);

let limitedPartnership = {};
if (transactionId && submissionId) {
limitedPartnership =
await this.registrationService.getLimitedPartnership(
tokens,
transactionId,
submissionId
);
}

const cache = await this.cacheService.getDataFromCache(session);

pageRouting.data = {
...pageRouting.data,
limitedPartnership,
cache,
};

Expand Down
15 changes: 7 additions & 8 deletions src/presentation/controller/registration/Routing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { PageRouting, PagesRouting } from "../PageRouting";
import {
BASE_URL,
BASE_WITH_IDS_URL,
} from "../../../config/constants";
import { BASE_URL, BASE_WITH_IDS_URL } from "../../../config/constants";

import RegistrationPageType from "./PageType";
import PageType from "../PageType";
Expand All @@ -13,14 +10,17 @@ export const NAME_TEMPLATE = RegistrationPageType.name;
export const EMAIL_TEMPLATE = RegistrationPageType.email;
export const GENERAL_PARTNERS_TEMPLATE = RegistrationPageType.generalPartners;
export const LIMITED_PARTNERS_TEMPLATE = RegistrationPageType.limitedPartners;
export const GENERAL_PARTNER_CHOICE_TEMPLATE = RegistrationPageType.generalPartnerChoice;
export const LIMITED_PARTNER_CHOICE_TEMPLATE = RegistrationPageType.limitedPartnerChoice;
export const GENERAL_PARTNER_CHOICE_TEMPLATE =
RegistrationPageType.generalPartnerChoice;
export const LIMITED_PARTNER_CHOICE_TEMPLATE =
RegistrationPageType.limitedPartnerChoice;
export const NEXT_TEMPLATE = RegistrationPageType.next;

// URLs
const START_URL = `${BASE_URL}/start`;
export const WHICH_TYPE_URL = `${BASE_URL}/${WHICH_TYPE_TEMPLATE}`;
export const NAME_URL = `${BASE_URL}/${NAME_TEMPLATE}`;
export const NAME_WITH_IDS_URL = `${BASE_WITH_IDS_URL}/${NAME_TEMPLATE}`;
export const EMAIL_URL = `${BASE_WITH_IDS_URL}/${EMAIL_TEMPLATE}`;
export const GENERAL_PARTNERS_URL = `${BASE_WITH_IDS_URL}/${GENERAL_PARTNERS_TEMPLATE}`;
export const GENERAL_PARTNER_CHOICE_URL = `${BASE_WITH_IDS_URL}/${GENERAL_PARTNER_CHOICE_TEMPLATE}`;
Expand All @@ -44,7 +44,7 @@ const registrationRoutingName = {
};

const registrationRoutingEmail = {
previousUrl: NAME_URL,
previousUrl: NAME_WITH_IDS_URL,
currentUrl: EMAIL_URL,
nextUrl: GENERAL_PARTNERS_URL,
pageType: RegistrationPageType.email,
Expand Down Expand Up @@ -94,7 +94,6 @@ const list = [
registrationRoutingGeneralPartnerChoice,
registrationRoutingLimitedPartnerChoice,
registrationRoutingNext,

];

export const registrationsRouting: PagesRouting = new Map<
Expand Down
17 changes: 16 additions & 1 deletion src/presentation/test/builder/LimitedPartnershipBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { NameEndingType } from "@companieshouse/api-sdk-node/dist/services/limited-partnerships";
import {
NameEndingType,
PartnershipType,
} from "@companieshouse/api-sdk-node/dist/services/limited-partnerships";
import TransactionLimitedPartnership from "../../../domain/entities/TransactionLimitedPartnership";

class LimitedPartnershipBuilder {
_id: string = "123456";
data = {
partnership_name: "partnership_name test",
name_ending: NameEndingType.LIMITED_PARTNERSHIP,
partnership_type: PartnershipType.LP,
email: "test@email.com",
};

withId(id: string) {
Expand All @@ -23,6 +28,16 @@ class LimitedPartnershipBuilder {
return this;
}

withPartnershipType(partnershipType: PartnershipType) {
this.data.partnership_type = partnershipType;
return this;
}

withEmail(email: string) {
this.data.email = email;
return this;
}

build(): TransactionLimitedPartnership {
return {
_id: this["_id"],
Expand Down
15 changes: 15 additions & 0 deletions src/presentation/test/integration/registration/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import LimitedPartnershipBuilder from "../../builder/LimitedPartnershipBuilder";
describe("Email Page", () => {
beforeEach(() => {
setLocalesEnabled(false);

appDevDependencies.registrationGateway.feedLimitedPartnerships([]);
});

const setLocalesEnabled = (bool: boolean) => {
Expand Down Expand Up @@ -46,6 +48,19 @@ describe("Email Page", () => {
expect(res.text).toContain(cyTranslationText.emailPage.emailHint);
expect(res.text).toContain(cyTranslationText.buttons.saveAndContinue);
});

it("should load the name page with data from api", async () => {
const limitedPartnership = new LimitedPartnershipBuilder().build();

appDevDependencies.registrationGateway.feedLimitedPartnerships([
limitedPartnership,
]);

const res = await request(app).get(EMAIL_URL);

expect(res.status).toBe(200);
expect(res.text).toContain(limitedPartnership?.data?.email);
});
});

describe("Post email", () => {
Expand Down
56 changes: 51 additions & 5 deletions src/presentation/test/integration/registration/gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { EMAIL_URL, NAME_URL } from "../../../controller/registration/Routing";
import RegistrationPageType from "../../../controller/registration/PageType";
import enTranslationText from "../../../../../locales/en/translations.json";
import { RefreshTokenService } from "@companieshouse/api-sdk-node/dist/services/refresh-token";
import LimitedPartnershipBuilder from "../../builder/LimitedPartnershipBuilder";

jest.mock("@companieshouse/api-sdk-node");

Expand All @@ -37,6 +38,10 @@ const value = {
httpStatusCode: 200,
resource: {},
}),
getLimitedPartnership: () => ({
httpStatusCode: 200,
resource: new LimitedPartnershipBuilder().build(),
}),
},
refreshToken: {
...RefreshTokenService.prototype,
Expand All @@ -51,12 +56,14 @@ const mockCreateApiClient = createApiClient as jest.Mock;
mockCreateApiClient.mockReturnValue(value);

describe("Gateway", () => {
describe("Create transaction and the first submission", () => {
beforeAll(() => {
appDevDependencies.registrationGateway.feedLimitedPartnerships([]);
appDevDependencies.registrationGateway.feedErrors([]);
});
beforeEach(() => {
appDevDependencies.registrationGateway.feedLimitedPartnerships([]);
appDevDependencies.registrationGateway.feedErrors([]);

mockCreateApiClient.mockReturnValue(value);
});

describe("Create transaction and the first submission", () => {
it("should create a transaction and the first submission", async () => {
const url = appDevDependencies.registrationController.insertIdsInUrl(
NAME_URL,
Expand Down Expand Up @@ -167,4 +174,43 @@ describe("Gateway", () => {
});
});
});

describe("Get Limited Parnership", () => {
it("should load the name page with data from api", async () => {
const url = appDevDependencies.registrationController.insertIdsInUrl(
EMAIL_URL,
appDevDependencies.registrationGateway.transactionId,
appDevDependencies.registrationGateway.submissionId
);

const res = await request(appRealDependencies).get(url);

expect(res.status).toBe(200);
expect(res.text).toContain(enTranslationText.emailPage.whatIsEmail);
});

it("should load error page if submissionId is incorrect", async () => {
mockCreateApiClient.mockReturnValue({
...value,
limitedPartnershipsService: {
...value.limitedPartnershipsService,
getLimitedPartnership: () => ({
httpStatusCode: 404,
errors: ["Not Found"],
}),
},
});

const url = appDevDependencies.registrationController.insertIdsInUrl(
EMAIL_URL,
appDevDependencies.registrationGateway.transactionId,
"wrong-id"
);

const res = await request(appRealDependencies).get(url);

expect(res.status).toBe(500);
expect(res.text).toContain(enTranslationText.errorPage.title);
});
});
});
Loading

0 comments on commit b28b1ed

Please sign in to comment.