From 37129055e67afee24d2e17759dbc53e71a276bab Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 28 Jan 2026 17:02:27 +0100 Subject: [PATCH] Update supervisorId handling in ChurchApplication and FalukantService: Allow supervisorId to be null for entry-level positions, enhancing flexibility in application processing. Improve prerequisite office type updates in initializeFalukantTypes for better data integrity. --- .../falukant/data/church_application.js | 4 +- backend/services/falukantService.js | 77 ++++++++++--------- .../utils/falukant/initializeFalukantTypes.js | 12 ++- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/backend/models/falukant/data/church_application.js b/backend/models/falukant/data/church_application.js index 2515c09..ebcc758 100644 --- a/backend/models/falukant/data/church_application.js +++ b/backend/models/falukant/data/church_application.js @@ -23,8 +23,8 @@ ChurchApplication.init({ }, supervisorId: { type: DataTypes.INTEGER, - allowNull: false, - comment: 'ID des Vorgesetzten, der über die Bewerbung entscheidet' + allowNull: true, + comment: 'ID des Vorgesetzten, der über die Bewerbung entscheidet (null für Einstiegspositionen ohne Supervisor)' }, status: { type: DataTypes.ENUM('pending', 'approved', 'rejected'), diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index f9f8536..da828df 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -5282,44 +5282,49 @@ class FalukantService extends BaseService { throw new Error('No available seats'); } - // Finde Supervisor - const higherOfficeTypeIds = await ChurchOfficeType.findAll({ - where: { - hierarchyLevel: { [Op.gt]: officeType.hierarchyLevel } - }, - attributes: ['id'] - }).then(types => types.map(t => t.id)); - - if (higherOfficeTypeIds.length === 0) { - throw new Error('No supervisor office type found'); - } - - const supervisorOffice = await ChurchOffice.findOne({ - where: { - regionId: regionId, - officeTypeId: { [Op.in]: higherOfficeTypeIds } - }, - include: [ - { - model: ChurchOfficeType, - as: 'type', - attributes: ['hierarchyLevel'] + // Finde Supervisor (nur wenn es nicht die niedrigste Position ist) + let supervisorId = null; + if (officeType.hierarchyLevel > 0) { + const higherOfficeTypeIds = await ChurchOfficeType.findAll({ + where: { + hierarchyLevel: { [Op.gt]: officeType.hierarchyLevel } }, - { - model: FalukantCharacter, - as: 'holder', - attributes: ['id'] - } - ], - order: [ - [{ model: ChurchOfficeType, as: 'type' }, 'hierarchyLevel', 'ASC'] - ], - limit: 1 - }); + attributes: ['id'] + }).then(types => types.map(t => t.id)); - if (!supervisorOffice || !supervisorOffice.holder) { - throw new Error('No supervisor found'); + if (higherOfficeTypeIds.length > 0) { + const supervisorOffice = await ChurchOffice.findOne({ + where: { + regionId: regionId, + officeTypeId: { [Op.in]: higherOfficeTypeIds } + }, + include: [ + { + model: ChurchOfficeType, + as: 'type', + attributes: ['hierarchyLevel'] + }, + { + model: FalukantCharacter, + as: 'holder', + attributes: ['id'] + } + ], + order: [ + [{ model: ChurchOfficeType, as: 'type' }, 'hierarchyLevel', 'ASC'] + ], + limit: 1 + }); + + if (!supervisorOffice || !supervisorOffice.holder) { + throw new Error('No supervisor found for this position'); + } + supervisorId = supervisorOffice.holder.id; + } else { + throw new Error('No supervisor office type found'); + } } + // Für Einstiegspositionen (hierarchyLevel 0) ist kein Supervisor erforderlich // Prüfe ob bereits eine Bewerbung existiert const existingApplication = await ChurchApplication.findOne({ @@ -5340,7 +5345,7 @@ class FalukantService extends BaseService { officeTypeId: officeTypeId, characterId: character.id, regionId: regionId, - supervisorId: supervisorOffice.holder.id, + supervisorId: supervisorId, // Kann null sein für Einstiegspositionen status: 'pending' }); diff --git a/backend/utils/falukant/initializeFalukantTypes.js b/backend/utils/falukant/initializeFalukantTypes.js index 1473742..ad31e05 100644 --- a/backend/utils/falukant/initializeFalukantTypes.js +++ b/backend/utils/falukant/initializeFalukantTypes.js @@ -1139,7 +1139,17 @@ export const initializeChurchOfficePrerequisites = async () => { prerequisiteOfficeTypeId: prerequisiteOfficeTypeId } }); - if (wasCreated) created++; else existing++; + if (wasCreated) { + created++; + } else { + // Aktualisiere, falls sich die Voraussetzung geändert hat + if (record.prerequisiteOfficeTypeId !== prerequisiteOfficeTypeId) { + await record.update({ prerequisiteOfficeTypeId: prerequisiteOfficeTypeId }); + created++; // Zähle als Update + } else { + existing++; + } + } } catch (e) { if (falukantDebug) console.error('[Falukant] ChurchOfficePrereq Fehler', { officeId: office?.id, error: e.message }); throw e;