refactor: tighten backend and logic boundaries
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
import type { GamePrisma, GamePrismaClient } from './gamePrisma.js';
|
||||
|
||||
export interface ErrorLogQueryOptions {
|
||||
limit?: number;
|
||||
beforeId?: number;
|
||||
category?: string;
|
||||
}
|
||||
|
||||
export interface ErrorLogCreateInput {
|
||||
category: string;
|
||||
message: string;
|
||||
source?: string;
|
||||
trace?: string;
|
||||
context?: GamePrisma.InputJsonValue;
|
||||
}
|
||||
|
||||
export interface ErrorLogView {
|
||||
id: number;
|
||||
category: string;
|
||||
source: string | null;
|
||||
message: string;
|
||||
trace: string | null;
|
||||
context: GamePrisma.JsonValue;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
const buildPaginationWhere = (
|
||||
base: GamePrisma.ErrorLogWhereInput,
|
||||
options: ErrorLogQueryOptions
|
||||
): GamePrisma.ErrorLogWhereInput => {
|
||||
if (options.beforeId) {
|
||||
return {
|
||||
...base,
|
||||
id: { lt: options.beforeId },
|
||||
};
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
const buildFindArgs = (
|
||||
where: GamePrisma.ErrorLogWhereInput,
|
||||
options: ErrorLogQueryOptions
|
||||
): GamePrisma.ErrorLogFindManyArgs => ({
|
||||
where: buildPaginationWhere(where, options),
|
||||
orderBy: { id: 'desc' },
|
||||
take: options.limit ?? 50,
|
||||
});
|
||||
|
||||
export class ErrorLogRepository {
|
||||
constructor(private readonly prisma: GamePrismaClient) {}
|
||||
|
||||
async createErrorLog(input: ErrorLogCreateInput): Promise<ErrorLogView> {
|
||||
return this.prisma.errorLog.create({
|
||||
data: {
|
||||
category: input.category,
|
||||
source: input.source ?? null,
|
||||
message: input.message,
|
||||
trace: input.trace ?? null,
|
||||
context: input.context ?? {},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async listErrorLogs(options: ErrorLogQueryOptions = {}): Promise<ErrorLogView[]> {
|
||||
const base: GamePrisma.ErrorLogWhereInput = options.category ? { category: options.category } : {};
|
||||
return this.prisma.errorLog.findMany(buildFindArgs(base, options));
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,5 @@ export type { GamePrismaClient } from './gamePrisma.js';
|
||||
export { createGatewayPostgresConnector, GatewayPrisma } from './gatewayPrisma.js';
|
||||
export type { GatewayPrismaClient } from './gatewayPrisma.js';
|
||||
export * from './db.js';
|
||||
export * from './errorLogRepository.js';
|
||||
export * from './logRepository.js';
|
||||
export * from './redis.js';
|
||||
export * from './turnEngineDb.js';
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
import { LogCategory, LogScope } from '@sammo-ts/logic';
|
||||
|
||||
import type { GamePrisma, GamePrismaClient } from './gamePrisma.js';
|
||||
|
||||
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: GamePrisma.LogEntryWhereInput,
|
||||
options: LogQueryOptions
|
||||
): GamePrisma.LogEntryWhereInput => {
|
||||
if (options.beforeId) {
|
||||
return {
|
||||
...base,
|
||||
id: { lt: options.beforeId },
|
||||
};
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
const buildFindArgs = (
|
||||
where: GamePrisma.LogEntryWhereInput,
|
||||
options: LogQueryOptions
|
||||
): GamePrisma.LogEntryFindManyArgs => ({
|
||||
where: buildPaginationWhere(where, options),
|
||||
orderBy: { id: 'desc' },
|
||||
take: options.limit ?? 50,
|
||||
});
|
||||
|
||||
export class LogRepository {
|
||||
constructor(private readonly prisma: GamePrismaClient) {}
|
||||
|
||||
// 전역(시스템) 로그 조회
|
||||
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