refactor(api): replace new Logger() with DI LoggerService and split large files

- Migrate 30 files from `new Logger(ClassName.name)` to injected LoggerService
  for consistent PII masking and centralized logging config
- Split prisma-admin-query.repository.ts (313→121 lines) into admin-stats.queries.ts
  and admin-user.queries.ts
- Split admin.controller.ts (285→154 lines) into admin-moderation.controller.ts
- Split prisma-listing.repository.ts (274→111 lines) into listing-read.queries.ts
- Update 28 test files with mock LoggerService
- All 831 tests passing, zero direct new Logger() calls remaining

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-10 05:35:04 +07:00
parent 4e71036ddd
commit 34202f2527
67 changed files with 851 additions and 653 deletions

View File

@@ -23,9 +23,12 @@ describe('CreateReviewHandler', () => {
mockEventBus = { publish: vi.fn() };
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new CreateReviewHandler(
mockReviewRepo as any,
mockEventBus as unknown as EventBus,
mockLogger as any,
);
});

View File

@@ -31,9 +31,12 @@ describe('DeleteReviewHandler', () => {
mockEventBus = { publish: vi.fn() };
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new DeleteReviewHandler(
mockReviewRepo as any,
mockEventBus as unknown as EventBus,
mockLogger as any,
);
});

View File

@@ -1,7 +1,7 @@
import { Inject, Logger } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import { CommandHandler, type EventBus, type ICommandHandler } from '@nestjs/cqrs';
import { createId } from '@paralleldrive/cuid2';
import { ConflictException, ValidationException } from '@modules/shared';
import { ConflictException, ValidationException, type LoggerService } from '@modules/shared';
import { ReviewEntity } from '../../../domain/entities/review.entity';
import { REVIEW_REPOSITORY, type IReviewRepository } from '../../../domain/repositories/review.repository';
import { Rating } from '../../../domain/value-objects/rating.vo';
@@ -17,11 +17,10 @@ export interface CreateReviewResult {
@CommandHandler(CreateReviewCommand)
export class CreateReviewHandler implements ICommandHandler<CreateReviewCommand> {
private readonly logger = new Logger(CreateReviewHandler.name);
constructor(
@Inject(REVIEW_REPOSITORY) private readonly reviewRepo: IReviewRepository,
private readonly eventBus: EventBus,
private readonly logger: LoggerService,
) {}
async execute(command: CreateReviewCommand): Promise<CreateReviewResult> {
@@ -65,7 +64,7 @@ export class CreateReviewHandler implements ICommandHandler<CreateReviewCommand>
this.eventBus.publish(event);
}
this.logger.log(`Review ${id} created by user ${command.userId} for ${command.targetType}:${command.targetId}`);
this.logger.log(`Review ${id} created by user ${command.userId} for ${command.targetType}:${command.targetId}`, 'CreateReviewHandler');
return {
id,

View File

@@ -1,16 +1,15 @@
import { Inject, Logger } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import { CommandHandler, type EventBus, type ICommandHandler } from '@nestjs/cqrs';
import { ForbiddenException, NotFoundException } from '@modules/shared';
import { ForbiddenException, NotFoundException, type LoggerService } from '@modules/shared';
import { REVIEW_REPOSITORY, type IReviewRepository } from '../../../domain/repositories/review.repository';
import { DeleteReviewCommand } from './delete-review.command';
@CommandHandler(DeleteReviewCommand)
export class DeleteReviewHandler implements ICommandHandler<DeleteReviewCommand> {
private readonly logger = new Logger(DeleteReviewHandler.name);
constructor(
@Inject(REVIEW_REPOSITORY) private readonly reviewRepo: IReviewRepository,
private readonly eventBus: EventBus,
private readonly logger: LoggerService,
) {}
async execute(command: DeleteReviewCommand): Promise<void> {
@@ -32,6 +31,6 @@ export class DeleteReviewHandler implements ICommandHandler<DeleteReviewCommand>
this.eventBus.publish(event);
}
this.logger.log(`Review ${command.reviewId} deleted by user ${command.userId}`);
this.logger.log(`Review ${command.reviewId} deleted by user ${command.userId}`, 'DeleteReviewHandler');
}
}