From 6c245239d3455ad892d97412d703dd12cf2924b4 Mon Sep 17 00:00:00 2001 From: Thomas Feldtkeller Date: Tue, 26 Nov 2024 15:24:22 +0100 Subject: [PATCH] wip begin working on adding guests to schools --- .../modules/user/service/user.service.spec.ts | 24 +++++++++++++++++++ .../src/modules/user/service/user.service.ts | 11 +++++++++ 2 files changed, 35 insertions(+) diff --git a/apps/server/src/modules/user/service/user.service.spec.ts b/apps/server/src/modules/user/service/user.service.spec.ts index e32e1910090..8bdb5895ff2 100644 --- a/apps/server/src/modules/user/service/user.service.spec.ts +++ b/apps/server/src/modules/user/service/user.service.spec.ts @@ -504,6 +504,30 @@ describe('UserService', () => { }); }); + describe('updateSecondarySchoolRole', () => { + describe('when user is not in the school yet', () => { + it('should add user with role to school', async () => { + const user = userDoFactory.buildWithId(); + const roleName = RoleName.GUESTTEACHER; + const role = roleFactory.buildWithId({ name: roleName }); + const school = schoolFactory.build(); + + userDORepo.findByIds.mockResolvedValueOnce([user]); + roleService.findByName.mockResolvedValueOnce(role); + + await service.addSecondarySchoolToUsers([user.id as string], school.id); + + expect(userDORepo.saveAll).toHaveBeenCalledWith([ + expect.objectContaining>({ + secondarySchools: [{ schoolId: school.id, role: { id: role.id, name: roleName } }], + }), + ]); + }); + // TODO: student + // TODO: user already in school + }); + }); + describe('saveAll is called', () => { it('should call the repo with given users', async () => { const users: UserDO[] = [userDoFactory.buildWithId()]; diff --git a/apps/server/src/modules/user/service/user.service.ts b/apps/server/src/modules/user/service/user.service.ts index badad094de1..66884c66a41 100644 --- a/apps/server/src/modules/user/service/user.service.ts +++ b/apps/server/src/modules/user/service/user.service.ts @@ -146,6 +146,17 @@ export class UserService implements DeletionService, IEventHandler { + const users = await this.userDORepo.findByIds(userIds, true); + const role = await this.roleService.findByName(RoleName.GUESTTEACHER); + + users.forEach((user) => { + user.secondarySchools.push({ schoolId, role: new RoleReference(role) }); + }); + await this.userDORepo.saveAll(users); + return Promise.resolve(); + } + async findByExternalId(externalId: string, systemId: EntityId): Promise { const user: Promise = this.userDORepo.findByExternalId(externalId, systemId);