Skip to content

Commit

Permalink
fix filterer
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Oct 27, 2023
1 parent 932d819 commit 66458f2
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions apps/server/src/modules/group/uc/group.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ export class GroupUc {
schoolId: string,
schoolYearQueryType: SchoolYearQueryType | undefined
): Promise<ClassInfoDto[]> {
const [classInfosFromClasses, classInfosFromGroups] = await Promise.all([
await this.findClassesForSchool(schoolId, schoolYearQueryType),
await this.findGroupsOfTypeClassForSchool(schoolId),
]);
let classInfosFromGroups: ClassInfoDto[] = [];

const classInfosFromClasses = await this.findClassesForSchool(schoolId, schoolYearQueryType);

if (schoolYearQueryType === SchoolYearQueryType.CURRENT_YEAR) {
classInfosFromGroups = await this.findGroupsOfTypeClassForSchool(schoolId);
}

const combinedClassInfo: ClassInfoDto[] = [...classInfosFromClasses, ...classInfosFromGroups];

Expand All @@ -80,29 +83,35 @@ export class GroupUc {
const classes: Class[] = await this.classService.findClassesForSchool(schoolId);
const currentYear: SchoolYearEntity = await this.schoolYearService.getCurrentSchoolYear();

const filteredClasses: Class[] = await Promise.all(
classes.filter(async (clazz: Class): Promise<boolean> => {
const classesWithSchoolYear: { clazz: Class; schoolYear?: SchoolYearEntity }[] = await Promise.all(
classes.map(async (clazz) => {
let schoolYear: SchoolYearEntity | undefined;
if (clazz.year) {
schoolYear = await this.schoolYearService.findById(clazz.year);
}

return this.isClassOfQueryType(currentYear, schoolYear, schoolYearQueryType);
return {
clazz,
schoolYear,
};
})
);

const classInfosFromClasses: ClassInfoDto[] = await Promise.all(
filteredClasses.map(async (clazz: Class): Promise<ClassInfoDto> => {
const filteredClassesForSchoolYear = classesWithSchoolYear.filter((classWithSchoolYear) =>
this.isClassOfQueryType(currentYear, classWithSchoolYear.schoolYear, schoolYearQueryType)
);

const classInfosFromClasses = await Promise.all(
filteredClassesForSchoolYear.map(async (classWithSchoolYear): Promise<ClassInfoDto> => {
const teachers: UserDO[] = await Promise.all(
clazz.teacherIds.map((teacherId: EntityId) => this.userService.findById(teacherId))
classWithSchoolYear.clazz.teacherIds.map((teacherId: EntityId) => this.userService.findById(teacherId))
);

let schoolYear: SchoolYearEntity | undefined;
if (clazz.year) {
schoolYear = await this.schoolYearService.findById(clazz.year);
}

const mapped: ClassInfoDto = GroupUcMapper.mapClassToClassInfoDto(clazz, teachers, schoolYear);
const mapped: ClassInfoDto = GroupUcMapper.mapClassToClassInfoDto(
classWithSchoolYear.clazz,
teachers,
classWithSchoolYear.schoolYear
);

return mapped;
})
Expand Down

0 comments on commit 66458f2

Please sign in to comment.