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

BC-4579- Rewriting the user module user data deletion method #4441

Merged
merged 19 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
51 changes: 50 additions & 1 deletion apps/server/src/modules/user/service/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -13,6 +13,7 @@ import { ICurrentUser } from '@src/modules/authentication';
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 { InternalServerErrorException } from '@nestjs/common';
import { UserQuery } from './user-query.type';

describe('UserService', () => {
Expand Down Expand Up @@ -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);
});
});
});
});
12 changes: 11 additions & 1 deletion apps/server/src/modules/user/service/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { EntityId, IFindOptions, LanguageType, User } from '@shared/domain';
import { RoleReference } from '@shared/domain/domainobject';
Expand All @@ -12,6 +11,7 @@ import { ICurrentUser } from '@src/modules/authentication';
import { CurrentUserMapper } from '@src/modules/authentication/mapper';
import { RoleDto } from '@src/modules/role/service/dto/role.dto';
import { RoleService } from '@src/modules/role/service/role.service';
import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common';
import { IUserConfig } from '../interfaces';
import { UserMapper } from '../mapper/user.mapper';
import { UserDto } from '../uc/dto/user.dto';
Expand Down Expand Up @@ -107,4 +107,14 @@ export class UserService {
throw new BadRequestException('Language is not activated.');
}
}

async deleteUser(userId: EntityId): Promise<void> {
if (!userId) {
throw new InternalServerErrorException('User id is missing');
}
sszafGCA marked this conversation as resolved.
Show resolved Hide resolved

const user = await this.userRepo.findById(userId);

await this.userRepo.delete(user);
}
}
22 changes: 22 additions & 0 deletions apps/server/src/shared/repo/user/user.repo.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
4 changes: 4 additions & 0 deletions apps/server/src/shared/repo/user/user.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export class UserRepo extends BaseRepo<User> {
return promise;
}

async delete(user: User): Promise<void> {
await this._em.removeAndFlush(user);
}
bn-pass marked this conversation as resolved.
Show resolved Hide resolved

private async populateRoles(roles: Role[]): Promise<void> {
for (let i = 0; i < roles.length; i += 1) {
const role = roles[i];
Expand Down