import { BodyParam , Post , Param , JsonController , UseBefore , Authorized , CurrentUser , ForbiddenError , NotFoundError } from 'routing-controllers' ; import passportJwtMiddleware from '../security/passportJwtMiddleware' ; import { IUser } from '../../../shared/models/IUser' ; import { IUnit } from '../../../shared/models/units/IUnit' ; import { ILectureModel , Lecture } from '../models/Lecture' ; import { IUnitModel , Unit } from '../models/units/Unit' ; import { Course , ICourseModel } from '../models/Course' ; import { ILecture } from '../../../shared/models/ILecture' ; import { ICourse } from '../../../shared/models/ICourse' ; import { IDuplicationResponse } from '../../../shared/models/IDuplicationResponse' ; import { extractSingleMongoId } from '../utilities/ExtractMongoId' ; import { errorCodes } from '../config/errorCodes' ; @ JsonController ( '/duplicate' ) @ UseBefore ( passportJwtMiddleware ) @ Authorized ( [ 'teacher' , 'admin' ] ) export class DuplicationController { private static async assertUserDuplicationAuthorization ( user , course ) { if ( ! course . checkPrivileges ( user ) . userCanEditCourse ) { throw new ForbiddenError ( ) ; } } private static extractCommonResponse ( duplicate ) { return { _id : extractSingleMongoId ( duplicate ) } ; } api apiName apiGroup apiPermission apiPermission apiParam apiParam apiSuccess apiSuccessExample apiError apiError @ Post ( '/course/:id' ) async duplicateCourse ( @ Param ( 'id' ) id , @ BodyParam ( 'courseAdmin' , { required : false } ) newCourseAdminId , @ CurrentUser ( ) currentUser ) { const courseModel = await Course . findById ( id ) ; if ( ! courseModel ) { throw new NotFoundError ( ) ; } await DuplicationController . assertUserDuplicationAuthorization ( currentUser , courseModel ) ; newCourseAdminId = typeof newCourseAdminId === 'string' ? newCourseAdminId : extractSingleMongoId ( currentUser ) ; const exportedCourse = await courseModel . exportJSON ( false ) ; delete exportedCourse . students ; const duplicate = await Course . schema . statics . importJSON ( exportedCourse , newCourseAdminId ) ; return DuplicationController . extractCommonResponse ( duplicate ) ; } api apiName apiGroup apiPermission apiPermission apiParam apiParam apiSuccess apiSuccessExample apiError apiError @ Post ( '/lecture/:id' ) async duplicateLecture ( @ Param ( 'id' ) id , @ BodyParam ( 'courseId' , { required : true } ) targetCourseId , @ CurrentUser ( ) currentUser ) { const course = await Course . findOne ( { lectures : id } ) ; if ( ! course ) { throw new NotFoundError ( ) ; } await DuplicationController . assertUserDuplicationAuthorization ( currentUser , course ) ; const targetCourse = await Course . findById ( targetCourseId ) ; if ( ! targetCourse ) { throw new NotFoundError ( errorCodes . duplication . targetNotFound . text ) ; } await DuplicationController . assertUserDuplicationAuthorization ( currentUser , targetCourse ) ; const lectureModel = await Lecture . findById ( id ) ; const exportedLecture = await lectureModel . exportJSON ( ) ; const duplicate = await Lecture . schema . statics . importJSON ( exportedLecture , targetCourseId ) ; return DuplicationController . extractCommonResponse ( duplicate ) ; } api apiName apiGroup apiPermission apiPermission apiParam apiParam apiSuccess apiSuccessExample apiError apiError @ Post ( '/unit/:id' ) async duplicateUnit ( @ Param ( 'id' ) id , @ BodyParam ( 'lectureId' , { required : true } ) targetLectureId , @ CurrentUser ( ) currentUser ) { const unitModel = await Unit . findById ( id ) ; if ( ! unitModel ) { throw new NotFoundError ( ) ; } const course = await Course . findById ( unitModel . _course ) ; await DuplicationController . assertUserDuplicationAuthorization ( currentUser , course ) ; const targetCourse = await Course . findOne ( { lectures : targetLectureId } ) ; if ( ! targetCourse ) { throw new NotFoundError ( errorCodes . duplication . targetNotFound . text ) ; } await DuplicationController . assertUserDuplicationAuthorization ( currentUser , targetCourse ) ; const targetCourseId = extractSingleMongoId ( targetCourse ) ; const exportedUnit = await unitModel . exportJSON ( ) ; const duplicate = await Unit . schema . statics . importJSON ( exportedUnit , targetCourseId , targetLectureId ) ; return DuplicationController . extractCommonResponse ( duplicate ) ; } }