diff --git a/backend/src/routes/groups.js b/backend/src/routes/groups.js index 0c2e2af..7c0ffb3 100644 --- a/backend/src/routes/groups.js +++ b/backend/src/routes/groups.js @@ -35,22 +35,12 @@ router.get('/moderation/groups', async (req, res) => { try { const { workshopOnly, platform } = req.query; - let groups; - - if (workshopOnly === 'true') { - // Filter: Nur Gruppen mit Werkstatt-Consent aber ohne Social Media - groups = await GroupRepository.getGroupsByConsentStatus(true, null); - } else if (platform) { - // Filter: Gruppen mit bestimmter Social Media Platform - groups = await GroupRepository.getGroupsByConsentStatus(true, platform); - } else { - // Alle Gruppen mit Consent-Daten - groups = await GroupRepository.getAllGroupsWithModerationInfo(); - } + // Hole alle Gruppen mit vollständigen Infos (inkl. Bilder) + let allGroups = await GroupRepository.getAllGroupsWithModerationInfo(); // Füge Consent-Daten für jede Gruppe hinzu const groupsWithConsents = await Promise.all( - groups.map(async (group) => { + allGroups.map(async (group) => { const consents = await GroupRepository.getSocialMediaConsentsForGroup(group.groupId); return { ...group, @@ -59,11 +49,30 @@ router.get('/moderation/groups', async (req, res) => { }) ); + // Jetzt filtern wir basierend auf den Query-Parametern + let filteredGroups = groupsWithConsents; + + if (workshopOnly === 'true') { + // Filter: Nur Gruppen mit Werkstatt-Consent aber OHNE Social Media Consents + filteredGroups = groupsWithConsents.filter(group => + group.display_in_workshop && + (!group.socialMediaConsents || group.socialMediaConsents.length === 0) + ); + } else if (platform) { + // Filter: Gruppen mit bestimmter Social Media Platform + filteredGroups = groupsWithConsents.filter(group => + group.socialMediaConsents && + group.socialMediaConsents.some(consent => + consent.platform_name === platform && consent.consented + ) + ); + } + res.json({ - groups: groupsWithConsents, - totalCount: groupsWithConsents.length, - pendingCount: groupsWithConsents.filter(g => !g.approved).length, - approvedCount: groupsWithConsents.filter(g => g.approved).length + groups: filteredGroups, + totalCount: filteredGroups.length, + pendingCount: filteredGroups.filter(g => !g.approved).length, + approvedCount: filteredGroups.filter(g => g.approved).length }); } catch (error) { console.error('Error fetching moderation groups:', error);