Spaces:
Paused
Paused
File size: 7,540 Bytes
5a548f8 c904036 5a548f8 c904036 5a548f8 c904036 5a548f8 c904036 5a548f8 c904036 5a548f8 4234229 5a548f8 c904036 5a548f8 c904036 5a548f8 c904036 5a548f8 c904036 5a548f8 c904036 5a548f8 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 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 252 253 254 255 256 257 258 259 260 261 262 263 | const mongoose = require('mongoose');
const Event = require('../Models/Event');
const User = require('../Models/User');
const EventParticipationLog = require('../Models/EventParticipationLog');
const { asyncHandler } = require('../Middleware/errorHandler');
// Host dashboard: list events and detailed analytics
const getHostDashboard = asyncHandler(async (req, res) => {
const userId = req.user.id;
// Optimized: Use a single aggregation pipeline to fetch events and their analytics
const eventsWithAnalytics = await Event.aggregate([
{ $match: { hostUserId: new mongoose.Types.ObjectId(userId) } },
{
$lookup: {
from: 'eventparticipationlogs',
localField: '_id',
foreignField: 'eventId',
as: 'logs'
}
},
{
$project: {
title: 1,
date: 1,
location: 1,
status: 1,
capacity: 1,
type: 1,
isPaid: 1,
price: 1,
bannerURL: 1,
logoURL: 1,
verificationStatus: 1,
createdAt: 1,
updatedAt: 1,
analytics: {
totalRegistered: { $size: '$logs' },
totalAttended: {
$size: {
$filter: {
input: '$logs',
as: 'log',
cond: { $eq: ['$$log.status', 'attended'] }
}
}
},
totalWaitlisted: {
$size: {
$filter: {
input: '$logs',
as: 'log',
cond: { $eq: ['$$log.status', 'waitlisted'] }
}
}
},
totalPaid: {
$size: {
$filter: {
input: '$logs',
as: 'log',
cond: { $eq: ['$$log.paymentType', 'paid'] }
}
}
},
totalFree: {
$size: {
$filter: {
input: '$logs',
as: 'log',
cond: { $eq: ['$$log.paymentType', 'free'] }
}
}
}
}
}
},
{
$addFields: {
'analytics.attendanceRate': {
$cond: [
{ $gt: ['$analytics.totalRegistered', 0] },
{
$multiply: [
{ $divide: ['$analytics.totalAttended', '$analytics.totalRegistered'] },
100
]
},
0
]
}
}
}
]);
const totalEvents = eventsWithAnalytics.length;
const totalParticipants = eventsWithAnalytics.reduce(
(sum, e) => sum + e.analytics.totalRegistered,
0,
);
const totalAttendedCount = eventsWithAnalytics.reduce(
(sum, e) => sum + e.analytics.totalAttended,
0,
);
const upcomingEventsCount = eventsWithAnalytics.filter(
(e) => e.date && new Date(e.date) > new Date()
).length;
// Formatting attendanceRate to string as per original implementation
eventsWithAnalytics.forEach(event => {
event.analytics.attendanceRate = event.analytics.attendanceRate.toFixed(2);
});
return res.json({
totalEvents,
totalParticipants,
totalAttended: totalAttendedCount,
upcomingEvents: upcomingEventsCount,
events: eventsWithAnalytics,
});
});
// Create a new event (host only)
const createEvent = asyncHandler(async (req, res) => {
const userId = req.user.id;
const eventData = req.body;
eventData.hostUserId = userId;
const event = await Event.create(eventData);
// Optionally add event to user's eventHistory.hosted
await User.findByIdAndUpdate(userId, {
$push: { 'eventHistory.hosted': event._id },
});
return res.status(201).json(event);
});
// Update an event (host only)
const updateEvent = asyncHandler(async (req, res) => {
const userId = req.user.id;
const eventId = req.params.id;
const event = await Event.findOne({ _id: eventId, hostUserId: userId });
if (!event)
return res
.status(404)
.json({ error: 'Event not found or not owned by user.' });
// Whitelist allow-able fields
const allowedUpdates = [
'title', 'description', 'type', 'organizationName',
'location', 'capacity', 'date', 'isPaid', 'price',
'tags', 'requirements', 'audienceType', 'features',
'sessions', 'about', 'socialLinks'
];
allowedUpdates.forEach((field) => {
if (req.body[field] !== undefined) {
event[field] = req.body[field];
}
});
// Handle special cases safely if needed (e.g. nested objects)
// For now, the direct assignment works for top-level schema fields.
await event.save();
return res.json(event);
});
// Delete an event (host only)
const deleteEvent = asyncHandler(async (req, res) => {
const userId = req.user.id;
const eventId = req.params.id;
const event = await Event.findOneAndDelete({
_id: eventId,
hostUserId: userId,
});
if (!event)
return res
.status(404)
.json({ error: 'Event not found or not owned by user.' });
// Optionally remove from user's eventHistory.hosted
await User.findByIdAndUpdate(userId, {
$pull: { 'eventHistory.hosted': eventId },
});
return res.json({ message: 'Event deleted.' });
});
// List all events hosted by the current user
const getMyEvents = asyncHandler(async (req, res) => {
const userId = req.user.id;
const events = await Event.find({
$or: [
{ hostUserId: userId },
{ coHosts: userId }
]
}).lean();
const eventIds = events.map(e => e._id);
const logs = await EventParticipationLog.find({ eventId: { $in: eventIds } }).lean();
const users = await User.find({ _id: { $in: logs.map(l => l.userId) } }).lean();
const userMap = {};
users.forEach(u => { userMap[u._id.toString()] = u; });
const eventParticipantsMap = {};
logs.forEach(log => {
if (!eventParticipantsMap[log.eventId]) eventParticipantsMap[log.eventId] = [];
eventParticipantsMap[log.eventId].push({
userId: log.userId,
name: userMap[log.userId.toString()]?.name,
email: userMap[log.userId.toString()]?.email,
phone: userMap[log.userId.toString()]?.phone,
status: log.status,
paymentType: log.paymentType,
paymentStatus: log.paymentStatus,
attendanceTimestamp: log.attendanceTimestamp,
timestamp: log.timestamp,
});
});
const eventsWithParticipants = events.map(event => ({
...event,
participants: eventParticipantsMap[event._id] || []
}));
return res.json(eventsWithParticipants);
});
// Get participants for a given event (host only)
const getEventParticipants = asyncHandler(async (req, res) => {
const userId = req.user.id;
const eventId = req.params.id;
const event = await Event.findOne({ _id: eventId, hostUserId: userId });
if (!event)
return res
.status(404)
.json({ error: 'Event not found or not owned by user.' });
// Get detailed participant information from EventParticipationLog
const logs = await EventParticipationLog.find({ eventId }).populate(
'userId',
'name email phone',
);
const participants = logs.map((log) => ({
userId: log.userId._id,
name: log.userId.name,
email: log.userId.email,
phone: log.userId.phone,
status: log.status,
paymentType: log.paymentType,
paymentStatus: log.paymentStatus,
attendanceTimestamp: log.attendanceTimestamp,
timestamp: log.timestamp,
}));
return res.json({ participants });
});
module.exports = {
getHostDashboard,
createEvent,
updateEvent,
deleteEvent,
getMyEvents,
getEventParticipants,
};
|