Skip to content

Commit

Permalink
impl deletion in user module
Browse files Browse the repository at this point in the history
  • Loading branch information
sszafGCA committed Sep 26, 2023
1 parent d324a16 commit 9eb3fbb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
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 @@ -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';

Check failure on line 17 in apps/server/src/modules/user/service/user.service.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

`@nestjs/common` import should occur before import of `./user-query.type`

describe('UserService', () => {
let service: 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,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';
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');
}

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);
});
});
});
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 @@ -153,6 +153,10 @@ export class UserRepo extends BaseRepo<User> {
return promise;
}

async delete(user: User): Promise<void> {
await this._em.removeAndFlush(user);
}

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

0 comments on commit 9eb3fbb

Please sign in to comment.