web / lib/services/audit-log-service
lib/services/audit-log-service
Service for creating immutable audit log entries.
Provides a single function to record sensitive account actions. PII (email, IP address) is hashed automatically. Errors are caught and logged — audit logging never blocks the primary action.
Interfaces
AuditLogEntry
Properties
action
action:
AuditAction
The action being recorded.
userId
userId:
number
The user ID the action pertains to.
userEmail
userEmail:
string
The user’s email (will be hashed before storage).
performedBy?
optionalperformedBy:number
Admin user ID who initiated the action (null for self-initiated).
ipAddress?
optionalipAddress:string
Client IP address (stored raw for 30 days, then cleared; hash is permanent).
details?
optionaldetails:Record<string,unknown>
Action-specific structured data.
FieldAuditConfig
Configuration for detecting field changes and creating audit entries.
Properties
action
action:
AuditAction
The audit action to record for this field change.
fieldPath
fieldPath:
string
Dot-separated path to the field on the document.
detailsFn()?
optionaldetailsFn: (oldValue,newValue) =>Record<string,unknown>
Optional transform for the audit details. Receives (oldValue, newValue).
Parameters
oldValue
unknown
newValue
unknown
Returns
Record<string, unknown>
Type Aliases
AuditAction
AuditAction = typeof
AUDIT_ACTIONS[keyof typeofAUDIT_ACTIONS]
Variables
AUDIT_ACTIONS
constAUDIT_ACTIONS:object
Action type constants for type safety (not credentials).
Type Declaration
EMAIL_CHANGED
readonlyEMAIL_CHANGED:"account.email_changed"="account.email_changed"
PASSWORD_CHANGED
readonlyPASSWORD_CHANGED:"account.password_changed"="account.password_changed"
DELETION_SCHEDULED
readonlyDELETION_SCHEDULED:"account.deletion_scheduled"="account.deletion_scheduled"
DELETION_CANCELLED
readonlyDELETION_CANCELLED:"account.deletion_cancelled"="account.deletion_cancelled"
DELETION_EXECUTED
readonlyDELETION_EXECUTED:"account.deletion_executed"="account.deletion_executed"
PASSWORD_VERIFY_FAILED
readonlyPASSWORD_VERIFY_FAILED:"account.password_verify_failed"="account.password_verify_failed"
TRUST_LEVEL_CHANGED
readonlyTRUST_LEVEL_CHANGED:"admin.trust_level_changed"="admin.trust_level_changed"
ROLE_CHANGED
readonlyROLE_CHANGED:"admin.role_changed"="admin.role_changed"
USER_ACTIVATED
readonlyUSER_ACTIVATED:"admin.user_activated"="admin.user_activated"
USER_DEACTIVATED
readonlyUSER_DEACTIVATED:"admin.user_deactivated"="admin.user_deactivated"
CUSTOM_QUOTAS_CHANGED
readonlyCUSTOM_QUOTAS_CHANGED:"admin.custom_quotas_changed"="admin.custom_quotas_changed"
QUOTA_OVERRIDDEN
readonlyQUOTA_OVERRIDDEN:"admin.quota_overridden"="admin.quota_overridden"
CATALOG_VISIBILITY_CHANGED
readonlyCATALOG_VISIBILITY_CHANGED:"data.catalog_visibility_changed"="data.catalog_visibility_changed"
DATASET_VISIBILITY_CHANGED
readonlyDATASET_VISIBILITY_CHANGED:"data.dataset_visibility_changed"="data.dataset_visibility_changed"
CATALOG_OWNERSHIP_TRANSFERRED
readonlyCATALOG_OWNERSHIP_TRANSFERRED:"data.catalog_ownership_transferred"="data.catalog_ownership_transferred"
DATASET_OWNERSHIP_TRANSFERRED
readonlyDATASET_OWNERSHIP_TRANSFERRED:"data.dataset_ownership_transferred"="data.dataset_ownership_transferred"
FEATURE_FLAG_CHANGED
readonlyFEATURE_FLAG_CHANGED:"system.feature_flag_changed"="system.feature_flag_changed"
SETTINGS_CHANGED
readonlySETTINGS_CHANGED:"system.settings_changed"="system.settings_changed"
IMPORT_JOB_STAGE_OVERRIDE
readonlyIMPORT_JOB_STAGE_OVERRIDE:"import.job_stage_override"="import.job_stage_override"
SCHEDULED_IMPORT_ADMIN_MODIFIED
readonlySCHEDULED_IMPORT_ADMIN_MODIFIED:"import.scheduled_import_admin_modified"="import.scheduled_import_admin_modified"
Functions
auditLog()
auditLog(
payload,entry):Promise<void>
Create an immutable audit log entry. Hashes PII internally.
This function catches all errors and logs them — it never throws. Audit logging must not prevent the primary operation from completing.
Parameters
payload
BasePayload
entry
Returns
Promise<void>
auditFieldChanges()
auditFieldChanges(
payload,args,fields):Promise<void>
Detect field-level changes between previousDoc and doc, and fire audit log entries for each changed field. Designed for Payload afterChange hooks.
Parameters
payload
BasePayload
args
previousDoc
Record<string, unknown> | undefined
doc
Record<string, unknown>
userId
number
userEmail
string
performedBy?
number
ipAddress?
string
fields
Returns
Promise<void>