feat: add system configuration platform
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
import { getDatabase } from "@/modules/core/server/db";
|
||||
import { auditLogs } from "@/modules/core/server/schema";
|
||||
import type { AuditLog, AuditResult, PublicAccount } from "@/modules/core/types";
|
||||
import { updateData } from "@/modules/core/server/store";
|
||||
|
||||
type AuditInput = {
|
||||
actor?: PublicAccount;
|
||||
organizationId?: string;
|
||||
action: string;
|
||||
targetType: string;
|
||||
targetId?: string;
|
||||
@@ -12,13 +14,27 @@ type AuditInput = {
|
||||
reason: string;
|
||||
};
|
||||
|
||||
export async function recordAuditLog(input: AuditInput): Promise<AuditLog> {
|
||||
const timestamp = new Date().toISOString();
|
||||
function toAuditLog(row: typeof auditLogs.$inferSelect): AuditLog {
|
||||
return {
|
||||
id: row.id,
|
||||
organizationId: row.organizationId ?? undefined,
|
||||
timestamp: row.timestamp.toISOString(),
|
||||
actorAccountId: row.actorAccountId ?? undefined,
|
||||
actorEmail: row.actorEmail ?? undefined,
|
||||
action: row.action,
|
||||
targetType: row.targetType,
|
||||
targetId: row.targetId ?? undefined,
|
||||
result: row.result,
|
||||
reason: row.reason,
|
||||
};
|
||||
}
|
||||
|
||||
return await updateData((data) => {
|
||||
const nextLog: AuditLog = {
|
||||
id: randomUUID(),
|
||||
timestamp,
|
||||
export async function recordAuditLog(input: AuditInput): Promise<AuditLog> {
|
||||
const database = getDatabase();
|
||||
const rows = await database
|
||||
.insert(auditLogs)
|
||||
.values({
|
||||
organizationId: input.organizationId,
|
||||
actorAccountId: input.actor?.id,
|
||||
actorEmail: input.actor?.email,
|
||||
action: input.action,
|
||||
@@ -26,10 +42,27 @@ export async function recordAuditLog(input: AuditInput): Promise<AuditLog> {
|
||||
targetId: input.targetId,
|
||||
result: input.result,
|
||||
reason: input.reason,
|
||||
};
|
||||
})
|
||||
.returning();
|
||||
const row = rows[0];
|
||||
if (!row) {
|
||||
throw new Error("审计日志写入失败");
|
||||
}
|
||||
|
||||
data.auditLogs = [nextLog, ...data.auditLogs].slice(0, 500);
|
||||
return nextLog;
|
||||
});
|
||||
return toAuditLog(row);
|
||||
}
|
||||
|
||||
export async function listAuditLogs(params: { organizationId?: string; limit?: number }): Promise<AuditLog[]> {
|
||||
const database = getDatabase();
|
||||
const limit = params.limit ?? 100;
|
||||
const rows = params.organizationId
|
||||
? await database
|
||||
.select()
|
||||
.from(auditLogs)
|
||||
.where(eq(auditLogs.organizationId, params.organizationId))
|
||||
.orderBy(desc(auditLogs.timestamp))
|
||||
.limit(limit)
|
||||
: await database.select().from(auditLogs).orderBy(desc(auditLogs.timestamp)).limit(limit);
|
||||
|
||||
return rows.map(toAuditLog);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user