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

@@ -35,10 +35,13 @@ describe('CreatePaymentHandler', () => {
mockEventBus = { publish: vi.fn() };
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new CreatePaymentHandler(
mockPaymentRepo as any,
mockGatewayFactory as any,
mockEventBus as any,
mockLogger as any,
);
});

View File

@@ -48,10 +48,13 @@ describe('HandleCallbackHandler — edge cases', () => {
mockGatewayFactory = { getGateway: vi.fn().mockReturnValue(mockGateway) };
mockEventBus = { publish: vi.fn() };
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new HandleCallbackHandler(
mockPaymentRepo as any,
mockGatewayFactory as any,
mockEventBus as any,
mockLogger as any,
);
});

View File

@@ -43,10 +43,13 @@ describe('HandleCallbackHandler', () => {
mockGatewayFactory = { getGateway: vi.fn().mockReturnValue(mockGateway) };
mockEventBus = { publish: vi.fn() };
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new HandleCallbackHandler(
mockPaymentRepo as any,
mockGatewayFactory as any,
mockEventBus as any,
mockLogger as any,
);
});

View File

@@ -38,9 +38,12 @@ describe('RefundPaymentHandler', () => {
mockGatewayFactory = { getGateway: vi.fn().mockReturnValue(mockGateway) };
const mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), verbose: vi.fn() };
handler = new RefundPaymentHandler(
mockPaymentRepo as any,
mockGatewayFactory as any,
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 { PaymentEntity } from '../../../domain/entities/payment.entity';
import {
PAYMENT_REPOSITORY,
@@ -22,14 +22,13 @@ export interface CreatePaymentResult {
@CommandHandler(CreatePaymentCommand)
export class CreatePaymentHandler implements ICommandHandler<CreatePaymentCommand> {
private readonly logger = new Logger(CreatePaymentHandler.name);
constructor(
@Inject(PAYMENT_REPOSITORY)
private readonly paymentRepo: IPaymentRepository,
@Inject(PAYMENT_GATEWAY_FACTORY)
private readonly gatewayFactory: IPaymentGatewayFactory,
private readonly eventBus: EventBus,
private readonly logger: LoggerService,
) {}
async execute(command: CreatePaymentCommand): Promise<CreatePaymentResult> {
@@ -86,6 +85,7 @@ export class CreatePaymentHandler implements ICommandHandler<CreatePaymentComman
this.logger.log(
`Payment created: id=${paymentId}, provider=${command.provider}, amount=${command.amountVND}`,
'CreatePaymentHandler',
);
return { paymentId, paymentUrl, providerTxId };

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 { type PaymentStatus } from '@prisma/client';
import { NotFoundException, ValidationException } from '@modules/shared';
import { NotFoundException, ValidationException, type LoggerService } from '@modules/shared';
import {
PAYMENT_REPOSITORY,
type IPaymentRepository,
@@ -20,14 +20,13 @@ export interface HandleCallbackResult {
@CommandHandler(HandleCallbackCommand)
export class HandleCallbackHandler implements ICommandHandler<HandleCallbackCommand> {
private readonly logger = new Logger(HandleCallbackHandler.name);
constructor(
@Inject(PAYMENT_REPOSITORY)
private readonly paymentRepo: IPaymentRepository,
@Inject(PAYMENT_GATEWAY_FACTORY)
private readonly gatewayFactory: IPaymentGatewayFactory,
private readonly eventBus: EventBus,
private readonly logger: LoggerService,
) {}
async execute(command: HandleCallbackCommand): Promise<HandleCallbackResult> {
@@ -37,6 +36,7 @@ export class HandleCallbackHandler implements ICommandHandler<HandleCallbackComm
if (!result.isValid) {
this.logger.warn(
`Invalid callback signature for provider=${command.provider}`,
'HandleCallbackHandler',
);
throw new ValidationException('Chữ ký callback không hợp lệ');
}
@@ -57,13 +57,14 @@ export class HandleCallbackHandler implements ICommandHandler<HandleCallbackComm
// Either payment doesn't exist or is already in a terminal state
const existing = await this.paymentRepo.findById(result.orderId);
if (!existing) {
this.logger.warn(`Payment not found for orderId=${result.orderId}`);
this.logger.warn(`Payment not found for orderId=${result.orderId}`, 'HandleCallbackHandler');
throw new NotFoundException('Payment', result.orderId);
}
// Already processed — return idempotent response
this.logger.log(
`Payment ${existing.id} already in terminal state: ${existing.status}`,
'HandleCallbackHandler',
);
return {
paymentId: existing.id,
@@ -86,6 +87,7 @@ export class HandleCallbackHandler implements ICommandHandler<HandleCallbackComm
this.logger.log(
`Payment ${updated.id} callback processed: status=${updated.status}`,
'HandleCallbackHandler',
);
return {

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 { NotFoundException, ValidationException } from '@modules/shared';
import { NotFoundException, ValidationException, type LoggerService } from '@modules/shared';
import {
PAYMENT_REPOSITORY,
type IPaymentRepository,
@@ -19,13 +19,12 @@ export interface RefundPaymentResult {
@CommandHandler(RefundPaymentCommand)
export class RefundPaymentHandler implements ICommandHandler<RefundPaymentCommand> {
private readonly logger = new Logger(RefundPaymentHandler.name);
constructor(
@Inject(PAYMENT_REPOSITORY)
private readonly paymentRepo: IPaymentRepository,
@Inject(PAYMENT_GATEWAY_FACTORY)
private readonly gatewayFactory: IPaymentGatewayFactory,
private readonly logger: LoggerService,
) {}
async execute(command: RefundPaymentCommand): Promise<RefundPaymentResult> {
@@ -59,6 +58,7 @@ export class RefundPaymentHandler implements ICommandHandler<RefundPaymentComman
this.logger.log(
`Refund ${result.success ? 'successful' : 'failed'} for payment ${command.paymentId}`,
'RefundPaymentHandler',
);
return {