Optimiere die Funktion zum Abrufen von Worship-Optionen: Reduziere die Anzahl der Datenbankabfragen, indem Organizer und SacristanService in einer Abfrage zusammengefasst werden. Verbessere die Fehlerbehandlung durch detailliertere Fehlermeldungen.

This commit is contained in:
Torsten Schulz (local)
2025-10-07 17:10:18 +02:00
parent ecd03d29f4
commit 6de8cac0bc

View File

@@ -123,45 +123,28 @@ exports.getFilteredWorships = async (req, res) => {
exports.getWorshipOptions = async (req, res) => { exports.getWorshipOptions = async (req, res) => {
try { try {
// Alle eindeutigen Organizer-Werte abrufen // Alle Worships mit organizer und sacristanService abrufen
const organizers = await Worship.findAll({ const worships = await Worship.findAll({
attributes: [[sequelize.fn('DISTINCT', sequelize.col('organizer')), 'organizer']], attributes: ['organizer', 'sacristanService'],
where: {
organizer: {
[Op.not]: null,
[Op.ne]: ''
}
},
raw: true
});
// Alle eindeutigen Sacristan-Service-Werte abrufen
const sacristanServices = await Worship.findAll({
attributes: [[sequelize.fn('DISTINCT', sequelize.col('sacristanService')), 'sacristanService']],
where: {
sacristanService: {
[Op.not]: null,
[Op.ne]: ''
}
},
raw: true raw: true
}); });
// Strings aufteilen (kommasepariert) und alle eindeutigen Werte sammeln // Strings aufteilen (kommasepariert) und alle eindeutigen Werte sammeln
const organizerSet = new Set(); const organizerSet = new Set();
organizers.forEach(item => { const sacristanSet = new Set();
if (item.organizer) {
item.organizer.split(',').forEach(org => { worships.forEach(worship => {
// Organizer verarbeiten
if (worship.organizer && worship.organizer.trim() !== '') {
worship.organizer.split(',').forEach(org => {
const trimmed = org.trim(); const trimmed = org.trim();
if (trimmed) organizerSet.add(trimmed); if (trimmed) organizerSet.add(trimmed);
}); });
} }
});
const sacristanSet = new Set(); // SacristanService verarbeiten
sacristanServices.forEach(item => { if (worship.sacristanService && worship.sacristanService.trim() !== '') {
if (item.sacristanService) { worship.sacristanService.split(',').forEach(sac => {
item.sacristanService.split(',').forEach(sac => {
const trimmed = sac.trim(); const trimmed = sac.trim();
if (trimmed) sacristanSet.add(trimmed); if (trimmed) sacristanSet.add(trimmed);
}); });
@@ -173,7 +156,7 @@ exports.getWorshipOptions = async (req, res) => {
sacristanServices: Array.from(sacristanSet).sort() sacristanServices: Array.from(sacristanSet).sort()
}); });
} catch (error) { } catch (error) {
console.log(error); console.error('Fehler beim Abrufen der Worship-Optionen:', error);
res.status(500).json({ message: 'Fehler beim Abrufen der Worship-Optionen' }); res.status(500).json({ message: 'Fehler beim Abrufen der Worship-Optionen', error: error.message });
} }
}; };