Skip to content

Commit

Permalink
requested changes WIP 3
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Apr 3, 2024
1 parent c8e59f4 commit d9caccb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ describe('Group (API)', () => {
},
expectGroup,
],
limit: 10,
skip: 0,
total: 2,
});
});
Expand All @@ -393,6 +395,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 1,
skip: 1,
total: 2,
});
});
Expand All @@ -405,6 +409,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 10,
skip: 0,
total: 1,
});
});
Expand All @@ -419,6 +425,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 10,
skip: 0,
total: 1,
});
});
Expand All @@ -429,7 +437,7 @@ describe('Group (API)', () => {
const response = await loggedInClient.get().query({ availableGroupsForCourseSync: true, skip: 1, limit: 1 });

expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({ data: [], total: 1 });
expect(response.body).toEqual({ data: [], limit: 1, skip: 1, total: 1 });
});

it('should return available groups according to name query', async () => {
Expand All @@ -440,6 +448,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 10,
skip: 0,
total: 1,
});
});
Expand Down Expand Up @@ -538,6 +548,8 @@ describe('Group (API)', () => {
},
expectGroup,
],
limit: 10,
skip: 0,
total: 2,
});
});
Expand All @@ -550,6 +562,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 1,
skip: 1,
total: 2,
});
});
Expand All @@ -562,6 +576,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 10,
skip: 0,
total: 1,
});
});
Expand All @@ -576,6 +592,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 10,
skip: 0,
total: 1,
});
});
Expand All @@ -586,7 +604,7 @@ describe('Group (API)', () => {
const response = await loggedInClient.get().query({ availableGroupsForCourseSync: true, skip: 1, limit: 1 });

expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({ data: [], total: 1 });
expect(response.body).toEqual({ data: [], limit: 1, skip: 1, total: 1 });
});

it('should return all available groups according to name query', async () => {
Expand All @@ -597,6 +615,8 @@ describe('Group (API)', () => {
expect(response.status).toEqual(HttpStatus.OK);
expect(response.body).toEqual({
data: [expectGroup],
limit: 10,
skip: 0,
total: 1,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class GroupController {
params.availableGroupsForCourseSync,
params.nameQuery
);
const response: GroupListResponse = GroupResponseMapper.mapToGroupListResponse(groups);
const response: GroupListResponse = GroupResponseMapper.mapToGroupListResponse(groups, pagination);

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ClassInfoSearchListResponse,
ExternalSourceResponse,
GroupListResponse,
GroupPaginationParams,
GroupResponse,
GroupTypeResponse,
GroupUserResponse,
Expand Down Expand Up @@ -82,12 +83,17 @@ export class GroupResponseMapper {
return mapped;
}

static mapToGroupListResponse(groups: Page<ResolvedGroupDto>): GroupListResponse {
const mapped: GroupResponse[] = groups.data.map(
static mapToGroupListResponse(groups: Page<ResolvedGroupDto>, pagination: GroupPaginationParams): GroupListResponse {
const groupResponseData: GroupResponse[] = groups.data.map(
(group: ResolvedGroupDto): GroupResponse => this.mapToGroupResponse(group)
);

const response: GroupListResponse = { data: mapped, total: groups.total };
const response: GroupListResponse = new GroupListResponse(
groupResponseData,
groups.total,
pagination.skip,
pagination.limit
); // { data: mapped, total: groups.total };

return response;
}
Expand Down
14 changes: 8 additions & 6 deletions apps/server/src/modules/group/repo/group.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,18 @@ export class GroupRepo extends BaseDomainObjectRepo<Group, GroupEntity> {
});
}

const mongoEntities = await this.em.aggregate(GroupEntity, pipeline);
const mongoEntitiesFacet = (await this.em.aggregate(GroupEntity, pipeline)) as [
{ total: [{ count: number }]; data: GroupEntity[] }
];

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const total: number = mongoEntities[0]?.total[0]?.count ?? 0;
const total: number = mongoEntitiesFacet[0]?.total[0]?.count ?? 0;

// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const entities: GroupEntity[] = mongoEntities[0].data.map((entity: GroupEntity) => {
const { ...newGroupEntity } = entity;
return this.em.map(GroupEntity, newGroupEntity);
});
const entities: GroupEntity[] = mongoEntitiesFacet[0].data.map((entity: GroupEntity) =>
// const { ...newGroupEntity } = entity;
this.em.map(GroupEntity, entity)
);

const domainObjects: Group[] = entities.map((entity) => GroupDomainMapper.mapEntityToDo(entity));

Expand Down
10 changes: 5 additions & 5 deletions apps/server/src/modules/group/uc/group.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,11 @@ describe('GroupUc', () => {

await uc.findAllClasses(teacherUser.id, teacherUser.school.id);

expect(groupService.findGroupsBySchoolIdAndGroupTypes).toHaveBeenCalledWith<[School, GroupTypes[], number]>(
school,
[GroupTypes.CLASS, GroupTypes.COURSE, GroupTypes.OTHER],
0
);
expect(groupService.findGroupsBySchoolIdAndGroupTypes).toHaveBeenCalledWith<[School, GroupTypes[]]>(school, [
GroupTypes.CLASS,
GroupTypes.COURSE,
GroupTypes.OTHER,
]);
});
});

Expand Down
5 changes: 2 additions & 3 deletions apps/server/src/modules/group/uc/group.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ export class GroupUc {
private async findGroupsForSchool(school: School): Promise<ClassInfoDto[]> {
const groups: Page<Group> = await this.groupService.findGroupsBySchoolIdAndGroupTypes(
school,
this.ALLOWED_GROUP_TYPES,
0
this.ALLOWED_GROUP_TYPES
);

const classInfosFromGroups: ClassInfoDto[] = await this.getClassInfosFromGroups(groups.data);
Expand Down Expand Up @@ -416,7 +415,7 @@ export class GroupUc {
} else {
foundGroups = await this.groupService.findGroupsBySchoolIdAndGroupTypes(
school,
this.ALLOWED_GROUP_TYPES,
undefined,
skip,
limit,
nameQuery
Expand Down

0 comments on commit d9caccb

Please sign in to comment.