Skip to content

Commit

Permalink
add classFilter params and filterlogic
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Oct 26, 2023
1 parent 4f594ed commit 932d819
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './class-sort-params';
export * from './group-id-params';
export * from './class-filter-params';
7 changes: 5 additions & 2 deletions apps/server/src/modules/group/controller/group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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<ClassInfoSearchListResponse> {
const board: Page<ClassInfoDto> = await this.groupUc.findAllClassesForSchool(
currentUser.userId,
currentUser.schoolId,
schoolYearQuery.type,
pagination.skip,
pagination.limit,
sortingQuery.sortBy,
Expand Down
55 changes: 50 additions & 5 deletions apps/server/src/modules/group/uc/group.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand All @@ -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)
Expand All @@ -57,9 +59,12 @@ export class GroupUc {
return page;
}

private async findCombinedClassListForSchool(schoolId: string): Promise<ClassInfoDto[]> {
private async findCombinedClassListForSchool(
schoolId: string,
schoolYearQueryType: SchoolYearQueryType | undefined
): Promise<ClassInfoDto[]> {
const [classInfosFromClasses, classInfosFromGroups] = await Promise.all([
await this.findClassesForSchool(schoolId),
await this.findClassesForSchool(schoolId, schoolYearQueryType),
await this.findGroupsOfTypeClassForSchool(schoolId),
]);

Expand All @@ -68,11 +73,26 @@ export class GroupUc {
return combinedClassInfo;
}

private async findClassesForSchool(schoolId: EntityId): Promise<ClassInfoDto[]> {
private async findClassesForSchool(
schoolId: EntityId,
schoolYearQueryType: SchoolYearQueryType | undefined
): Promise<ClassInfoDto[]> {
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> => {
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<ClassInfoDto> => {
filteredClasses.map(async (clazz: Class): Promise<ClassInfoDto> => {
const teachers: UserDO[] = await Promise.all(
clazz.teacherIds.map((teacherId: EntityId) => this.userService.findById(teacherId))
);
Expand All @@ -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<ClassInfoDto[]> {
const groupsOfTypeClass: Group[] = await this.groupService.findClassesForSchool(schoolId);

Expand Down

0 comments on commit 932d819

Please sign in to comment.