feat: add logging and caching enhancements across core services

Integrate `Logger` for consistent logging in services like `reports`, `categories`, `users`, `contents`, and more. Introduce caching capabilities with `CacheInterceptor` and manual cache clearing logic for categories, users, and contents. Add request throttling to critical auth endpoints for enhanced rate limiting.
This commit is contained in:
Mathis HERRIOT
2026-01-10 16:31:06 +01:00
parent 9654553940
commit 5a22ad7480
12 changed files with 129 additions and 13 deletions

View File

@@ -11,7 +11,9 @@ import {
Query,
Req,
UseGuards,
UseInterceptors,
} from "@nestjs/common";
import { CacheInterceptor, CacheKey, CacheTTL } from "@nestjs/cache-manager";
import { AuthService } from "../auth/auth.service";
import { Roles } from "../auth/decorators/roles.decorator";
import { AuthGuard } from "../auth/guards/auth.guard";
@@ -41,6 +43,8 @@ export class UsersController {
// Listing public d'un profil
@Get("public/:username")
@UseInterceptors(CacheInterceptor)
@CacheTTL(60000) // 1 minute
findPublicProfile(@Param("username") username: string) {
return this.usersService.findPublicProfile(username);
}

View File

@@ -1,4 +1,6 @@
import { Injectable } from "@nestjs/common";
import { Injectable, Logger, Inject } from "@nestjs/common";
import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Cache } from "cache-manager";
import { eq, sql } from "drizzle-orm";
import { CryptoService } from "../crypto/crypto.service";
import { DatabaseService } from "../database/database.service";
@@ -11,11 +13,20 @@ import { UpdateUserDto } from "./dto/update-user.dto";
@Injectable()
export class UsersService {
private readonly logger = new Logger(UsersService.name);
constructor(
private readonly databaseService: DatabaseService,
private readonly cryptoService: CryptoService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}
private async clearUserCache(username?: string) {
if (username) {
await this.cacheManager.del(`users/profile/${username}`);
}
}
async create(data: {
username: string;
email: string;
@@ -119,11 +130,17 @@ export class UsersService {
}
async update(uuid: string, data: UpdateUserDto) {
return await this.databaseService.db
this.logger.log(`Updating user profile for ${uuid}`);
const result = await this.databaseService.db
.update(users)
.set({ ...data, updatedAt: new Date() })
.where(eq(users.uuid, uuid))
.returning();
if (result[0]) {
await this.clearUserCache(result[0].username);
}
return result;
}
async updateConsent(