|
|
|
@@ -17,8 +17,8 @@ export class FriendshipService {
|
|
|
|
) { }
|
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async findOneFriend(friendshipId: string, userId: string) {
|
|
|
|
async findOneFriend(friendshipId: string, username: string) {
|
|
|
|
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterId: userId, status: FriendshipStatus.ACCEPTED } });
|
|
|
|
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } });
|
|
|
|
if (!friendship)
|
|
|
|
if (!friendship)
|
|
|
|
throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
|
|
|
|
throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
|
|
|
|
return friendship;
|
|
|
|
return friendship;
|
|
|
|
@@ -31,12 +31,12 @@ export class FriendshipService {
|
|
|
|
return friendship;
|
|
|
|
return friendship;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async findAllFriends(userId: string) {
|
|
|
|
async findAllFriends(username: string) {
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
|
|
|
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
|
|
|
.andWhere('friendship.addresseeId = :addressee', { addressee: userId })
|
|
|
|
.andWhere('friendship.addresseeUsername = :addressee', { addressee: username })
|
|
|
|
.orWhere('friendship.requesterId = :requester', { requester: userId })
|
|
|
|
.orWhere('friendship.requesterUsername = :requester', { requester: username })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
|
|
|
.getMany();
|
|
|
|
.getMany();
|
|
|
|
for (const friend of friendship)
|
|
|
|
for (const friend of friendship)
|
|
|
|
@@ -44,40 +44,40 @@ export class FriendshipService {
|
|
|
|
return friendship;
|
|
|
|
return friendship;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async findAllBlockedFriends(userId: string) {
|
|
|
|
async findAllBlockedFriends(username: string) {
|
|
|
|
return await this.friendshipRepository
|
|
|
|
return await this.friendshipRepository
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.where('friendship.requesterId = :requestee', { requestee: userId })
|
|
|
|
.where('friendship.requesterUsername = :requestee', { requestee: username })
|
|
|
|
.orWhere('friendship.addresseeId = :addressee', { addressee: userId })
|
|
|
|
.orWhere('friendship.addresseeUsername = :addressee', { addressee: username })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
|
|
|
.getMany();
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async findAllPendantRequestsForFriendship(userId: string) {
|
|
|
|
async findAllPendantRequestsForFriendship(username: string) {
|
|
|
|
return await this.friendshipRepository
|
|
|
|
return await this.friendshipRepository
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.where('friendship.requesterId = :requestee', { requestee: userId })
|
|
|
|
.where('friendship.requesterUsername = :requestee', { requestee: username })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
|
|
|
.getMany();
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async findAllReceivedRequestsForFriendship(userId: string) {
|
|
|
|
async findAllReceivedRequestsForFriendship(username: string) {
|
|
|
|
return await this.friendshipRepository
|
|
|
|
return await this.friendshipRepository
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.where('friendship.addresseeId = :addressee', { addressee: userId })
|
|
|
|
.where('friendship.addresseeUsername = :addressee', { addressee: username })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
|
|
|
.getMany();
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//GROS CHANTIER
|
|
|
|
//GROS CHANTIER
|
|
|
|
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
|
|
|
|
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
|
|
|
|
const addressee = await this.userRepository.findOneBy({ id: +createFriendshipDto.addresseeId });
|
|
|
|
const addressee = await this.userRepository.findOneBy({ id: +createFriendshipDto.addresseeUsername });
|
|
|
|
if (!addressee)
|
|
|
|
if (!addressee)
|
|
|
|
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
|
|
|
|
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
|
|
|
|
if (creator.id === addressee.id)
|
|
|
|
if (creator.id === addressee.id)
|
|
|
|
throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN);
|
|
|
|
throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN);
|
|
|
|
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
|
|
|
|
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
|
|
|
|
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
|
|
|
|
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) {
|
|
|
|
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
|
|
|
|
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
|
|
|
|
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
|
|
|
|
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
|
|
|
|
@@ -96,7 +96,7 @@ export class FriendshipService {
|
|
|
|
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
|
|
|
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
|
|
|
if (!relation)
|
|
|
|
if (!relation)
|
|
|
|
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
|
|
|
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);
|
|
|
|
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status === FriendshipStatus.ACCEPTED)
|
|
|
|
if (status === FriendshipStatus.ACCEPTED)
|
|
|
|
@@ -122,8 +122,8 @@ export class FriendshipService {
|
|
|
|
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) {
|
|
|
|
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) {
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
.where('friendship.requesterId = :requestee', { requestee: userConnectedId })
|
|
|
|
.where('friendship.requesterUsername = :requestee', { requestee: userConnectedId })
|
|
|
|
.orWhere('friendship.requesterId = :requesteeBis', { requesteeBis: userToCheckId })
|
|
|
|
.orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
|
|
|
.getOne();
|
|
|
|
.getOne();
|
|
|
|
if (friendship)
|
|
|
|
if (friendship)
|
|
|
|
|