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

@@ -1,4 +1,7 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Cache } from "cache-manager";
import { Inject } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import {
and,
@@ -27,13 +30,25 @@ import { CreateContentDto } from "./dto/create-content.dto";
@Injectable()
export class ContentsService {
private readonly logger = new Logger(ContentsService.name);
constructor(
private readonly databaseService: DatabaseService,
private readonly s3Service: S3Service,
private readonly mediaService: MediaService,
private readonly configService: ConfigService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}
private async clearContentsCache() {
this.logger.log("Clearing contents cache");
const keys = await this.cacheManager.store.keys();
const contentsKeys = keys.filter((key) => key.startsWith("contents/"));
for (const key of contentsKeys) {
await this.cacheManager.del(key);
}
}
async getUploadUrl(userId: string, fileName: string) {
const key = `uploads/${userId}/${Date.now()}-${fileName}`;
const url = await this.s3Service.getUploadUrl(key);
@@ -50,6 +65,7 @@ export class ContentsService {
tags?: string[];
},
) {
this.logger.log(`Uploading and processing file for user ${userId}`);
// 0. Validation du format et de la taille
const allowedMimeTypes = [
"image/png",
@@ -225,6 +241,7 @@ export class ContentsService {
}
async create(userId: string, data: CreateContentDto) {
this.logger.log(`Creating content for user ${userId}: ${data.title}`);
const { tags: tagNames, ...contentData } = data;
const slug = await this.ensureUniqueSlug(contentData.title);
@@ -264,6 +281,7 @@ export class ContentsService {
}
}
await this.clearContentsCache();
return newContent;
});
}
@@ -285,11 +303,17 @@ export class ContentsService {
}
async remove(id: string, userId: string) {
return await this.databaseService.db
this.logger.log(`Removing content ${id} for user ${userId}`);
const result = await this.databaseService.db
.update(contents)
.set({ deletedAt: new Date() })
.where(and(eq(contents.id, id), eq(contents.userId, userId)))
.returning();
if (result.length > 0) {
await this.clearContentsCache();
}
return result;
}
async findOne(idOrSlug: string) {