diff --git a/src/api/controllers/UserController.ts b/src/api/controllers/UserController.ts index f8578d7..23dbdc7 100644 --- a/src/api/controllers/UserController.ts +++ b/src/api/controllers/UserController.ts @@ -59,6 +59,11 @@ export class UserController { return { user: await this.userService.unblockUser(user, unblockUserRequest) } } + @Post('id/:id/blocked/') + async getBlockedUsersById(@Params() params: UuidParam): Promise { + return { users: await this.userService.getBlockedUsersById(params) }; + } + @Delete('id/:id/') async deleteUser(@Params() params: UuidParam, @CurrentUser() user: UserModel): Promise { return { user: await this.userService.deleteUser(user, params) }; diff --git a/src/repositories/UserRepository.ts b/src/repositories/UserRepository.ts index 6d6a63e..261bc9c 100644 --- a/src/repositories/UserRepository.ts +++ b/src/repositories/UserRepository.ts @@ -19,6 +19,14 @@ export class UserRepository extends AbstractRepository { .getOne(); } + public async getBlockedUsersById(id: Uuid): Promise { + return await this.repository + .createQueryBuilder("user") + .leftJoinAndSelect("user.blocking", "user_blocking_users.blocking") + .where("user.id = :id", { id }) + .getOne(); + } + public async getUserByGoogleId(googleId: Uuid): Promise { return await this.repository .createQueryBuilder("user") diff --git a/src/services/UserService.ts b/src/services/UserService.ts index 37346b6..e84fa3c 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -114,6 +114,15 @@ export class UserService { }); } + public async getBlockedUsersById(params: UuidParam): Promise { + return this.transactions.readOnly(async (transactionalEntityManager) => { + const userRepository = Repositories.user(transactionalEntityManager); + const user = await userRepository.getBlockedUsersById(params.id); + if (!user) throw new NotFoundError('User not found!'); + return user.blocking ?? []; + }); + } + public async deleteUser(user: UserModel, params: UuidParam): Promise { return this.transactions.readWrite(async (transactionalEntityManager) => { const userRepository = Repositories.user(transactionalEntityManager); diff --git a/src/tests/UserTest.test.ts b/src/tests/UserTest.test.ts index 1bc2d5b..d0d8a4e 100644 --- a/src/tests/UserTest.test.ts +++ b/src/tests/UserTest.test.ts @@ -329,4 +329,30 @@ describe('user tests', () => { expect(error.message).toBe('User does not have permission to delete other users'); } }); + + test('get blocked users by id - no blocked users', async () => { + const user = UserFactory.fake(); + + await new DataFactory() + .createUsers(user) + .write(); + + const userUuid = {id: user.id}; + + const getBlockedUsersResponse = await userController.getBlockedUsersById(userUuid); + expect(getBlockedUsersResponse.users).toHaveLength(0); + }); + + test('get blocked users by id', async () => { + const [user1, user2] = UserFactory.create(2); + + await new DataFactory() + .createUsers(user1, user2) + .write(); + + await userController.blockUser({blocked: user2.id}, user1); + const user1Uuid = {id: user1.id}; + const getBlockedUsersResponse = await userController.getBlockedUsersById(user1Uuid); + expect(getBlockedUsersResponse.users).toHaveLength(1); + }); }); \ No newline at end of file