11/* eslint-disable class-methods-use-this */
2- import mongoose , { ClientSession , Schema , startSession } from "mongoose" ;
2+ import { ClientSession , Schema , startSession } from "mongoose" ;
33import { PDFDocument } from "pdf-lib" ;
44import MgCourseModule , {
55 CourseModule ,
@@ -10,6 +10,7 @@ import CoursePageModel, {
1010import MgCourseUnit , { CourseUnit } from "../../models/courseunit.mgmodel" ;
1111import {
1212 CourseModuleDTO ,
13+ CourseModuleLeanDTO ,
1314 CreateCourseModuleDTO ,
1415 ModuleStatus ,
1516 UpdateCourseModuleDTO ,
@@ -276,54 +277,33 @@ class CourseModuleService implements ICourseModuleService {
276277 /**
277278 * Publish a module (Draft → Published or Unpublished → Published).
278279 */
279- async publishCourseModule (
280- courseUnitId : string ,
281- moduleId : string ,
282- ) : Promise < CourseModuleDTO > {
283- return this . changeStatus ( courseUnitId , moduleId , ModuleStatus . Published ) ;
280+ async publishCourseModule ( moduleId : string ) : Promise < CourseModuleLeanDTO > {
281+ return this . changeStatus ( moduleId , ModuleStatus . Published ) ;
284282 }
285283
286284 /**
287285 * Unpublish a module (Published → Unpublished).
288286 */
289- async unpublishCourseModule (
290- courseUnitId : string ,
291- moduleId : string ,
292- ) : Promise < CourseModuleDTO > {
293- return this . changeStatus ( courseUnitId , moduleId , ModuleStatus . Unpublished ) ;
287+ async unpublishCourseModule ( moduleId : string ) : Promise < CourseModuleLeanDTO > {
288+ return this . changeStatus ( moduleId , ModuleStatus . Unpublished ) ;
294289 }
295290
296291 /**
297292 * Internal helper that validates state transitions.
298293 */
299294 private async changeStatus (
300- courseUnitId : string ,
301295 moduleId : string ,
302296 newStatus : ModuleStatus ,
303- ) : Promise < CourseModuleDTO > {
297+ ) : Promise < CourseModuleLeanDTO > {
304298 const session = await startSession ( ) ;
305299 session . startTransaction ( ) ;
306300 try {
307- const courseUnit = await MgCourseUnit . findById ( courseUnitId ) . session (
308- session ,
309- ) ;
310- const newModuleId = new mongoose . Types . ObjectId ( moduleId ) ;
311-
312- const belongsToUnit = courseUnit ?. modules . some ( ( m ) =>
313- ( m as unknown as mongoose . Types . ObjectId ) . equals ( newModuleId ) ,
314- ) ;
315- if ( ! courseUnit || ! belongsToUnit ) {
316- throw new Error ( "Module not found in specified unit" ) ;
317- }
318-
319301 const module = await MgCourseModule . findById ( moduleId ) . session ( session ) ;
320302 if ( ! module ) {
321303 throw new Error ( "Module not found" ) ;
322304 }
323305
324- if (
325- ! validTransitions [ module . status as ModuleStatus ] . includes ( newStatus )
326- ) {
306+ if ( ! validTransitions [ module . status ] . includes ( newStatus ) ) {
327307 const msg = `Cannot transition from "${ module . status } " to "${ newStatus } "` ;
328308 throw new Error ( msg ) ;
329309 }
@@ -332,11 +312,7 @@ class CourseModuleService implements ICourseModuleService {
332312 await module . save ( { session } ) ;
333313 await session . commitTransaction ( ) ;
334314
335- return {
336- id : module . id ,
337- title : module . title ,
338- status : module . status as ModuleStatus ,
339- } as CourseModuleDTO ;
315+ return module . toObject ( ) ;
340316 } catch ( error ) {
341317 await session . abortTransaction ( ) ;
342318 Logger . error (
0 commit comments