diff --git a/apps/server/src/modules/learnroom/service/course.service.spec.ts b/apps/server/src/modules/learnroom/service/course.service.spec.ts index 67c581d6818..fbf2f09b698 100644 --- a/apps/server/src/modules/learnroom/service/course.service.spec.ts +++ b/apps/server/src/modules/learnroom/service/course.service.spec.ts @@ -56,6 +56,43 @@ describe('CourseService', () => { }); }); + describe('findAllCoursesByUserId', () => { + describe('when finding by userId', () => { + const setup = () => { + const user = userFactory.buildWithId(); + const course1 = courseFactory.buildWithId({ students: [user] }); + const course2 = courseFactory.buildWithId({ teachers: [user] }); + const course3 = courseFactory.buildWithId({ substitutionTeachers: [user] }); + const allCourses = [course1, course2, course3]; + + userRepo.findById.mockResolvedValue(user); + courseRepo.findAllByUserId.mockResolvedValue([allCourses, allCourses.length]); + + return { + user, + allCourses, + }; + }; + + it('should call courseRepo.findAllByUserId', async () => { + const { user } = setup(); + + await courseService.findAllCoursesByUserId(user.id); + + expect(courseRepo.findAllByUserId).toBeCalledWith(user.id); + }); + + it('should return array of courses with userId', async () => { + const { user, allCourses } = setup(); + + const [courses] = await courseService.findAllCoursesByUserId(user.id); + + expect(courses.length).toEqual(3); + expect(courses).toEqual(allCourses); + }); + }); + }); + describe('when deleting by userId', () => { const setup = () => { const user = userFactory.buildWithId(); diff --git a/apps/server/src/modules/learnroom/service/course.service.ts b/apps/server/src/modules/learnroom/service/course.service.ts index c2e7364ffe9..76b441b42d8 100644 --- a/apps/server/src/modules/learnroom/service/course.service.ts +++ b/apps/server/src/modules/learnroom/service/course.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { CourseRepo } from '@shared/repo'; -import { Course, EntityId } from '@shared/domain'; +import { Counted, Course, EntityId } from '@shared/domain'; @Injectable() export class CourseService { @@ -10,6 +10,12 @@ export class CourseService { return this.repo.findById(courseId); } + public async findAllCoursesByUserId(userId: EntityId): Promise> { + const [courses, count] = await this.repo.findAllByUserId(userId); + + return [courses, count]; + } + public async deleteUserDataFromCourse(userId: EntityId): Promise { const [courses, count] = await this.repo.findAllByUserId(userId); diff --git a/apps/server/src/modules/learnroom/service/coursegroup.service.spec.ts b/apps/server/src/modules/learnroom/service/coursegroup.service.spec.ts index 5a445792a23..48caf02c378 100644 --- a/apps/server/src/modules/learnroom/service/coursegroup.service.spec.ts +++ b/apps/server/src/modules/learnroom/service/coursegroup.service.spec.ts @@ -38,6 +38,40 @@ describe('CourseGroupService', () => { jest.clearAllMocks(); }); + describe('findAllCourseGroupsByUserId', () => { + describe('when finding by userId', () => { + const setup = () => { + const user = userFactory.buildWithId(); + const courseGroups = courseGroupFactory.buildListWithId(2, { students: [user] }); + + userRepo.findById.mockResolvedValue(user); + courseGroupRepo.findByUserId.mockResolvedValue([courseGroups, courseGroups.length]); + + return { + user, + courseGroups, + }; + }; + + it('should call courseGroupRepo.findByUserId', async () => { + const { user } = setup(); + + await courseGroupService.findAllCourseGroupsByUserId(user.id); + + expect(courseGroupRepo.findByUserId).toBeCalledWith(user.id); + }); + + it('should return array with coursesGroup with userId', async () => { + const { user, courseGroups } = setup(); + + const [courseGroup] = await courseGroupService.findAllCourseGroupsByUserId(user.id); + + expect(courseGroup.length).toEqual(2); + expect(courseGroup).toEqual(courseGroups); + }); + }); + }); + describe('when deleting by userId', () => { const setup = () => { const user = userFactory.buildWithId(); diff --git a/apps/server/src/modules/learnroom/service/coursegroup.service.ts b/apps/server/src/modules/learnroom/service/coursegroup.service.ts index acd8f5b9b42..622cdbc5e9e 100644 --- a/apps/server/src/modules/learnroom/service/coursegroup.service.ts +++ b/apps/server/src/modules/learnroom/service/coursegroup.service.ts @@ -1,11 +1,17 @@ import { Injectable } from '@nestjs/common'; -import { EntityId } from '@shared/domain'; +import { Counted, CourseGroup, EntityId } from '@shared/domain'; import { CourseGroupRepo } from '@shared/repo'; @Injectable() export class CourseGroupService { constructor(private readonly repo: CourseGroupRepo) {} + public async findAllCourseGroupsByUserId(userId: EntityId): Promise> { + const [courseGroups, count] = await this.repo.findByUserId(userId); + + return [courseGroups, count]; + } + public async deleteUserDataFromCourseGroup(userId: EntityId): Promise { const [courseGroups, count] = await this.repo.findByUserId(userId); diff --git a/apps/server/src/modules/lesson/service/lesson.service.spec.ts b/apps/server/src/modules/lesson/service/lesson.service.spec.ts index bd5431b326d..7f0179640b5 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.spec.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.spec.ts @@ -77,6 +77,47 @@ describe('LessonService', () => { }); }); + describe('findAllLessonsByUserId', () => { + describe('when finding by userId', () => { + const setup = () => { + const userId = new ObjectId().toHexString(); + const contentExample: IComponentProperties = { + title: 'title', + hidden: false, + user: userId, + component: ComponentType.TEXT, + content: { text: 'test of content' }, + }; + const lesson1 = lessonFactory.buildWithId({ contents: [contentExample] }); + const lesson2 = lessonFactory.buildWithId({ contents: [contentExample] }); + const lessons = [lesson1, lesson2]; + + lessonRepo.findByUserId.mockResolvedValue(lessons); + + return { + userId, + lessons, + }; + }; + + it('should call findByCourseIds from lesson repo', async () => { + const { userId } = setup(); + + await expect(lessonService.findAllLessonsByUserId(userId)).resolves.not.toThrow(); + expect(lessonRepo.findByUserId).toBeCalledWith(userId); + }); + + it('should return array of lessons with userId', async () => { + const { userId, lessons } = setup(); + + const result = await lessonService.findAllLessonsByUserId(userId); + + expect(result).toHaveLength(2); + expect(result).toEqual(lessons); + }); + }); + }); + describe('deleteUserDataFromTeams', () => { describe('when deleting by userId', () => { const setup = () => { diff --git a/apps/server/src/modules/lesson/service/lesson.service.ts b/apps/server/src/modules/lesson/service/lesson.service.ts index 5f8acb03e38..5a69a19f305 100644 --- a/apps/server/src/modules/lesson/service/lesson.service.ts +++ b/apps/server/src/modules/lesson/service/lesson.service.ts @@ -24,6 +24,12 @@ export class LessonService { return this.lessonRepo.findAllByCourseIds(courseIds); } + async findAllLessonsByUserId(userId: EntityId): Promise { + const lessons = await this.lessonRepo.findByUserId(userId); + + return lessons; + } + async deleteUserDataFromLessons(userId: EntityId): Promise { const lessons = await this.lessonRepo.findByUserId(userId);