forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit-event.entity.ts
More file actions
207 lines (171 loc) · 5.11 KB
/
Copy pathaudit-event.entity.ts
File metadata and controls
207 lines (171 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Comprehensive audit trail system for financial operations
// This module provides traceability for payments, exports, disputes, and admin actions
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index, ManyToOne, JoinColumn } from 'typeorm';
import { User } from '../entities/user.entity';
/**
* Audit action types that must be tracked
* These cover all sensitive operations in the system
*/
export enum AuditAction {
// Payment actions
PAYMENT_INITIATED = 'payment.initiated',
PAYMENT_COMPLETED = 'payment.completed',
PAYMENT_FAILED = 'payment.failed',
PAYMENT_REFUNDED = 'payment.refunded',
PAYMENT_CANCELLED = 'payment.cancelled',
PAYMENT_DISPUTED = 'payment.disputed',
// Settlement actions
SETTLEMENT_CREATED = 'settlement.created',
SETTLEMENT_COMPLETED = 'settlement.completed',
SETTLEMENT_FAILED = 'settlement.failed',
// Split actions
SPLIT_CREATED = 'split.created',
SPLIT_UPDATED = 'split.updated',
SPLIT_DELETED = 'split.deleted',
SPLIT_COMPLETED = 'split.completed',
SPLIT_CANCELLED = 'split.cancelled',
// Dispute actions
DISPUTE_CREATED = 'dispute.created',
DISPUTE_UPDATED = 'dispute.updated',
DISPUTE_RESOLVED = 'dispute.resolved',
DISPUTE_REJECTED = 'dispute.rejected',
DISPUTE_EVIDENCE_ADDED = 'dispute.evidence_added',
// Export actions
EXPORT_CREATED = 'export.created',
EXPORT_DOWNLOADED = 'export.downloaded',
EXPORT_FAILED = 'export.failed',
// Receipt actions
RECEIPT_UPLOADED = 'receipt.uploaded',
RECEIPT_ACCESSED = 'receipt.accessed',
RECEIPT_DELETED = 'receipt.deleted',
// User management
USER_CREATED = 'user.created',
USER_UPDATED = 'user.updated',
USER_DELETED = 'user.deleted',
USER_ROLE_CHANGED = 'user.role_changed',
// Admin actions
ADMIN_NOTE_ADDED = 'admin.note_added',
ADMIN_STATUS_CHANGED = 'admin.status_changed',
ADMIN_SETTINGS_CHANGED = 'admin.settings_changed',
ADMIN_REFUND_PROCESSED = 'admin.refund_processed',
// Auth actions
LOGIN_SUCCESS = 'auth.login_success',
LOGIN_FAILED = 'auth.login_failed',
LOGOUT = 'auth.logout',
PASSWORD_CHANGED = 'auth.password_changed',
TFA_ENABLED = 'auth.tfa_enabled',
TFA_DISABLED = 'auth.tfa_disabled',
}
/**
* Resource types that can be audited
*/
export enum AuditResourceType {
PAYMENT = 'payment',
SETTLEMENT = 'settlement',
SPLIT = 'split',
DISPUTE = 'dispute',
EXPORT = 'export',
RECEIPT = 'receipt',
USER = 'user',
ADMIN = 'admin',
SESSION = 'session',
}
/**
* Severity level for audit events
*/
export enum AuditSeverity {
INFO = 'info',
WARNING = 'warning',
ERROR = 'error',
CRITICAL = 'critical',
}
/**
* Audit event entity for storing in database
*/
@Entity('audit_events')
@Index(['timestamp', 'action'])
@Index(['actorId', 'timestamp'])
@Index(['resourceType', 'resourceId'])
@Index(['severity'])
export class AuditEvent {
@PrimaryGeneratedColumn('uuid')
id!: string;
@Column({ type: 'enum', enum: AuditAction })
action!: AuditAction;
@Column({ type: 'enum', enum: AuditResourceType })
resourceType!: AuditResourceType;
@Column({ type: 'uuid' })
resourceId!: string;
@Column({ type: 'enum', enum: AuditSeverity, default: AuditSeverity.INFO })
severity!: AuditSeverity;
// Actor (who performed the action)
@Column({ type: 'uuid', nullable: true })
actorId?: string;
@Column({ type: 'varchar', nullable: true })
actorEmail?: string;
@Column({ type: 'varchar', nullable: true })
actorIp?: string;
@Column({ type: 'varchar', nullable: true })
actorUserAgent?: string;
// Context
@Column({ type: 'varchar', nullable: true })
sessionId?: string;
@Column({ type: 'jsonb', nullable: true })
requestMetadata?: {
method?: string;
path?: string;
correlationId?: string;
};
// Event data
@Column({ type: 'jsonb', nullable: true })
previousState?: Record<string, unknown>;
@Column({ type: 'jsonb', nullable: true })
newState?: Record<string, unknown>;
@Column({ type: 'text', nullable: true })
description?: string;
@Column({ type: 'text', nullable: true })
reason?: string;
// Timestamp
@CreateDateColumn({ type: 'timestamptz' })
timestamp!: Date;
// Metadata
@Column({ type: 'jsonb', nullable: true })
metadata?: Record<string, unknown>;
// Whether this event has been reviewed
@Column({ type: 'boolean', default: false })
reviewed!: boolean;
@Column({ type: 'uuid', nullable: true })
reviewedById?: string;
@Column({ type: 'timestamptz', nullable: true })
reviewedAt?: Date;
@Column({ type: 'text', nullable: true })
reviewNote?: string;
// ManyToOne relationship for actor
@ManyToOne(() => User, { nullable: true })
@JoinColumn({ name: 'actorId' })
actor?: User;
}
/**
* Audit query filters
*/
export interface AuditQueryFilters {
action?: AuditAction | AuditAction[];
resourceType?: AuditResourceType;
resourceId?: string;
actorId?: string;
severity?: AuditSeverity;
dateFrom?: Date;
dateTo?: Date;
reviewed?: boolean;
search?: string;
}
/**
* Paginated audit results
*/
export interface AuditQueryResult {
data: AuditEvent[];
total: number;
page: number;
limit: number;
hasMore: boolean;
}