feat: 로그 시스템 구현 및 관련 클래스 추가
This commit is contained in:
@@ -6,6 +6,22 @@ datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
enum LogScope {
|
||||
SYSTEM
|
||||
NATION
|
||||
GENERAL
|
||||
USER
|
||||
}
|
||||
|
||||
enum LogCategory {
|
||||
HISTORY
|
||||
SUMMARY
|
||||
ACTION
|
||||
BATTLE_BRIEF
|
||||
BATTLE_DETAIL
|
||||
USER
|
||||
}
|
||||
|
||||
model WorldState {
|
||||
id Int @id @default(autoincrement())
|
||||
scenarioCode String @map("scenario_code")
|
||||
@@ -133,3 +149,24 @@ model Event {
|
||||
|
||||
@@map("event")
|
||||
}
|
||||
|
||||
model LogEntry {
|
||||
id Int @id @default(autoincrement())
|
||||
scope LogScope
|
||||
category LogCategory
|
||||
subType String? @map("sub_type")
|
||||
year Int
|
||||
month Int
|
||||
text String
|
||||
generalId Int? @map("general_id")
|
||||
nationId Int? @map("nation_id")
|
||||
userId Int? @map("user_id")
|
||||
meta Json @default(dbgenerated("'{}'::jsonb"))
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
|
||||
@@index([scope, category, id])
|
||||
@@index([generalId, category, id])
|
||||
@@index([nationId, category, id])
|
||||
@@index([userId, category, id])
|
||||
@@map("log_entry")
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './postgres.js';
|
||||
export * from './logRepository.js';
|
||||
export * from './redis.js';
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { LogCategory, LogScope } from '@prisma/client';
|
||||
import type { Prisma, PrismaClient } from '@prisma/client';
|
||||
|
||||
export interface LogQueryOptions {
|
||||
limit?: number;
|
||||
beforeId?: number;
|
||||
}
|
||||
|
||||
export interface LogEntryView {
|
||||
id: number;
|
||||
scope: LogScope;
|
||||
category: LogCategory;
|
||||
subType: string | null;
|
||||
text: string;
|
||||
year: number;
|
||||
month: number;
|
||||
createdAt: Date;
|
||||
generalId: number | null;
|
||||
nationId: number | null;
|
||||
userId: number | null;
|
||||
}
|
||||
|
||||
const buildPaginationWhere = (
|
||||
base: Prisma.LogEntryWhereInput,
|
||||
options: LogQueryOptions
|
||||
): Prisma.LogEntryWhereInput => {
|
||||
if (options.beforeId) {
|
||||
return {
|
||||
...base,
|
||||
id: { lt: options.beforeId },
|
||||
};
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
const buildFindArgs = (
|
||||
where: Prisma.LogEntryWhereInput,
|
||||
options: LogQueryOptions
|
||||
): Prisma.LogEntryFindManyArgs => ({
|
||||
where: buildPaginationWhere(where, options),
|
||||
orderBy: { id: 'desc' },
|
||||
take: options.limit ?? 50,
|
||||
});
|
||||
|
||||
export class LogRepository {
|
||||
constructor(private readonly prisma: PrismaClient) {}
|
||||
|
||||
// 전역(시스템) 로그 조회
|
||||
async listSystemLogs(
|
||||
category: LogCategory,
|
||||
options: LogQueryOptions = {}
|
||||
): Promise<LogEntryView[]> {
|
||||
return this.prisma.logEntry.findMany(
|
||||
buildFindArgs(
|
||||
{
|
||||
scope: LogScope.SYSTEM,
|
||||
category,
|
||||
},
|
||||
options
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 국가 로그 조회
|
||||
async listNationLogs(
|
||||
nationId: number,
|
||||
category: LogCategory,
|
||||
options: LogQueryOptions = {}
|
||||
): Promise<LogEntryView[]> {
|
||||
return this.prisma.logEntry.findMany(
|
||||
buildFindArgs(
|
||||
{
|
||||
scope: LogScope.NATION,
|
||||
category,
|
||||
nationId,
|
||||
},
|
||||
options
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 장수 로그 조회
|
||||
async listGeneralLogs(
|
||||
generalId: number,
|
||||
category: LogCategory,
|
||||
options: LogQueryOptions = {}
|
||||
): Promise<LogEntryView[]> {
|
||||
return this.prisma.logEntry.findMany(
|
||||
buildFindArgs(
|
||||
{
|
||||
scope: LogScope.GENERAL,
|
||||
category,
|
||||
generalId,
|
||||
},
|
||||
options
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 유저 로그 조회
|
||||
async listUserLogs(
|
||||
userId: number,
|
||||
options: LogQueryOptions & { subType?: string } = {}
|
||||
): Promise<LogEntryView[]> {
|
||||
return this.prisma.logEntry.findMany(
|
||||
buildFindArgs(
|
||||
{
|
||||
scope: LogScope.USER,
|
||||
category: LogCategory.USER,
|
||||
userId,
|
||||
subType: options.subType,
|
||||
},
|
||||
options
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user