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-2270 Change group visibility for teachers that can see students #5414

Merged
merged 3 commits into from
Jan 13, 2025
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
Expand Up @@ -481,10 +481,6 @@ describe('Group (API)', () => {
users: [{ user: teacherUser, role: teacherUser.roles[0] }],
type: GroupEntityTypes.COURSE,
});
const groupWithoutTeacher: GroupEntity = groupEntityFactory.buildWithId({
organization: school,
type: GroupEntityTypes.COURSE,
});

const syncedCourse: CourseEntity = courseEntityFactory.build({
school,
Expand Down Expand Up @@ -521,7 +517,6 @@ describe('Group (API)', () => {
teacherUser,
teachersGroup,
availableTeachersGroup,
groupWithoutTeacher,
school,
syncedCourse,
]);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { ClassSortQueryType } from './class-sort-query-type.enum';
export { SchoolYearQueryType } from './school-year-query-type.enum';
export { ClassRequestContext } from './class-request-context.enum';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from './class-sort-params';
export * from './group-id-params';
export * from './class-filter-params';
export { GroupPaginationParams } from './group-pagination.params';
export { ClassCallerParams } from './class-caller-params';
export { GroupParams } from './group-params';
3 changes: 0 additions & 3 deletions apps/server/src/modules/group/controller/group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ErrorResponse } from '@src/core/error/dto';
import { ClassGroupUc, GroupUc } from '../uc';
import { ClassInfoDto, ResolvedGroupDto } from '../uc/dto';
import {
ClassCallerParams,
ClassFilterParams,
ClassInfoSearchListResponse,
ClassSortParams,
Expand All @@ -36,14 +35,12 @@ export class GroupController {
@Query() pagination: GroupPaginationParams,
@Query() sortingQuery: ClassSortParams,
@Query() filterParams: ClassFilterParams,
@Query() callerParams: ClassCallerParams,
@CurrentUser() currentUser: ICurrentUser
): Promise<ClassInfoSearchListResponse> {
const board: Page<ClassInfoDto> = await this.classGroupUc.findAllClasses(
currentUser.userId,
currentUser.schoolId,
filterParams.type,
callerParams.calledFrom,
pagination,
sortingQuery.sortBy,
sortingQuery.sortOrder
Expand Down
83 changes: 0 additions & 83 deletions apps/server/src/modules/group/domain/group-aggregate.scope.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { GroupEntityTypes } from '../entity';
import { GroupAggregateScope } from './group-aggregate.scope';
import { GroupTypes } from './group-types';
import { GroupVisibilityPermission } from './group-visibility-permission.enum';

describe(GroupAggregateScope.name, () => {
const defaultFacetQuery = {
Expand All @@ -12,87 +10,6 @@ describe(GroupAggregateScope.name, () => {
},
};

describe('byUserPermission', () => {
describe('when the user is allowed to see all groups of a school', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const schoolId = new ObjectId().toString();

return {
userId,
schoolId,
};
};

it('should build the correct query', () => {
const { userId, schoolId } = setup();

const result = new GroupAggregateScope()
.byUserPermission(userId, schoolId, GroupVisibilityPermission.ALL_SCHOOL_GROUPS)
.build();

expect(result).toEqual([{ $match: { organization: new ObjectId(schoolId) } }, defaultFacetQuery]);
});
});

describe('when the user is allowed to see his own groups and all classes of a school', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const schoolId = new ObjectId().toHexString();

return {
userId,
schoolId,
};
};

it('should build the correct query', () => {
const { userId, schoolId } = setup();

const result = new GroupAggregateScope()
.byUserPermission(userId, schoolId, GroupVisibilityPermission.ALL_SCHOOL_CLASSES)
.build();

expect(result).toEqual([
{
$match: {
$or: [
{ organization: new ObjectId(schoolId), type: GroupEntityTypes.CLASS },
{ users: { $elemMatch: { user: new ObjectId(userId) } } },
],
},
},
defaultFacetQuery,
]);
});
});

describe('when the user is allowed to only see his own groups', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const schoolId = new ObjectId().toHexString();

return {
userId,
schoolId,
};
};

it('should build the correct query', () => {
const { userId, schoolId } = setup();

const result = new GroupAggregateScope()
.byUserPermission(userId, schoolId, GroupVisibilityPermission.OWN_GROUPS)
.build();

expect(result).toEqual([
{ $match: { users: { $elemMatch: { user: new ObjectId(userId) } } } },
defaultFacetQuery,
]);
});
});
});

describe('byAvailableForSync', () => {
describe('when filtering for groups that are available for a course synchronization', () => {
it('should build the correct query', () => {
Expand Down
30 changes: 5 additions & 25 deletions apps/server/src/modules/group/domain/group-aggregate.scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,11 @@ import { ObjectId } from '@mikro-orm/mongodb';
import { StringValidator } from '@shared/common';
import { EntityId } from '@shared/domain/types';
import { MongoDbScope, MongoPatterns } from '@shared/repo';
import { GroupEntity, GroupEntityTypes } from '../entity';
import { GroupEntity } from '../entity';
import { GroupTypes } from './group-types';
import { GroupVisibilityPermission } from './group-visibility-permission.enum';

export class GroupAggregateScope extends MongoDbScope<GroupEntity> {
byUserPermission(userId: EntityId, schoolId: EntityId, permission: GroupVisibilityPermission): this {
if (permission === GroupVisibilityPermission.ALL_SCHOOL_GROUPS) {
this.byOrganization(schoolId);
} else if (permission === GroupVisibilityPermission.ALL_SCHOOL_CLASSES) {
this.pipeline.push({
$match: {
$or: [
{ organization: new ObjectId(schoolId), type: GroupEntityTypes.CLASS },
{ users: { $elemMatch: { user: new ObjectId(userId) } } },
],
},
});
} else {
this.byUser(userId);
}

return this;
}

byAvailableForSync(value: boolean | undefined): this {
public byAvailableForSync(value: boolean | undefined): this {
if (value) {
this.pipeline.push(
{
Expand Down Expand Up @@ -57,23 +37,23 @@ export class GroupAggregateScope extends MongoDbScope<GroupEntity> {
return this;
}

byUser(id: EntityId | undefined): this {
public byUser(id: EntityId | undefined): this {
if (id) {
this.pipeline.push({ $match: { users: { $elemMatch: { user: new ObjectId(id) } } } });
}

return this;
}

byOrganization(id: EntityId | undefined): this {
public byOrganization(id: EntityId | undefined): this {
if (id) {
this.pipeline.push({ $match: { organization: new ObjectId(id) } });
}

return this;
}

byName(nameQuery: string | undefined): this {
public byName(nameQuery: string | undefined): this {
const escapedName: string | undefined = nameQuery
?.replace(MongoPatterns.REGEX_MONGO_LANGUAGE_PATTERN_WHITELIST, '')
.trim();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export enum GroupVisibilityPermission {
OWN_GROUPS,
ALL_SCHOOL_CLASSES,
ALL_SCHOOL_GROUPS,
}
Loading
Loading