Skip to content

Commit

Permalink
Merge pull request #78 from cuappdev/unblockuserrequest
Browse files Browse the repository at this point in the history
Add unblock user request
  • Loading branch information
akmatchev authored Mar 27, 2024
2 parents d0ec877 + d041855 commit 9dad55e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Body, CurrentUser, Get, JsonController, Param, Params, Post, Delete} fr

import { UserModel } from '../../models/UserModel';
import { UserService } from '../../services/UserService';
import { BlockUserRequest, EditProfileRequest, GetUserByEmailRequest, GetUserResponse, GetUsersResponse, SaveTokenRequest, SetAdminByEmailRequest } from '../../types';
import { BlockUserRequest, UnblockUserRequest, EditProfileRequest, GetUserByEmailRequest, GetUserResponse, GetUsersResponse, SaveTokenRequest, SetAdminByEmailRequest } from '../../types';
import { UuidParam } from '../validators/GenericRequests';

@JsonController('user/')
Expand Down Expand Up @@ -55,8 +55,8 @@ export class UserController {
}

@Post('unblock/')
async unblockUser(@Body() blockUserRequest: BlockUserRequest, @CurrentUser() user: UserModel): Promise<GetUserResponse> {
return { user: await this.userService.unblockUser(user, blockUserRequest) }
async unblockUser(@Body() unblockUserRequest: UnblockUserRequest, @CurrentUser() user: UserModel): Promise<GetUserResponse> {
return { user: await this.userService.unblockUser(user, unblockUserRequest) }
}

@Delete('id/:id/')
Expand Down
8 changes: 4 additions & 4 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { InjectManager } from 'typeorm-typedi-extensions';
import { UuidParam } from '../api/validators/GenericRequests';
import { UserModel } from '../models/UserModel';
import Repositories, { TransactionsManager } from '../repositories';
import { EditProfileRequest, SaveTokenRequest, SetAdminByEmailRequest, BlockUserRequest } from '../types';
import { EditProfileRequest, SaveTokenRequest, SetAdminByEmailRequest, BlockUserRequest, UnblockUserRequest } from '../types';
import { uploadImage } from '../utils/Requests';

@Service()
Expand Down Expand Up @@ -102,12 +102,12 @@ export class UserService {
});
}

public async unblockUser(user: UserModel, blockUserRequest: BlockUserRequest): Promise<UserModel> {
public async unblockUser(user: UserModel, blockUserRequest: UnblockUserRequest): Promise<UserModel> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
const blocked = await userRepository.getUserById(blockUserRequest.blocked);
const blocked = await userRepository.getUserById(blockUserRequest.unblocked);
if (!blocked) throw new NotFoundError('Blocked user not found!');
if (!user.blocking?.find((blockedUser) => blockedUser.id === blockUserRequest.blocked)) {
if (!user.blocking?.find((blockedUser) => blockedUser.id === blockUserRequest.unblocked)) {
throw new UnauthorizedError('User is not blocked!');
}
return userRepository.unblockUser(user, blocked);
Expand Down
4 changes: 2 additions & 2 deletions src/tests/UserTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('user tests', () => {
expect(blockUserResponse.user?.blockers).toBeUndefined();
expect(user1.blocking).toHaveLength(1);

const unblockUserResponse = await userController.unblockUser({blocked: user2.id}, user1);
const unblockUserResponse = await userController.unblockUser({unblocked: user2.id}, user1);
expect(unblockUserResponse.user?.blocking).toBeUndefined();
expect(unblockUserResponse.user?.blockers).toBeUndefined();
});
Expand All @@ -264,7 +264,7 @@ describe('user tests', () => {
.write();

try {
await userController.unblockUser({blocked: user2.id}, user1);
await userController.unblockUser({unblocked: user2.id}, user1);
} catch (error) {
expect(error.message).toBe('User is not blocked!');
}
Expand Down
4 changes: 4 additions & 0 deletions src/types/ApiRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export interface BlockUserRequest {
blocked: Uuid;
}

export interface UnblockUserRequest {
unblocked: Uuid;
}

// POST

export interface CreatePostRequest {
Expand Down

0 comments on commit 9dad55e

Please sign in to comment.