119 lines
4.2 KiB
TypeScript
119 lines
4.2 KiB
TypeScript
import { prisma } from '../config/database';
|
|
import { Prisma } from '@prisma/client';
|
|
import { config } from '../config';
|
|
import { logger } from '../config/logger';
|
|
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
|
|
import jwt from 'jsonwebtoken';
|
|
import {
|
|
createCourses,
|
|
CreateCourseInput,
|
|
createCourseResponse,
|
|
GetMyCourseResponse,
|
|
ListMyCourseResponse,
|
|
addinstructorCourse,
|
|
addinstructorCourseResponse,
|
|
removeinstructorCourse,
|
|
removeinstructorCourseResponse,
|
|
setprimaryCourseInstructor,
|
|
setprimaryCourseInstructorResponse
|
|
} from "../types/CoursesInstructor.types";
|
|
|
|
export class CoursesInstructorService {
|
|
static async createCourse(courseData: CreateCourseInput, userId: number): Promise<createCourseResponse> {
|
|
try {
|
|
// Map custom input to Prisma format
|
|
const courseCreated = await prisma.course.create({
|
|
data: {
|
|
category_id: courseData.category_id,
|
|
title: courseData.title,
|
|
slug: courseData.slug,
|
|
description: courseData.description,
|
|
thumbnail_url: courseData.thumbnail_url,
|
|
price: courseData.price || 0,
|
|
is_free: courseData.is_free ?? false,
|
|
have_certificate: courseData.have_certificate ?? false,
|
|
created_by: userId, // Required field from JWT
|
|
status: 'DRAFT' // Default status
|
|
}
|
|
});
|
|
return {
|
|
code: 201,
|
|
message: 'Course created successfully',
|
|
data: courseCreated
|
|
};
|
|
} catch (error) {
|
|
logger.error('Failed to create course', { error });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async listMyCourses(token: string): Promise<ListMyCourseResponse> {
|
|
try {
|
|
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; type: string };
|
|
const courseInstructors = await prisma.courseInstructor.findMany({
|
|
where: {
|
|
user_id: decoded.id
|
|
},
|
|
include: {
|
|
course: true
|
|
}
|
|
});
|
|
|
|
const courses = courseInstructors.map(ci => ci.course);
|
|
|
|
return {
|
|
code: 200,
|
|
message: 'Courses retrieved successfully',
|
|
data: courses,
|
|
total: courses.length
|
|
};
|
|
} catch (error) {
|
|
logger.error('Failed to retrieve courses', { error });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async getmyCourse(token: string, courseId: number): Promise<GetMyCourseResponse> {
|
|
try {
|
|
const decoded = jwt.verify(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
|
|
},
|
|
include: {
|
|
course: {
|
|
include: {
|
|
chapters: {
|
|
include: {
|
|
lessons: {
|
|
include: {
|
|
attachments: true,
|
|
progress: true,
|
|
quiz: true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if (!courseInstructor) {
|
|
throw new ForbiddenError('You are not an instructor of this course');
|
|
}
|
|
|
|
return {
|
|
code: 200,
|
|
message: 'Course retrieved successfully',
|
|
data: courseInstructor.course
|
|
};
|
|
} catch (error) {
|
|
logger.error('Failed to retrieve course', { error });
|
|
throw error;
|
|
}
|
|
}
|
|
}
|