diff --git a/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts b/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts index ce813d82..a59e195f 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts @@ -2,10 +2,12 @@ import { IsEnum, IsNotEmpty, IsString, IsPositive } from 'class-validator'; import { FriendshipStatus } from '../entities/friendship.entity'; export class CreateFriendshipDto { - // @IsNotEmpty() @IsPositive() // @Max(1000) ? readonly receiverId: number; + @IsNotEmpty() + @IsString() + readonly receiverUsername: string; @IsEnum(FriendshipStatus) readonly status: FriendshipStatus; } diff --git a/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts b/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts index 0790bf18..7ef8f238 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts @@ -20,15 +20,20 @@ export class Friendship { @ManyToOne(type => User, user => user.username) sender: User; + // hold on do i want this instead? + // @ManyToOne(type => User, user => user.id) @ManyToOne(type => User, user => user.username) receiver: User; + // ok so the username is still here because i think it might be useful for the frontend to have access to it, but really all i need is the ID's + // since i have the ID's do i even need the User ? @Column() senderUsername : string; - @Column() senderId : number; + @Column() + receiverUsername : string; @Column() receiverId : number; 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 100a736e..f49ad6f7 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -25,7 +25,7 @@ export class FriendshipController { @Get('findfriends') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) - findOne(@Query('username') otherUsername: string, @Query('id') id: string, @Req() req) { + findOne(@Query('username') otherUsername: string, @Query('id') id: number, @Req() req) { console.log('GET myfriend') const user = req.user; if (id !== undefined) { @@ -105,7 +105,7 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) findAllPendantFriendshipRequested(@Req() req) { const user = req.user; - return this.friendshipService.findAllPendantRequestsForFriendship(user.username); + return this.friendshipService.findAllPendantRequestsForFriendship(user.id.toString()); } // GET http://transcendance:8080/api/v2/network/received @@ -114,14 +114,14 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) findAllPendantFriendshipReceived(@Req() req) { const user = req.user; - return this.friendshipService.findAllReceivedRequestsForFriendship(user.username); + return this.friendshipService.findAllReceivedRequestsForFriendship(user.id); } // GET http://transcendance:8080/api/v2/network/blocked @Get('blocked') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) - findBlocked(@Query('relationshipId') relationshipId: string, @Req() req) { + findBlocked(@Query('relationshipId') relationshipId: number, @Req() req) { const user = req.user; if (relationshipId === undefined) return this.friendshipService.findAllBlockedFriends(user.id); 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 2f7d445a..69030939 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -16,23 +16,21 @@ export class FriendshipService { ) { } - async findOneRelationshipById(friendId : string, userId : string) { + async findOneRelationshipById(friendId : number, userId : number) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .leftJoinAndSelect('friendship.sender', 'sender') - .leftJoinAndSelect('friendship.receiver', 'receiver') .where( new Brackets((qb) => { qb.where( new Brackets((subAQb) => { - subAQb.where('sender.id = :userId', { userId : userId}) - .andWhere('receiver.id = :friendId', {friendId : friendId}) + subAQb.where('friendship.senderId = :userId', { userId : userId}) + .andWhere('friendship.receiverId = :friendId', {friendId : friendId}) }) ) .orWhere( new Brackets((subBQb) => { - subBQb.where('sender.id = :friendId', {friendId : friendId}) - .andWhere('receiver.id = :userId', {userId : userId}) + subBQb.where('friendship.senderId = :friendId', {friendId : friendId}) + .andWhere('friendship.receiverId = :userId', {userId : userId}) }) ) }), @@ -84,12 +82,12 @@ export class FriendshipService { return friendship; } - async findAllFriends(username: string) { + async findAllFriends(userId: number) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') .where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) - .andWhere('friendship.receiverUsername = :addressee', { addressee: username }) - .orWhere('friendship.senderUsername = :requester', { requester: username }) + .andWhere('friendship.receiverId = :addressee', { addressee: userId }) + .orWhere('friendship.senderId = :requester', { requester: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .getMany(); // for (const friend of friendship) @@ -97,17 +95,17 @@ export class FriendshipService { return friendship; } - async findOneBlocked(friendshipId: string, username: string) { - const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.BLOCKED } }); + async findOneBlocked(friendshipId: number, userId: number) { + const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderId: userId, status: FriendshipStatus.BLOCKED } }); if (!friendship) throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND); return friendship; } - async findOneBlockedByUsername(blockedUsername : string, username : string) { + async findOneBlockedByUsername(blockedUsername : string, userId : number) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.senderUsername = :username', {username : username}) + .where('friendship.senderId = :senderId', {senderId : userId}) .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : blockedUsername}) .andWhere('friendship.status = :status ', {status : FriendshipStatus.BLOCKED}) .getOne() @@ -117,49 +115,56 @@ export class FriendshipService { return friendship; } - async findAllBlockedFriends(username: string) { + async findAllBlockedFriends(userId: number) { const friendships : Friendship[] = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.senderUsername = :requestee', { requestee: username }) + .where('friendship.senderId = :requestee', { requestee: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getMany(); - let partialFriendship : Partial[] = []; - for (const friendship of friendships) { - partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status}); - } + // let partialFriendship : Partial[] = []; + // for (const friendship of friendships) { + // partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status}); + // } console.log('friendship.service findAllBlockedFriends, friendships:') console.log({...friendships}) console.log('friendship.service findAllBlockedFriends, partial friendship:') - console.log({...partialFriendship}) - return partialFriendship; + // console.log({...partialFriendship}) + // return partialFriendship; + return friendships; } - async findAllPendantRequestsForFriendship(username: string) { - const friendship = await this.friendshipRepository + async findAllPendantRequestsForFriendship(userId: number) { + const friendships = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.senderUsername = :requestee', { requestee: username }) + .where('friendship.senderId = :requestee', { requestee: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); - let partialFriendship : Partial[] = []; - for (const friend of friendship) { - console.log("FRIENDSHIP : " + friend); - partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); - } - console.log("Pendant requests : " + partialFriendship); - return partialFriendship; + // let partialFriendship : Partial[] = []; + // for (const friend of friendships) { + // console.log("FRIENDSHIP : " + friend); + // partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); + // } + // console.log("Pendant requests : " + partialFriendship); + // return partialFriendship; + console.log('friendships services pendant friendships:') + console.log({...friendships}) + return friendships; } - async findAllReceivedRequestsForFriendship(username: string) { - const friendship = await this.friendshipRepository + async findAllReceivedRequestsForFriendship(userId: number) { + const friendships = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.receiverUsername = :addressee', { addressee: username }) + .where('friendship.receiverId = :addressee', { addressee: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); - let partialFriendship : Partial[] = []; - for (const friend of friendship) { - partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); - } - return partialFriendship; + // let partialFriendship : Partial[] = []; + // for (const friend of friendship) { + // partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); + // } + // return partialFriendship; + console.log('friendship service received requests') + console.log({...friendships}) + return friendships; } async create(createFriendshipDto: CreateFriendshipDto, creator : User) : Promise > { @@ -189,15 +194,17 @@ export class FriendshipService { const newFriendship = new Friendship(); newFriendship.sender = creator; newFriendship.senderUsername = creator.username; + newFriendship.senderId = creator.id; newFriendship.receiver = receiver; newFriendship.receiverUsername = receiver.username; + newFriendship.receiverId = receiver.id; newFriendship.status = createFriendshipDto.status; const savedFriendship = this.friendshipRepository.save(newFriendship); const partialFriendship : Partial = { id : (await savedFriendship).id, date : (await savedFriendship).date, receiverUsername: (await savedFriendship).receiverUsername, - // receiverId: (await savedFriendship).receiver.id, + receiverId: (await savedFriendship).receiverId, status : (await savedFriendship).status } console.log('friendship.service create friendship, partial friendship') @@ -208,20 +215,21 @@ export class FriendshipService { } async acceptFriendship(relationshipId: string, user: User) { - const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); + const relation = await this.friendshipRepository.findOneBy({id: +relationshipId }); // console.log('.service accept friendship') // console.log({...relation}) - if (!relation[0]) + if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (relation[0].sender.id === user.id) { + if (relation.senderId === user.id) { throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); } - relation[0].status = FriendshipStatus.ACCEPTED; - const savedFriendship = this.friendshipRepository.save(relation[0]); + relation.status = FriendshipStatus.ACCEPTED; + const savedFriendship = this.friendshipRepository.save(relation); const partialFriendship : Partial = { id : (await savedFriendship).id, date : (await savedFriendship).date, receiverUsername: (await savedFriendship).receiverUsername, + receiverId: (await savedFriendship).receiverId, status : (await savedFriendship).status } console.log('.service accept friendship END') @@ -251,20 +259,20 @@ export class FriendshipService { } async blockFriendship(relationshipId: string, user: User) { - let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); - if (!relation[0]) + let relation = await this.friendshipRepository.find({id: +relationshipId }); + if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); // do i need to check if they've already been blocked? - if (relation[0].receiver.id === user.id) { + if (relation.receiverId === user.id) { // throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND); console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block') console.log({...relation}) // const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED) // const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"}) // we create a new one where you are the sender - const newFriendshipDto = {"receiverUsername": relation[0].sender.username, "status": FriendshipStatus.BLOCKED}; + const newFriendshipDto = {"receiverUsername": relation.senderUsername, "receiverId": relation.senderId, "status": FriendshipStatus.BLOCKED}; // can't do it this way cuz READONLY // const newFriendshipDto = new CreateFriendshipDto(); // newFriendshipDto.receiverUsername = relation[0].sender.username; @@ -274,12 +282,13 @@ export class FriendshipService { // we set it to blocked like we do below... return await this.create(newFriendshipDto, user); } else { - relation[0].status = FriendshipStatus.BLOCKED; - const savedFriendship = this.friendshipRepository.save(relation[0]); + relation.status = FriendshipStatus.BLOCKED; + const savedFriendship = this.friendshipRepository.save(relation); const partialFriendship : Partial = { id : (await savedFriendship).id, date : (await savedFriendship).date, receiverUsername: (await savedFriendship).receiverUsername, + receiverId: (await savedFriendship).receiverId, status : (await savedFriendship).status } return partialFriendship @@ -289,45 +298,43 @@ export class FriendshipService { async removeFriendship(relationshipId: string, user : User) { // 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']} ); + const friendship = await this.friendshipRepository.find({id: +relationshipId }); console.log('deleting a friendship .service') console.log({...friendship}) console.log('who is the user') console.log({...user}) - if (!friendship[0]) + if (!friendship) throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND); - if (friendship[0].sender.id !== user.id && friendship[0].receiver.id !== user.id) { + if (friendship.senderId !== user.id && friendship.receiverId !== user.id) { throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN); } console.log('.service deleted a friendship') - return this.friendshipRepository.remove(friendship[0]); + return this.friendshipRepository.remove(friendship); } //i think maybe i should be using ID rather than username... // async findIfUserIsBlockedOrHasBlocked(userConnectedUsername: string, userToFindUsername: string) { - async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) { + async findIfUserIsBlockedOrHasBlocked(userConnectedId: number, userToFindId: number) { console.log("finding if user is blocked") console.log('user connected: ' + userConnectedId) console.log('user to find: ' + userToFindId) const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .leftJoinAndSelect('friendship.sender', 'sender') - .leftJoinAndSelect('friendship.receiver', 'receiver') .where( new Brackets((qb) => { qb.where( new Brackets((subAQb) => { - subAQb.where('sender.id = :senderId', { senderId: userConnectedId}) - .andWhere('receiver.id = :receiverId', { receiverId: userToFindId}) + subAQb.where('friendship.senderId = :senderId', { senderId: userConnectedId}) + .andWhere('friendship.receiverId = :receiverId', { receiverId: userToFindId}) }) ) .orWhere( new Brackets((subBQb) => { - subBQb.where('sender.id = :senderId', {senderId : userToFindId}) - .andWhere('receiver.id = :receiverId', {receiverUsername : userConnectedId}) + subBQb.where('friendship.senderId = :senderId', {senderId : userToFindId}) + .andWhere('friendship.receiverId = :receiverId', {receiverUsername : userConnectedId}) }) ) }), @@ -346,8 +353,4 @@ export class FriendshipService { console.log('we are not blocked in friendship service') return false; } -} - - - -// SELECT "friendship"."id" AS "friendship_id", "friendship"."date" AS "friendship_date", "friendship"."senderUsername" AS "friendship_senderUsername", "friendship"."receiverUsername" AS "friendship_receiverUsername", "friendship"."status" AS "friendship_status", "friendship"."senderId" AS "friendship_senderId", "friendship"."receiverId" AS "friendship_receiverId", "sender"."id" AS "sender_id", "sender"."fortyTwoId" AS "sender_fortyTwoId", "sender"."username" AS "sender_username", "sender"."email" AS "sender_email", "sender"."image_url" AS "sender_image_url", "sender"."phone" AS "sender_phone", "sender"."status" AS "sender_status", "sender"."isEnabledTwoFactorAuth" AS "sender_isEnabledTwoFactorAuth", "sender"."isTwoFactorAuthenticated" AS "sender_isTwoFactorAuthenticated", "sender"."secretTwoFactorAuth" AS "sender_secretTwoFactorAuth", "sender"."statsId" AS "sender_statsId", "receiver"."id" AS "receiver_id", "receiver"."fortyTwoId" AS "receiver_fortyTwoId", "receiver"."username" AS "receiver_username", "receiver"."email" AS "receiver_email", "receiver"."image_url" AS "receiver_image_url", "receiver"."phone" AS "receiver_phone", "receiver"."status" AS "receiver_status", "receiver"."isEnabledTwoFactorAuth" AS "receiver_isEnabledTwoFactorAuth", "receiver"."isTwoFactorAuthenticated" AS "receiver_isTwoFactorAuthenticated", "receiver"."secretTwoFactorAuth" AS "receiver_secretTwoFactorAuth", "receiver"."statsId" AS "receiver_statsId" FROM "friendships" "friendship" LEFT JOIN "user" "sender" ON "sender"."id"="friendship"."senderId" LEFT JOIN "user" "receiver" ON "receiver"."id"="friendship"."receiverId" WHERE (("sender"."id" = $1 AND "receiver"."id" = :receiverId) OR ("sender"."id" = $2 AND "receiver"."id" = :receiverId)) AND "friendship"."status" = $3 \ No newline at end of file +} \ No newline at end of file diff --git a/srcs/requirements/nestjs/api_back/src/users/users.controller.ts b/srcs/requirements/nestjs/api_back/src/users/users.controller.ts index ebbe61a4..01617b9d 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.controller.ts @@ -45,27 +45,24 @@ export class UsersController { @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @Get() - findOne(@Query('username') username: string, @Req() req) { - if (username === undefined) { + findOne(@Query('id') toFindId: number, @Req() req) { + if (toFindId === undefined) { console.log("Backend Getting current user"); return this.usersService.findOne(req.user.id); } else { const user : User = req.user; - console.log('we have a query: ' + username) - return this.usersService.findOneByUsername(user.id.toString(),username); + console.log('we have a query: ' + toFindId) + return this.usersService.findOneById(user.id, toFindId); } } -// 99% sure this is useless // GET http://transcendance:8080/user?username=NomDuUser - // @UseGuards(AuthenticateGuard) - // @UseGuards(TwoFactorGuard) - // @Get('search') - // findOneByUsername(@Query('username') username: string, @Req() req) { - // const user : User = req.user; - // console.log('searching for user' + user.username); - // return this.usersService.findOneByUsername(user.id.toString(),username); - // } + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + @Get() + findMe(@Req() req) { + return this.usersService.findOne(req.user.id); + } // also seems useless... // GET http://transcendance:8080/user/stats diff --git a/srcs/requirements/nestjs/api_back/src/users/users.service.ts b/srcs/requirements/nestjs/api_back/src/users/users.service.ts index 892eb302..61e02012 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -60,29 +60,30 @@ export class UsersService { return true; } - async findOneByUsername(userConnectedId : string, usernameToFind: string) { - const userToFind : User = await this.userRepository.createQueryBuilder('user') - .leftJoinAndSelect('user.stats', 'stats') - .where('user.username = :username', { username: usernameToFind }) - .getOne(); - console.log('USERNAME OF FOUND USER : ' + userToFind.username); - // console.log({...user}) - // you can't do that, you need to do user === undefined - // if (user === undefined) - if (!userToFind) - throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND); - if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id.toString())) { - console.log('we are blocked in user.service') - throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND); - } - const partialUser : Partial = { - username: userToFind.username, - image_url: userToFind.image_url, - status: userToFind.status, - stats: userToFind.stats, - }; - return partialUser; - } + // ok yea this shit makes no fucking sense any more since we're always gonna have the id, this is meant to return someone by username, fuck that + // async findOneById(userId : number, toFindId: number) { + // const userToFind : User = await this.userRepository.createQueryBuilder('user') + // .leftJoinAndSelect('user.stats', 'stats') + // .where('user.id = :toFindId', { toFindId: toFindId }) + // .getOne(); + // console.log('USERNAME OF FOUND USER : ' + userToFind.username); + // // console.log({...user}) + // // you can't do that, you need to do user === undefined + // // if (user === undefined) + // if (!userToFind) + // throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND); + // if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id)) { + // console.log('we are blocked in user.service') + // throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND); + // } + // const partialUser : Partial = { + // username: userToFind.username, + // image_url: userToFind.image_url, + // status: userToFind.status, + // stats: userToFind.stats, + // }; + // return partialUser; + // } // fuck pagination Query // async findAll(paginationquery : PaginationQueryDto, currentUser: User) { @@ -100,7 +101,7 @@ export class UsersService { // console.log('user.services findIF Blocked... : ') // console.log(tmp) // if (tmp === false) { - if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id.toString(), otherUser.id.toString()) === false) { + if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id) === false) { partialUsers.push({id: otherUser.id, username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats}); } }