11import express from 'express' ;
2- import expressSession from 'express-session' ;
32import bodyParser from 'body-parser' ;
3+ import NodeCache from 'node-cache' ;
44import { Request , Response , NextFunction } from "express" ;
55import { CustomSessionData } from "./session" ;
66import { ExpressIntraUser } from '../sync/oauth' ;
77import { isStaff } from '../utils' ;
8+ import { prisma } from '../main' ;
89
910
1011const checkIfAuthenticated = function ( req : Request , res : Response , next : NextFunction ) {
@@ -44,6 +45,33 @@ const includeUser = function(req: Request, res: Response, next: NextFunction) {
4445 next ( ) ;
4546} ;
4647
48+ const coalitionCache = new NodeCache ( { stdTTL : 3000 , checkperiod : 300 } ) ;
49+ const includeCoalitions = async function ( req : Request , res : Response , next : NextFunction ) {
50+ if ( coalitionCache . has ( 'coalitions' ) ) {
51+ res . locals . coalitions = coalitionCache . get ( 'coalitions' ) ;
52+ return next ( ) ;
53+ }
54+ const coalitions = await prisma . codamCoalition . findMany ( {
55+ select : {
56+ id : true ,
57+ description : true ,
58+ tagline : true ,
59+ intra_coalition : {
60+ select : {
61+ id : true ,
62+ name : true ,
63+ color : true ,
64+ image_url : true ,
65+ cover_url : true ,
66+ } ,
67+ } ,
68+ } ,
69+ } ) ;
70+ coalitionCache . set ( 'coalitions' , coalitions ) ;
71+ res . locals . coalitions = coalitions ;
72+ next ( ) ;
73+ } ;
74+
4775const staffMiddleware = async function ( req : Request , res : Response , next : NextFunction ) {
4876 const user = req . user as ExpressIntraUser ;
4977 if ( await isStaff ( user ) ) {
@@ -58,6 +86,7 @@ export const setupExpressMiddleware = function(app: any) {
5886 app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
5987 app . use ( checkIfAuthenticated ) ;
6088 app . use ( includeUser ) ;
89+ app . use ( includeCoalitions ) ;
6190 app . all ( '/admin*' , staffMiddleware ) ; // require staff accounts to access admin routes
6291 app . use ( expressErrorHandler ) ; // should remain last
6392 // More middleware for session management and authentication are defined in usePassport in authentication.ts
0 commit comments