fix: Fix moderation filter - load all groups with images first, then filter

Problem: Filtered groups were missing preview images because
getGroupsByConsentStatus() only returned group metadata without images.

Solution: Load all groups with getAllGroupsWithModerationInfo() first
(includes images), add consent data, then filter in-memory based on
query parameters. This ensures preview images are always included.
This commit is contained in:
Matthias Lotz 2025-11-09 22:28:59 +01:00
parent a34d7eab7a
commit 8d2f09f71a

View File

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