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.

This commit is contained in:
Torsten Schulz (local)
2026-01-28 17:02:27 +01:00
parent 934e80c2ab
commit 37129055e6
3 changed files with 54 additions and 39 deletions

View File

@@ -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'),

View File

@@ -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'
});

View File

@@ -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;