File size: 4,683 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
import { Schema, model, Document, Types } from 'mongoose';

// Enum for action types
export enum ActivityAction {
  // Authentication
  LOGIN = 'login',
  LOGOUT = 'logout',
  SIGNUP = 'signup',
  PASSWORD_CHANGE = 'password_change',
  PASSWORD_RESET = 'password_reset',
  
  // Profile Management
  PROFILE_UPDATE = 'profile_update',
  PROFILE_PHOTO_UPLOAD = 'profile_photo_upload',
  LOCATION_UPDATE = 'location_update',
  
  // Event Management
  EVENT_CREATE = 'event_create',
  EVENT_UPDATE = 'event_update',
  EVENT_DELETE = 'event_delete',
  EVENT_JOIN = 'event_join',
  EVENT_LEAVE = 'event_leave',
  EVENT_CHECKIN = 'event_checkin',
  EVENT_VIEW = 'event_view',
  EVENT_SEARCH = 'event_search',
  
  // Form Management
  FORM_CREATE = 'form_create',
  FORM_UPDATE = 'form_update',
  FORM_DELETE = 'form_delete',
  FORM_SUBMIT = 'form_submit',
  FORM_VIEW = 'form_view',
  FORM_EXPORT = 'form_export',
  
  // Verification Actions
  TRAINER_VERIFY = 'trainer_verify',
  TRAINER_REJECT = 'trainer_reject',
  ORG_VERIFY = 'organization_verify',
  ORG_REJECT = 'organization_reject',
  
  // Document Actions
  DOCUMENT_UPLOAD = 'document_upload',
  DOCUMENT_VIEW = 'document_view',
  
  // Analytics
  ANALYTICS_VIEW = 'analytics_view',
  REPORT_EXPORT = 'report_export',
  
  // API Actions
  API_ERROR = 'api_error',
  UNAUTHORIZED_ACCESS = 'unauthorized_access'
}

export enum ActivityCategory {
  AUTH = 'authentication',
  PROFILE = 'profile',
  EVENT = 'event',
  FORM = 'form',
  VERIFICATION = 'verification',
  ANALYTICS = 'analytics',
  SYSTEM = 'system'
}

interface IActivityMetadata {
  // Request details
  ipAddress?: string;
  userAgent?: string;
  device?: string;
  browser?: string;
  
  // Resource references
  eventId?: Types.ObjectId;
  formId?: Types.ObjectId;
  organizationId?: Types.ObjectId;
  trainerId?: Types.ObjectId;
  
  // Action-specific data
  changes?: Record<string, any>; // For updates
  errorMessage?: string; // For errors
  statusCode?: number;
  
  // Geographic data
  location?: {
    type: "Point";
    coordinates: [number, number];
  };
  
  // Additional context
  duration?: number; // milliseconds
  method?: string; // HTTP method
  endpoint?: string; // API endpoint
  queryParams?: Record<string, any>;
  responseSize?: number; // bytes
}

export interface IUserActivity extends Document {
  user: Types.ObjectId;
  action: ActivityAction;
  category: ActivityCategory;
  description: string;
  metadata: IActivityMetadata;
  timestamp: Date;
  sessionId?: string; // Track user sessions
  success: boolean;
  createdAt: Date;
}

const UserActivitySchema = new Schema<IUserActivity>({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    index: true
  },
  
  action: {
    type: String,
    enum: Object.values(ActivityAction),
    required: true,
    index: true
  },
  
  category: {
    type: String,
    enum: Object.values(ActivityCategory),
    required: true,
    index: true
  },
  
  description: {
    type: String,
    required: true
  },
  
  metadata: {
    ipAddress: String,
    userAgent: String,
    device: String,
    browser: String,
    
    eventId: {
      type: Schema.Types.ObjectId,
      ref: 'Event'
    },
    formId: {
      type: Schema.Types.ObjectId,
      ref: 'Form'
    },
    organizationId: {
      type: Schema.Types.ObjectId,
      ref: 'User'
    },
    trainerId: {
      type: Schema.Types.ObjectId,
      ref: 'User'
    },
    
    changes: Schema.Types.Mixed,
    errorMessage: String,
    statusCode: Number,
    
    location: {
      type: {
        type: String,
        enum: ['Point']
      },
      coordinates: [Number]
    },
    
    duration: Number,
    method: String,
    endpoint: String,
    queryParams: Schema.Types.Mixed,
    responseSize: Number
  },
  
  timestamp: {
    type: Date,
    default: Date.now,
    index: true
  },
  
  sessionId: {
    type: String,
    index: true
  },
  
  success: {
    type: Boolean,
    default: true,
    index: true
  }
}, {
  timestamps: { createdAt: true, updatedAt: false }
});

// Compound indexes for common queries
UserActivitySchema.index({ user: 1, timestamp: -1 });
UserActivitySchema.index({ user: 1, category: 1, timestamp: -1 });
UserActivitySchema.index({ user: 1, action: 1, timestamp: -1 });
UserActivitySchema.index({ sessionId: 1, timestamp: -1 });
UserActivitySchema.index({ 'metadata.eventId': 1 });
UserActivitySchema.index({ 'metadata.formId': 1 });

// TTL index - automatically delete logs older than 2 years
UserActivitySchema.index({ timestamp: 1 }, { expireAfterSeconds: 63072000 });

export default model<IUserActivity>('UserActivity', UserActivitySchema);