Skip to content

Commit

Permalink
Merge pull request #105 from companieshouse/fix/lp-317-name-page-type
Browse files Browse the repository at this point in the history
Fix/lp 317 name page type
  • Loading branch information
lduranteau authored Dec 18, 2024
2 parents 6554c09 + a14ae76 commit 2cac58c
Show file tree
Hide file tree
Showing 20 changed files with 476 additions and 80 deletions.
28 changes: 28 additions & 0 deletions src/application/CacheService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Session } from "@companieshouse/node-session-handler";

import ICacheRepository from "../domain/ICacheRepository";

class CacheService {
cacheRepository: ICacheRepository;

constructor(cacheRepository: ICacheRepository) {
this.cacheRepository = cacheRepository;
}

getDataFromCache(session: Session): Promise<Record<string, any>> {
return this.cacheRepository.getData(session);
}

async addDataToCache(
session: Session,
data: Record<string, any>
): Promise<void> {
await this.cacheRepository.addData(session, data);
}

async removeDataFromCache(session: Session, key: string): Promise<void> {
await this.cacheRepository.deleteData(session, key);
}
}

export default CacheService;
6 changes: 5 additions & 1 deletion src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export const LOG_LEVEL = getEnvironmentValue("LOG_LEVEL");
export const NODE_ENV = process.env["NODE_ENV"];
export const OAUTH2_CLIENT_ID = getEnvironmentValue("OAUTH2_CLIENT_ID");
export const OAUTH2_CLIENT_SECRET = getEnvironmentValue("OAUTH2_CLIENT_SECRET");
export const PIWIK_REGISTRATION_START_GOAL_ID = getEnvironmentValue("PIWIK_REGISTRATION_START_GOAL_ID");
export const PIWIK_REGISTRATION_START_GOAL_ID = getEnvironmentValue(
"PIWIK_REGISTRATION_START_GOAL_ID"
);
export const PIWIK_SITE_ID = getEnvironmentValue("PIWIK_SITE_ID");
export const PIWIK_URL = getEnvironmentValue("PIWIK_URL");
export const PORT = getEnvironmentValue("PORT");
export const isLocalesEnabled = () =>
getEnvironmentValueAsBoolean("LOCALES_ENABLED");

export const SERVICE_NAME = "File for a limited partnership";
export const APPLICATION_CACHE_KEY = "limited_partnership";
export const APPLICATION_CACHE_KEY_PREFIX_REGISTRATION = "registration_";

// Templates
export const ERROR_TEMPLATE = "error-page";
Expand Down
10 changes: 8 additions & 2 deletions src/config/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import GlobalController from "../presentation/controller/global/Controller";
import RegistrationService from "../application/registration/Service";
import RegistrationGateway from "../infrastructure/gateway/RegistrationGateway";
import RegistrationGateway from "../infrastructure/gateway/registration/RegistrationGateway";
import CacheRepository from "../infrastructure/repository/CacheRepository";
import RegistrationController from "../presentation/controller/registration/Controller";
import CacheService from "../application/CacheService";

const globalController: GlobalController = new GlobalController();

const registrationGateway: RegistrationGateway = new RegistrationGateway();
const registrationService: RegistrationService = new RegistrationService(
registrationGateway
);

const cacheRepository = new CacheRepository();
const cacheService = new CacheService(cacheRepository);

const registrationController: RegistrationController =
new RegistrationController(registrationService);
new RegistrationController(registrationService, cacheService);

export const appDependencies = {
globalController,
Expand Down
12 changes: 10 additions & 2 deletions src/config/dev-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import GlobalController from "../presentation/controller/global/Controller";
import RegistrationService from "../application/registration/Service";
import RegistrationInMemoryGateway from "../infrastructure/gateway/RegistrationInMemoryGateway";
import RegistrationInMemoryGateway from "../infrastructure/gateway/registration/RegistrationInMemoryGateway";
import CacheInMemoryRepository from "../infrastructure/repository/CacheInMemoryRepository";
import RegistrationController from "../presentation/controller/registration/Controller";
import CacheService from "../application/CacheService";

const globalController: GlobalController = new GlobalController();

const registrationGateway: RegistrationInMemoryGateway =
new RegistrationInMemoryGateway();

const registrationService: RegistrationService = new RegistrationService(
registrationGateway
);

const cacheRepository = new CacheInMemoryRepository();
const cacheService = new CacheService(cacheRepository);

const registrationController: RegistrationController =
new RegistrationController(registrationService);
new RegistrationController(registrationService, cacheService);

export const appDevDependencies = {
globalController,
registrationGateway,
cacheRepository,
registrationService,
registrationController,
};
9 changes: 9 additions & 0 deletions src/domain/ICacheRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Session } from "@companieshouse/node-session-handler";

