From bbdf98b952b1a2bb28b3e250400f8f244d85266c Mon Sep 17 00:00:00 2001 From: akm99 Date: Sun, 31 Mar 2024 17:46:12 -0400 Subject: [PATCH 1/4] Implement Get Blocked Users Route --- src/api/controllers/UserController.ts | 5 +++++ src/repositories/UserRepository.ts | 8 ++++++++ src/services/UserService.ts | 9 +++++++++ src/tests/UserTest.test.ts | 28 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) 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..4d5de66 100644 --- a/src/repositories/UserRepository.ts +++ b/src/repositories/UserRepository.ts @@ -19,6 +19,14 @@ export class UserRepository extends AbstractRepository { .getOne(); } + public async getUserBlocked(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..53a7a1f 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.getUserBlocked(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..5e692ef 100644 --- a/src/tests/UserTest.test.ts +++ b/src/tests/UserTest.test.ts @@ -13,6 +13,8 @@ let expectedUser: UserModel; let conn: Connection; let userController: UserController; +jest.setTimeout(200000000) + beforeAll(async () => { await DatabaseConnection.connect(); }); @@ -329,4 +331,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 From 765de259f491957414182db9740cb28ed96f7291 Mon Sep 17 00:00:00 2001 From: akm99 Date: Sun, 31 Mar 2024 17:48:50 -0400 Subject: [PATCH 2/4] Minor change --- src/services/UserService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/UserService.ts b/src/services/UserService.ts index 53a7a1f..e84fa3c 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -117,7 +117,7 @@ export class UserService { public async getBlockedUsersById(params: UuidParam): Promise { return this.transactions.readOnly(async (transactionalEntityManager) => { const userRepository = Repositories.user(transactionalEntityManager); - const user = await userRepository.getUserBlocked(params.id); + const user = await userRepository.getBlockedUsersById(params.id); if (!user) throw new NotFoundError('User not found!'); return user.blocking ?? []; }); From b53bc94adff3313a24b4f1a7aa3fd4ab759266b1 Mon Sep 17 00:00:00 2001 From: akm99 Date: Sun, 31 Mar 2024 17:49:18 -0400 Subject: [PATCH 3/4] Minor change 2 --- src/repositories/UserRepository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repositories/UserRepository.ts b/src/repositories/UserRepository.ts index 4d5de66..261bc9c 100644 --- a/src/repositories/UserRepository.ts +++ b/src/repositories/UserRepository.ts @@ -19,7 +19,7 @@ export class UserRepository extends AbstractRepository { .getOne(); } - public async getUserBlocked(id: Uuid): Promise { + public async getBlockedUsersById(id: Uuid): Promise { return await this.repository .createQueryBuilder("user") .leftJoinAndSelect("user.blocking", "user_blocking_users.blocking") From 9ba5e46a4df0c7b5eb3ddd510798607cce818b85 Mon Sep 17 00:00:00 2001 From: akm99 Date: Mon, 1 Apr 2024 12:36:50 -0400 Subject: [PATCH 4/4] Remove timeout --- src/tests/UserTest.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/tests/UserTest.test.ts b/src/tests/UserTest.test.ts index 5e692ef..d0d8a4e 100644 --- a/src/tests/UserTest.test.ts +++ b/src/tests/UserTest.test.ts @@ -13,8 +13,6 @@ let expectedUser: UserModel; let conn: Connection; let userController: UserController; -jest.setTimeout(200000000) - beforeAll(async () => { await DatabaseConnection.connect(); });