Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x | const Event = require('../Models/Event');
const User = require('../Models/User');
const EventParticipationLog = require('../Models/EventParticipationLog');
// Host dashboard: list events and detailed analytics
async function getHostDashboard(req, res) {
try {
const userId = req.user.id;
const events = await Event.find({ hostUserId: userId });
const totalEvents = events.length;
const upcomingEvents = events.filter(e => e.schedule && e.schedule.start > new Date());
// Get detailed analytics for each event
const eventsWithAnalytics = await Promise.all(events.map(async (event) => {
const logs = await EventParticipationLog.find({ eventId: event._id });
const totalRegistered = logs.length;
const totalAttended = logs.filter(l => l.status === 'attended').length;
const totalWaitlisted = logs.filter(l => l.status === 'waitlisted').length;
const totalPaid = logs.filter(l => l.paymentType === 'paid').length;
const totalFree = logs.filter(l => l.paymentType === 'free').length;
return {
...event.toObject(),
analytics: {
totalRegistered,
totalAttended,
totalWaitlisted,
totalPaid,
totalFree,
attendanceRate: totalRegistered > 0 ? (totalAttended / totalRegistered * 100).toFixed(2) : 0
}
};
}));
const totalParticipants = eventsWithAnalytics.reduce((sum, e) => sum + e.analytics.totalRegistered, 0);
const totalAttended = eventsWithAnalytics.reduce((sum, e) => sum + e.analytics.totalAttended, 0);
return res.json({
totalEvents,
totalParticipants,
totalAttended,
upcomingEvents: upcomingEvents.length,
events: eventsWithAnalytics
});
} catch (err) {
return res.status(500).json({ error: 'Server error fetching host dashboard.' });
}
}
// Create a new event (host only)
async function createEvent(req, res) {
try {
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);
} catch (err) {
return res.status(500).json({ error: 'Server error creating event.' });
}
}
// Update an event (host only)
async function updateEvent(req, res) {
try {
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.' });
Object.assign(event, req.body);
await event.save();
return res.json(event);
} catch (err) {
return res.status(500).json({ error: 'Server error updating event.' });
}
}
// Delete an event (host only)
async function deleteEvent(req, res) {
try {
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.' });
} catch (err) {
return res.status(500).json({ error: 'Server error deleting event.' });
}
}
// List all events hosted by the current user
async function getMyEvents(req, res) {
try {
const userId = req.user.id;
const events = await Event.find({ hostUserId: userId });
return res.json(events);
} catch (err) {
return res.status(500).json({ error: 'Server error fetching events.' });
}
}
// Get participants for a given event (host only)
async function getEventParticipants(req, res) {
try {
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 });
} catch (err) {
return res.status(500).json({ error: 'Server error fetching participants.' });
}
}
module.exports = {
getHostDashboard,
createEvent,
updateEvent,
deleteEvent,
getMyEvents,
getEventParticipants
}; |