From de06007d60dc95ca593f5e701006cced692bfb97 Mon Sep 17 00:00:00 2001 From: Me Date: Fri, 16 Dec 2022 04:14:44 +0100 Subject: [PATCH] fixed some stuff in the Friendship backend, but had to cut some corners, it's kinda gross, will fix later, better flow to frontend friendship page, most stuff works, still a few kinks to work out --- .../src/friendship/friendship.controller.ts | 2 +- .../src/friendship/friendship.service.ts | 86 ++++++++++++++----- .../src/pages/profile/ProfileFriends.svelte | 74 ++++++++++------ 3 files changed, 114 insertions(+), 48 deletions(-) diff --git a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts index 5e565ee8..02afb571 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -114,7 +114,7 @@ export class FriendshipController { } // DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId - @Delete(':relationshipId') + @Delete('myfriends/:relationshipId') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) remove(@Param('relationshipId') relationshipId: string, @Req() req) { diff --git a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts index 03ff021b..5482fdd5 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -24,7 +24,8 @@ export class FriendshipService { } async findOneFriendByUsername(friendUsername : string, username : string) { - const friendship = await this.friendshipRepository + // const friendship = await this.friendshipRepository + let friendship = await this.friendshipRepository .createQueryBuilder('friendship') .where( new Brackets((qb) => { @@ -50,8 +51,40 @@ export class FriendshipService { ) .getOne() - console.log('Find one friend by username: ') - console.log({...friendship}) + // console.log('MIDDLE Find one friend by username: ') + // console.log({...friendship}) + + if (!friendship) { + friendship = await this.friendshipRepository + .createQueryBuilder('friendship') + .where( + new Brackets((qb) => { + qb.where( + new Brackets((subAQb) => { + subAQb.where('friendship.senderUsername = :username', {username : username}) + .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : friendUsername}) + }) + ) + .orWhere( + new Brackets((subBQb) => { + subBQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername}) + .andWhere('friendship.receiverUsername = :username', {username : username}) + }) + ) + }), + ) + .andWhere( + new Brackets((qb2) => { + // qb2.where('friendship.status = :status', {status : FriendshipStatus.ACCEPTED}) + // .orWhere('friendship.status = :status', {status : FriendshipStatus.REQUESTED}) + qb2.where('friendship.status = :status', {status : FriendshipStatus.REQUESTED}) + }), + ) + .getOne() + } + + // console.log('END Find one friend by username: ') + // console.log({...friendship}) if (!friendship) { throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND); @@ -266,9 +299,9 @@ VS ------------------------------------------------ // const relation = await this.friendshipRepository.findOneBy({where: {id: +relationshipId }, relations: ['sender', 'receiver']}); // const relation = await this.friendshipRepository.findOne({where: {id: +relationshipId }, relations: ['sender', 'receiver']}); - console.log('.service accept friendship') - console.log({...relation}) - if (!relation) + // console.log('.service accept friendship') + // console.log({...relation}) + if (!relation[0]) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); // console.log(relation.sender) // ok so you can't query sender cuz it's not a column in the entity, not sure how you access it then... @@ -293,14 +326,15 @@ VS ------------------------------------------------ } async declineFriendship(relationshipId: string, user: User) { - const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); - if (!relation) + // const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); + if (!relation[0]) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (relation.sender.id === user.id) { - throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); + if (relation[0].sender.id === user.id) { + throw new HttpException(`You can't decline your own request.`, HttpStatus.NOT_FOUND); } - relation.status = FriendshipStatus.DECLINED; - const savedFriendship = this.friendshipRepository.save(relation); + relation[0].status = FriendshipStatus.DECLINED; + const savedFriendship = this.friendshipRepository.save(relation[0]); const partialFriendship : Partial = { id : (await savedFriendship).id, date : (await savedFriendship).date, @@ -311,14 +345,15 @@ VS ------------------------------------------------ } async blockFriendship(relationshipId: string, user: User) { - const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); - if (!relation) + // const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); + if (!relation[0]) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (relation.sender.id === user.id) { - throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); + if (relation[0].sender.id === user.id) { + throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND); } - relation.status = FriendshipStatus.BLOCKED; - const savedFriendship = this.friendshipRepository.save(relation); + relation[0].status = FriendshipStatus.BLOCKED; + const savedFriendship = this.friendshipRepository.save(relation[0]); const partialFriendship : Partial = { id : (await savedFriendship).id, date : (await savedFriendship).date, @@ -329,13 +364,20 @@ VS ------------------------------------------------ } async removeFriendship(relationshipId: string, user : User) { - const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId }); - if (!friendship) + // const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + // once again we need find() not findOneBy() so once again have to grab first elem of an array + const friendship = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); + console.log('deleting a friendship .service') + console.log({...friendship}) + console.log('who is the user') + console.log({...user}) + if (!friendship[0]) throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND); - if (friendship.sender.id !== user.id || friendship.receiver.id !== user.id) { + if (friendship[0].sender.id !== user.id && friendship[0].receiver.id !== user.id) { throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN); } - return this.friendshipRepository.remove(friendship); + console.log('.service deleted a friendship') + return this.friendshipRepository.remove(friendship[0]); } async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) { diff --git a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte index 565d54bc..20ec692e 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -41,6 +41,33 @@ }); + /* TMP for Testing */ + let cherifFetch; + const fetchCherif = async() => { + cherifFetch = await fetch('http://transcendance:8080/api/v2/network/myfriends/chbadad') + .then( x => x.json() ); + console.log('Cherif Fetched ') + console.log({...cherifFetch}) + }; + + let ericFetch; + const fetchEric = async() => { + ericFetch = await fetch('http://transcendance:8080/api/v2/network/myfriends/erlazo') + .then( x => x.json() ); + console.log('Eric Fetched ') + console.log({...ericFetch}) + }; + + let tmpTest; + const testBack = async(relationshipId) => { + console.log('test back request') + tmpTest = undefined; + // PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/test + tmpTest = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/test`) + .then( x => x.json()); + }; + /* End of Tests */ + const fetchAllUsers = async() => { allUsers = await fetch('http://transcendance:8080/api/v2/user/all') .then( x => x.json() ); @@ -69,22 +96,6 @@ console.log({...requestsRecieved}) }; - let cherifFetch; - const fetchCherif = async() => { - cherifFetch = await fetch('http://transcendance:8080/api/v2/network/myfriends/chbadad') - .then( x => x.json() ); - console.log('Cherif Fetched ') - console.log({...cherifFetch}) - }; - - let ericFetch; - const fetchEric = async() => { - ericFetch = await fetch('http://transcendance:8080/api/v2/network/myfriends/erlazo') - .then( x => x.json() ); - console.log('Eric Fetched ') - console.log({...ericFetch}) - }; - let sentFriendRequest; const sendFriendRequest = async(potentialFriendUsername) => { set.friendUsername = ''; @@ -134,15 +145,6 @@ }; - let tmpTest; - const testBack = async(relationshipId) => { - console.log('test back request') - tmpTest = undefined; - // PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/test - tmpTest = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/test`) - .then( x => x.json()); - }; - const acceptFriendRequest = async(relationshipId) => { console.log('accept friend request') @@ -157,6 +159,20 @@ await areWeFriends(usernameBeingViewed); }; + const declineFriendRequest = async(relationshipId) => { + console.log('decline friend request') + friendshipFetch = undefined; + // PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/decline + friendshipFetch = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/decline`, { + method: "PATCH"}) + .then( x => x.json()); + // maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now... + console.log('declined friend request, now response') + console.log({...friendshipFetch}) + await areWeFriends(usernameBeingViewed); + }; + + const unfriend = async(relationshipId) => { console.log('Unfriend') friendshipFetch = undefined; @@ -252,10 +268,18 @@ {:else} + {/if} {:else if friendshipStatusFull.status === 'A'}
You are Friends
+ {:else if friendshipStatusFull.status === 'D'} + {#if friendshipStatusFull.senderUsername === user.username} +
Your friend request was denied, hang in there bud.
+ {:else} +
You declined the friend request but you could still change your mind
+ + {/if} {/if} {:else}