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(':relationshipId')
@Delete('myfriends/:relationshipId')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
remove(@Param('relationshipId') relationshipId: string, @Req() req) {

View File

@@ -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<Friendship> = {
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<Friendship> = {
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) {