compleate
This commit is contained in:
phaichayon
2026-06-15 13:20:39 +07:00
parent b8cd39eaa4
commit 8148850fda
33 changed files with 4800 additions and 35 deletions

View File

@@ -1,8 +1,9 @@
import { and, desc, eq, type SQL } from 'drizzle-orm';
import { headers } from 'next/headers';
import { trAuditLogs } from '@/db/schema';
import { db } from '@/lib/db';
import { getCurrentUserContext } from '@/features/foundation/auth-context/service';
import type { AuditLogInput, AuditLogRecord } from './types';
import type { AuditLogInput, AuditLogListFilters, AuditLogRecord } from './types';
async function resolveAuditContext(input: AuditLogInput) {
const context = await getCurrentUserContext();
@@ -96,3 +97,21 @@ export async function auditDelete(input: Omit<AuditLogInput, 'action'>) {
action: 'delete'
});
}
export async function listAuditLogs(filters: AuditLogListFilters): Promise<AuditLogRecord[]> {
const whereFilters: SQL[] = [
...(filters.organizationId ? [eq(trAuditLogs.organizationId, filters.organizationId)] : []),
...(filters.entityType ? [eq(trAuditLogs.entityType, filters.entityType)] : []),
...(filters.entityId ? [eq(trAuditLogs.entityId, filters.entityId)] : [])
];
const where = whereFilters.length > 1 ? and(...whereFilters) : whereFilters[0];
const limit = filters.limit ?? 20;
const rows = await db.query.trAuditLogs.findMany({
where,
orderBy: [desc(trAuditLogs.createdAt)],
limit
});
return rows.map(mapAuditRecord);
}

View File

@@ -23,3 +23,10 @@ export interface AuditLogRecord {
requestId: string | null;
createdAt: string;
}
export interface AuditLogListFilters {
organizationId?: string;
entityType?: string;
entityId?: string;
limit?: number;
}