Skip to content

Commit

Permalink
fix(user): incorrect logic in unfollow not removing relation from t…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
davidrdsilva committed Jun 16, 2024
1 parent 3c49e96 commit 35841d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
31 changes: 18 additions & 13 deletions src/providers/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,25 +224,30 @@ export class UserService {
return activities;
}

async follow(followerId: string, request: Request) {
async follow(request: Request, userToFollowId: string) {
const user: User = request['user'];
const follower = await this.userRepository.findOne({ where: { id: followerId }, relations: ['following'] });
const following = await this.userRepository.findOne({ where: { id: user.id } });
const follower = await this.userRepository.findOne({
where: { id: user.id },
relations: ['following'],
});
const userToFollow = await this.userRepository.findOne({ where: { id: userToFollowId } });

if (follower && following) {
follower.following.push(following);
return await this.userRepository.save(follower);
if (!follower || !userToFollow) {
throw new NotFoundException('Users not found.');
}

follower.following.push(userToFollow);

return await this.userRepository.save(follower);
}

async unfollow(followerId: string, request: Request) {
const requestUser: User = request['user'];
const follower = await this.userRepository.findOne({ where: { id: followerId }, relations: ['following'] });
async unfollow(request: Request, userToUnfollowId: string) {
const user: User = request['user'];
const follower = await this.userRepository.findOne({ where: { id: user.id }, relations: ['following'] });

if (follower) {
follower.following = follower.following.filter((user) => user.id !== requestUser.id);
return await this.userRepository.save(follower);
}
follower.following = follower.following.filter((user) => user.id !== userToUnfollowId);

return await this.userRepository.save(follower);
}

update(id: number, updateUserInput: UpdateUserInput) {
Expand Down
14 changes: 10 additions & 4 deletions src/resolvers/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ export class UserResolver {

@UseGuards(JwtAuthGuard)
@Mutation(() => User)
follow(@Args('followingId', { type: () => String }) followingId: string, @Context() context: { req: Request }) {
return this.userService.follow(followingId, context.req);
follow(
@Args('userToFollowId', { type: () => String }) userToFollowId: string,
@Context() context: { req: Request },
) {
return this.userService.follow(context.req, userToFollowId);
}

@UseGuards(JwtAuthGuard)
@Mutation(() => User)
unfollow(@Args('followingId', { type: () => String }) followingId: string, @Context() context: { req: Request }) {
return this.userService.unfollow(followingId, context.req);
unfollow(
@Args('userToUnfollowId', { type: () => String }) userToUnfollowId: string,
@Context() context: { req: Request },
) {
return this.userService.unfollow(context.req, userToUnfollowId);
}
}

0 comments on commit 35841d4

Please sign in to comment.