Refactor user and relationship loading in FalukantService: Replace eager loading of UserHouse and relationships with separate queries to prevent EagerLoadingError. This change enhances data retrieval reliability and maintains performance during user-related operations.
This commit is contained in:
@@ -405,22 +405,17 @@ class FalukantService extends BaseService {
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
model: UserHouse,
|
||||
as: 'userHouse',
|
||||
include: [
|
||||
{
|
||||
model: HouseType,
|
||||
as: 'houseType',
|
||||
'attributes': ['labelTr', 'position']
|
||||
},
|
||||
],
|
||||
attributes: ['roofCondition'],
|
||||
},
|
||||
],
|
||||
attributes: ['money', 'creditAmount', 'todayCreditTaken',]
|
||||
});
|
||||
if (!u) throw new Error('User not found');
|
||||
// Load UserHouse in a separate query to avoid EagerLoadingError if association is not registered
|
||||
const userHouse = await UserHouse.findOne({
|
||||
where: { userId: u.id },
|
||||
attributes: ['roofCondition'],
|
||||
include: [{ model: HouseType, as: 'houseType', attributes: ['labelTr', 'position'] }]
|
||||
});
|
||||
if (userHouse) u.setDataValue('userHouse', userHouse);
|
||||
if (u.character?.birthdate) u.character.setDataValue('age', calcAge(u.character.birthdate));
|
||||
return u;
|
||||
}
|
||||
@@ -476,37 +471,40 @@ class FalukantService extends BaseService {
|
||||
model: FalukantCharacter,
|
||||
as: 'character',
|
||||
attributes: ['id', 'birthdate', 'health', 'reputation'],
|
||||
include: [
|
||||
{
|
||||
model: Relationship,
|
||||
as: 'relationshipsAsCharacter1',
|
||||
required: false,
|
||||
attributes: ['id', 'character2Id', 'relationshipTypeId'],
|
||||
include: [{
|
||||
model: RelationshipType,
|
||||
as: 'relationshipType',
|
||||
attributes: ['tr'],
|
||||
where: { tr: { [Op.not]: 'lover' } }
|
||||
}]
|
||||
},
|
||||
{
|
||||
model: Relationship,
|
||||
as: 'relationshipsAsCharacter2',
|
||||
required: false,
|
||||
attributes: ['id', 'character1Id', 'relationshipTypeId'],
|
||||
include: [{
|
||||
model: RelationshipType,
|
||||
as: 'relationshipType',
|
||||
attributes: ['tr'],
|
||||
where: { tr: { [Op.not]: 'lover' } }
|
||||
}]
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
attributes: ['id', 'money']
|
||||
});
|
||||
if (!falukantUser) throw new Error('User not found');
|
||||
// Load relationships in separate queries to avoid EagerLoadingError if associations are not registered
|
||||
if (falukantUser.character?.id) {
|
||||
const [relsAs1, relsAs2] = await Promise.all([
|
||||
Relationship.findAll({
|
||||
where: { character1Id: falukantUser.character.id },
|
||||
attributes: ['id', 'character2Id', 'relationshipTypeId'],
|
||||
include: [{
|
||||
model: RelationshipType,
|
||||
as: 'relationshipType',
|
||||
attributes: ['tr'],
|
||||
where: { tr: { [Op.not]: 'lover' } },
|
||||
required: true
|
||||
}]
|
||||
}),
|
||||
Relationship.findAll({
|
||||
where: { character2Id: falukantUser.character.id },
|
||||
attributes: ['id', 'character1Id', 'relationshipTypeId'],
|
||||
include: [{
|
||||
model: RelationshipType,
|
||||
as: 'relationshipType',
|
||||
attributes: ['tr'],
|
||||
where: { tr: { [Op.not]: 'lover' } },
|
||||
required: true
|
||||
}]
|
||||
})
|
||||
]);
|
||||
falukantUser.character.setDataValue('relationshipsAsCharacter1', relsAs1);
|
||||
falukantUser.character.setDataValue('relationshipsAsCharacter2', relsAs2);
|
||||
}
|
||||
if (falukantUser.character?.birthdate) falukantUser.character.setDataValue('age', calcAge(falukantUser.character.birthdate));
|
||||
|
||||
// Aggregate status additions: children counts and unread notifications
|
||||
|
||||
Reference in New Issue
Block a user