Skip to content

Commit

Permalink
bug fix: guards
Browse files Browse the repository at this point in the history
  • Loading branch information
jrCleber committed Dec 20, 2023
1 parent b652d85 commit b0ea822
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
24 changes: 10 additions & 14 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ export async function AppModule(context: Map<string, any>) {
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');

Expand All @@ -143,20 +153,6 @@ export async function AppModule(context: Map<string, any>) {
);
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');

Expand Down
10 changes: 2 additions & 8 deletions src/guards/instance.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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')) {
Expand All @@ -88,7 +84,6 @@ export class InstanceGuard {
instance.instanceName,
this.waMonitor,
this.redisCache,
this.instanceController,
)
) {
throw new ForbiddenException(
Expand Down Expand Up @@ -116,7 +111,6 @@ export class InstanceGuard {
param.instanceName,
this.waMonitor,
this.redisCache,
this.instanceController,
);

if (!fetch) {
Expand Down

0 comments on commit b0ea822

Please sign in to comment.