import { NextFunction, Request, Response } from "express"; import { Account } from "../auth/Account"; import { mySqlConnection } from "@rolesvillesoftware/tiberhealthmodel"; import { Connection } from "@rolesvillesoftware/tsentity/dist"; /** * Constructor * * @class BaseRoute */ export abstract class BaseRoute> { protected title: string; private scripts: string[]; abstract get concrete(): T; get warehouseConnection(): mySqlConnection { if ((this.req).warehouseConnection == null) { return null; } return (this.req).warehouseConnection; } get contextConnection(): Connection { if ((this.req).contextConnection == null) { return null; } return (this.req).contextConnection; } /** * Constructor * * @class BaseRoute * @constructor */ constructor(protected req: Request, protected res: Response, protected next: NextFunction, checkAuth = true) { //initialize variables this.title = "Tiber Health Analytics Portal"; this.scripts = []; } protected checkAuth(callback: (controller: T) => void) { if (this.req.user == null || !(this.req.user instanceof Account)) { let redirect = ""; if (this.req.originalUrl !== "/") { redirect = `?returnUrl=${encodeURIComponent(this.req.originalUrl)}`; } this.res.redirect(`/login${redirect}`); } else { if (callback != null) { callback(this.concrete);; } } } /** * Add a JS external file to the request. * * @class BaseRoute * @method addScript * @param src {string} The src to the external JS file. * @return {BaseRoute} Self for chaining */ public addScript(src: string): BaseRoute { this.scripts.push(src); return this; } /** * Render a page. * * @class BaseRoute * @method render * @param req {Request} The request object. * @param res {Response} The response object. * @param view {String} The view to render. * @param options {Object} Additional options to append to the view's local scope. * @return void */ public render(req: Request, res: Response, view: string, options?: Object) { //add constants res.locals.BASE_URL = "/"; //add scripts res.locals.scripts = this.scripts; //add title res.locals.title = this.title; //render view res.render(view, options); } }