Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/controllers/certificate.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,33 @@ export const getCertificateByCourse = async (req: Request, res: Response) => {
res.status(500).json({ message: 'Server error', error });
}
};

export const getCertificatesByInstructor = async (req: Request, res: Response) => {
try {
const instructorId = req.user?._id;

if (!instructorId) {
return res.status(401).json({ message: 'User not authenticated' });
}

// First, find all courses created by this instructor
const Course = mongoose.model('Course');
const instructorCourses = await Course.find({ authorId: instructorId }).select('_id');

if (instructorCourses.length === 0) {
return res.status(200).json([]);
}

const courseIds = instructorCourses.map(course => course._id);

// Then, find all certificates for these courses
const certificates = await Certificate.find({ course: { $in: courseIds } })
.populate('user', 'name email')
.populate('course', 'title authorId')
.sort({ createdAt: -1 });

res.status(200).json(certificates);
} catch (error) {
res.status(500).json({ message: 'Server error', error });
}
};
8 changes: 7 additions & 1 deletion src/routes/certificate.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import { getCertificateById, getCertificateByCourse, getAllCertificates, getCertificateByUser } from '../controllers/certificate.controller';
import { getCertificateById, getCertificateByCourse, getAllCertificates, getCertificateByUser, getCertificatesByInstructor } from '../controllers/certificate.controller';
import { isAuthenticated } from '../middlewares/auth/isAuthenticated';
import { authorizeRoles } from '../middlewares/auth/authorizeRoles';

const router = express.Router();

Expand All @@ -22,4 +23,9 @@ router.get('/course/:courseId', isAuthenticated, (req, res, next) => {
getCertificateByCourse(req, res).catch(next);
});

// Lấy tất cả certificate của các khóa học do instructor tạo
router.get('/instructor/courses', isAuthenticated, authorizeRoles('instructor'), (req, res, next) => {
getCertificatesByInstructor(req, res).catch(next);
});

export default router;