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

This commit is contained in:
Me
2022-12-16 04:14:44 +01:00
parent db02946268
commit de06007d60
3 changed files with 114 additions and 48 deletions

View File

@@ -114,7 +114,7 @@ export class FriendshipController {
} }
// DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId // DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId
@Delete(':relationshipId') @Delete('myfriends/:relationshipId')
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
remove(@Param('relationshipId') relationshipId: string, @Req() req) { remove(@Param('relationshipId') relationshipId: string, @Req() req) {

View File

@@ -24,7 +24,8 @@ export class FriendshipService {
} }
async findOneFriendByUsername(friendUsername : string, username : string) { async findOneFriendByUsername(friendUsername : string, username : string) {
const friendship = await this.friendshipRepository // const friendship = await this.friendshipRepository
let friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where( .where(
new Brackets((qb) => { new Brackets((qb) => {
@@ -50,8 +51,40 @@ export class FriendshipService {
) )
.getOne() .getOne()
console.log('Find one friend by username: ') // console.log('MIDDLE Find one friend by username: ')
console.log({...friendship}) // 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) { if (!friendship) {
throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND); 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.findOneBy({where: {id: +relationshipId }, relations: ['sender', 'receiver']});
// const relation = await this.friendshipRepository.findOne({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('.service accept friendship')
console.log({...relation}) // console.log({...relation})
if (!relation) if (!relation[0])
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
// console.log(relation.sender) // 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... // 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) { async declineFriendship(relationshipId: string, user: User) {
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); // const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
if (!relation) 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); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.sender.id === user.id) { if (relation[0].sender.id === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); throw new HttpException(`You can't decline your own request.`, HttpStatus.NOT_FOUND);
} }
relation.status = FriendshipStatus.DECLINED; relation[0].status = FriendshipStatus.DECLINED;
const savedFriendship = this.friendshipRepository.save(relation); const savedFriendship = this.friendshipRepository.save(relation[0]);
const partialFriendship : Partial<Friendship> = { const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id, id : (await savedFriendship).id,
date : (await savedFriendship).date, date : (await savedFriendship).date,
@@ -311,14 +345,15 @@ VS ------------------------------------------------
} }
async blockFriendship(relationshipId: string, user: User) { async blockFriendship(relationshipId: string, user: User) {
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); // const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
if (!relation) 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); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.sender.id === user.id) { if (relation[0].sender.id === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND);
} }
relation.status = FriendshipStatus.BLOCKED; relation[0].status = FriendshipStatus.BLOCKED;
const savedFriendship = this.friendshipRepository.save(relation); const savedFriendship = this.friendshipRepository.save(relation[0]);
const partialFriendship : Partial<Friendship> = { const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id, id : (await savedFriendship).id,
date : (await savedFriendship).date, date : (await savedFriendship).date,
@@ -329,13 +364,20 @@ VS ------------------------------------------------
} }
async removeFriendship(relationshipId: string, user : User) { async removeFriendship(relationshipId: string, user : User) {
const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId }); // const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId });
if (!friendship) // 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); 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); 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) { async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) {

View File

@@ -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() => { const fetchAllUsers = async() => {
allUsers = await fetch('http://transcendance:8080/api/v2/user/all') allUsers = await fetch('http://transcendance:8080/api/v2/user/all')
.then( x => x.json() ); .then( x => x.json() );
@@ -69,22 +96,6 @@
console.log({...requestsRecieved}) 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; let sentFriendRequest;
const sendFriendRequest = async(potentialFriendUsername) => { const sendFriendRequest = async(potentialFriendUsername) => {
set.friendUsername = ''; 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) => { const acceptFriendRequest = async(relationshipId) => {
console.log('accept friend request') console.log('accept friend request')
@@ -157,6 +159,20 @@
await areWeFriends(usernameBeingViewed); 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) => { const unfriend = async(relationshipId) => {
console.log('Unfriend') console.log('Unfriend')
friendshipFetch = undefined; friendshipFetch = undefined;
@@ -252,10 +268,18 @@
{:else} {:else}
<!-- <Button type="secondary" on:click={() => acceptFriendRequest(usernameBeingViewed)}>Unfriend</Button> --> <!-- <Button type="secondary" on:click={() => acceptFriendRequest(usernameBeingViewed)}>Unfriend</Button> -->
<Button type="secondary" on:click={() => acceptFriendRequest(friendshipStatusFull.id)}>Accept Friend Request</Button> <Button type="secondary" on:click={() => acceptFriendRequest(friendshipStatusFull.id)}>Accept Friend Request</Button>
<Button on:click={() => declineFriendRequest(friendshipStatusFull.id)}>Decline Friend Request</Button>
{/if} {/if}
{:else if friendshipStatusFull.status === 'A'} {:else if friendshipStatusFull.status === 'A'}
<div class="tile">You are Friends</div> <div class="tile">You are Friends</div>
<Button on:click={() => unfriend(friendshipStatusFull.id)}>Unfriend</Button> <Button on:click={() => unfriend(friendshipStatusFull.id)}>Unfriend</Button>
{:else if friendshipStatusFull.status === 'D'}
{#if friendshipStatusFull.senderUsername === user.username}
<div class="tile">Your friend request was denied, hang in there bud.</div>
{:else}
<div class="tile">You declined the friend request but you could still change your mind</div>
<Button on:click={() => acceptFriendRequest(friendshipStatusFull.id)}>acceptFriendRequest</Button>
{/if}
{/if} {/if}
{:else} {:else}
<Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button> <Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button>