interface ICacheRepository {
getData(session: Session): Promise<Record<string, any>>;
addData(session: Session, data: Record<string, any>): Promise<void>;
deleteData(session: Session, key: string): Promise<void>;
}

export default ICacheRepository;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LimitedPartnership } from "@companieshouse/api-sdk-node/dist/services/limited-partnerships";

import TransactionLimitedPartnership from "../../domain/entities/TransactionLimitedPartnership";
import PageType from "../../presentation/controller/PageType";
import TransactionLimitedPartnership from "../../../domain/entities/TransactionLimitedPartnership";
import PageType from "../../../presentation/controller/PageType";

class LimitedPartnershipGatewayBuilder {
created_at?: Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { LimitedPartnership } from "@companieshouse/api-sdk-node/dist/services/l
import { Transaction } from "@companieshouse/api-sdk-node/dist/services/transaction/types";
import { createApiClient, Resource } from "@companieshouse/api-sdk-node";

import RegistrationPageType from "../../presentation/controller/registration/PageType";
import IRegistrationGateway from "../../domain/IRegistrationGateway";
import RegistrationPageType from "../../../presentation/controller/registration/PageType";
import IRegistrationGateway from "../../../domain/IRegistrationGateway";
import LimitedPartnershipGatewayBuilder from "./LimitedPartnershipGatewayBuilder";

class RegistrationGateway implements IRegistrationGateway {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
NameEndingType,
} from "@companieshouse/api-sdk-node/dist/services/limited-partnerships";

import RegistrationPageType from "../../presentation/controller/registration/PageType";
import IRegistrationGateway from "../../domain/IRegistrationGateway";
import CustomError from "../../domain/entities/CustomError";
import TransactionLimitedPartnership from "../../domain/entities/TransactionLimitedPartnership";
import RegistrationPageType from "../../../presentation/controller/registration/PageType";
import IRegistrationGateway from "../../../domain/IRegistrationGateway";
import CustomError from "../../../domain/entities/CustomError";
import TransactionLimitedPartnership from "../../../domain/entities/TransactionLimitedPartnership";
import LimitedPartnershipGatewayBuilder from "./LimitedPartnershipGatewayBuilder";

class RegistrationInMemoryGateway implements IRegistrationGateway {
Expand Down
51 changes: 51 additions & 0 deletions src/infrastructure/repository/CacheInMemoryRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable */

import { Session } from "@companieshouse/node-session-handler";

import { APPLICATION_CACHE_KEY } from "../../config/constants";
import ICacheRepository from "../../domain/ICacheRepository";

class CacheInMemoryRepository implements ICacheRepository {
cache: { [APPLICATION_CACHE_KEY]: Record<string, any> } | null = null;

feedCache(value: Record<string, any> | null) {
this.cache = value ? { [APPLICATION_CACHE_KEY]: value } : value;
}

async getData(session: Session): Promise<Record<string, any>> {
return this.cache?.[APPLICATION_CACHE_KEY] ?? {};
}

async addData(session: Session, data: Record<string, any>): Promise<void> {
if (this.cache?.[APPLICATION_CACHE_KEY]) {
this.cache = {
[APPLICATION_CACHE_KEY]: {
...this.cache?.[APPLICATION_CACHE_KEY],
...data,
},
};

return;
}

this.cache = {
[APPLICATION_CACHE_KEY]: {
...data,
},
};
}

async deleteData(session: Session, key: string): Promise<void> {
const data = await this.getData(session);

delete data[key];

this.cache = {
[APPLICATION_CACHE_KEY]: {
...data,
},
};
}
}

export default CacheInMemoryRepository;
30 changes: 30 additions & 0 deletions src/infrastructure/repository/CacheRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Session } from "@companieshouse/node-session-handler";
import ICacheRepository from "../../domain/ICacheRepository";
import { APPLICATION_CACHE_KEY } from "../../config/constants";

class CacheRepository implements ICacheRepository {
async getData(session: Session): Promise<Record<string, any>> {
return (await session?.getExtraData(APPLICATION_CACHE_KEY)) ?? {};
}

async addData(session: Session, data: Record<string, any>): Promise<void> {
const cache = await this.getData(session);

const updatedCache = {
...cache,
...data,
};

session?.setExtraData(APPLICATION_CACHE_KEY, updatedCache);
}

async deleteData(session: Session, key: string): Promise<void> {
const cache = await this.getData(session);

delete cache[key];

session?.setExtraData(APPLICATION_CACHE_KEY, cache);
}
}

export default CacheRepository;
2 changes: 2 additions & 0 deletions src/presentation/controller/PageRouting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ export const pageRoutingDefault = {
currentUrl: "",
nextUrl: "",
pageType: PageDefault.default,
data: null,
errors: null
};
49 changes: 39 additions & 10 deletions src/presentation/controller/registration/Controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { NextFunction, Request, RequestHandler, Response } from "express";
import escape from "escape-html";
import { Session } from "@companieshouse/node-session-handler";

import RegistrationService from "../../../application/registration/Service";
import registrationsRouting from "./Routing";
import AbstractController from "../AbstractController";
import PageType from "./PageType";
import { SUBMISSION_ID, TRANSACTION_ID } from "../../../config/constants";
import RegistrationPageType from "./PageType";
import {
APPLICATION_CACHE_KEY_PREFIX_REGISTRATION,
SUBMISSION_ID,
TRANSACTION_ID,
} from "../../../config/constants";
import CacheService from "../../../application/CacheService";

class RegistrationController extends AbstractController {
private registrationService: RegistrationService;
private cacheService: CacheService;

constructor(registrationService: RegistrationService) {
constructor(
registrationService: RegistrationService,
cacheService: CacheService
) {
super();
this.registrationService = registrationService;
this.cacheService = cacheService;
}

getPageRouting(): RequestHandler {
return (request: Request, response: Response, next: NextFunction) => {
return async (request: Request, response: Response, next: NextFunction) => {
try {
const session = request.session as Session;
const pageType = super.pageType(request.path);
const { transactionId, submissionId } = this.extractIds(request);

Expand All @@ -28,10 +40,15 @@ class RegistrationController extends AbstractController {
submissionId
);

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

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

response.render(super.templateName(pageRouting.currentUrl), {
props: { ...pageRouting, parameters },
props: { ...pageRouting },
});
} catch (error) {
next(error);
Expand All @@ -42,6 +59,7 @@ class RegistrationController extends AbstractController {
createTransactionAndFirstSubmission(): RequestHandler {
return async (request: Request, response: Response, next: NextFunction) => {
try {
const session = request.session as Session;
const access_token = this.extractAccessToken(request);
const pageType = this.extractPageTypeOrThrowError(request);

Expand Down Expand Up @@ -70,16 +88,22 @@ class RegistrationController extends AbstractController {
result.submissionId
);

await this.cacheService.removeDataFromCache(
session,
`${APPLICATION_CACHE_KEY_PREFIX_REGISTRATION}${RegistrationPageType.whichType}`
);

response.redirect(url);
} catch (error) {
next(error);
}
};
}

redirectWithParameter(): RequestHandler {
return (request: Request, response: Response, next: NextFunction) => {
redirectAndCacheSelection(): RequestHandler {
return async (request: Request, response: Response, next: NextFunction) => {
try {
const session = request.session as Session;
const type = this.extractPageTypeOrThrowError(request);

const registrationRouting = super.getRouting(
Expand All @@ -92,7 +116,12 @@ class RegistrationController extends AbstractController {
const pageType = escape(request.body.pageType);
const parameter = escape(request.body.parameter);

response.redirect(`${registrationRouting.nextUrl}?${pageType}=${parameter}`);
await this.cacheService.addDataToCache(session, {
[`${APPLICATION_CACHE_KEY_PREFIX_REGISTRATION}${pageType}`]:
parameter,
});

response.redirect(registrationRouting.nextUrl);
} catch (error) {
next(error);
}
Expand Down Expand Up @@ -136,7 +165,7 @@ class RegistrationController extends AbstractController {
}

private extractPageTypeOrThrowError(request) {
const pageTypeList = Object.values(PageType);
const pageTypeList = Object.values(RegistrationPageType);
const pageType = request.body.pageType;

if (!pageTypeList.includes(pageType)) {
Expand Down
Loading

0 comments on commit 2cac58c

Please sign in to comment.