Skip to content

Commit

Permalink
function to remove guest from school
Browse files Browse the repository at this point in the history
  • Loading branch information
Metauriel committed Nov 27, 2024
1 parent 4e313a1 commit 35fd67a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
28 changes: 28 additions & 0 deletions apps/server/src/modules/user/service/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,34 @@ describe('UserService', () => {
});
});

describe('removeSecondarySchool', () => {
describe('when user is guest in targetSchool', () => {
const setup = () => {
const targetSchool = schoolFactory.build();
const role = roleFactory.buildWithId({ name: RoleName.TEACHER });
const guestTeacher = roleFactory.buildWithId({ name: RoleName.GUESTTEACHER });
const user = userDoFactory.buildWithId({
roles: [role],
secondarySchools: [{ schoolId: targetSchool.id, role: new RoleDto(guestTeacher) }],
});

userDORepo.findByIds.mockResolvedValueOnce([user]);

return { user, targetSchool, guestTeacher };
};

it('should remove user from secondary school', async () => {
const { user, targetSchool } = setup();

await service.removeSecondarySchoolFromUsers([user.id as EntityId], targetSchool.id);

expect(userDORepo.saveAll).toHaveBeenCalledWith(
expect.arrayContaining([expect.objectContaining({ id: user.id, secondarySchools: [] })])
);
});
});
});

describe('saveAll is called', () => {
it('should call the repo with given users', async () => {
const users: UserDO[] = [userDoFactory.buildWithId()];
Expand Down
11 changes: 11 additions & 0 deletions apps/server/src/modules/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ export class UserService implements DeletionService, IEventHandler<UserDeletedEv
return Promise.resolve();
}

async removeSecondarySchoolFromUsers(userIds: string[], schoolId: EntityId): Promise<void> {
const users = await this.userDORepo.findByIds(userIds, true);

users.forEach((user) => {
user.secondarySchools = user.secondarySchools.filter((school) => school.schoolId !== schoolId);
});

await this.userDORepo.saveAll(users);
return Promise.resolve();
}

async findByExternalId(externalId: string, systemId: EntityId): Promise<UserDO | null> {
const user: Promise<UserDO | null> = this.userDORepo.findByExternalId(externalId, systemId);

Expand Down

0 comments on commit 35fd67a

Please sign in to comment.