diff --git a/src/core/request_handler.ts b/src/core/request_handler.ts index 5c807c4..f9b4d2b 100644 --- a/src/core/request_handler.ts +++ b/src/core/request_handler.ts @@ -345,7 +345,7 @@ export class RequestHandler { menu = this.router.getMenu(id); } else if (state.isStart) { // If it is a start request, lookup for the start menu - const value = this.router.getStartMenu(this.request, this.response); + const value = await this.router.getStartMenu(this.request, this.response); id = value.id; menu = value.obj; } diff --git a/src/menus/base.menu.ts b/src/menus/base.menu.ts index 713526b..7262318 100644 --- a/src/menus/base.menu.ts +++ b/src/menus/base.menu.ts @@ -7,7 +7,7 @@ export abstract class BaseMenu { constructor( protected readonly request: Request, protected readonly response: Response, - ) {} + ) { } async validate(data?: string): Promise { return true; @@ -30,7 +30,7 @@ export abstract class BaseMenu { return this.request.query?.sessionid!; } - get isStart(): Promise { + async isStart(): Promise { return Promise.resolve(false); } diff --git a/src/menus/index.ts b/src/menus/index.ts index bd17738..0f0c778 100644 --- a/src/menus/index.ts +++ b/src/menus/index.ts @@ -3,19 +3,19 @@ export { BaseMenu } from "./base.menu"; export { DynamicMenu } from "./dynamic_menu.menu"; export { ValidationResponse } from "@src/types"; -// TODO: Keep list of menus cached in a map, globally import { Request, Response } from "@src/types/request"; import { Type } from "@src/types"; import { BaseMenu } from "@src/menus/base.menu"; import { DynamicMenu } from "@src/menus"; import { MENU_CACHE } from "@src/core/state.core"; +import { menuType } from ".."; export class Menus { private static instance: Menus; // private items: { [menuId: string]: Type | DynamicMenu } = {}; - private constructor() {} + private constructor() { } public static getInstance(): Menus { if (!Menus.instance) { @@ -34,8 +34,6 @@ export class Menus { add(cls: Type, name: string): void { // const _menu = new cls(cls.name, cls); MENU_CACHE[name] = cls; - - // return _menu; } menu(id: string): DynamicMenu { @@ -49,25 +47,40 @@ export class Menus { return MENU_CACHE; } - getStartMenu( + async getStartMenu( req: Request, res: Response, - ): { id: string; obj: DynamicMenu | Type } { - const start = Object.keys(MENU_CACHE).find((id) => { + ): Promise<{ id: string; obj: DynamicMenu | Type; }> { + let startId: string | undefined; + + for (const id in MENU_CACHE) { + let isStart = false; const menu = MENU_CACHE[id]; - if (menu instanceof BaseMenu) { - // @ts-ignore - return new menu(req, res).isStart; + + if (menuType(menu) == "class") { + if (menu instanceof BaseMenu) { + isStart = await menu.isStart() + } else { + // @ts-ignore + isStart = (await new menu(req, res).isStart) + } + } else { + + isStart = (menu as DynamicMenu).isStart } - return (menu as DynamicMenu).isStart; - }); + if (isStart) { + startId = id; + break; + } + } - if (start == undefined) { + // console.log(start, MENU_CACHE) + if (startId == undefined) { throw new Error("No start menu defined. Please define a start menu"); } - return { id: start, obj: MENU_CACHE[start] }; + return { id: startId, obj: MENU_CACHE[startId] }; } getMenu(id: string): DynamicMenu | Type { diff --git a/thesis.code-workspace b/thesis.code-workspace index 65ee647..bdb1fac 100644 --- a/thesis.code-workspace +++ b/thesis.code-workspace @@ -4,7 +4,7 @@ "path": "." }, { - "path": "../instant-settlement" + "path": "../direct-debit-ussd" } ], "settings": {