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 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const Event = require('../Models/Event');
const User = require('../Models/User');
const Certificate = require('../Models/Certificate');
const EventParticipationLog = require('../Models/EventParticipationLog');
const SearchAnalytics = require('../Models/SearchAnalytics');
// Advanced event search (filter, sort, paginate)
const advancedEventSearch = async (req, res) => {
try {
const { q, tags, type, startDate, endDate, sort = 'date', order = 'desc', page = 1, limit = 10 } = req.query;
// Input validation
const validSortFields = ['date', 'title', 'type']; // Add other valid fields as needed
const validOrderValues = ['asc', 'desc'];
const filter = {};
if (q && typeof q === 'string') filter.title = { $regex: q, $options: 'i' };
if (tags) {
const tagsArray = Array.isArray(tags) ? tags : tags.split(',');
filter.tags = { $in: tagsArray.map(tag => tag.trim()).filter(tag => tag) };
}
if (type && typeof type === 'string') filter.type = type;
if (startDate || endDate) {
filter['schedule.start'] = {};
if (startDate && !isNaN(Date.parse(startDate))) filter['schedule.start'].$gte = new Date(startDate);
if (endDate && !isNaN(Date.parse(endDate))) filter['schedule.start'].$lte = new Date(endDate);
}
const sortField = validSortFields.includes(sort) ? (sort === 'date' ? 'schedule.start' : sort) : 'schedule.start';
const sortOrder = validOrderValues.includes(order) ? (order === 'asc' ? 1 : -1) : -1;
const parsedPage = parseInt(page);
const parsedLimit = parseInt(limit);
const validatedPage = Number.isInteger(Number(page)) && Number(page) > 0 ? Number(page) : 1;
const validatedLimit = Number.isInteger(Number(limit)) && Number(limit) > 0 ? Number(limit) : 10;
const skip = (validatedPage - 1) * validatedLimit;
const total = await Event.countDocuments(filter);
const events = await Event.find(filter)
.sort({ [sortField]: sortOrder })
.skip(skip)
.limit(validatedLimit);
res.json({
total,
page: validatedPage,
limit: validatedLimit,
events
});
} catch (err) {
res.status(500).json({ error: 'Error searching events.' });
}
};
// User analytics (participation stats)
const getUserAnalytics = async (req, res) => {
try {
const userId = req.params.userId;
const user = await User.findById(userId);
if (!user) return res.status(404).json({ error: 'User not found.' });
const hosted = await Event.countDocuments({ hostUserId: userId });
const cohosted = await Event.countDocuments({ coHosts: userId });
const attended = await EventParticipationLog.countDocuments({ userId, status: 'attended' });
const registered = await EventParticipationLog.countDocuments({ userId, status: 'registered' });
const waitlisted = await EventParticipationLog.countDocuments({ userId, status: 'waitlisted' });
const certificates = await Certificate.countDocuments({ userId });
res.json({
user: { id: user._id, name: user.name, email: user.email },
hosted,
cohosted,
attended,
registered,
waitlisted,
certificates
});
} catch (err) {
res.status(500).json({ error: 'Error fetching user analytics.' });
}
};
// Platform insights (global stats)
const getPlatformInsights = async (req, res) => {
try {
const totalUsers = await User.countDocuments();
const totalEvents = await Event.countDocuments();
const totalCertificates = await Certificate.countDocuments();
const totalParticipations = await EventParticipationLog.countDocuments();
const totalAttended = await EventParticipationLog.countDocuments({ status: 'attended' });
const totalRegistered = await EventParticipationLog.countDocuments({ status: 'registered' });
const totalWaitlisted = await EventParticipationLog.countDocuments({ status: 'waitlisted' });
res.json({
totalUsers,
totalEvents,
totalCertificates,
totalParticipations,
totalAttended,
totalRegistered,
totalWaitlisted
});
} catch (err) {
res.status(500).json({ error: 'Error fetching platform insights.' });
}
};
// Search analytics (admin only)
const getSearchAnalytics = async (req, res) => {
try {
const { page = 1, limit = 20 } = req.query;
const skip = (parseInt(page) - 1) * parseInt(limit);
const total = await SearchAnalytics.countDocuments();
const analytics = await SearchAnalytics.find()
.sort({ timestamp: -1 })
.skip(skip)
.limit(parseInt(limit))
.populate('userId', 'name email');
res.json({
total,
page: parseInt(page),
limit: parseInt(limit),
analytics
});
} catch (err) {
res.status(500).json({ error: 'Error fetching search analytics.' });
}
};
// Advanced event analytics (demographics, engagement, certificates, top participants)
const getAdvancedEventAnalytics = async (req, res) => {
try {
const eventId = req.params.eventId;
const event = await Event.findById(eventId);
if (!event) return res.status(404).json({ error: 'Event not found.' });
const demographics = await EventParticipationLog.aggregate([
{ $match: { eventId } },
{ $group: { _id: '$userId', count: { $sum: 1 } } }
]);
const engagement = await EventParticipationLog.countDocuments({ eventId });
const certificatesIssued = await Certificate.countDocuments({ eventId });
const topParticipants = await EventParticipationLog.aggregate([
{ $match: { eventId } },
{ $group: { _id: '$userId', participationCount: { $sum: 1 } } },
{ $sort: { participationCount: -1 } },
{ $limit: 5 }
]);
res.json({
event: { id: event._id, title: event.title },
demographics,
engagement,
certificatesIssued,
topParticipants
});
} catch (err) {
res.status(500).json({ error: 'Error fetching advanced event analytics.' });
}
};
// User activity timeline (participation over time)
const getUserActivityTimeline = async (req, res) => {
try {
const userId = req.params.userId;
const logs = await EventParticipationLog.find({ userId }).sort({ timestamp: 1 });
if (!logs.length) return res.status(404).json({ error: 'No activity found for the user.' });
const timeline = logs.map(log => ({
eventId: log.eventId,
status: log.status,
timestamp: log.timestamp
}));
res.json({ userId, timeline });
} catch (err) {
res.status(500).json({ error: 'Error fetching user activity timeline.' });
}
};
// Platform growth trends (new users/events/certificates per month)
const getGrowthTrends = async (req, res) => {
try {
const userGrowth = await User.aggregate([
{ $group: { _id: { year: { $year: '$createdAt' }, month: { $month: '$createdAt' } }, count: { $sum: 1 } } },
{ $sort: { '_id.year': 1, '_id.month': 1 } }
]);
const eventGrowth = await Event.aggregate([
{ $group: { _id: { year: { $year: '$createdAt' }, month: { $month: '$createdAt' } }, count: { $sum: 1 } } },
{ $sort: { '_id.year': 1, '_id.month': 1 } }
]);
const certificateGrowth = await Certificate.aggregate([
{ $group: { _id: { year: { $year: '$createdAt' }, month: { $month: '$createdAt' } }, count: { $sum: 1 } } },
{ $sort: { '_id.year': 1, '_id.month': 1 } }
]);
res.json({
userGrowth,
eventGrowth,
certificateGrowth
});
} catch (err) {
res.status(500).json({ error: 'Error fetching growth trends.' });
}
};
// Zero-result searches (admin only)
const getZeroResultSearches = async (req, res) => {
try {
// Check if the user is an admin (placeholder logic, replace with actual authentication/authorization)
if (!req.user || !req.user.isAdmin) {
return res.status(403).json({ error: 'Access denied. Admins only.' });
}
const { page = 1, limit = 20 } = req.query;
const skip = (parseInt(page) - 1) * parseInt(limit);
// Query for zero-result searches
const filter = { resultsCount: 0 }; // Assuming `resultsCount` field tracks the number of results
const total = await SearchAnalytics.countDocuments(filter);
const zeroResultSearches = await SearchAnalytics.find(filter)
.sort({ timestamp: -1 })
.skip(skip)
.limit(parseInt(limit))
.populate('userId', 'name email'); // Populate user details if needed
res.json({
total,
page: parseInt(page),
limit: parseInt(limit),
zeroResultSearches
});
} catch (err) {
res.status(500).json({ error: 'Error fetching zero-result searches.' });
}
};
module.exports = {
advancedEventSearch,
getUserAnalytics,
getPlatformInsights,
getSearchAnalytics,
getAdvancedEventAnalytics,
getUserActivityTimeline,
getGrowthTrends,
getZeroResultSearches
}; |