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 | 1x 1x 1x 1x 1x 1x | const Institution = require('../Models/Institution');
const User = require('../Models/User');
const Event = require('../Models/Event');
const EventParticipationLog = require('../Models/EventParticipationLog');
const Certificate = require('../Models/Certificate');
// Create a new institution (admin only)
async function createInstitution(req, res) {
try {
const { name, type, location, emailDomain } = req.body;
const institution = await Institution.create({ name, type, location, emailDomain, isVerified: false });
res.status(201).json(institution);
} catch (err) {
res.status(500).json({ error: 'Error creating institution.' });
}
}
// Get all institutions (admin only)
async function getInstitutions(req, res) {
try {
const institutions = await Institution.find();
res.json(institutions);
} catch (err) {
res.status(500).json({ error: 'Error fetching institutions.' });
}
}
// Get institution by ID (admin or self)
async function getInstitutionById(req, res) {
try {
const institution = await Institution.findById(req.params.id);
if (!institution) return res.status(404).json({ error: 'Institution not found.' });
res.json(institution);
} catch (err) {
res.status(500).json({ error: 'Error fetching institution.' });
}
}
// Update institution (admin only)
async function updateInstitution(req, res) {
try {
const institution = await Institution.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!institution) return res.status(404).json({ error: 'Institution not found.' });
res.json(institution);
} catch (err) {
res.status(500).json({ error: 'Error updating institution.' });
}
}
// Delete institution (admin only)
async function deleteInstitution(req, res) {
try {
const institution = await Institution.findByIdAndDelete(req.params.id);
if (!institution) return res.status(404).json({ error: 'Institution not found.' });
res.json({ message: 'Institution deleted.' });
} catch (err) {
res.status(500).json({ error: 'Error deleting institution.' });
}
}
// Request institution verification (student)
async function requestInstitutionVerification(req, res) {
try {
const institution = await Institution.findById(req.params.id);
if (!institution) return res.status(404).json({ error: 'Institution not found.' });
institution.verificationRequested = true;
await institution.save();
res.json({ message: 'Verification request submitted.' });
} catch (err) {
res.status(500).json({ error: 'Error requesting verification.' });
}
}
// Approve institution verification (admin only)
async function approveInstitutionVerification(req, res) {
try {
const institution = await Institution.findById(req.params.id);
if (!institution) return res.status(404).json({ error: 'Institution not found.' });
institution.isVerified = true;
institution.verificationRequested = false;
await institution.save();
res.json({ message: 'Institution verified.' });
} catch (err) {
res.status(500).json({ error: 'Error approving verification.' });
}
}
// Reject institution verification (admin only)
async function rejectInstitutionVerification(req, res) {
try {
const institution = await Institution.findById(req.params.id);
if (!institution) return res.status(404).json({ error: 'Institution not found.' });
institution.verificationRequested = false;
await institution.save();
res.json({ message: 'Institution verification rejected.' });
} catch (err) {
res.status(500).json({ error: 'Error rejecting verification.' });
}
}
// Get institution analytics (institution or admin)
async function getInstitutionAnalytics(req, res) {
try {
const institutionId = req.params.id;
const studentCount = await User.countDocuments({ institutionId });
const eventCount = await Event.countDocuments({ institutionId });
// Optionally, add more analytics (certificates, achievements, etc.)
res.json({ studentCount, eventCount });
} catch (err) {
res.status(500).json({ error: 'Error fetching analytics.' });
}
}
// Get institution dashboard (institution or admin)
async function getInstitutionDashboard(req, res) {
try {
const institutionId = req.params.id;
// Total students
const studentCount = await User.countDocuments({ institutionId });
// Total events
const eventCount = await Event.countDocuments({ institutionId });
// Total event participations
const participationLogs = await EventParticipationLog.find({ eventId: { $in: (await Event.find({ institutionId }, '_id')).map(e => e._id) } });
const participationCount = participationLogs.length;
// Participation rate
const participationRate = studentCount > 0 ? participationCount / studentCount : 0;
// Certificates issued
const certificateCount = await Certificate.countDocuments({ eventId: { $in: (await Event.find({ institutionId }, '_id')).map(e => e._id) } });
// Active students (participated in at least one event)
const activeStudentIds = [...new Set(participationLogs.map(log => log.userId.toString()))];
const activeStudents = activeStudentIds.length;
// Inactive students
const inactiveStudents = studentCount - activeStudents;
// Recent/upcoming events
const now = new Date();
const recentEvents = await Event.find({ institutionId }).sort({ 'schedule.start': -1 }).limit(5);
// Event participation breakdown
const eventBreakdown = await Promise.all(
recentEvents.map(async (event) => {
const logs = participationLogs.filter(log => log.eventId.toString() === event._id.toString());
return {
id: event._id,
title: event.title,
registered: logs.length,
attended: logs.filter(l => l.status === 'attended').length,
waitlisted: logs.filter(l => l.status === 'waitlisted').length
};
})
);
// Top events (by participation)
const topEvents = eventBreakdown.sort((a, b) => b.registered - a.registered).slice(0, 3);
// Top students (by participations/certificates)
const studentParticipationMap = {};
participationLogs.forEach(log => {
const id = log.userId.toString();
studentParticipationMap[id] = (studentParticipationMap[id] || 0) + 1;
});
const topStudents = Object.entries(studentParticipationMap)
.sort((a, b) => b[1] - a[1])
.slice(0, 3)
.map(([id, participations]) => ({
id,
participations
}));
// Event types breakdown
const events = await Event.find({ institutionId });
const eventTypes = {};
events.forEach(event => {
eventTypes[event.type] = (eventTypes[event.type] || 0) + 1;
});
// Pending verifications (students/events)
const pendingStudentVerifications = await User.countDocuments({ institutionId, isVerified: false });
const pendingEventVerifications = await Event.countDocuments({ institutionId, verificationStatus: { $ne: 'approved' } });
// Feedback/ratings (if available)
// Placeholder: If you have a Feedback model, aggregate here
const feedback = {
averageRating: null,
recentComments: []
};
res.json({
studentCount,
eventCount,
participationCount,
participationRate,
certificateCount,
activeStudents,
inactiveStudents,
recentEvents,
eventBreakdown,
topEvents,
topStudents,
eventTypes,
pendingVerifications: {
students: pendingStudentVerifications,
events: pendingEventVerifications
},
feedback
});
} catch (err) {
res.status(500).json({ error: 'Error fetching dashboard.' });
}
}
module.exports = {
createInstitution,
getInstitutions,
getInstitutionById,
updateInstitution,
deleteInstitution,
requestInstitutionVerification,
approveInstitutionVerification,
rejectInstitutionVerification,
getInstitutionAnalytics,
getInstitutionDashboard
}; |