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