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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user