From b0ea8227d2b4595f39420700ea0ebd77ecb25e45 Mon Sep 17 00:00:00 2001 From: jrcleber Date: Wed, 20 Dec 2023 18:32:23 +0000 Subject: [PATCH] bug fix: guards --- src/app.module.ts | 24 ++++++++++-------------- src/guards/instance.guard.ts | 10 ++-------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 7c625003..3eda3291 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -128,6 +128,16 @@ export async function AppModule(context: Map) { await waMonitor.loadInstance(); logger.info('Load Instances - ON'); + const middlewares = [ + async (req: Request, res: Response, next: NextFunction) => + await new LoggerMiddleware(repository, configService).use(req, res, next), + async (req: Request, res: Response, next: NextFunction) => + await new JwtGuard(configService).canActivate(req, res, next), + async (req: Request, res: Response, next: NextFunction) => + await new InstanceGuard(waMonitor, redisCache).canActivate(req, res, next), + ]; + logger.info('Middlewares - ON'); + const webhookService = new WebhookService(waMonitor); logger.info('WebhookService - ON'); @@ -143,20 +153,6 @@ export async function AppModule(context: Map) { ); logger.info('InstanceController - ON'); - const middlewares = [ - async (req: Request, res: Response, next: NextFunction) => - await new LoggerMiddleware(repository, configService).use(req, res, next), - async (req: Request, res: Response, next: NextFunction) => - await new JwtGuard(configService).canActivate(req, res, next), - async (req: Request, res: Response, next: NextFunction) => - await new InstanceGuard(waMonitor, redisCache, instanceController).canActivate( - req, - res, - next, - ), - ]; - logger.info('Middlewares - ON'); - const instanceRouter = InstanceRouter(instanceController, ...middlewares); logger.info('InstanceRouter - ON'); diff --git a/src/guards/instance.guard.ts b/src/guards/instance.guard.ts index ae453a61..4cd3e7c8 100644 --- a/src/guards/instance.guard.ts +++ b/src/guards/instance.guard.ts @@ -56,13 +56,10 @@ async function fetchInstanceFromCache( instanceName: string, waMonitor: WAMonitoringService, redisCache: RedisCache, - instanceController: InstanceController, ) { try { - const exists = - !!waMonitor.waInstances.get(instanceName) || - !!(await instanceController.fetchInstances({ instanceName })); - if (redisCache.isConnected) { + const exists = !!waMonitor.waInstances.get(instanceName); + if (redisCache?.isConnected) { const keyExists = await redisCache.keys('*'); return exists || keyExists[0]; } @@ -78,7 +75,6 @@ export class InstanceGuard { constructor( private readonly waMonitor: WAMonitoringService, private readonly redisCache: RedisCache, - private readonly instanceController: InstanceController, ) {} async canActivate(req: Request, _: Response, next: NextFunction) { if (req.originalUrl.includes('/instance/create')) { @@ -88,7 +84,6 @@ export class InstanceGuard { instance.instanceName, this.waMonitor, this.redisCache, - this.instanceController, ) ) { throw new ForbiddenException( @@ -116,7 +111,6 @@ export class InstanceGuard { param.instanceName, this.waMonitor, this.redisCache, - this.instanceController, ); if (!fetch) {