Modifiche

This commit is contained in:
Lorenzo Sanesi
2026-04-01 23:49:19 +02:00
parent 8752acd2c4
commit e2fe8b264f
52 changed files with 1351 additions and 400 deletions
+8 -19
View File
@@ -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;
+2 -10
View File
@@ -6,16 +6,8 @@ class GroupsService {
this.usersRepository = usersRepository;
}
async getAllGroups() {
return await this.groupsRepository.findAll({
include: [
{
model: this.usersRepository.User,
as: 'members',
through: { attributes: [] },
},
],
});
async getAllGroups(userId) {
return await this.groupsRepository.findByUserId(userId);
}
async getGroupById(groupId) {