Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21-1602 fixes classes limit #4647

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { PaginationParams } from '@shared/controller';
import { IsInt } from 'class-validator';

export class GroupPaginationParams extends PaginationParams {
@IsInt()
@ApiPropertyOptional({ description: 'Page limit, defaults to 10.' })
override limit?: number = 10;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './class-sort-params';
export * from './group-id-params';
export * from './class-filter-params';
export { GroupPaginationParams } from './group-pagination.params';
12 changes: 9 additions & 3 deletions apps/server/src/modules/group/controller/group.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Authenticate, CurrentUser, ICurrentUser } from '@modules/authentication';
import { Controller, Get, HttpStatus, Param, Query } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { PaginationParams } from '@shared/controller';
import { Page } from '@shared/domain/domainobject';
import { ErrorResponse } from '@src/core/error/dto';
import { GroupUc } from '../uc';
import { ClassInfoDto, ResolvedGroupDto } from '../uc/dto';
import { ClassFilterParams, ClassInfoSearchListResponse, ClassSortParams, GroupIdParams, GroupResponse } from './dto';
import {
ClassFilterParams,
ClassInfoSearchListResponse,
ClassSortParams,
GroupIdParams,
GroupResponse,
GroupPaginationParams,
} from './dto';
import { GroupResponseMapper } from './mapper';

@ApiTags('Group')
Expand All @@ -21,7 +27,7 @@ export class GroupController {
@ApiResponse({ status: '5XX', type: ErrorResponse })
@Get('/class')
public async findClasses(
@Query() pagination: PaginationParams,
@Query() pagination: GroupPaginationParams,
@Query() sortingQuery: ClassSortParams,
@Query() filterParams: ClassFilterParams,
@CurrentUser() currentUser: ICurrentUser
Expand Down
41 changes: 39 additions & 2 deletions apps/server/src/modules/group/uc/group.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ describe('GroupUc', () => {
});

describe('when accessing as a user with elevated permission', () => {
const setup = () => {
const setup = (generateClasses = false) => {
const school: LegacySchoolDo = legacySchoolDoFactory.buildWithId();
const { studentUser } = UserAndAccountTestFactory.buildStudent();
const { teacherUser } = UserAndAccountTestFactory.buildTeacher();
Expand Down Expand Up @@ -551,6 +551,15 @@ describe('GroupUc', () => {
roles: [{ id: studentUser.roles[0].id, name: studentUser.roles[0].name }],
});
const schoolYear: SchoolYearEntity = schoolYearFactory.buildWithId();
let clazzes: Class[] = [];
if (generateClasses) {
clazzes = classFactory.buildList(11, {
name: 'A',
teacherIds: [teacherUser.id],
source: 'LDAP',
year: schoolYear.id,
});
}
const clazz: Class = classFactory.build({
name: 'A',
teacherIds: [teacherUser.id],
Expand Down Expand Up @@ -579,7 +588,7 @@ describe('GroupUc', () => {
schoolService.getSchoolById.mockResolvedValueOnce(school);
authorizationService.getUserWithPermissions.mockResolvedValueOnce(adminUser);
authorizationService.hasAllPermissions.mockReturnValueOnce(true);
classService.findClassesForSchool.mockResolvedValueOnce([clazz]);
classService.findClassesForSchool.mockResolvedValueOnce([...clazzes, clazz]);
groupService.findGroupsBySchoolIdAndGroupTypes.mockResolvedValueOnce([group, groupWithSystem]);
systemService.findById.mockResolvedValue(system);

Expand Down Expand Up @@ -788,6 +797,34 @@ describe('GroupUc', () => {
total: 3,
});
});

it('should return classes with expected limit', async () => {
const { adminUser } = setup(true);

const result: Page<ClassInfoDto> = await uc.findAllClasses(
adminUser.id,
adminUser.school.id,
undefined,
0,
5
);

expect(result.data.length).toEqual(5);
});

it('should return all classes without limit', async () => {
const { adminUser } = setup(true);

const result: Page<ClassInfoDto> = await uc.findAllClasses(
adminUser.id,
adminUser.school.id,
undefined,
0,
-1
);

expect(result.data.length).toEqual(14);
});
});
});

Expand Down
10 changes: 8 additions & 2 deletions apps/server/src/modules/group/uc/group.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,14 @@ export class GroupUc {
return resolvedGroupUsers;
}

private applyPagination(combinedClassInfo: ClassInfoDto[], skip: number, limit: number | undefined) {
const page: ClassInfoDto[] = combinedClassInfo.slice(skip, limit ? skip + limit : combinedClassInfo.length);
private applyPagination(combinedClassInfo: ClassInfoDto[], skip: number, limit: number | undefined): ClassInfoDto[] {
let page: ClassInfoDto[];

if (limit === -1) {
page = combinedClassInfo.slice(skip);
} else {
page = combinedClassInfo.slice(skip, limit ? skip + limit : combinedClassInfo.length);
}

return page;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import {
ApiBody,
ApiCreatedResponse,
ApiExtraModels,
ApiForbiddenResponse,
ApiNotFoundResponse,
ApiOkResponse,
Expand Down Expand Up @@ -45,6 +46,7 @@ export class SchoolController {
@ApiForbiddenResponse()
@ApiUnprocessableEntityResponse()
@ApiNotFoundResponse()
@ApiExtraModels(SchulConneXProvisioningOptionsResponse)
public async getProvisioningOptions(
@CurrentUser() currentUser: ICurrentUser,
@Param() params: SchoolSystemParams
Expand Down Expand Up @@ -79,6 +81,7 @@ export class SchoolController {
@ApiForbiddenResponse()
@ApiUnprocessableEntityResponse()
@ApiNotFoundResponse()
@ApiExtraModels(SchulConneXProvisioningOptionsResponse)
public async setProvisioningOptions(
@CurrentUser() currentUser: ICurrentUser,
@Param() params: SchoolSystemParams,
Expand Down
Loading