From 932d8198ea890a5f89b61bc2b2d1aea811473ea6 Mon Sep 17 00:00:00 2001 From: Igor Richter Date: Thu, 26 Oct 2023 18:40:31 +0200 Subject: [PATCH] add classFilter params and filterlogic --- .../dto/request/class-filter-params.ts | 15 +++++ .../group/controller/dto/request/index.ts | 1 + .../group/controller/group.controller.ts | 7 ++- apps/server/src/modules/group/uc/group.uc.ts | 55 +++++++++++++++++-- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 apps/server/src/modules/group/controller/dto/request/class-filter-params.ts diff --git a/apps/server/src/modules/group/controller/dto/request/class-filter-params.ts b/apps/server/src/modules/group/controller/dto/request/class-filter-params.ts new file mode 100644 index 00000000000..6f060d42eb9 --- /dev/null +++ b/apps/server/src/modules/group/controller/dto/request/class-filter-params.ts @@ -0,0 +1,15 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; +import { IsEnum, IsOptional } from 'class-validator'; + +export enum SchoolYearQueryType { + NEXT_YEAR = 'nextYear', + CURRENT_YEAR = 'currentYear', + PREVIOUS_YEARS = 'previousYears', +} + +export class ClassFilterParams { + @IsOptional() + @IsEnum(SchoolYearQueryType) + @ApiPropertyOptional() + type?: SchoolYearQueryType; +} diff --git a/apps/server/src/modules/group/controller/dto/request/index.ts b/apps/server/src/modules/group/controller/dto/request/index.ts index 17ecd658b7d..ceef988aa92 100644 --- a/apps/server/src/modules/group/controller/dto/request/index.ts +++ b/apps/server/src/modules/group/controller/dto/request/index.ts @@ -1,2 +1,3 @@ export * from './class-sort-params'; export * from './group-id-params'; +export * from './class-filter-params'; diff --git a/apps/server/src/modules/group/controller/group.controller.ts b/apps/server/src/modules/group/controller/group.controller.ts index 9e5f4b3b51a..20aa07eaa0f 100644 --- a/apps/server/src/modules/group/controller/group.controller.ts +++ b/apps/server/src/modules/group/controller/group.controller.ts @@ -6,7 +6,7 @@ import { ErrorResponse } from '@src/core/error/dto'; import { ICurrentUser, Authenticate, CurrentUser } from '@modules/authentication'; import { GroupUc } from '../uc'; import { ClassInfoDto, ResolvedGroupDto } from '../uc/dto'; -import { ClassInfoSearchListResponse, ClassSortParams, GroupIdParams, GroupResponse } from './dto'; +import { ClassInfoSearchListResponse, ClassSortParams, GroupIdParams, GroupResponse, ClassFilterParams } from './dto'; import { GroupResponseMapper } from './mapper'; @ApiTags('Group') @@ -23,11 +23,14 @@ export class GroupController { public async findClassesForSchool( @Query() pagination: PaginationParams, @Query() sortingQuery: ClassSortParams, - @CurrentUser() currentUser: ICurrentUser + @Query() schoolYearQuery: ClassFilterParams, + @CurrentUser() + currentUser: ICurrentUser ): Promise { const board: Page = await this.groupUc.findAllClassesForSchool( currentUser.userId, currentUser.schoolId, + schoolYearQuery.type, pagination.skip, pagination.limit, sortingQuery.sortBy, diff --git a/apps/server/src/modules/group/uc/group.uc.ts b/apps/server/src/modules/group/uc/group.uc.ts index 2421e444e73..1e844e66549 100644 --- a/apps/server/src/modules/group/uc/group.uc.ts +++ b/apps/server/src/modules/group/uc/group.uc.ts @@ -13,6 +13,7 @@ import { GroupService } from '../service'; import { SortHelper } from '../util'; import { ClassInfoDto, ResolvedGroupDto, ResolvedGroupUser } from './dto'; import { GroupUcMapper } from './mapper/group-uc.mapper'; +import { SchoolYearQueryType } from '../controller/dto'; @Injectable() export class GroupUc { @@ -30,6 +31,7 @@ export class GroupUc { public async findAllClassesForSchool( userId: EntityId, schoolId: EntityId, + schoolYearQueryType: SchoolYearQueryType | undefined, skip = 0, limit?: number, sortBy: keyof ClassInfoDto = 'name', @@ -44,7 +46,7 @@ export class GroupUc { AuthorizationContextBuilder.read([Permission.CLASS_LIST, Permission.GROUP_LIST]) ); - const combinedClassInfo: ClassInfoDto[] = await this.findCombinedClassListForSchool(schoolId); + const combinedClassInfo: ClassInfoDto[] = await this.findCombinedClassListForSchool(schoolId, schoolYearQueryType); combinedClassInfo.sort((a: ClassInfoDto, b: ClassInfoDto): number => SortHelper.genericSortFunction(a[sortBy], b[sortBy], sortOrder) @@ -57,9 +59,12 @@ export class GroupUc { return page; } - private async findCombinedClassListForSchool(schoolId: string): Promise { + private async findCombinedClassListForSchool( + schoolId: string, + schoolYearQueryType: SchoolYearQueryType | undefined + ): Promise { const [classInfosFromClasses, classInfosFromGroups] = await Promise.all([ - await this.findClassesForSchool(schoolId), + await this.findClassesForSchool(schoolId, schoolYearQueryType), await this.findGroupsOfTypeClassForSchool(schoolId), ]); @@ -68,11 +73,26 @@ export class GroupUc { return combinedClassInfo; } - private async findClassesForSchool(schoolId: EntityId): Promise { + private async findClassesForSchool( + schoolId: EntityId, + schoolYearQueryType: SchoolYearQueryType | undefined + ): Promise { 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 => { + let schoolYear: SchoolYearEntity | undefined; + if (clazz.year) { + schoolYear = await this.schoolYearService.findById(clazz.year); + } + + return this.isClassOfQueryType(currentYear, schoolYear, schoolYearQueryType); + }) + ); const classInfosFromClasses: ClassInfoDto[] = await Promise.all( - classes.map(async (clazz: Class): Promise => { + filteredClasses.map(async (clazz: Class): Promise => { const teachers: UserDO[] = await Promise.all( clazz.teacherIds.map((teacherId: EntityId) => this.userService.findById(teacherId)) ); @@ -91,6 +111,31 @@ export class GroupUc { return classInfosFromClasses; } + private isClassOfQueryType( + currentYear: SchoolYearEntity, + schoolYear: SchoolYearEntity | undefined, + schoolYearQueryType: SchoolYearQueryType | undefined + ): boolean { + if (schoolYearQueryType === undefined) { + return true; + } + + if (schoolYear === undefined) { + return schoolYearQueryType === SchoolYearQueryType.CURRENT_YEAR; + } + + switch (schoolYearQueryType) { + case SchoolYearQueryType.CURRENT_YEAR: + return schoolYear.startDate === currentYear.startDate; + case SchoolYearQueryType.NEXT_YEAR: + return schoolYear.startDate > currentYear.startDate; + case SchoolYearQueryType.PREVIOUS_YEARS: + return schoolYear.startDate < currentYear.startDate; + default: + return true; + } + } + private async findGroupsOfTypeClassForSchool(schoolId: EntityId): Promise { const groupsOfTypeClass: Group[] = await this.groupService.findClassesForSchool(schoolId);