File size: 5,176 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
import { Schema, model, Document, Types } from 'mongoose';

interface IGeoPoint {
    type: "Point";
    coordinates: [number, number]; // [lng, lat]
}

interface IEventDayPhoto {
    url: string;
    publicId: string; // Cloudinary public ID for deletion
    caption?: string;
    uploadedBy: Types.ObjectId;
    uploadedAt: Date;
}

interface IEventDayAttendance {
    user: Types.ObjectId;
    checkInTime: Date;
    checkInMethod: 'gps' | 'qr' | 'manual' | 'hotspot';
    checkInLocation?: IGeoPoint;
    verifiedBy?: Types.ObjectId; // Trainer who verified (for manual)
}

export interface IEventDay extends Document {
    // Reference to parent event
    event: Types.ObjectId;

    // Date information
    date: Date; // Specific date (YYYY-MM-DD)
    dayNumber: number; // Day 1, Day 2, etc.

    // Time slots
    startTime: Date; // Planned start time for this day's session
    endTime: Date; // Planned end time for this day's session

    // Photos
    photos: IEventDayPhoto[];

    // Attendance
    attendance: IEventDayAttendance[];

    // Optional metadata for disaster management training
    notes?: string; // Trainer's notes for the day
    weatherCondition?: string; // e.g., "Sunny", "Rainy", "Cloudy"
    activitiesConducted?: string[]; // List of activities/drills conducted
    incidentsReported?: string; // Any incidents/issues during the day
    equipmentUsed?: string[]; // Equipment/resources used
    cvDetectedCount?: number; // Number of people detected in photos via CV (optional)

    // Status
    hasStarted: boolean; // Event day has been started by trainer
    isCompleted: boolean; // Mark day as completed
    completedAt?: Date;

    // Metadata
    createdAt: Date;
    updatedAt: Date;
}

const EventDaySchema = new Schema<IEventDay>({
    event: {
        type: Schema.Types.ObjectId,
        ref: 'Event',
        required: true,
        index: true
    },

    date: {
        type: Date,
        required: true,
        index: true
    },

    dayNumber: {
        type: Number,
        required: true,
        min: 1
    },

    startTime: {
        type: Date,
        required: true
    },

    endTime: {
        type: Date,
        required: true
    },

    photos: [{
        url: {
            type: String,
            required: true
        },
        publicId: {
            type: String,
            required: true
        },
        caption: {
            type: String,
            trim: true
        },
        uploadedBy: {
            type: Schema.Types.ObjectId,
            ref: 'User',
            required: true
        },
        uploadedAt: {
            type: Date,
            default: Date.now
        }
    }],

    attendance: [{
        user: {
            type: Schema.Types.ObjectId,
            ref: 'User',
            required: true
        },
        checkInTime: {
            type: Date,
            default: Date.now
        },
        checkInMethod: {
            type: String,
            enum: ['gps', 'qr', 'manual', 'hotspot'],
            required: true
        },
        checkInLocation: {
            type: {
                type: String,
                enum: ['Point']
            },
            coordinates: {
                type: [Number]
            }
        },
        verifiedBy: {
            type: Schema.Types.ObjectId,
            ref: 'User'
        }
    }],

    notes: {
        type: String,
        trim: true
    },

    weatherCondition: {
        type: String,
        trim: true
    },

    activitiesConducted: [{
        type: String,
        trim: true
    }],

    incidentsReported: {
        type: String,
        trim: true
    },

    equipmentUsed: [{
        type: String,
        trim: true
    }],

    cvDetectedCount: {
        type: Number,
        min: 0
    },

    hasStarted: {
        type: Boolean,
        default: false
    },

    isCompleted: {
        type: Boolean,
        default: false
    },

    completedAt: {
        type: Date
    }
}, {
    timestamps: true
});

// Indexes
EventDaySchema.index({ event: 1, date: 1 }, { unique: true }); // Unique day per event
EventDaySchema.index({ event: 1, dayNumber: 1 }); // Sequential access
EventDaySchema.index({ 'attendance.user': 1 }); // User attendance queries
EventDaySchema.index({ date: 1 }); // Date-based queries

// Pre-save hook to validate and set completion status
EventDaySchema.pre('save', function (this: IEventDay) {
    // Validate time slots
    if (this.endTime <= this.startTime) {
        throw new Error('End time must be after start time');
    }

    // Validate completion requirements
    if (this.isModified('isCompleted') && this.isCompleted) {
        // Ensure photos and attendance exist
        if (this.photos.length === 0) {
            throw new Error('Cannot mark as completed: No photos uploaded');
        }
        if (this.attendance.length === 0) {
            throw new Error('Cannot mark as completed: No attendance recorded');
        }

        // Set completedAt timestamp
        if (!this.completedAt) {
            this.completedAt = new Date();
        }
    }
});

export default model<IEventDay>('EventDay', EventDaySchema);