import { AuditContextService } from './audit-context.service'; import { EntityAuditService } from './entity-audit.service'; import { AuditOperation } from '../enums'; /** * 审计动作选项 */ export interface AuditActionOptions { /** * 动作名称,如 'createOrder', 'updateUser' */ actionName: string; /** * 操作模板键(可选) * @example 'order.create' */ operationTemplateKey?: string; /** * 描述参数,用于模板填充 * @example { productCount: 3, receiver: '张三', totalAmount: 299.99 } */ descriptionParams?: Record; /** * 自定义元数据 */ metadata?: Record; } /** * 审计动作结果 */ export interface AuditActionResult { actionId: string; actionName: string; entityChanges: Array<{ entityType: string; entityId: string; operation: string; sequence: number; }>; duration: number; success: boolean; error?: Error; } /** * 审计动作服务 * 提供声明式的审计动作管理,自动关联同一业务操作的所有实体变化 */ export declare class AuditActionService { private readonly contextService; private readonly auditService; private readonly logger; constructor(contextService: AuditContextService, auditService: EntityAuditService); /** * 在审计动作上下文中执行操作 * 自动关联该动作内的所有实体变化 * * @example * ```typescript * const result = await auditActionService.executeAction( * { * actionName: 'createOrder', * operationTemplateKey: 'order.create', * descriptionParams: { * productCount: orderItems.length, * receiver: order.receiverName, * totalAmount: order.totalAmount, * address: order.shippingAddress, * }, * }, * async () => { * // 1. 创建订单 * const order = await this.orderRepository.save(newOrder); * * // 2. 创建订单项 * await this.orderItemRepository.save(orderItems); * * // 3. 扣减库存 * await this.inventoryService.deduct(orderItems); * * // 4. 创建物流信息 * await this.shippingService.create(order.id); * * return order; * } * ); * * // 结果中包含所有实体变化: * // result.entityChanges = [ * // { entityType: 'Order', entityId: '1', operation: 'CREATE', sequence: 1 }, * // { entityType: 'OrderItem', entityId: '1', operation: 'CREATE', sequence: 2 }, * // { entityType: 'OrderItem', entityId: '2', operation: 'CREATE', sequence: 3 }, * // { entityType: 'Inventory', entityId: '1', operation: 'UPDATE', sequence: 4 }, * // { entityType: 'Shipping', entityId: '1', operation: 'CREATE', sequence: 5 }, * // ] * ``` */ executeAction(options: AuditActionOptions, operation: () => Promise): Promise; /** * 记录审计动作汇总日志 */ private recordActionSummary; /** * 获取审计动作的所有实体变化 */ getActionAuditLogs(actionId: string): Promise<{ actionId: string; logs: import("..").EntityAuditLogEntity[]; summary: { totalChanges: number; operations: { [k: string]: number; }; entities: { [k: string]: number; }; }; }>; /** * 获取请求内的所有审计动作 */ getRequestActions(requestId: string): Promise<{ requestId: string; actions: { actionId: string; actionName: string; entityChanges: { entityType: string; entityId: string; operation: AuditOperation; sequence: number; timestamp: Date; }[]; logs: import("..").EntityAuditLogEntity[]; }[]; noActionLogs: import("..").EntityAuditLogEntity[]; summary: { totalActions: number; totalChanges: number; }; }>; private groupByOperation; private groupByEntity; }