forked from rinafcode/teachLink_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit-log.entity.ts
More file actions
132 lines (101 loc) · 5.01 KB
/
Copy pathaudit-log.entity.ts
File metadata and controls
132 lines (101 loc) · 5.01 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
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
Index,
VersionColumn,
} from 'typeorm';
import { AuditAction, AuditSeverity, AuditCategory } from './enums/audit-action.enum';
export enum HttpMethod {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
PATCH = 'PATCH',
}
/**
* Immutable audit log record.
*
* Rows are append-only — never updated or soft-deleted. Expiry is handled
* exclusively via `applyRetentionPolicy`, which hard-deletes rows whose
* `retentionUntil` has passed.
*
* Index strategy:
* Composite (column + timestamp) indexes support the most common queries:
* "all events for user X, newest first", "all CRITICAL events this week", etc.
* The `retentionUntil` index supports efficient bulk-delete during retention
* policy runs without a full table scan.
*/
@Entity('audit_logs')
@Index('IDX_audit_logs_user_timestamp', ['userId', 'timestamp'])
@Index('IDX_audit_logs_action_timestamp', ['action', 'timestamp'])
@Index('IDX_audit_logs_category_timestamp', ['category', 'timestamp'])
@Index('IDX_audit_logs_severity_timestamp', ['severity', 'timestamp'])
@Index('IDX_audit_logs_entity', ['entityType', 'entityId', 'timestamp'])
@Index('IDX_audit_logs_ip_address', ['ipAddress', 'timestamp'])
@Index('IDX_audit_logs_timestamp', ['timestamp'])
@Index(['retentionUntil']) // required for efficient retention policy deletes
export class AuditLog {
@PrimaryGeneratedColumn('uuid')
id: string;
@VersionColumn({ default: 1 })
version: number;
// ── Actor ──────────────────────────────────────────────────────────────────
@Column({ name: 'user_id', nullable: true })
userId: string | null;
@Column({ name: 'user_email', nullable: true })
userEmail: string | null;
// ── Event classification ───────────────────────────────────────────────────
@Column({ type: 'enum', enum: AuditAction })
action: AuditAction;
@Column({ type: 'enum', enum: AuditCategory })
category: AuditCategory;
@Column({ type: 'enum', enum: AuditSeverity, default: AuditSeverity.INFO })
severity: AuditSeverity;
// ── Target entity ──────────────────────────────────────────────────────────
@Column({ name: 'entity_type', nullable: true })
entityType: string | null;
@Column({ name: 'entity_id', nullable: true })
entityId: string | null;
// ── Payload ────────────────────────────────────────────────────────────────
@Column({ type: 'text', nullable: true })
description: string | null;
@Column({ type: 'jsonb', nullable: true })
metadata: Record<string, unknown> | null;
@Column({ name: 'old_values', type: 'jsonb', nullable: true })
oldValues: Record<string, unknown> | null;
@Column({ name: 'new_values', type: 'jsonb', nullable: true })
newValues: Record<string, unknown> | null;
// ── Request context ────────────────────────────────────────────────────────
@Column({ name: 'ip_address', nullable: true })
ipAddress: string | null;
@Column({ name: 'user_agent', nullable: true })
userAgent: string | null;
@Column({ name: 'session_id', nullable: true })
sessionId: string | null;
@Column({ name: 'request_id', nullable: true })
requestId: string | null;
@Column({ name: 'api_endpoint', nullable: true })
apiEndpoint: string | null;
/** Constrained to known HTTP verbs — free strings invite silent typos. */
@Column({ name: 'http_method', type: 'enum', enum: HttpMethod, nullable: true })
httpMethod: HttpMethod | null;
@Column({ name: 'status_code', nullable: true })
statusCode: number | null;
@Column({ name: 'response_time_ms', nullable: true })
responseTimeMs: number | null;
// ── Multi-tenancy ──────────────────────────────────────────────────────────
@Column({ name: 'tenant_id', nullable: true })
tenantId: string | null;
// ── Timestamps ─────────────────────────────────────────────────────────────
@CreateDateColumn({ name: 'timestamp', type: 'timestamptz' })
timestamp: Date;
/**
* Absolute expiry date for this record.
* Null means the record is kept indefinitely (e.g. CRITICAL severity logs).
* Indexed — see class-level @Index.
*/
@Column({ name: 'retention_until', type: 'timestamptz', nullable: true })
retentionUntil: Date | null;
}