Refactor user and relationship data retrieval in FalukantService: Update loading logic to use separate queries for UserHouse and relationships, improving reliability and preventing EagerLoadingError. Enhance heir selection UI with new translations in German and English, providing better user guidance during character selection.

This commit is contained in:
Torsten Schulz (local)
2026-02-04 14:12:25 +01:00
parent 16e54d20d0
commit d038d72cde
3 changed files with 44 additions and 23 deletions

View File

@@ -409,12 +409,18 @@ class FalukantService extends BaseService {
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
// Load UserHouse and HouseType in separate queries to avoid EagerLoadingError
const userHouse = await UserHouse.findOne({
where: { userId: u.id },
attributes: ['roofCondition'],
include: [{ model: HouseType, as: 'houseType', attributes: ['labelTr', 'position'] }]
attributes: ['roofCondition', 'houseTypeId']
});
if (userHouse?.houseTypeId) {
const houseType = await HouseType.findOne({
where: { id: userHouse.houseTypeId },
attributes: ['labelTr', 'position']
});
if (houseType) userHouse.setDataValue('houseType', houseType);
}
if (userHouse) u.setDataValue('userHouse', userHouse);
if (u.character?.birthdate) u.character.setDataValue('age', calcAge(u.character.birthdate));
return u;
@@ -476,34 +482,33 @@ class FalukantService extends BaseService {
attributes: ['id', 'money']
});
if (!falukantUser) throw new Error('User not found');
// Load relationships in separate queries to avoid EagerLoadingError if associations are not registered
// Load relationships and types in separate queries to avoid EagerLoadingError
if (falukantUser.character?.id) {
const [relsAs1, relsAs2] = await Promise.all([
const [rawRelsAs1, rawRelsAs2] = 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
}]
attributes: ['id', 'character2Id', 'relationshipTypeId']
}),
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
}]
attributes: ['id', 'character1Id', 'relationshipTypeId']
})
]);
falukantUser.character.setDataValue('relationshipsAsCharacter1', relsAs1);
falukantUser.character.setDataValue('relationshipsAsCharacter2', relsAs2);
const typeIds = [...new Set([
...rawRelsAs1.map(r => r.relationshipTypeId),
...rawRelsAs2.map(r => r.relationshipTypeId)
])].filter(Boolean);
const types = typeIds.length
? await RelationshipType.findAll({ where: { id: typeIds, tr: { [Op.not]: 'lover' } }, attributes: ['id', 'tr'] })
: [];
const typeMap = Object.fromEntries(types.map(t => [t.id, t]));
const attachType = (r) => {
const t = typeMap[r.relationshipTypeId];
if (t) r.setDataValue('relationshipType', t);
return r;
};
falukantUser.character.setDataValue('relationshipsAsCharacter1', rawRelsAs1.filter(r => typeMap[r.relationshipTypeId]).map(attachType));
falukantUser.character.setDataValue('relationshipsAsCharacter2', rawRelsAs2.filter(r => typeMap[r.relationshipTypeId]).map(attachType));
}
if (falukantUser.character?.birthdate) falukantUser.character.setDataValue('age', calcAge(falukantUser.character.birthdate));