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

@@ -27,9 +27,12 @@ describe('RecalculateQualityScoreHandler', () => {
agent: { findUnique: vi.fn() },
};
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new RecalculateQualityScoreHandler(
mockAgentRepo as any,
mockPrisma as any,
mockLogger as any,
);
});

View File

@@ -8,7 +8,8 @@ describe('ReviewEventsListener', () => {
beforeEach(() => {
mockCommandBus = { execute: vi.fn() };
listener = new ReviewEventsListener(mockCommandBus as unknown as CommandBus);
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
listener = new ReviewEventsListener(mockCommandBus as unknown as CommandBus, mockLogger as any);
});
describe('onReviewCreated', () => {

View File

@@ -1,6 +1,6 @@
import { Inject, Logger } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs';
import { type PrismaService } from '@modules/shared';
import { type PrismaService, type LoggerService } from '@modules/shared';
import {
AGENT_REPOSITORY,
type IAgentRepository,
@@ -12,18 +12,17 @@ import { RecalculateQualityScoreCommand } from './recalculate-quality-score.comm
export class RecalculateQualityScoreHandler
implements ICommandHandler<RecalculateQualityScoreCommand>
{
private readonly logger = new Logger(RecalculateQualityScoreHandler.name);
constructor(
@Inject(AGENT_REPOSITORY)
private readonly agentRepo: IAgentRepository,
private readonly prisma: PrismaService,
private readonly logger: LoggerService,
) {}
async execute(command: RecalculateQualityScoreCommand): Promise<void> {
const agent = await this.agentRepo.findById(command.agentId);
if (!agent) {
this.logger.warn(`Agent ${command.agentId} not found, skipping recalculation`);
this.logger.warn(`Agent ${command.agentId} not found, skipping recalculation`, 'RecalculateQualityScoreHandler');
return;
}
@@ -79,6 +78,7 @@ export class RecalculateQualityScoreHandler
`(rating=${avgRating.toFixed(2)}, reviews=${totalReviews}, ` +
`conversion=${(conversionRate * 100).toFixed(1)}%, ` +
`activeListings=${activeListings}/${totalListings})`,
'RecalculateQualityScoreHandler',
);
}
}

View File

@@ -1,13 +1,15 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { type CommandBus } from '@nestjs/cqrs';
import { OnEvent } from '@nestjs/event-emitter';
import { type LoggerService } from '@modules/shared';
import { RecalculateQualityScoreCommand } from '../commands/recalculate-quality-score/recalculate-quality-score.command';
@Injectable()
export class ReviewEventsListener {
private readonly logger = new Logger(ReviewEventsListener.name);
constructor(private readonly commandBus: CommandBus) {}
constructor(
private readonly commandBus: CommandBus,
private readonly logger: LoggerService,
) {}
@OnEvent('review.created', { async: true })
async onReviewCreated(event: {
@@ -17,6 +19,7 @@ export class ReviewEventsListener {
if (event.targetType === 'AGENT') {
this.logger.log(
`Recalculating quality score for agent ${event.targetId}`,
'ReviewEventsListener',
);
await this.commandBus.execute(
new RecalculateQualityScoreCommand(event.targetId),
@@ -32,6 +35,7 @@ export class ReviewEventsListener {
if (event.targetType === 'AGENT') {
this.logger.log(
`Recalculating quality score for agent ${event.targetId} after review deletion`,
'ReviewEventsListener',
);
await this.commandBus.execute(
new RecalculateQualityScoreCommand(event.targetId),