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 all 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
48 changes: 47 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 Down Expand Up @@ -329,4 +329,50 @@ 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;

userRepo.deleteUser.mockResolvedValue(0);

return {
userId,
};
};

it('should return 0', async () => {
const { userId } = setup();

const result = await service.deleteUser(userId);

expect(result).toEqual(0);
});
});

describe('when deleting by userId', () => {
const setup = () => {
const user1: User = userFactory.asStudent().buildWithId();
userFactory.asStudent().buildWithId();

userRepo.findById.mockResolvedValue(user1);
userRepo.deleteUser.mockResolvedValue(1);

return {
user1,
};
};

it('should delete user by userId', async () => {
const { user1 } = setup();

const result = await service.deleteUser(user1.id);

expect(userRepo.deleteUser).toHaveBeenCalledWith(user1.id);
expect(result).toEqual(1);
});
});
});
});
8 changes: 7 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 } 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,10 @@ export class UserService {
throw new BadRequestException('Language is not activated.');
}
}

async deleteUser(userId: EntityId): Promise<number> {
const deletedUserNumber: Promise<number> = this.userRepo.deleteUser(userId);

return deletedUserNumber;
}
}
41 changes: 41 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,45 @@ describe('user repo', () => {
expect(user.id).not.toBeNull();
});
});

describe('delete', () => {
const setup = async () => {
const user1: User = userFactory.buildWithId();
const user2: User = userFactory.buildWithId();
const user3: User = userFactory.buildWithId();
await em.persistAndFlush([user1, user2, user3]);

return {
user1,
user2,
user3,
};
};
it('should delete user', async () => {
const { user1, user2, user3 } = await setup();
const deleteResult = await repo.deleteUser(user1.id);
expect(deleteResult).toEqual(1);

const result1 = await em.find(User, { id: user1.id });
expect(result1).toHaveLength(0);

const result2 = await repo.findById(user2.id);
expect(result2).toMatchObject({
firstName: user2.firstName,
lastName: user2.lastName,
email: user2.email,
roles: user2.roles,
school: user2.school,
});
sszafGCA marked this conversation as resolved.
Show resolved Hide resolved

const result3 = await repo.findById(user3.id);
expect(result3).toMatchObject({
firstName: user3.firstName,
lastName: user3.lastName,
email: user3.email,
roles: user3.roles,
school: user3.school,
});
});
});
});
7 changes: 7 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,13 @@ export class UserRepo extends BaseRepo<User> {
return promise;
}

async deleteUser(userId: EntityId): Promise<number> {
const deletedUserNumber: Promise<number> = this._em.nativeDelete(User, {
id: userId,
});
return deletedUserNumber;
}

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