Skip to content

Commit

Permalink
wip begin working on adding guests to schools
Browse files Browse the repository at this point in the history
  • Loading branch information
Metauriel committed Nov 26, 2024
1 parent eaf18e2 commit 6c24523
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 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 @@ -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<Partial<UserDO>>({
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()];
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 @@ -146,6 +146,17 @@ export class UserService implements DeletionService, IEventHandler<UserDeletedEv
return result;
}

async addSecondarySchoolToUsers(userIds: string[], schoolId: EntityId): Promise<void> {
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<UserDO | null> {
const user: Promise<UserDO | null> = this.userDORepo.findByExternalId(externalId, systemId);

Expand Down

0 comments on commit 6c24523

Please sign in to comment.