From d038d72cde8c7e76a59d541dd56ff5c80a416d20 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 4 Feb 2026 14:12:25 +0100 Subject: [PATCH] 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. --- backend/services/falukantService.js | 51 ++++++++++++---------- frontend/src/i18n/locales/de/falukant.json | 8 ++++ frontend/src/i18n/locales/en/falukant.json | 8 ++++ 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index 439dbd3..c136a8a 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -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)); diff --git a/frontend/src/i18n/locales/de/falukant.json b/frontend/src/i18n/locales/de/falukant.json index af976c9..fd8aad9 100644 --- a/frontend/src/i18n/locales/de/falukant.json +++ b/frontend/src/i18n/locales/de/falukant.json @@ -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", diff --git a/frontend/src/i18n/locales/en/falukant.json b/frontend/src/i18n/locales/en/falukant.json index 2d06f38..cf70ead 100644 --- a/frontend/src/i18n/locales/en/falukant.json +++ b/frontend/src/i18n/locales/en/falukant.json @@ -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",