66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import type { HydraCollection } from '~/utils/api'
|
|
import { extractHydraMembers } from '~/utils/api'
|
|
|
|
export type AuditLogAction = 'create' | 'update' | 'delete'
|
|
|
|
export type AuditLogItem = {
|
|
id: string
|
|
'@id'?: string
|
|
entityType: string
|
|
entityId: string
|
|
action: AuditLogAction
|
|
changes: Record<string, unknown>
|
|
performedBy: string
|
|
performedAt: string
|
|
ipAddress: string | null
|
|
requestId: string | null
|
|
}
|
|
|
|
export type AuditLogQuery = {
|
|
page?: number
|
|
entityType?: string
|
|
action?: AuditLogAction
|
|
}
|
|
|
|
export type AuditLogPage = {
|
|
items: AuditLogItem[]
|
|
totalItems: number
|
|
}
|
|
|
|
export type AuditLogEntityTypes = {
|
|
'@id'?: string
|
|
entityTypes: string[]
|
|
}
|
|
|
|
export function useAuditLogService() {
|
|
const api = useApi()
|
|
|
|
async function list(params: AuditLogQuery = {}): Promise<AuditLogPage> {
|
|
const query: Record<string, unknown> = {}
|
|
if (params.page !== undefined) {
|
|
query.page = params.page
|
|
}
|
|
if (params.entityType) {
|
|
query.entity_type = params.entityType
|
|
}
|
|
if (params.action) {
|
|
query.action = params.action
|
|
}
|
|
|
|
const data = await api.get<HydraCollection<AuditLogItem>>('/audit-logs', query)
|
|
return {
|
|
items: extractHydraMembers(data),
|
|
totalItems: data['hydra:totalItems'] ?? data['totalItems'] ?? 0,
|
|
}
|
|
}
|
|
|
|
async function entityTypes(): Promise<string[]> {
|
|
// `/audit-log-entity-types` is a single API Platform item resource
|
|
// (not a hydra collection): it returns `{ entityTypes: string[] }`.
|
|
const data = await api.get<AuditLogEntityTypes>('/audit-log-entity-types')
|
|
return data.entityTypes ?? []
|
|
}
|
|
|
|
return { list, entityTypes }
|
|
}
|