Add region relationship to ChurchOffice model: Establish associations between ChurchOffice and RegionData, allowing for better organization of church offices by region. Enhance logging in getChurchOverview method for improved debugging and error handling.

This commit is contained in:
Torsten Schulz (local)
2026-01-28 16:45:40 +01:00
parent a8b76bc21a
commit afc36161ed
2 changed files with 70 additions and 45 deletions

View File

@@ -913,6 +913,16 @@ export default function setupAssociations() {
as: 'supervisor' as: 'supervisor'
}); });
// Region relationship
ChurchOffice.belongsTo(RegionData, {
foreignKey: 'regionId',
as: 'region'
});
RegionData.hasMany(ChurchOffice, {
foreignKey: 'regionId',
as: 'churchOffices'
});
// Applications for church office // Applications for church office
ChurchApplication.belongsTo(ChurchOfficeType, { ChurchApplication.belongsTo(ChurchOfficeType, {
foreignKey: 'officeTypeId', foreignKey: 'officeTypeId',

View File

@@ -4825,17 +4825,20 @@ class FalukantService extends BaseService {
} }
async getChurchOverview(hashedUserId) { async getChurchOverview(hashedUserId) {
try {
const user = await getFalukantUserOrFail(hashedUserId); const user = await getFalukantUserOrFail(hashedUserId);
const character = await FalukantCharacter.findOne({ const character = await FalukantCharacter.findOne({
where: { userId: user.id }, where: { userId: user.id },
attributes: ['id', 'regionId'] attributes: ['id', 'regionId']
}); });
if (!character) { if (!character) {
console.log('[getChurchOverview] No character found for user', user.id);
return []; return [];
} }
// Alle relevanten Regionen (Region + Eltern) laden // Alle relevanten Regionen (Region + Eltern) laden
const relevantRegionIds = await this.getRegionAndParentIds(character.regionId); const relevantRegionIds = await this.getRegionAndParentIds(character.regionId);
console.log('[getChurchOverview] Relevant region IDs:', relevantRegionIds);
// Aktuell besetzte Kirchenämter in diesen Regionen laden // Aktuell besetzte Kirchenämter in diesen Regionen laden
const offices = await ChurchOffice.findAll({ const offices = await ChurchOffice.findAll({
@@ -4859,21 +4862,25 @@ class FalukantService extends BaseService {
model: FalukantCharacter, model: FalukantCharacter,
as: 'holder', as: 'holder',
attributes: ['id', 'gender'], attributes: ['id', 'gender'],
required: false,
include: [ include: [
{ {
model: FalukantPredefineFirstname, model: FalukantPredefineFirstname,
as: 'definedFirstName', as: 'definedFirstName',
attributes: ['name'] attributes: ['name'],
required: false
}, },
{ {
model: FalukantPredefineLastname, model: FalukantPredefineLastname,
as: 'definedLastName', as: 'definedLastName',
attributes: ['name'] attributes: ['name'],
required: false
}, },
{ {
model: TitleOfNobility, model: TitleOfNobility,
as: 'nobleTitle', as: 'nobleTitle',
attributes: ['labelTr'] attributes: ['labelTr'],
required: false
} }
] ]
}, },
@@ -4881,20 +4888,22 @@ class FalukantService extends BaseService {
model: FalukantCharacter, model: FalukantCharacter,
as: 'supervisor', as: 'supervisor',
attributes: ['id', 'gender'], attributes: ['id', 'gender'],
required: false,
include: [ include: [
{ {
model: FalukantPredefineFirstname, model: FalukantPredefineFirstname,
as: 'definedFirstName', as: 'definedFirstName',
attributes: ['name'] attributes: ['name'],
required: false
}, },
{ {
model: FalukantPredefineLastname, model: FalukantPredefineLastname,
as: 'definedLastName', as: 'definedLastName',
attributes: ['name'] attributes: ['name'],
}
],
required: false required: false
} }
]
}
], ],
order: [ order: [
[{ model: ChurchOfficeType, as: 'type' }, 'hierarchyLevel', 'DESC'], [{ model: ChurchOfficeType, as: 'type' }, 'hierarchyLevel', 'DESC'],
@@ -4902,6 +4911,7 @@ class FalukantService extends BaseService {
] ]
}); });
console.log('[getChurchOverview] Found', offices.length, 'offices');
return offices.map(office => { return offices.map(office => {
const o = office.get({ plain: true }); const o = office.get({ plain: true });
return { return {
@@ -4928,6 +4938,11 @@ class FalukantService extends BaseService {
: null : null
}; };
}); });
} catch (error) {
console.error('[getChurchOverview] Error:', error);
console.error('[getChurchOverview] Stack:', error.stack);
throw error;
}
} }
async getAvailableChurchPositions(hashedUserId) { async getAvailableChurchPositions(hashedUserId) {