diff --git a/apps/server/src/modules/learnroom/controller/api-test/course.api.spec.ts b/apps/server/src/modules/learnroom/controller/api-test/course.api.spec.ts index c41abc21d77..d257c42ee98 100644 --- a/apps/server/src/modules/learnroom/controller/api-test/course.api.spec.ts +++ b/apps/server/src/modules/learnroom/controller/api-test/course.api.spec.ts @@ -14,6 +14,7 @@ import { } from '@shared/testing'; import { readFile } from 'node:fs/promises'; import { CourseMetadataListResponse } from '../dto'; +import { CourseCommonCartridgeMetadataResponse } from '../dto/course-cc-metadata.response'; const createStudent = () => { const { studentUser, studentAccount } = UserAndAccountTestFactory.buildStudent({}, [Permission.COURSE_VIEW]); @@ -243,4 +244,30 @@ describe('Course Controller (API)', () => { expect(data[teacher.user.id].length).toBeGreaterThan(0); }); }); + + describe('[GET] /courses/:courseId', () => { + const setup = async () => { + const teacher = createTeacher(); + const course = courseFactory.buildWithId({ + teachers: [teacher.user], + students: [], + }); + + await em.persistAndFlush([teacher.account, teacher.user, course]); + em.clear(); + + return { course, teacher }; + }; + + it('should return common cartridge metadata of a course', async () => { + const { course, teacher } = await setup(); + + const loggedInClient = await testApiClient.login(teacher.account); + const response = await loggedInClient.get(`${course.id}`); + const data = response.body as CourseCommonCartridgeMetadataResponse; + + expect(response.statusCode).toBe(200); + expect(data.id).toBe(course.id); + }); + }); }); diff --git a/apps/server/src/modules/learnroom/controller/course.controller.ts b/apps/server/src/modules/learnroom/controller/course.controller.ts index 0a801a0aed2..c8114d067df 100644 --- a/apps/server/src/modules/learnroom/controller/course.controller.ts +++ b/apps/server/src/modules/learnroom/controller/course.controller.ts @@ -32,6 +32,7 @@ import { CourseExportUc, CourseImportUc, CourseSyncUc, CourseUc } from '../uc'; import { CommonCartridgeFileValidatorPipe } from '../utils'; import { CourseImportBodyParams, CourseMetadataListResponse, CourseQueryParams, CourseUrlParams } from './dto'; import { CourseExportBodyParams } from './dto/course-export.body.params'; +import { CourseCommonCartridgeMetadataResponse } from './dto/course-cc-metadata.response'; @ApiTags('Courses') @Authenticate('jwt') @@ -129,4 +130,14 @@ export class CourseController { [currentUser.userId]: permissions, }; } + + @Get(':courseId') + @ApiOperation({ summary: 'Get common cartridge metadata of a course by Id.' }) + @ApiBadRequestResponse({ description: 'Request data has invalid format.' }) + @ApiInternalServerErrorResponse({ description: 'Internal server error.' }) + public async getCourseById(@Param() param: CourseUrlParams): Promise { + const course = await this.courseUc.findCourseById(param.courseId); + + return CourseMapper.mapToCommonCartridgeMetadataResponse(course); + } } diff --git a/apps/server/src/modules/learnroom/controller/dto/course-cc-metadata.response.ts b/apps/server/src/modules/learnroom/controller/dto/course-cc-metadata.response.ts new file mode 100644 index 00000000000..a2f4c56885a --- /dev/null +++ b/apps/server/src/modules/learnroom/controller/dto/course-cc-metadata.response.ts @@ -0,0 +1,32 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { EntityId } from '@shared/domain/types'; + +export class CourseCommonCartridgeMetadataResponse { + constructor(id: EntityId, title: string, copyrightOwners: string[], creationDate?: Date) { + this.id = id; + this.title = title; + this.creationDate = creationDate; + this.copyRightOwners = copyrightOwners; + } + + @ApiProperty({ + description: 'The id of the course', + pattern: '[a-f0-9]{24}', + }) + id: string; + + @ApiProperty({ + description: 'Title of the course', + }) + title: string; + + @ApiProperty({ + description: 'Creation date of the course', + }) + creationDate?: Date; + + @ApiProperty({ + description: 'Copy right owners of the course', + }) + copyRightOwners: string[]; +} diff --git a/apps/server/src/modules/learnroom/mapper/course.mapper.ts b/apps/server/src/modules/learnroom/mapper/course.mapper.ts index 498922cc633..b87bcbff711 100644 --- a/apps/server/src/modules/learnroom/mapper/course.mapper.ts +++ b/apps/server/src/modules/learnroom/mapper/course.mapper.ts @@ -1,5 +1,6 @@ import { Course } from '@shared/domain/entity'; import { CourseMetadataResponse } from '../controller/dto'; +import { CourseCommonCartridgeMetadataResponse } from '../controller/dto/course-cc-metadata.response'; export class CourseMapper { static mapToMetadataResponse(course: Course): CourseMetadataResponse { @@ -15,4 +16,17 @@ export class CourseMapper { ); return dto; } + + static mapToCommonCartridgeMetadataResponse(course: Course): CourseCommonCartridgeMetadataResponse { + const courseMetadata = course.getMetadata(); + const teachers = course.teachers.toArray().map((teacher) => `${teacher.firstName} ${teacher.lastName}`); + const courseCCMetadataResopne: CourseCommonCartridgeMetadataResponse = new CourseCommonCartridgeMetadataResponse( + courseMetadata.id, + courseMetadata.title, + teachers, + courseMetadata.startDate + ); + + return courseCCMetadataResopne; + } } diff --git a/apps/server/src/modules/learnroom/uc/course.uc.spec.ts b/apps/server/src/modules/learnroom/uc/course.uc.spec.ts index 3c2a082fd4a..17dbd918dc7 100644 --- a/apps/server/src/modules/learnroom/uc/course.uc.spec.ts +++ b/apps/server/src/modules/learnroom/uc/course.uc.spec.ts @@ -103,4 +103,20 @@ describe('CourseUc', () => { expect(roleService.findByName).toHaveBeenCalledWith(RoleName.TEACHER); }); }); + + describe('findCourseById', () => { + const setup = () => { + const course = courseFactory.buildWithId(); + courseService.findById.mockResolvedValue(course); + return { course }; + }; + + it('should return course by id', async () => { + const { course } = setup(); + const result = await uc.findCourseById(course.id); + + expect(result).toEqual(course); + expect(courseService.findById).toHaveBeenCalledWith(course.id); + }); + }); }); diff --git a/apps/server/src/modules/learnroom/uc/course.uc.ts b/apps/server/src/modules/learnroom/uc/course.uc.ts index ed7abf98a1d..5f562c229e5 100644 --- a/apps/server/src/modules/learnroom/uc/course.uc.ts +++ b/apps/server/src/modules/learnroom/uc/course.uc.ts @@ -30,4 +30,8 @@ export class CourseUc { return role.permissions ?? []; } + + public async findCourseById(courseId: EntityId): Promise { + return this.courseService.findById(courseId); + } }