Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-4973-rewrite-data-retrival-in-course #4422

Merged
merged 13 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/server/src/modules/learnroom/service/course.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ describe('CourseService', () => {
});
});

describe('findUserDataFromCourses', () => {
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.findUserDataFromCourses(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.findUserDataFromCourses(user.id);

expect(courses.length).toEqual(3);
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
expect(courses).toEqual(allCourses);
});
});
});

describe('when deleting by userId', () => {
const setup = () => {
const user = userFactory.buildWithId();
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/modules/learnroom/service/course.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,6 +10,12 @@ export class CourseService {
return this.repo.findById(courseId);
}

public async findUserDataFromCourses(userId: EntityId): Promise<Counted<Course[]>> {
WojciechGrancow marked this conversation as resolved.
Show resolved Hide resolved
const [courses, count] = await this.repo.findAllByUserId(userId);

return [courses, count];
}

public async deleteUserDataFromCourse(userId: EntityId): Promise<number> {
const [courses, count] = await this.repo.findAllByUserId(userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ describe('CourseGroupService', () => {
jest.clearAllMocks();
});

describe('finUserDataFromCourseGroup', () => {
describe('when finding by userId', () => {
const setup = () => {
const user = userFactory.buildWithId();
const courseGroup1 = courseGroupFactory.buildWithId({ students: [user] });
WojciechGrancow marked this conversation as resolved.
Show resolved Hide resolved
const courseGroup2 = courseGroupFactory.buildWithId({ students: [user] });
const courseGroups = [courseGroup1, courseGroup2];

userRepo.findById.mockResolvedValue(user);
courseGroupRepo.findByUserId.mockResolvedValue([courseGroups, courseGroups.length]);

return {
user,
courseGroups,
};
};

it('should call courseGroupRepo.findByUserId', async () => {
const { user } = setup();

await courseGroupService.finUserDataFromCourseGroup(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.finUserDataFromCourseGroup(user.id);

expect(courseGroup.length).toEqual(2);
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
expect(courseGroup).toEqual(courseGroups);
});
});
});

describe('when deleting by userId', () => {
const setup = () => {
const user = userFactory.buildWithId();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 finUserDataFromCourseGroup(userId: EntityId): Promise<Counted<CourseGroup[]>> {
const [courseGroups, count] = await this.repo.findByUserId(userId);

return [courseGroups, count];
}

public async deleteUserDataFromCourseGroup(userId: EntityId): Promise<number> {
const [courseGroups, count] = await this.repo.findByUserId(userId);

Expand Down
41 changes: 41 additions & 0 deletions apps/server/src/modules/lesson/service/lesson.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,47 @@ describe('LessonService', () => {
});
});

describe('findUserDataFromLessons', () => {
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.findUserDataFromLessons(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.findUserDataFromLessons(userId);

expect(result).toHaveLength(2);
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
expect(result).toEqual(lessons);
});
});
});

describe('deleteUserDataFromTeams', () => {
describe('when deleting by userId', () => {
const setup = () => {
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/modules/lesson/service/lesson.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export class LessonService {
return this.lessonRepo.findAllByCourseIds(courseIds);
}

async findUserDataFromLessons(userId: EntityId): Promise<LessonEntity[]> {
WojciechGrancow marked this conversation as resolved.
Show resolved Hide resolved
const lessons = await this.lessonRepo.findByUserId(userId);

return lessons;
}

async deleteUserDataFromLessons(userId: EntityId): Promise<number> {
const lessons = await this.lessonRepo.findByUserId(userId);

Expand Down
Loading