feat: Add instructor capabilities to update, delete, and submit courses for review.

This commit is contained in:
JakkrapartXD 2026-01-16 17:36:32 +07:00
parent 38648581ec
commit 2e536ad193
3 changed files with 268 additions and 16 deletions

View file

@ -5,8 +5,8 @@ import { logger } from '../config/logger';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
import jwt from 'jsonwebtoken';
import {
createCourses,
CreateCourseInput,
UpdateCourseInput,
createCourseResponse,
GetMyCourseResponse,
ListMyCourseResponse,
@ -15,7 +15,11 @@ import {
removeinstructorCourse,
removeinstructorCourseResponse,
setprimaryCourseInstructor,
setprimaryCourseInstructorResponse
setprimaryCourseInstructorResponse,
submitCourseResponse,
listinstructorCourseResponse,
sendCourseForReview,
getmyCourse
} from "../types/CoursesInstructor.types";
export class CoursesInstructorService {
@ -73,15 +77,15 @@ export class CoursesInstructorService {
}
}
static async getmyCourse(token: string, courseId: number): Promise<GetMyCourseResponse> {
static async getmyCourse(getmyCourse: getmyCourse): Promise<GetMyCourseResponse> {
try {
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; type: string };
const decoded = jwt.verify(getmyCourse.token, config.jwt.secret) as { id: number; type: string };
// Check if user is instructor of this course
const courseInstructor = await prisma.courseInstructor.findFirst({
where: {
user_id: decoded.id,
course_id: courseId
course_id: getmyCourse.course_id
},
include: {
course: {
@ -116,4 +120,169 @@ export class CoursesInstructorService {
throw error;
}
}
static async updateCourse(token: string, courseId: number, courseData: UpdateCourseInput): Promise<createCourseResponse> {
try {
const courseInstructorId = await this.validateCourseInstructor(token, courseId);
const course = await prisma.course.update({
where: {
id: courseInstructorId.user_id
},
data: courseData
});
return {
code: 200,
message: 'Course updated successfully',
data: course
};
} catch (error) {
logger.error('Failed to update course', { error });
throw error;
}
}
static async deleteCourse(token: string, courseId: number): Promise<createCourseResponse> {
try {
const courseInstructorId = await this.validateCourseInstructor(token, courseId);
if (!courseInstructorId.is_primary) {
throw new ForbiddenError('You have no permission to delete this course');
}
const course = await prisma.course.delete({
where: {
id: courseInstructorId.user_id
}
});
return {
code: 200,
message: 'Course deleted successfully',
data: course
};
} catch (error) {
logger.error('Failed to delete course', { error });
throw error;
}
}
static async sendCourseForReview(sendCourseForReview: sendCourseForReview): Promise<submitCourseResponse> {
try {
const decoded = jwt.verify(sendCourseForReview.token, config.jwt.secret) as { id: number; type: string };
await prisma.courseApproval.create({
data: {
course_id: sendCourseForReview.course_id,
submitted_by: decoded.id,
}
});
return {
code: 200,
message: 'Course sent for review successfully',
};
} catch (error) {
logger.error('Failed to send course for review', { error });
throw error;
}
}
async addInstructorToCourse(addinstructorCourse: addinstructorCourse): Promise<addinstructorCourseResponse> {
try {
const decoded = jwt.verify(addinstructorCourse.token, config.jwt.secret) as { id: number; type: string };
await prisma.courseInstructor.create({
data: {
course_id: addinstructorCourse.course_id,
user_id: decoded.id,
}
});
return {
code: 200,
message: 'Instructor added to course successfully',
};
} catch (error) {
logger.error('Failed to add instructor to course', { error });
throw error;
}
}
async removeInstructorFromCourse(removeinstructorCourse: removeinstructorCourse): Promise<removeinstructorCourseResponse> {
try {
const decoded = jwt.verify(removeinstructorCourse.token, config.jwt.secret) as { id: number; type: string };
await prisma.courseInstructor.delete({
where: {
course_id_user_id: {
course_id: removeinstructorCourse.course_id,
user_id: removeinstructorCourse.user_id,
},
}
});
return {
code: 200,
message: 'Instructor removed from course successfully',
};
} catch (error) {
logger.error('Failed to remove instructor from course', { error });
throw error;
}
}
async listInstructorsOfCourse(setprimaryCourseInstructor: setprimaryCourseInstructor): Promise<listinstructorCourseResponse> {
try {
const decoded = jwt.verify(setprimaryCourseInstructor.token, config.jwt.secret) as { id: number; type: string };
const courseInstructors = await prisma.courseInstructor.findMany({
where: {
course_id: setprimaryCourseInstructor.course_id,
},
include: {
user: true,
}
});
return {
code: 200,
message: 'Instructors retrieved successfully',
data: courseInstructors,
};
} catch (error) {
logger.error('Failed to retrieve instructors of course', { error });
throw error;
}
}
async setPrimaryInstructor(setprimaryCourseInstructor: setprimaryCourseInstructor): Promise<setprimaryCourseInstructorResponse> {
try {
const decoded = jwt.verify(setprimaryCourseInstructor.token, config.jwt.secret) as { id: number; type: string };
await prisma.courseInstructor.update({
where: {
course_id_user_id: {
course_id: setprimaryCourseInstructor.course_id,
user_id: setprimaryCourseInstructor.user_id,
},
},
data: {
is_primary: true,
}
});
return {
code: 200,
message: 'Primary instructor set successfully',
};
} catch (error) {
logger.error('Failed to set primary instructor', { error });
throw error;
}
}
private static async validateCourseInstructor(token: string, courseId: number): Promise<{ user_id: number; is_primary: boolean }> {
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; type: string };
const courseInstructor = await prisma.courseInstructor.findFirst({
where: {
user_id: decoded.id,
course_id: courseId
}
});
if (!courseInstructor) {
throw new ForbiddenError('You are not an instructor of this course');
} else return { user_id: courseInstructor.user_id, is_primary: courseInstructor.is_primary };
}
}