From 871a5fe78a48fa355016f3353f61d6f64495dabb Mon Sep 17 00:00:00 2001 From: dahyeon Date: Sat, 27 Jun 2026 14:19:00 -0700 Subject: [PATCH] chore: restore api test baseline and logging --- .../modules/dakao/dakao.subscriber.spec.ts | 10 +++--- .../global/database/connection.service.ts | 33 +++++++++++++------ apps/api/src/redis-io.adapter.ts | 6 ++-- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/apps/api/src/modules/dakao/dakao.subscriber.spec.ts b/apps/api/src/modules/dakao/dakao.subscriber.spec.ts index ebf8e425..fa97773d 100644 --- a/apps/api/src/modules/dakao/dakao.subscriber.spec.ts +++ b/apps/api/src/modules/dakao/dakao.subscriber.spec.ts @@ -56,20 +56,20 @@ describe('DakaoSubscriber', () => { expect(segments).toEqual([ { kind: 'mention', userId: '12345', displayText: '프리리' }, - { kind: 'text', text: ' 사용법\n' }, + { kind: 'text', text: ' 내전 명령어 사용법\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, - { kind: 'text', text: ' !내전 일정\n' }, + { kind: 'text', text: ' !내전 일정\u3000\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', - text: ' !내전 신청 코드 소환사이름#태그 포지션1,포지션2\n', + text: ' !내전 신청 코드 소환사이름#태그 포지션1,포지션2\u3000\n', }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, - { kind: 'text', text: ' !내전 계정등록 소환사이름#태그\n' }, + { kind: 'text', text: ' !내전 계정등록 소환사이름#태그\u3000\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', - text: ' !내전 선호포지션등록 포지션1,포지션2\n', + text: ' !내전 선호포지션등록 포지션1,포지션2\u3000\n', }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { diff --git a/apps/api/src/modules/global/database/connection.service.ts b/apps/api/src/modules/global/database/connection.service.ts index 2ef95351..dc60c262 100644 --- a/apps/api/src/modules/global/database/connection.service.ts +++ b/apps/api/src/modules/global/database/connection.service.ts @@ -1,4 +1,4 @@ -import { Injectable, Scope, OnModuleInit } from '@nestjs/common'; +import { Injectable, Logger, Scope, OnModuleInit } from '@nestjs/common'; import mongoose from 'mongoose'; import { SecretService } from '../secret/secret.service'; import { tournamentModelDefinitions, TournamentModelTypes } from './schema'; @@ -36,13 +36,17 @@ export function resolveMongoConnectionOptions( @Injectable({ scope: Scope.DEFAULT }) export class ConnectionService implements OnModuleInit { + private readonly logger = new Logger(ConnectionService.name); private connection: mongoose.Connection | null = null; constructor(private readonly secretService: SecretService) {} private tournamentModels: Map> | null = null; public async getConnection(): Promise { - if (this.connection !== null && this.connection.readyState === 1) { + if ( + this.connection !== null && + this.connection.readyState === mongoose.ConnectionStates.connected + ) { return this.connection; } @@ -51,15 +55,15 @@ export class ConnectionService implements OnModuleInit { ); const dbName = resolveMongoDbName(); const appName = resolveMongoAppName(); + const connectionOptions = resolveMongoConnectionOptions(dbName, appName); const defaultConnection = await mongoose - .createConnection( - connectionString, - resolveMongoConnectionOptions(dbName, appName), - ) + .createConnection(connectionString, connectionOptions) .asPromise(); this.connection = defaultConnection; - console.log( - `Default Connection이 새로 생성됨. dbName=${dbName} appName=${appName}`, + this.logger.log( + `MongoDB connection created. dbName=${dbName} appName=${appName} autoIndex=${ + connectionOptions.autoIndex === true + }`, ); return this.connection; @@ -113,15 +117,24 @@ export class ConnectionService implements OnModuleInit { const session = await connection.startSession(options); return session as unknown as mongoose.ClientSession; } catch (error) { - console.log(error); + this.logError('Failed to start MongoDB session', error); } } public async onModuleInit() { - await this.getConnection().catch(console.error); + await this.getConnection().catch((error: unknown) => { + this.logError('Failed to initialize MongoDB connection', error); + }); } public async onModuleDestroy() { await this.connection?.close(); } + + private logError(message: string, error: unknown): void { + this.logger.error( + `${message}: ${error instanceof Error ? error.message : String(error)}`, + error instanceof Error ? error.stack : undefined, + ); + } } diff --git a/apps/api/src/redis-io.adapter.ts b/apps/api/src/redis-io.adapter.ts index 3066e65f..8b0a0bfa 100644 --- a/apps/api/src/redis-io.adapter.ts +++ b/apps/api/src/redis-io.adapter.ts @@ -1,7 +1,7 @@ import { INestApplicationContext } from '@nestjs/common'; import { IoAdapter } from '@nestjs/platform-socket.io'; import { createAdapter } from '@socket.io/redis-adapter'; -import type { ServerOptions } from 'socket.io'; +import type { Server, ServerOptions } from 'socket.io'; import { AuctionRedisService, type AuctionRedisClient, @@ -26,8 +26,8 @@ export class RedisIoAdapter extends IoAdapter { this.adapterConstructor = createAdapter(pubClient, this.subClient); } - override createIOServer(port: number, options?: ServerOptions) { - const server = super.createIOServer(port, options); + override createIOServer(port: number, options?: ServerOptions): Server { + const server = super.createIOServer(port, options) as Server; if (this.adapterConstructor) { server.adapter(this.adapterConstructor); }