feat: 로그 시스템 구현 및 관련 클래스 추가
This commit is contained in:
@@ -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