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
@@ -1,5 +1,5 @@
const { NotFoundError, DatabaseError } = require('../../shared/errors');
const { Op } = require('sequelize');
const { NotFoundError, DatabaseError } = require('../../shared/errors');
class FriendRequestsRepository {
constructor({ sequelize }) {
@@ -30,7 +30,7 @@ class FriendRequestsRepository {
try {
return await this.FriendRequest.findAll({
where: {
toUserId: userId,
[Op.or]: [{ toUserId: userId }, { fromUserId: userId }],
status: 'PENDING',
},
include: [
@@ -39,10 +39,7 @@ class FriendRequestsRepository {
],
});
} catch (error) {
throw new DatabaseError(
`Failed to fetch pending requests for user ${userId}`,
error
);
throw new DatabaseError(`Failed to fetch pending requests for user ${userId}`, error);
}
}
@@ -64,11 +61,12 @@ class FriendRequestsRepository {
async create(fromUserId, toUserId) {
try {
return await this.FriendRequest.create({
const created = await this.FriendRequest.create({
fromUserId,
toUserId,
status: 'PENDING',
});
return await this.findById(created.id);
} catch (error) {
throw new DatabaseError('Failed to create friend request', error);
}
@@ -1,3 +1,4 @@
const { Op } = require('sequelize');
const { NotFoundError, DatabaseError } = require('../../shared/errors');
class GroupsRepository {
@@ -15,6 +16,29 @@ class GroupsRepository {
}
}
async findByUserId(userId) {
try {
return await this.Group.findAll({
include: [
{
model: this.User,
as: 'members',
through: { attributes: [] },
},
],
where: {
id: {
[Op.in]: this.sequelize.literal(
`(SELECT "groupId" FROM "UserGroups" WHERE "userId" = ${parseInt(userId, 10)})`
),
},
},
});
} catch (error) {
throw new DatabaseError('Failed to fetch groups for user', error);
}
}
async findById(id, includeMembers = false) {
try {
const options = {};