diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 899c695..874f9aa 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -138,14 +138,12 @@ export class UsersController { @FtUser() profile: Profile, @Param('username') username: string ): Promise { - const target: User | null = await this.usersService.findUserByName( - username - ) - if (target == null) throw new BadRequestException('Target unknown.') - if (profile.id === target.ftId) { + const target: User | null = await this.usersService.findUserByName( username ) + if (!target) throw new BadRequestException(`User ${username} not found.`) + if (+profile.id === target.ftId) throw new BadRequestException("You can't invit yourself.") - } - await this.usersService.invit(profile.id, target.id) + const ret: string = await this.usersService.invit(profile.id, target.ftId); + if (ret !== "OK") throw new BadRequestException(ret) } @Get('avatar/:id') diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 7526dd0..a38ea6a 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -142,7 +142,7 @@ export class UsersService { return leaderboard.findIndex((user) => user.ftId === ftId); } - async invit(ftId: number, targetFtId: number): Promise { + async invit(ftId: number, targetFtId: number): Promise { const user: User | null = await this.usersRepository.findOne({ where: { ftId }, relations: { @@ -150,9 +150,9 @@ export class UsersService { friends: true, }, }); - if (user == null) throw new BadRequestException("User not found."); + if (!user) throw new BadRequestException("User not found."); if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { - throw new BadRequestException("You are already friends."); + return "You are already friends."; } const target: User | null = await this.usersRepository.findOne({ where: { ftId: targetFtId }, @@ -161,16 +161,20 @@ export class UsersService { friends: true, }, }); - if (target == null) throw new BadRequestException("Target not found."); + if (!target) return "Target not found."; const id = user.followers.findIndex( (follower) => follower.ftId === targetFtId ); - if (id !== -1) { + if (target.followers.findIndex((follower) => follower.ftId === user.ftId) !== -1) { + return "Invitation already sent."; + }else if (user.followers.findIndex((follower) => follower.ftId === targetFtId) !== -1) { user.friends.push(target); - if (user.ftId !== target.ftId) target.friends.push(user); + target.friends.push(user); user.followers.slice(id, 1); await this.usersRepository.save(user); - } else target.followers.push(user); + } else + target.followers.push(user); await this.usersRepository.save(target); + return "OK" } } diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 14670ec..d8ffb5e 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -250,7 +250,7 @@ {/if} {#if appState === APPSTATE.HISTORY_ID}
- +
{/if} {#if appState === APPSTATE.PROFILE} diff --git a/front/volume/src/components/Friends.svelte b/front/volume/src/components/Friends.svelte index f5a37d7..39bc9c0 100644 --- a/front/volume/src/components/Friends.svelte +++ b/front/volume/src/components/Friends.svelte @@ -19,7 +19,8 @@ if (response.ok) { alert("Invitation send."); } else { - alert("Invitation failed."); + const error = (await response.json()).message + alert("Invitation failed: " + error);; } } diff --git a/front/volume/src/components/MatchHistory.svelte b/front/volume/src/components/MatchHistory.svelte index 1475701..86216bb 100644 --- a/front/volume/src/components/MatchHistory.svelte +++ b/front/volume/src/components/MatchHistory.svelte @@ -9,6 +9,7 @@