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 4285b556..3fb61e19 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 @@ -3,9 +3,9 @@ import { FriendshipStatus } from '../entities/friendship.entity'; export class CreateFriendshipDto { @IsString() - readonly requesterId: string; + readonly requesterUsername: string; @IsString() - readonly addresseeId: string; + readonly addresseeUsername: 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 0b8f08bd..12526de6 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 @@ -18,12 +18,12 @@ export class Friendship { date : Date; @Column() - @ManyToOne(type => User, user => user.requesterId) - requesterId: string; + @ManyToOne(type => User, user => user.username) + requesterUsername: string; @Column() - @ManyToOne(type => User, user => user.addresseeId) - addresseeId: string; + @ManyToOne(type => User, user => user.username) + addresseeUsername: string; @Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED}) status: FriendshipStatus; 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 75b10f93..db740ffa 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -33,8 +33,8 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { const user = req.user; - console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterId}\nStatus: ${createFriendshipDto.status}`); - if (user.id === +createFriendshipDto.requesterId) + console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterUsername}\nStatus: ${createFriendshipDto.status}`); + if (user.username === createFriendshipDto.requesterUsername) return this.friendshipService.create(createFriendshipDto, user); return new HttpException('You can\'t request a frienship for another user', HttpStatus.FORBIDDEN); } @@ -64,7 +64,7 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) findAllBlocked(@Req() req) { const user = req.user; - return this.friendshipService.findAllBlockedFriends(user.id); + return this.friendshipService.findAllBlockedFriends(user.username); } // GET http://transcendance:8080/api/v2/network/pending 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 6066a881..52f52d12 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -17,8 +17,8 @@ export class FriendshipService { ) { } - async findOneFriend(friendshipId: string, userId: string) { - const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterId: userId, status: FriendshipStatus.ACCEPTED } }); + async findOneFriend(friendshipId: string, username: string) { + const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } }); if (!friendship) throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND); return friendship; @@ -31,12 +31,12 @@ export class FriendshipService { return friendship; } - async findAllFriends(userId: string) { + async findAllFriends(username: string) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') .where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) - .andWhere('friendship.addresseeId = :addressee', { addressee: userId }) - .orWhere('friendship.requesterId = :requester', { requester: userId }) + .andWhere('friendship.addresseeUsername = :addressee', { addressee: username }) + .orWhere('friendship.requesterUsername = :requester', { requester: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .getMany(); for (const friend of friendship) @@ -44,40 +44,39 @@ export class FriendshipService { return friendship; } - async findAllBlockedFriends(userId: string) { + async findAllBlockedFriends(username: string) { return await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.requesterId = :requestee', { requestee: userId }) - .orWhere('friendship.addresseeId = :addressee', { addressee: userId }) + .where('friendship.requesterUsername = :requestee', { requestee: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getMany(); } - async findAllPendantRequestsForFriendship(userId: string) { + async findAllPendantRequestsForFriendship(username: string) { return await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.requesterId = :requestee', { requestee: userId }) + .where('friendship.requesterUsername = :requestee', { requestee: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); } - async findAllReceivedRequestsForFriendship(userId: string) { + async findAllReceivedRequestsForFriendship(username: string) { return await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.addresseeId = :addressee', { addressee: userId }) + .where('friendship.addresseeUsername = :addressee', { addressee: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); } //GROS CHANTIER async create(createFriendshipDto: CreateFriendshipDto, creator : User) { - const addressee = await this.userRepository.findOneBy({ id: +createFriendshipDto.addresseeId }); + const addressee = await ; if (!addressee) throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); if (creator.id === addressee.id) throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN); if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED) throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND); - const friendship = await this.friendshipRepository.findOneBy({ requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId }); + const friendship = await this.friendshipRepository.findOneBy({ requesterUsername: createFriendshipDto.requesterUsername, addresseeUsername: createFriendshipDto.addresseeUsername }); if (friendship) { if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED) throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK); @@ -96,7 +95,7 @@ export class FriendshipService { const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (+relation.requesterId === user.id) { + if (+relation.requesterUsername === user.id) { throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); } if (status === FriendshipStatus.ACCEPTED) @@ -122,8 +121,8 @@ export class FriendshipService { async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.requesterId = :requestee', { requestee: userConnectedId }) - .orWhere('friendship.requesterId = :requesteeBis', { requesteeBis: userToCheckId }) + .where('friendship.requesterUsername = :requestee', { requestee: userConnectedId }) + .orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getOne(); if (friendship)