From 9eb3fbb50208bf1e65bf8a2318bbbe7330024109 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Tue, 26 Sep 2023 17:08:26 +0200 Subject: [PATCH] impl deletion in user module --- .../modules/user/service/user.service.spec.ts | 51 ++++++++++++++++++- .../src/modules/user/service/user.service.ts | 12 ++++- .../repo/user/user.repo.integration.spec.ts | 22 ++++++++ apps/server/src/shared/repo/user/user.repo.ts | 4 ++ 4 files changed, 87 insertions(+), 2 deletions(-) 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 ea29891cee7..8e1f569f46b 100644 --- a/apps/server/src/modules/user/service/user.service.spec.ts +++ b/apps/server/src/modules/user/service/user.service.spec.ts @@ -2,7 +2,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { EntityManager } from '@mikro-orm/core'; import { ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; -import { IFindOptions, LanguageType, Permission, Role, RoleName, SortOrder, User } from '@shared/domain'; +import { EntityId, IFindOptions, LanguageType, Permission, Role, RoleName, SortOrder, User } from '@shared/domain'; import { UserDO } from '@shared/domain/domainobject/user.do'; import { UserRepo } from '@shared/repo'; import { UserDORepo } from '@shared/repo/user/user-do.repo'; @@ -14,6 +14,7 @@ import { RoleService } from '@src/modules/role/service/role.service'; import { UserService } from '@src/modules/user/service/user.service'; import { UserDto } from '@src/modules/user/uc/dto/user.dto'; import { UserQuery } from './user-query.type'; +import { InternalServerErrorException } from '@nestjs/common'; describe('UserService', () => { let service: UserService; @@ -329,4 +330,52 @@ describe('UserService', () => { expect(userDORepo.saveAll).toHaveBeenCalledWith(users); }); }); + + describe('deleteUser', () => { + describe('when user is missing', () => { + const setup = () => { + const user: UserDO = userDoFactory.build({ id: undefined }); + const userId: EntityId = user.id as EntityId; + + return { + userId, + }; + }; + + it('should throw an error', async () => { + const { userId } = setup(); + + await expect(service.deleteUser(userId)).rejects.toThrowError(InternalServerErrorException); + }); + }); + + describe('when deleting by userId', () => { + const setup = () => { + const user1: User = userFactory.asStudent().buildWithId(); + userFactory.asStudent().buildWithId(); + + userRepo.findById.mockResolvedValue(user1); + + return { + user1, + }; + }; + + it('should call userRepo.findById', async () => { + const { user1 } = setup(); + + await service.deleteUser(user1.id); + + expect(userRepo.findById).toBeCalledWith(user1.id); + }); + + it('should call userRepo.findById', async () => { + const { user1 } = setup(); + + await service.deleteUser(user1.id); + + expect(userRepo.delete).toBeCalledWith(user1); + }); + }); + }); }); diff --git a/apps/server/src/modules/user/service/user.service.ts b/apps/server/src/modules/user/service/user.service.ts index 8f7bf0beeba..29e86aff751 100644 --- a/apps/server/src/modules/user/service/user.service.ts +++ b/apps/server/src/modules/user/service/user.service.ts @@ -1,4 +1,4 @@ -import { BadRequestException, Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { EntityId, IFindOptions, LanguageType, User } from '@shared/domain'; import { RoleReference } from '@shared/domain/domainobject'; @@ -107,4 +107,14 @@ export class UserService { throw new BadRequestException('Language is not activated.'); } } + + async deleteUser(userId: EntityId): Promise { + if (!userId) { + throw new InternalServerErrorException('User id is missing'); + } + + const user = await this.userRepo.findById(userId); + + await this.userRepo.delete(user); + } } diff --git a/apps/server/src/shared/repo/user/user.repo.integration.spec.ts b/apps/server/src/shared/repo/user/user.repo.integration.spec.ts index b20522a0629..b23b4a84fc4 100644 --- a/apps/server/src/shared/repo/user/user.repo.integration.spec.ts +++ b/apps/server/src/shared/repo/user/user.repo.integration.spec.ts @@ -399,4 +399,26 @@ describe('user repo', () => { expect(user.id).not.toBeNull(); }); }); + + describe('delete', () => { + it('should delete user', async () => { + // Arrange + const user1: User = userFactory.buildWithId(); + const user2: User = userFactory.buildWithId(); + const user3: User = userFactory.buildWithId(); + + await em.persistAndFlush([user1, user2, user3]); + // em.clear(); + + await repo.delete(user1); + const result1 = await em.find(User, { id: user1.id }); + expect(result1).toHaveLength(0); + + const result2 = await repo.findById(user2.id); + expect(result2).toBeInstanceOf(User); + + const result3 = await repo.findById(user3.id); + expect(result3).toBeInstanceOf(User); + }); + }); }); diff --git a/apps/server/src/shared/repo/user/user.repo.ts b/apps/server/src/shared/repo/user/user.repo.ts index b0e5887c60e..de8ff728dc6 100644 --- a/apps/server/src/shared/repo/user/user.repo.ts +++ b/apps/server/src/shared/repo/user/user.repo.ts @@ -153,6 +153,10 @@ export class UserRepo extends BaseRepo { return promise; } + async delete(user: User): Promise { + await this._em.removeAndFlush(user); + } + private async populateRoles(roles: Role[]): Promise { for (let i = 0; i < roles.length; i += 1) { const role = roles[i];