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

Update routes to be more testable #161

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ EMAIL_TEMPLATE_PASSWORDRESET_RESETLINK=http://localhost:3000/auth/password-reset
EMAIL_TEMPLATE_VERIFYUSER_VERIFYLINK=http://localhost:3000/auth/verify?token={token}

DB_HOST=
DB_ROOT_HOST=
DB_PORT=
DB_NAME=
DB_AUTH_USER=
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ yarn-error.log
.DS_Store
secret.txt

.docker
.terraform/
*.tfvars
*.tfstate
22 changes: 11 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import AppDataSource from "./database/dataSources/appDataSource";

import { AuthRouter } from "./routes/auth";
import { DashboardRouter } from "./routes/dashboard";
import { IndexRouter } from "./routes";
import SettingsRouter from "./routes/settingsRouter";
import ItemsRouter from "./routes/itemsRouter";
import ItemPurchasesRouter from "./routes/itemPurchasesRouter";
import SuppliesRouters from "./routes/suppliesRouter";
import SupplyPurchasesRouter from "./routes/SupplyPurchasesRouter";
import ListingsRouter from "./routes/listingsRouter";
import OrdersRouter from "./routes/ordersRouter";
import StorageRouter from "./routes/storageRouter";
import ReturnsRouter from "./routes/ReturnsRouter";
import PostagePolicyRouter from "./routes/postagePolicyRouter";
import { IndexRouter } from "./routes/index";
Vylpes marked this conversation as resolved.
Show resolved Hide resolved
import SettingsRouter from "./routes/settings";
import ItemsRouter from "./routes/items";
import ItemPurchasesRouter from "./routes/itemPurchases";
import SuppliesRouters from "./routes/supplies";
import SupplyPurchasesRouter from "./routes/supplyPurchases";
import ListingsRouter from "./routes/listings";
import OrdersRouter from "./routes/orders";
import StorageRouter from "./routes/storage";
import ReturnsRouter from "./routes/returns";
import PostagePolicyRouter from "./routes/postagePolicy";

export class App {
private _app: Express;
Expand Down
31 changes: 6 additions & 25 deletions src/contracts/Page.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import { Router } from "express";
import { NextFunction, Request, Response } from "express";

interface IPage {
Route(): any;
OnGet(): any;
OnPost(): any;
}
export default interface Page {
OnGet?(req: Request, res: Response, next: NextFunction): void;
OnPost?(req: Request, res: Response, next: NextFunction): void;

export class Page implements IPage {
private _router: Router;

constructor(router: Router) {
this._router = router;
}

get router() {
return this._router;
}

Route() {
this.OnGet();
this.OnPost();
}

OnGet() {}

OnPost() {}
OnGetAsync?(req: Request, res: Response, next: NextFunction): Promise<void>;
OnPostAsync?(req: Request, res: Response, next: NextFunction): Promise<void>;
}
66 changes: 58 additions & 8 deletions src/contracts/Route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,71 @@
import { Router } from "express"
import { NextFunction, Router, Request, Response, response } from "express"
import Page from "./Page";
import { UserMiddleware } from "../middleware/userMiddleware";

interface IRoute {
Route(): Router;
}

export class Route implements IRoute {
export class Route {
private _router: Router;
private _pages: { path: string, page: Page, authorise: boolean, adminAuthorise: boolean }[];
private _subRoutes: { path: string, route: Route }[];

private _authorise: boolean;
private _adminAuthorise: boolean;

constructor() {
constructor(authorise: boolean = false, adminAuthorise: boolean = false) {
this._router = Router();
this._pages = [];
this._subRoutes = [];

this._authorise = authorise;
this._adminAuthorise = adminAuthorise;
}

get router() {
return this._router;
}

public AddPage(path: string, page: Page, authorise: boolean = false, adminAuthorise: boolean = false) {
this._pages.push({ path, page, authorise, adminAuthorise});
}

public AddSubRoute(path: string, route: Route) {
this._subRoutes.push({ path, route });
}

Route(): Router {
for (let page of this._pages) {
if (page.page.OnGet) {
this._router.get(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnGet);
} else if (page.page.OnGetAsync) {
this._router.get(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnGetAsync);
}

if (page.page.OnPost) {
this._router.post(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnPost);
} else if (page.page.OnPostAsync) {
this._router.post(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnPostAsync);
}
}

for (let subRoute of this._subRoutes) {
this._router.use(subRoute.path, subRoute.route.Route());
}

return this._router;
}
}

private DoAuthorise(authorise: boolean, req: Request, res: Response, next: NextFunction) {
if (authorise) {
UserMiddleware.Authorise(req, res, next);
} else {
next();
}
}

private DoAdminAuthorise(adminAuthorise: boolean, req: Request, res: Response, next: NextFunction) {
if (adminAuthorise) {
UserMiddleware.AdminAuthorise(req, res, next);
} else {
next();
}
}
}
Loading
Loading