tsk-C
compleate
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -23,3 +23,10 @@ export interface AuditLogRecord {
|
||||
requestId: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface AuditLogListFilters {
|
||||
organizationId?: string;
|
||||
entityType?: string;
|
||||
entityId?: string;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user