File size: 5,158 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import { Schema, model, Document, Types } from 'mongoose';
interface IGeoPoint {
type: "Point";
coordinates: [number, number]; // [lng, lat]
}
interface IEventParticipant {
user: Types.ObjectId;
joinedAt: Date;
joinMethod: 'code' | 'link' | 'qr' | 'manual';
}
export interface IEvent extends Document {
// Basic Info
title: string;
code: string; // Short join code (e.g., NDMA-FIRE-23)
theme: string; // e.g., "Fire Safety", "Disaster Response"
description?: string;
// Organization
organization: Types.ObjectId;
createdBy: Types.ObjectId; // Trainer who created it
// Capacity & Timing
capacity: number;
startDate: Date;
endDate: Date;
defaultStartTime: string; // Default session start time (e.g., "16:00")
defaultEndTime: string; // Default session end time (e.g., "18:00")
// Type & Location
type: 'in-person' | 'online' | 'hybrid';
venue?: string; // Physical address for in-person/hybrid
location?: IGeoPoint; // Geo coordinates for in-person/hybrid
geofenceRadius?: number; // Meters - for GPS check-in
onlineLink?: string; // Meeting link for online/hybrid
// Join Settings
joinToken: string; // Secret token for join links
allowSelfJoin: boolean; // Can trainees join without approval?
// Participants
participants: IEventParticipant[];
waitlist: Types.ObjectId[]; // If capacity is full
// Status
status: 'draft' | 'published' | 'ongoing' | 'completed' | 'cancelled';
// Notifications
reminderSent: boolean;
// Metadata
createdAt: Date;
updatedAt: Date;
}
const EventSchema = new Schema<IEvent>({
title: {
type: String,
required: true,
trim: true,
index: 'text' // For text search
},
code: {
type: String,
required: true,
unique: true,
uppercase: true,
trim: true,
index: true
},
theme: {
type: String,
required: true,
index: true
},
description: {
type: String,
trim: true
},
organization: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
index: true
},
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
index: true
},
capacity: {
type: Number,
required: true,
min: 1
},
startDate: {
type: Date,
required: true,
index: true
},
endDate: {
type: Date,
required: true,
index: true
},
defaultStartTime: {
type: String,
required: true,
match: /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/ // HH:MM format
},
defaultEndTime: {
type: String,
required: true,
match: /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/ // HH:MM format
},
type: {
type: String,
enum: ['in-person', 'online', 'hybrid'],
required: true,
index: true
},
venue: {
type: String,
trim: true
},
location: {
type: {
type: String,
enum: ['Point'],
default: 'Point'
},
coordinates: {
type: [Number], // [lng, lat]
validate: {
validator: function (v: number[]) {
return v.length === 2 &&
v[0] >= -180 && v[0] <= 180 &&
v[1] >= -90 && v[1] <= 90;
},
message: 'Invalid coordinates'
}
}
},
geofenceRadius: {
type: Number,
default: 100, // 100 meters default
min: 10,
max: 5000
},
onlineLink: {
type: String,
trim: true
},
joinToken: {
type: String,
required: true,
index: true
},
allowSelfJoin: {
type: Boolean,
default: true
},
participants: [{
user: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
joinedAt: {
type: Date,
default: Date.now
},
joinMethod: {
type: String,
enum: ['code', 'link', 'qr', 'manual'],
required: true
}
}],
waitlist: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
status: {
type: String,
enum: ['draft', 'published', 'ongoing', 'completed', 'cancelled'],
default: 'draft',
index: true
},
reminderSent: {
type: Boolean,
default: false
}
}, {
timestamps: true
});
// Indexes
EventSchema.index({ location: '2dsphere' }); // For geospatial queries
EventSchema.index({ startDate: 1, status: 1 }); // For upcoming events
EventSchema.index({ organization: 1, status: 1 }); // Org events
EventSchema.index({ 'participants.user': 1 }); // User's events
// Text search on title and theme
EventSchema.index({ title: 'text', theme: 'text', description: 'text' });
// Validation: endDate must be after startDate
EventSchema.pre('save', function (this: IEvent) {
if (this.endDate <= this.startDate) {
throw new Error('End date must be after start date');
}
});
// Auto-generate EventDay records when event is created
EventSchema.post('save', async function (doc: IEvent) {
const { generateEventDays } = await import('../util/eventDay.util');
// Check if this is a new document (not an update)
if (this.isNew) {
try {
await generateEventDays(doc);
} catch (err) {
console.error('Failed to generate event days:', err);
}
}
});
export default model<IEvent>('Event', EventSchema); |