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

@@ -1,8 +1,8 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { type EventBus } from '@nestjs/cqrs';
import { createId } from '@paralleldrive/cuid2';
import { type OAuthProvider, type Prisma } from '@prisma/client';
import { type PrismaService } from '@modules/shared';
import { type PrismaService, type LoggerService } from '@modules/shared';
import { UserEntity } from '../../domain/entities/user.entity';
import { UserRegisteredEvent } from '../../domain/events/user-registered.event';
import { USER_REPOSITORY, type IUserRepository } from '../../domain/repositories/user.repository';
@@ -25,13 +25,12 @@ export interface OAuthUserProfile {
@Injectable()
export class OAuthService {
private readonly logger = new Logger(OAuthService.name);
constructor(
@Inject(USER_REPOSITORY) private readonly userRepo: IUserRepository,
private readonly tokenService: TokenService,
private readonly prisma: PrismaService,
private readonly eventBus: EventBus,
private readonly logger: LoggerService,
) {}
/**
@@ -66,7 +65,7 @@ export class OAuthService {
throw new Error('Tài khoản đã bị vô hiệu hóa');
}
this.logger.log(`OAuth login: existing account for ${profile.provider}/${profile.providerUserId}`);
this.logger.log(`OAuth login: existing account for ${profile.provider}/${profile.providerUserId}`, 'OAuthService');
return this.generateTokensForUser(existingOAuth.user);
}
@@ -78,7 +77,7 @@ export class OAuthService {
throw new Error('Tài khoản đã bị vô hiệu hóa');
}
await this.createOAuthAccount(existingUser.id, profile);
this.logger.log(`OAuth link: linked ${profile.provider} to existing user by email`);
this.logger.log(`OAuth link: linked ${profile.provider} to existing user by email`, 'OAuthService');
return this.generateTokensForUser({
id: existingUser.id,
phone: existingUser.phone.value,
@@ -97,7 +96,7 @@ export class OAuthService {
throw new Error('Tài khoản đã bị vô hiệu hóa');
}
await this.createOAuthAccount(existingUser.id, profile);
this.logger.log(`OAuth link: linked ${profile.provider} to existing user by phone`);
this.logger.log(`OAuth link: linked ${profile.provider} to existing user by phone`, 'OAuthService');
return this.generateTokensForUser({
id: existingUser.id,
phone: existingUser.phone.value,
@@ -130,7 +129,7 @@ export class OAuthService {
// Publish domain event
this.eventBus.publish(new UserRegisteredEvent(userId, phone.value, 'BUYER'));
this.logger.log(`OAuth register: new user created via ${profile.provider}`);
this.logger.log(`OAuth register: new user created via ${profile.provider}`, 'OAuthService');
return this.generateTokensForUser({
id: userId,
phone: phone.value,