diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index 4137a7b..4678fe8 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -6087,21 +6087,45 @@ class FalukantService extends BaseService { return averageCondition >= Number(requirement.requirementValue || 0); } - async getHighestPoliticalOfficeInfo(userId) { - const character = await FalukantCharacter.findOne({ - where: { userId }, + /** + * Robuste Auflösung der Charakter-ID: + * - primär: falukant_data.character.user_id = falukant_user.id + * - fallback: über community.user.id -> falukant_user.user_id -> character.user_id + */ + async resolveCharacterIdForOfficeChecks(userRefId) { + const directCharacter = await FalukantCharacter.findOne({ + where: { userId: userRefId }, + attributes: ['id'], + order: [['id', 'DESC']] + }); + if (directCharacter?.id) return directCharacter.id; + + const falukantUser = await FalukantUser.findOne({ + where: { userId: userRefId }, attributes: ['id'] }); - if (!character) return { rank: 0, name: null }; + if (!falukantUser?.id) return null; + + const mappedCharacter = await FalukantCharacter.findOne({ + where: { userId: falukantUser.id }, + attributes: ['id'], + order: [['id', 'DESC']] + }); + return mappedCharacter?.id || null; + } + + async getHighestPoliticalOfficeInfo(userId) { + const characterId = await this.resolveCharacterIdForOfficeChecks(userId); + if (!characterId) return { rank: 0, name: null }; const [politicalOffices, politicalHistories] = await Promise.all([ PoliticalOffice.findAll({ - where: { characterId: character.id }, + where: { characterId }, include: [{ model: PoliticalOfficeType, as: 'type', attributes: ['name'] }], attributes: ['officeTypeId'] }), PoliticalOfficeHistory.findAll({ - where: { characterId: character.id }, + where: { characterId }, include: [{ model: PoliticalOfficeType, as: 'officeTypeHistory', attributes: ['name'] }], attributes: ['officeTypeId'] }) @@ -6122,25 +6146,22 @@ class FalukantService extends BaseService { } async getHighestOfficeAnyInfo(userId) { - const character = await FalukantCharacter.findOne({ - where: { userId }, - attributes: ['id'] - }); - if (!character) return { rank: 0, name: null, source: null }; + const characterId = await this.resolveCharacterIdForOfficeChecks(userId); + if (!characterId) return { rank: 0, name: null, source: null }; const [politicalOffices, politicalHistories, churchOffices] = await Promise.all([ PoliticalOffice.findAll({ - where: { characterId: character.id }, + where: { characterId }, include: [{ model: PoliticalOfficeType, as: 'type', attributes: ['name'] }], attributes: ['officeTypeId'] }), PoliticalOfficeHistory.findAll({ - where: { characterId: character.id }, + where: { characterId }, include: [{ model: PoliticalOfficeType, as: 'officeTypeHistory', attributes: ['name'] }], attributes: ['officeTypeId'] }), ChurchOffice.findAll({ - where: { characterId: character.id }, + where: { characterId }, include: [{ model: ChurchOfficeType, as: 'type', attributes: ['name', 'hierarchyLevel'] }], attributes: ['officeTypeId'] }) @@ -6752,7 +6773,16 @@ class FalukantService extends BaseService { where: { characterId }, attributes: ['officeTypeId', 'startDate', 'endDate'] }); - const heldOfficeTypeIds = histories.map(h => h.officeTypeId); + const currentOffices = await PoliticalOffice.findAll({ + where: { characterId }, + attributes: ['officeTypeId'] + }); + const heldOfficeTypeIds = [ + ...new Set([ + ...histories.map((h) => Number(h.officeTypeId)), + ...currentOffices.map((o) => Number(o.officeTypeId)) + ]) + ]; const allTypes = await PoliticalOfficeType.findAll({ attributes: ['id', 'name'] }); const nameToId = Object.fromEntries(allTypes.map(t => [t.name, t.id])); const openPositions = await Election.findAll({ @@ -6834,7 +6864,14 @@ class FalukantService extends BaseService { where: { characterId: character.id }, attributes: ['officeTypeId', 'startDate', 'endDate'] }); - const heldOfficeTypeIds = new Set(histories.map((h) => Number(h.officeTypeId))); + const currentOffices = await PoliticalOffice.findAll({ + where: { characterId: character.id }, + attributes: ['officeTypeId'] + }); + const heldOfficeTypeIds = new Set([ + ...histories.map((h) => Number(h.officeTypeId)), + ...currentOffices.map((o) => Number(o.officeTypeId)) + ]); const allTypes = await PoliticalOfficeType.findAll({ attributes: ['id', 'name', 'regionType', 'seatsPerRegion', 'termLength', 'hierarchyLevel'],