File size: 2,298 Bytes
c904036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const mongoose = require('mongoose');

/**
 * EventParticipationLog Schema for CampVerse
 * Includes payment info, attendance timestamp, and QR token for ticketing
 */
const eventParticipationLogSchema = new mongoose.Schema({
  userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
  eventId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Event',
    required: true,
  },
  status: {
    type: String,
    enum: ['registered', 'waitlisted', 'attended'],
    required: true,
  },
  timestamp: { type: Date, default: Date.now },
  registeredAt: { type: Date, default: Date.now },
  paymentType: { type: String, enum: ['free', 'paid'], default: 'free' },
  paymentStatus: {
    type: String,
    enum: ['success', 'pending', 'failed'],
    default: 'success',
  },
  attendanceTimestamp: { type: Date },
  qrToken: { type: String }, // Secure token for QR code validation (legacy field)
  attendanceMarkedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, // Host/co-host who scanned
  attendanceMarkedAt: { type: Date }, // When attendance was marked (scan time)
  
  // Enhanced QR Code System
  qrCode: {
    token: { type: String, unique: true, sparse: true }, // Unique QR token
    imageUrl: { type: String }, // Cloud storage URL for QR image
    generatedAt: { type: Date, default: Date.now },
    expiresAt: { type: Date }, // Event end + 2 hours
    isUsed: { type: Boolean, default: false },
    usedAt: { type: Date },
    usedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } // Host who scanned
  }
});

// Unique index to prevent duplicate registrations
eventParticipationLogSchema.index({ userId: 1, eventId: 1 }, { unique: true });

// Performance indexes for common queries
eventParticipationLogSchema.index({ eventId: 1, status: 1 }); // For capacity checks
eventParticipationLogSchema.index({ userId: 1, status: 1 }); // For user's registrations
eventParticipationLogSchema.index({ qrToken: 1 }, { sparse: true }); // For QR scanning (legacy)
eventParticipationLogSchema.index({ 'qrCode.expiresAt': 1 }, { sparse: true }); // For cleanup
eventParticipationLogSchema.index({ eventId: 1, status: 1, userId: 1 }); // Compound for analytics

module.exports = mongoose.model(
  'EventParticipationLog',
  eventParticipationLogSchema,
);