Modifiche
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
const { ValidationError, ConflictError } = require('../shared/errors');
|
||||
const { Op } = require('sequelize');
|
||||
const { ValidationError, ConflictError } = require('../shared/errors');
|
||||
|
||||
class FriendsService {
|
||||
constructor({
|
||||
friendshipsRepository,
|
||||
friendRequestsRepository,
|
||||
usersRepository,
|
||||
sequelize,
|
||||
}) {
|
||||
constructor({ friendshipsRepository, friendRequestsRepository, usersRepository, sequelize }) {
|
||||
this.friendshipsRepository = friendshipsRepository;
|
||||
this.friendRequestsRepository = friendRequestsRepository;
|
||||
this.usersRepository = usersRepository;
|
||||
@@ -33,9 +28,7 @@ class FriendsService {
|
||||
});
|
||||
|
||||
if (!toUser) {
|
||||
throw new ValidationError(
|
||||
'User not found with provided name or email'
|
||||
);
|
||||
throw new ValidationError('User not found with provided name or email');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,18 +36,12 @@ class FriendsService {
|
||||
throw new ValidationError('Cannot send friend request to yourself');
|
||||
}
|
||||
|
||||
const friendshipExists = await this.friendshipsRepository.exists(
|
||||
fromUserId,
|
||||
toUser.id
|
||||
);
|
||||
const friendshipExists = await this.friendshipsRepository.exists(fromUserId, toUser.id);
|
||||
if (friendshipExists) {
|
||||
throw new ConflictError('You are already friends with this user');
|
||||
}
|
||||
|
||||
const existingRequest = await this.friendRequestsRepository.findExisting(
|
||||
fromUserId,
|
||||
toUser.id
|
||||
);
|
||||
const existingRequest = await this.friendRequestsRepository.findExisting(fromUserId, toUser.id);
|
||||
if (existingRequest) {
|
||||
throw new ConflictError('Friend request already exists');
|
||||
}
|
||||
@@ -73,6 +60,8 @@ class FriendsService {
|
||||
throw new ValidationError('This request has already been processed');
|
||||
}
|
||||
|
||||
const newFriend = request.fromUser;
|
||||
|
||||
const t = await this.sequelize.transaction();
|
||||
try {
|
||||
await this.friendRequestsRepository.updateStatus(requestId, 'ACCEPTED');
|
||||
@@ -80,7 +69,7 @@ class FriendsService {
|
||||
await this.friendRequestsRepository.delete(requestId);
|
||||
await t.commit();
|
||||
|
||||
return { message: 'Friend request accepted successfully' };
|
||||
return { message: 'Friend request accepted successfully', newFriend };
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user