feat(falukant): refactor character ID resolution for political office checks
All checks were successful
Deploy to production / deploy (push) Successful in 2m46s

- Introduced a new method `resolveCharacterIdForOfficeChecks` to robustly resolve character IDs based on user references, improving the reliability of political office queries.
- Updated `getHighestPoliticalOfficeInfo` and `getHighestOfficeAnyInfo` methods to utilize the new character ID resolution logic, enhancing code clarity and reducing redundancy.
- Ensured that character ID retrieval handles both direct and fallback user references, streamlining the process of fetching political office information.
This commit is contained in:
Torsten Schulz (local)
2026-04-15 15:47:31 +02:00
parent 95ea6336b7
commit cb0e1eb2b1

View File

@@ -6087,21 +6087,45 @@ class FalukantService extends BaseService {
return averageCondition >= Number(requirement.requirementValue || 0); return averageCondition >= Number(requirement.requirementValue || 0);
} }
async getHighestPoliticalOfficeInfo(userId) { /**
const character = await FalukantCharacter.findOne({ * Robuste Auflösung der Charakter-ID:
where: { userId }, * - 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'] 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([ const [politicalOffices, politicalHistories] = await Promise.all([
PoliticalOffice.findAll({ PoliticalOffice.findAll({
where: { characterId: character.id }, where: { characterId },
include: [{ model: PoliticalOfficeType, as: 'type', attributes: ['name'] }], include: [{ model: PoliticalOfficeType, as: 'type', attributes: ['name'] }],
attributes: ['officeTypeId'] attributes: ['officeTypeId']
}), }),
PoliticalOfficeHistory.findAll({ PoliticalOfficeHistory.findAll({
where: { characterId: character.id }, where: { characterId },
include: [{ model: PoliticalOfficeType, as: 'officeTypeHistory', attributes: ['name'] }], include: [{ model: PoliticalOfficeType, as: 'officeTypeHistory', attributes: ['name'] }],
attributes: ['officeTypeId'] attributes: ['officeTypeId']
}) })
@@ -6122,25 +6146,22 @@ class FalukantService extends BaseService {
} }
async getHighestOfficeAnyInfo(userId) { async getHighestOfficeAnyInfo(userId) {
const character = await FalukantCharacter.findOne({ const characterId = await this.resolveCharacterIdForOfficeChecks(userId);
where: { userId }, if (!characterId) return { rank: 0, name: null, source: null };
attributes: ['id']
});
if (!character) return { rank: 0, name: null, source: null };
const [politicalOffices, politicalHistories, churchOffices] = await Promise.all([ const [politicalOffices, politicalHistories, churchOffices] = await Promise.all([
PoliticalOffice.findAll({ PoliticalOffice.findAll({
where: { characterId: character.id }, where: { characterId },
include: [{ model: PoliticalOfficeType, as: 'type', attributes: ['name'] }], include: [{ model: PoliticalOfficeType, as: 'type', attributes: ['name'] }],
attributes: ['officeTypeId'] attributes: ['officeTypeId']
}), }),
PoliticalOfficeHistory.findAll({ PoliticalOfficeHistory.findAll({
where: { characterId: character.id }, where: { characterId },
include: [{ model: PoliticalOfficeType, as: 'officeTypeHistory', attributes: ['name'] }], include: [{ model: PoliticalOfficeType, as: 'officeTypeHistory', attributes: ['name'] }],
attributes: ['officeTypeId'] attributes: ['officeTypeId']
}), }),
ChurchOffice.findAll({ ChurchOffice.findAll({
where: { characterId: character.id }, where: { characterId },
include: [{ model: ChurchOfficeType, as: 'type', attributes: ['name', 'hierarchyLevel'] }], include: [{ model: ChurchOfficeType, as: 'type', attributes: ['name', 'hierarchyLevel'] }],
attributes: ['officeTypeId'] attributes: ['officeTypeId']
}) })
@@ -6752,7 +6773,16 @@ class FalukantService extends BaseService {
where: { characterId }, where: { characterId },
attributes: ['officeTypeId', 'startDate', 'endDate'] 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 allTypes = await PoliticalOfficeType.findAll({ attributes: ['id', 'name'] });
const nameToId = Object.fromEntries(allTypes.map(t => [t.name, t.id])); const nameToId = Object.fromEntries(allTypes.map(t => [t.name, t.id]));
const openPositions = await Election.findAll({ const openPositions = await Election.findAll({
@@ -6834,7 +6864,14 @@ class FalukantService extends BaseService {
where: { characterId: character.id }, where: { characterId: character.id },
attributes: ['officeTypeId', 'startDate', 'endDate'] 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({ const allTypes = await PoliticalOfficeType.findAll({
attributes: ['id', 'name', 'regionType', 'seatsPerRegion', 'termLength', 'hierarchyLevel'], attributes: ['id', 'name', 'regionType', 'seatsPerRegion', 'termLength', 'hierarchyLevel'],