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));

View File

@@ -114,6 +114,14 @@
},
"overview": {
"title": "Falukant - Übersicht",
"heirSelection": {
"title": "Erben-Auswahl",
"description": "Dein bisheriger Charakter ist nicht mehr verfügbar. Wähle einen Erben aus der Liste, um mit diesem weiterzuspielen.",
"loading": "Lade mögliche Erben…",
"noHeirs": "Keine Erben verfügbar.",
"select": "Als Spielcharakter wählen",
"error": "Fehler beim Auswählen des Erben."
},
"metadata": {
"title": "Persönliches",
"name": "Name",

View File

@@ -95,6 +95,14 @@
},
"overview": {
"title": "Falukant - Overview",
"heirSelection": {
"title": "Heir Selection",
"description": "Your previous character is no longer available. Choose an heir from the list to continue playing.",
"loading": "Loading potential heirs…",
"noHeirs": "No heirs available.",
"select": "Select as play character",
"error": "Error selecting heir."
},
"metadata": {
"title": "Personal",
"name": "Name",