forked from Chatbot/v3-api
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
|
import { Document, Types } from 'mongoose';
|
|
|
|
@Schema({
|
|
timestamps: { createdAt: 'createdAt', updatedAt: false },
|
|
versionKey: false,
|
|
collection: 'audit_logs',
|
|
})
|
|
export class AuditLogModel extends Document {
|
|
@Prop({ type: Types.ObjectId, ref: 'UserModel', required: false })
|
|
userId?: Types.ObjectId;
|
|
|
|
@Prop({ type: String, required: true })
|
|
action: string; // e.g., 'joinRoom', 'sendMessage', 'expertOnline', etc.
|
|
|
|
@Prop({ type: String, required: false })
|
|
resource?: string; // e.g., 'Chat', 'Session', 'Expert', 'Message', etc.
|
|
|
|
@Prop({ type: String, required: false })
|
|
resourceId?: string; // e.g., sessionId, messageId, expertId, etc.
|
|
|
|
@Prop({ type: Object, required: false })
|
|
oldValues?: Record<string, any>; // Previous state before change
|
|
|
|
@Prop({ type: Object, required: false })
|
|
newValues?: Record<string, any>; // New state after change
|
|
|
|
@Prop({ type: Object, required: false })
|
|
changes?: Record<string, { from: any; to: any }>; // Detailed changes
|
|
|
|
@Prop({ type: String, required: false })
|
|
ipAddress?: string;
|
|
|
|
@Prop({ type: String, required: false })
|
|
userAgent?: string;
|
|
|
|
@Prop({ type: String, required: false })
|
|
method?: string; // For WebSocket: event name, for HTTP: GET, POST, etc.
|
|
|
|
@Prop({ type: String, required: false })
|
|
endpoint?: string; // For WebSocket: roomId or path, for HTTP: route path
|
|
|
|
@Prop({ type: Number, required: false })
|
|
statusCode?: number; // HTTP status code or success indicator
|
|
|
|
@Prop({ type: Object, required: false })
|
|
metadata?: Record<string, any>; // Additional context data
|
|
|
|
@Prop({ type: Date, default: Date.now })
|
|
createdAt: Date;
|
|
}
|
|
|
|
export const AuditLogSchema = SchemaFactory.createForClass(AuditLogModel);
|
|
|
|
// Create index for common queries
|
|
AuditLogSchema.index({ userId: 1, createdAt: -1 });
|
|
AuditLogSchema.index({ action: 1, createdAt: -1 });
|
|
AuditLogSchema.index({ resource: 1, resourceId: 1 });
|
|
AuditLogSchema.index({ createdAt: -1 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|