From f17596f81c4d0e97ec2bac7f59708dc25dc12d23 Mon Sep 17 00:00:00 2001 From: Gordon Nicholas <160246213+GordonNicky@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:52:44 +0200 Subject: [PATCH] N21-2095-bugfix-class-creation-schoolyear (#5131) --- .../modules/group/uc/class-group.uc.spec.ts | 23 +++++++++++++------ .../src/modules/group/uc/class-group.uc.ts | 9 ++++---- .../modules/school/domain/do/school-year.ts | 6 ++++- .../src/modules/school/domain/do/school.ts | 4 ++++ .../school/domain/service/school.service.ts | 5 ++++ 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/apps/server/src/modules/group/uc/class-group.uc.spec.ts b/apps/server/src/modules/group/uc/class-group.uc.spec.ts index 1a9bb2c0abf..e3274185de7 100644 --- a/apps/server/src/modules/group/uc/class-group.uc.spec.ts +++ b/apps/server/src/modules/group/uc/class-group.uc.spec.ts @@ -13,7 +13,7 @@ import { ProvisioningConfig } from '@modules/provisioning'; import { RoleService } from '@modules/role'; import { RoleDto } from '@modules/role/service/dto/role.dto'; import { School, SchoolService } from '@modules/school/domain'; -import { schoolFactory } from '@modules/school/testing'; +import { schoolFactory, schoolYearFactory } from '@modules/school/testing'; import { System, SystemService } from '@modules/system'; import { UserService } from '@modules/user'; import { ForbiddenException } from '@nestjs/common'; @@ -25,7 +25,7 @@ import { Permission, SortOrder } from '@shared/domain/interface'; import { groupFactory, roleDtoFactory, - schoolYearFactory, + schoolYearFactory as schoolYearEntityFactory, setupEntities, systemFactory, UserAndAccountTestFactory, @@ -155,7 +155,12 @@ describe('ClassGroupUc', () => { describe('when accessing as a normal user', () => { const setup = () => { - const school: School = schoolFactory.build({ permissions: { teacher: { STUDENT_LIST: true } } }); + const schoolYearDo = schoolYearFactory.build(); + const school: School = schoolFactory.build({ + permissions: { teacher: { STUDENT_LIST: true } }, + currentYear: schoolYearDo, + }); + const { studentUser } = UserAndAccountTestFactory.buildStudent(); const { teacherUser } = UserAndAccountTestFactory.buildTeacher(); const teacherRole: RoleDto = roleDtoFactory.buildWithId({ @@ -176,10 +181,13 @@ describe('ClassGroupUc', () => { lastName: studentUser.lastName, roles: [{ id: studentUser.roles[0].id, name: studentUser.roles[0].name }], }); - const schoolYear: SchoolYearEntity = schoolYearFactory.buildWithId(); - const nextSchoolYear: SchoolYearEntity = schoolYearFactory.buildWithId({ + + const startDate = schoolYearDo.getProps().startDate; + const schoolYear: SchoolYearEntity = schoolYearEntityFactory.buildWithId({ startDate }); + const nextSchoolYear: SchoolYearEntity = schoolYearEntityFactory.buildWithId({ startDate: schoolYear.endDate, }); + const clazz: Class = classFactory.build({ name: 'A', teacherIds: [teacherUser.id], @@ -216,6 +224,7 @@ describe('ClassGroupUc', () => { const synchronizedCourse: Course = courseFactory.build({ syncedWithGroup: group.id }); schoolService.getSchoolById.mockResolvedValueOnce(school); + schoolService.getCurrentYear.mockResolvedValueOnce(schoolYearDo); authorizationService.getUserWithPermissions.mockResolvedValueOnce(teacherUser); authorizationService.hasAllPermissions.mockReturnValueOnce(false); classService.findAllByUserId.mockResolvedValueOnce([clazz, successorClass, classWithoutSchoolYear]); @@ -594,7 +603,7 @@ describe('ClassGroupUc', () => { lastName: studentUser.lastName, roles: [{ id: studentUser.roles[0].id, name: studentUser.roles[0].name }], }); - const schoolYear: SchoolYearEntity = schoolYearFactory.buildWithId(); + const schoolYear: SchoolYearEntity = schoolYearEntityFactory.buildWithId(); let clazzes: Class[] = []; if (generateClasses) { clazzes = classFactory.buildList(11, { @@ -892,7 +901,7 @@ describe('ClassGroupUc', () => { roles: [{ id: teacherUser.roles[0].id, name: teacherUser.roles[0].name }], }); - const schoolYear: SchoolYearEntity = schoolYearFactory.buildWithId(); + const schoolYear: SchoolYearEntity = schoolYearEntityFactory.buildWithId(); const clazz: Class = classFactory.build({ name: 'A', teacherIds: [teacherUser.id, notFoundReferenceId], diff --git a/apps/server/src/modules/group/uc/class-group.uc.ts b/apps/server/src/modules/group/uc/class-group.uc.ts index 767591735fe..b3085101d1e 100644 --- a/apps/server/src/modules/group/uc/class-group.uc.ts +++ b/apps/server/src/modules/group/uc/class-group.uc.ts @@ -5,7 +5,7 @@ import { Course } from '@modules/learnroom/domain'; import { CourseDoService } from '@modules/learnroom/service/course-do.service'; import { SchoolYearService } from '@modules/legacy-school'; import { ProvisioningConfig } from '@modules/provisioning'; -import { School, SchoolService } from '@modules/school/domain'; +import { School, SchoolService, SchoolYear } from '@modules/school/domain'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { SortHelper } from '@shared/common'; @@ -138,7 +138,8 @@ export class ClassGroupUc { classes: Class[], schoolYearQueryType?: SchoolYearQueryType ): Promise { - const currentYear: SchoolYearEntity = await this.schoolYearService.getCurrentSchoolYear(); + const currentYear: SchoolYear | undefined = + classes.length > 0 ? await this.schoolService.getCurrentYear(classes[0].schoolId) : undefined; const classesWithSchoolYear: { clazz: Class; schoolYear?: SchoolYearEntity }[] = await this.addSchoolYearsToClasses( classes @@ -172,7 +173,7 @@ export class ClassGroupUc { } private isClassOfQueryType( - currentYear: SchoolYearEntity, + currentYear: SchoolYear | undefined, schoolYear?: SchoolYearEntity, schoolYearQueryType?: SchoolYearQueryType ): boolean { @@ -180,7 +181,7 @@ export class ClassGroupUc { return true; } - if (schoolYear === undefined) { + if (schoolYear === undefined || currentYear === undefined) { return schoolYearQueryType === SchoolYearQueryType.CURRENT_YEAR; } diff --git a/apps/server/src/modules/school/domain/do/school-year.ts b/apps/server/src/modules/school/domain/do/school-year.ts index eaeeb6384c9..885226ce132 100644 --- a/apps/server/src/modules/school/domain/do/school-year.ts +++ b/apps/server/src/modules/school/domain/do/school-year.ts @@ -1,6 +1,10 @@ import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; -export class SchoolYear extends DomainObject {} +export class SchoolYear extends DomainObject { + get startDate() { + return this.props.startDate; + } +} export interface SchoolYearProps extends AuthorizableObject { name: string; diff --git a/apps/server/src/modules/school/domain/do/school.ts b/apps/server/src/modules/school/domain/do/school.ts index 45ea43d255d..f5be38594ba 100644 --- a/apps/server/src/modules/school/domain/do/school.ts +++ b/apps/server/src/modules/school/domain/do/school.ts @@ -20,6 +20,10 @@ interface SchoolInfo { } export class School extends DomainObject { + get currentYear() { + return this.props.currentYear; + } + get systems(): EntityId[] { return this.props.systemIds; } diff --git a/apps/server/src/modules/school/domain/service/school.service.ts b/apps/server/src/modules/school/domain/service/school.service.ts index b590a6250f5..945c85fce98 100644 --- a/apps/server/src/modules/school/domain/service/school.service.ts +++ b/apps/server/src/modules/school/domain/service/school.service.ts @@ -53,6 +53,11 @@ export class SchoolService { return schoolsForExternalInvite; } + public async getCurrentYear(schoolId: EntityId) { + const school = await this.getSchoolById(schoolId); + return school.currentYear; + } + public async doesSchoolExist(schoolId: EntityId): Promise { try { await this.schoolRepo.getSchoolById(schoolId);