Skip to content

Commit

Permalink
N21-2095-bugfix-class-creation-schoolyear (#5131)
Browse files Browse the repository at this point in the history
  • Loading branch information
GordonNicholasCap authored Jul 23, 2024
1 parent c615cd2 commit f17596f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
23 changes: 16 additions & 7 deletions apps/server/src/modules/group/uc/class-group.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,7 +25,7 @@ import { Permission, SortOrder } from '@shared/domain/interface';
import {
groupFactory,
roleDtoFactory,
schoolYearFactory,
schoolYearFactory as schoolYearEntityFactory,
setupEntities,
systemFactory,
UserAndAccountTestFactory,
Expand Down Expand Up @@ -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({
Expand All @@ -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],
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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],
Expand Down
9 changes: 5 additions & 4 deletions apps/server/src/modules/group/uc/class-group.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -138,7 +138,8 @@ export class ClassGroupUc {
classes: Class[],
schoolYearQueryType?: SchoolYearQueryType
): Promise<ClassInfoDto[]> {
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
Expand Down Expand Up @@ -172,15 +173,15 @@ export class ClassGroupUc {
}

private isClassOfQueryType(
currentYear: SchoolYearEntity,
currentYear: SchoolYear | undefined,
schoolYear?: SchoolYearEntity,
schoolYearQueryType?: SchoolYearQueryType
): boolean {
if (schoolYearQueryType === undefined) {
return true;
}

if (schoolYear === undefined) {
if (schoolYear === undefined || currentYear === undefined) {
return schoolYearQueryType === SchoolYearQueryType.CURRENT_YEAR;
}

Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/modules/school/domain/do/school-year.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object';

export class SchoolYear extends DomainObject<SchoolYearProps> {}
export class SchoolYear extends DomainObject<SchoolYearProps> {
get startDate() {
return this.props.startDate;
}
}

export interface SchoolYearProps extends AuthorizableObject {
name: string;
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/modules/school/domain/do/school.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ interface SchoolInfo {
}

export class School extends DomainObject<SchoolProps> {
get currentYear() {
return this.props.currentYear;
}

get systems(): EntityId[] {
return this.props.systemIds;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
try {
await this.schoolRepo.getSchoolById(schoolId);
Expand Down

0 comments on commit f17596f

Please sign in to comment.