Skip to content

Commit

Permalink
Merge pull request #80 from cuappdev/getblockedusers
Browse files Browse the repository at this point in the history
Implement Get Blocked Users Route
  • Loading branch information
akmatchev authored Apr 1, 2024
2 parents 9dad55e + 9ba5e46 commit a895bac
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetUsersResponse> {
return { users: await this.userService.getBlockedUsersById(params) };
}

@Delete('id/:id/')
async deleteUser(@Params() params: UuidParam, @CurrentUser() user: UserModel): Promise<GetUserResponse> {
return { user: await this.userService.deleteUser(user, params) };
Expand Down
8 changes: 8 additions & 0 deletions src/repositories/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export class UserRepository extends AbstractRepository<UserModel> {
.getOne();
}

public async getBlockedUsersById(id: Uuid): Promise<UserModel | undefined> {
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<UserModel | undefined> {
return await this.repository
.createQueryBuilder("user")
Expand Down
9 changes: 9 additions & 0 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export class UserService {
});
}

public async getBlockedUsersById(params: UuidParam): Promise<UserModel[]> {
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<UserModel> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
Expand Down
26 changes: 26 additions & 0 deletions src/tests/UserTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit a895bac

Please sign in to comment.