feat: Add instructor capabilities to update, delete, and submit courses for review.
This commit is contained in:
parent
38648581ec
commit
2e536ad193
3 changed files with 268 additions and 16 deletions
|
|
@ -11,7 +11,11 @@ import {
|
||||||
removeinstructorCourse,
|
removeinstructorCourse,
|
||||||
removeinstructorCourseResponse,
|
removeinstructorCourseResponse,
|
||||||
setprimaryCourseInstructor,
|
setprimaryCourseInstructor,
|
||||||
setprimaryCourseInstructorResponse
|
setprimaryCourseInstructorResponse,
|
||||||
|
UpdateMyCourse,
|
||||||
|
UpdateMyCourseResponse,
|
||||||
|
DeleteMyCourseResponse,
|
||||||
|
submitCourseResponse
|
||||||
} from '../types/CoursesInstructor.types';
|
} from '../types/CoursesInstructor.types';
|
||||||
import { CreateCourseValidator } from "../validators/CoursesInstructor.validator";
|
import { CreateCourseValidator } from "../validators/CoursesInstructor.validator";
|
||||||
|
|
||||||
|
|
@ -45,7 +49,20 @@ export class CoursesInstructorController {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw new ValidationError('No token provided');
|
throw new ValidationError('No token provided');
|
||||||
}
|
}
|
||||||
return await CoursesInstructorService.getmyCourse(token, courseId);
|
return await CoursesInstructorService.getmyCourse({ token, course_id: courseId });
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put('{courseId}')
|
||||||
|
@Security('jwt', ['instructor'])
|
||||||
|
@SuccessResponse('200', 'Course updated successfully')
|
||||||
|
@Response('401', 'Invalid or expired token')
|
||||||
|
@Response('404', 'Course not found')
|
||||||
|
public async updateCourse(@Request() request: any, @Path() courseId: number, @Body() body: UpdateMyCourse): Promise<UpdateMyCourseResponse> {
|
||||||
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||||
|
if (!token) {
|
||||||
|
throw new ValidationError('No token provided');
|
||||||
|
}
|
||||||
|
return await CoursesInstructorService.updateCourse(token, courseId, body.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('')
|
@Post('')
|
||||||
|
|
@ -54,12 +71,38 @@ export class CoursesInstructorController {
|
||||||
@Response('400', 'Validation error')
|
@Response('400', 'Validation error')
|
||||||
@Response('500', 'Internal server error')
|
@Response('500', 'Internal server error')
|
||||||
public async createCourse(@Body() body: createCourses, @Request() request: any): Promise<createCourseResponse> {
|
public async createCourse(@Body() body: createCourses, @Request() request: any): Promise<createCourseResponse> {
|
||||||
const token = request.headers.authorization?.replace('Bearer ', '');
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||||
const decoded = jwt.verify(token, config.jwt.secret) as { id: number };
|
const decoded = jwt.verify(token, config.jwt.secret) as { id: number };
|
||||||
const { error, value } = CreateCourseValidator.validate(body.data);
|
const { error, value } = CreateCourseValidator.validate(body.data);
|
||||||
if (error) throw new ValidationError(error.details[0].message);
|
if (error) throw new ValidationError(error.details[0].message);
|
||||||
const course = await CoursesInstructorService.createCourse(value, decoded.id);
|
const course = await CoursesInstructorService.createCourse(value, decoded.id);
|
||||||
return course;
|
return course;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('{courseId}')
|
||||||
|
@Security('jwt', ['instructor'])
|
||||||
|
@SuccessResponse('200', 'Course deleted successfully')
|
||||||
|
@Response('401', 'Invalid or expired token')
|
||||||
|
@Response('404', 'Course not found')
|
||||||
|
public async deleteCourse(@Request() request: any, @Path() courseId: number): Promise<DeleteMyCourseResponse> {
|
||||||
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||||
|
if (!token) {
|
||||||
|
throw new ValidationError('No token provided');
|
||||||
|
}
|
||||||
|
return await CoursesInstructorService.deleteCourse(token, courseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('send-review/{courseId}')
|
||||||
|
@Security('jwt', ['instructor'])
|
||||||
|
@SuccessResponse('200', 'Course submitted successfully')
|
||||||
|
@Response('401', 'Invalid or expired token')
|
||||||
|
@Response('404', 'Course not found')
|
||||||
|
public async submitCourse(@Request() request: any, @Path() courseId: number): Promise<submitCourseResponse> {
|
||||||
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||||
|
if (!token) {
|
||||||
|
throw new ValidationError('No token provided');
|
||||||
|
}
|
||||||
|
return await CoursesInstructorService.sendCourseForReview({ token, course_id: courseId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@ import { logger } from '../config/logger';
|
||||||
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
|
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import {
|
import {
|
||||||
createCourses,
|
|
||||||
CreateCourseInput,
|
CreateCourseInput,
|
||||||
|
UpdateCourseInput,
|
||||||
createCourseResponse,
|
createCourseResponse,
|
||||||
GetMyCourseResponse,
|
GetMyCourseResponse,
|
||||||
ListMyCourseResponse,
|
ListMyCourseResponse,
|
||||||
|
|
@ -15,7 +15,11 @@ import {
|
||||||
removeinstructorCourse,
|
removeinstructorCourse,
|
||||||
removeinstructorCourseResponse,
|
removeinstructorCourseResponse,
|
||||||
setprimaryCourseInstructor,
|
setprimaryCourseInstructor,
|
||||||
setprimaryCourseInstructorResponse
|
setprimaryCourseInstructorResponse,
|
||||||
|
submitCourseResponse,
|
||||||
|
listinstructorCourseResponse,
|
||||||
|
sendCourseForReview,
|
||||||
|
getmyCourse
|
||||||
} from "../types/CoursesInstructor.types";
|
} from "../types/CoursesInstructor.types";
|
||||||
|
|
||||||
export class CoursesInstructorService {
|
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 {
|
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
|
// Check if user is instructor of this course
|
||||||
const courseInstructor = await prisma.courseInstructor.findFirst({
|
const courseInstructor = await prisma.courseInstructor.findFirst({
|
||||||
where: {
|
where: {
|
||||||
user_id: decoded.id,
|
user_id: decoded.id,
|
||||||
course_id: courseId
|
course_id: getmyCourse.course_id
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
course: {
|
course: {
|
||||||
|
|
@ -116,4 +120,169 @@ export class CoursesInstructorService {
|
||||||
throw error;
|
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 };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,30 @@ export interface GetMyCourseResponse {
|
||||||
data: Course;
|
data: Course;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface getmyCourse {
|
||||||
|
token: string;
|
||||||
|
course_id: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateCourseInput {
|
||||||
|
category_id?: number;
|
||||||
|
title?: {
|
||||||
|
th: string;
|
||||||
|
en: string;
|
||||||
|
};
|
||||||
|
slug?: string;
|
||||||
|
description?: {
|
||||||
|
th: string;
|
||||||
|
en: string;
|
||||||
|
};
|
||||||
|
thumbnail_url?: string;
|
||||||
|
price?: number;
|
||||||
|
is_free?: boolean;
|
||||||
|
have_certificate?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface UpdateMyCourse {
|
export interface UpdateMyCourse {
|
||||||
data: Prisma.CourseUpdateInput;
|
data: UpdateCourseInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateMyCourseResponse {
|
export interface UpdateMyCourseResponse {
|
||||||
|
|
@ -78,7 +100,7 @@ export interface listCourseinstructorResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface addinstructorCourse {
|
export interface addinstructorCourse {
|
||||||
user_id: number;
|
token: string;
|
||||||
course_id: number;
|
course_id: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,7 +109,18 @@ export interface addinstructorCourseResponse {
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface listinstructorCourseResponse {
|
||||||
|
code: number;
|
||||||
|
message: string;
|
||||||
|
data: {
|
||||||
|
user_id: number;
|
||||||
|
is_primary: boolean;
|
||||||
|
user: User;
|
||||||
|
}[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface removeinstructorCourse {
|
export interface removeinstructorCourse {
|
||||||
|
token: string;
|
||||||
user_id: number;
|
user_id: number;
|
||||||
course_id: number;
|
course_id: number;
|
||||||
}
|
}
|
||||||
|
|
@ -98,6 +131,7 @@ export interface removeinstructorCourseResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface setprimaryCourseInstructor {
|
export interface setprimaryCourseInstructor {
|
||||||
|
token: string;
|
||||||
user_id: number;
|
user_id: number;
|
||||||
course_id: number;
|
course_id: number;
|
||||||
}
|
}
|
||||||
|
|
@ -105,4 +139,10 @@ export interface setprimaryCourseInstructor {
|
||||||
export interface setprimaryCourseInstructorResponse {
|
export interface setprimaryCourseInstructorResponse {
|
||||||
code: number;
|
code: number;
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface sendCourseForReview {
|
||||||
|
token: string;
|
||||||
|
course_id: number;
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue