feat: add audit log system with comprehensive action tracking and user activity monitoring

Introduce AuditLog model to track system-wide user actions including authentication, course management, file operations, and user account changes. Add AuditAction enum with 17 action types (CREATE, UPDATE, DELETE, LOGIN, LOGOUT, ENROLL, UNENROLL, SUBMIT_QUIZ, APPROVE_COURSE, REJECT_COURSE, UPLOAD_FILE, DELETE_FILE, CHANGE_PASSWORD, RESET_PASSWORD, VERIFY_EMAIL, DEACTIVATE_USER, ACTIVATE_USER). Include fields
This commit is contained in:
JakkrapartXD 2026-02-05 15:07:07 +07:00
parent f12221c84a
commit 7465af1cb9
2 changed files with 461 additions and 0 deletions

View file

@ -64,6 +64,7 @@ model User {
updated_withdrawals WithdrawalRequest[] @relation("WithdrawalUpdater")
submitted_approvals CourseApproval[] @relation("ApprovalSubmitter")
reviewed_approvals CourseApproval[] @relation("ApprovalReviewer")
audit_logs AuditLog[]
@@index([username])
@@index([email])
@ -608,3 +609,52 @@ model WithdrawalRequest {
@@index([instructor_id, status])
@@map("withdrawal_requests")
}
// ============================================
// Audit Log System
// ============================================
enum AuditAction {
CREATE
UPDATE
DELETE
LOGIN
LOGOUT
ENROLL
UNENROLL
SUBMIT_QUIZ
APPROVE_COURSE
REJECT_COURSE
UPLOAD_FILE
DELETE_FILE
CHANGE_PASSWORD
RESET_PASSWORD
VERIFY_EMAIL
DEACTIVATE_USER
ACTIVATE_USER
}
model AuditLog {
id Int @id @default(autoincrement())
user_id Int?
action AuditAction
entity_type String @db.VarChar(100) // Course, User, Lesson, Quiz, etc.
entity_id Int?
old_value Json? // ค่าเดิม (สำหรับ UPDATE/DELETE)
new_value Json? // ค่าใหม่ (สำหรับ CREATE/UPDATE)
ip_address String? @db.VarChar(45) // รองรับ IPv6
user_agent String? @db.Text
metadata Json? // ข้อมูลเพิ่มเติม เช่น request_id, session_id
created_at DateTime @default(now())
user User? @relation(fields: [user_id], references: [id], onDelete: SetNull)
@@index([user_id])
@@index([action])
@@index([entity_type])
@@index([entity_type, entity_id])
@@index([created_at])
@@index([user_id, created_at])
@@index([action, created_at])
@@map("audit_logs")
}