feat: Create course and automatically assign the creator as the primary instructor using a transaction.
This commit is contained in:
parent
a38389cc9f
commit
715d94fbf9
1 changed files with 30 additions and 15 deletions
|
|
@ -26,25 +26,40 @@ import {
|
||||||
export class CoursesInstructorService {
|
export class CoursesInstructorService {
|
||||||
static async createCourse(courseData: CreateCourseInput, userId: number): Promise<createCourseResponse> {
|
static async createCourse(courseData: CreateCourseInput, userId: number): Promise<createCourseResponse> {
|
||||||
try {
|
try {
|
||||||
// Map custom input to Prisma format
|
// Use transaction to create course and instructor together
|
||||||
const courseCreated = await prisma.course.create({
|
const result = await prisma.$transaction(async (tx) => {
|
||||||
data: {
|
// Create the course
|
||||||
category_id: courseData.category_id,
|
const courseCreated = await tx.course.create({
|
||||||
title: courseData.title,
|
data: {
|
||||||
slug: courseData.slug,
|
category_id: courseData.category_id,
|
||||||
description: courseData.description,
|
title: courseData.title,
|
||||||
thumbnail_url: courseData.thumbnail_url,
|
slug: courseData.slug,
|
||||||
price: courseData.price || 0,
|
description: courseData.description,
|
||||||
is_free: courseData.is_free ?? false,
|
thumbnail_url: courseData.thumbnail_url,
|
||||||
have_certificate: courseData.have_certificate ?? false,
|
price: courseData.price || 0,
|
||||||
created_by: userId, // Required field from JWT
|
is_free: courseData.is_free ?? false,
|
||||||
status: 'DRAFT' // Default status
|
have_certificate: courseData.have_certificate ?? false,
|
||||||
}
|
created_by: userId,
|
||||||
|
status: 'DRAFT'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add creator as primary instructor
|
||||||
|
await tx.courseInstructor.create({
|
||||||
|
data: {
|
||||||
|
course_id: courseCreated.id,
|
||||||
|
user_id: userId,
|
||||||
|
is_primary: true,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return courseCreated;
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: 201,
|
code: 201,
|
||||||
message: 'Course created successfully',
|
message: 'Course created successfully',
|
||||||
data: courseCreated
|
data: result
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Failed to create course', { error });
|
logger.error('Failed to create course', { error });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue