import { TasksService } from './common/services/tasks.service'; import { Module, NestModule, MiddlewareConsumer } from "@nestjs/common"; import { PrismaModule } from "./prisma/prisma.module"; import { SupportChatModule } from './support-chat/support-chat.module'; import { ProductsModule } from "./products/products.module"; import { UsersModule } from "./users/users.module"; import { RedisModule } from "./redis/redis.module"; import { RedisService } from "./redis/redis.service"; import { AuthModule } from "./auth/auth.module"; import { PetsModule } from "./pets/pets.module"; import { OrdersModule } from "./orders/orders.module"; import { SettingsModule } from "./settings/settings.module"; import { ThrottlerModule, ThrottlerGuard } from "@nestjs/throttler"; import { APP_GUARD } from "@nestjs/core"; import { CsrfGuard } from "./common/guards/csrf.guard"; import { MetricsController } from "./common/metrics.controller"; import { AdminModule } from "./admin/admin.module"; import { HomeModule } from "./home/home.module"; import { BlogsModule } from "./blogs/blogs.module"; import { WikiModule } from "./wiki/wiki.module"; import { SeoModule } from "./seo/seo.module"; import { CmsModule } from "./cms/cms.module"; import { WholesaleModule } from "./wholesale/wholesale.module"; import { VideosModule } from "./videos/videos.module"; import { SmsModule } from "./common/sms.module"; import { ContactModule } from "./contact/contact.module"; import { BannersModule } from "./banners/banners.module"; import { SmartAdvisorModule } from "./smart-advisor/smart-advisor.module"; import { TestimonialsModule } from "./testimonials/testimonials.module"; import { IngredientsModule } from "./ingredients/ingredients.module"; import { PrescriptionsModule } from "./prescriptions/prescriptions.module"; import { B2BModule } from "./b2b/b2b.module"; import { PaymentModule } from "./payment/payment.module"; import { TicketsModule } from "./tickets/tickets.module"; import { ReviewsModule } from "./reviews/reviews.module"; import { DoctorsModule } from "./doctors/doctors.module"; import { FaqModule } from "./faq/faq.module"; import { MenuModule } from "./menu/menu.module"; import { RevalidationModule } from "./common/revalidation/revalidation.module"; import { Request, Response, NextFunction } from "express"; @Module({ imports: [ SupportChatModule, RevalidationModule, SmsModule, ContactModule, PrismaModule, RedisModule, ProductsModule, UsersModule, AuthModule, PetsModule, OrdersModule, SettingsModule, PaymentModule, TicketsModule, ReviewsModule, DoctorsModule, ThrottlerModule.forRoot([ { ttl: 60000, limit: 100, }, ]), AdminModule, HomeModule, BlogsModule, WikiModule, SeoModule, CmsModule, WholesaleModule, VideosModule, BannersModule, SmartAdvisorModule, TestimonialsModule, IngredientsModule, PrescriptionsModule, B2BModule, FaqModule, MenuModule, ], controllers: [MetricsController], providers: [ { provide: APP_GUARD, useClass: ThrottlerGuard, }, { provide: APP_GUARD, useClass: CsrfGuard, }, RedisService, TasksService, ], }) export class AppModule implements NestModule { constructor(private readonly redisService: RedisService) {} configure(consumer: MiddlewareConsumer) { consumer .apply((req: Request, res: Response, next: NextFunction) => { MetricsController.incrementRequestCount(); const url: string = req.originalUrl || req.url || ""; const method: string = req.method || ""; // Exclude options, admin panel requests, static assets, and health metrics from visit counter const isAdminPath = url.includes("/admin") || url.includes("/api/admin"); const isStaticAsset = url.match( /\.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|map)$/i, ); const isIgnoredMethod = method === "OPTIONS" || method === "HEAD"; if (!isAdminPath && !isStaticAsset && !isIgnoredMethod) { const todayKey = `visits:${new Date().toISOString().split("T")[0]}`; this.redisService.incr(todayKey, 86400).catch(() => {}); } next(); }) .exclude("metrics") .forRoutes("*"); } }