diff --git a/backend/services/vocabService.js b/backend/services/vocabService.js index 717d168..52c8ceb 100644 --- a/backend/services/vocabService.js +++ b/backend/services/vocabService.js @@ -612,15 +612,10 @@ export default class VocabService { // Kombiniere alle AND-Bedingungen // Wenn sowohl andConditions als auch direkte where-Eigenschaften existieren, // müssen sie kombiniert werden - const directWhereProps = Object.keys(where).filter(key => key !== Op.and && key !== Op.or); - - // Debug: Prüfe, ob direkte Eigenschaften gesetzt wurden - console.log(`[getCourses] Debug - Vor Kombination:`, { - directWhereProps, - whereKeys: Object.keys(where), - andConditionsLength: andConditions.length, - languageId, - nativeLanguageId + // WICHTIG: directWhereProps muss NACH dem Setzen aller direkten Eigenschaften berechnet werden + const directWhereProps = Object.keys(where).filter(key => { + // Filtere Op.and und Op.or heraus (sind Symbol-Keys) + return key !== Op.and && key !== Op.or && typeof key === 'string'; }); if (andConditions.length > 0) { @@ -632,15 +627,18 @@ export default class VocabService { delete where[key]; } andConditions.push(directWhere); - console.log(`[getCourses] Debug - Direkte Eigenschaften zu andConditions hinzugefügt:`, directWhere); } - where[Op.and] = andConditions; - console.log(`[getCourses] Debug - Finale andConditions:`, JSON.stringify(andConditions, null, 2)); + // Entferne leere Objekte aus andConditions + const filteredConditions = andConditions.filter(cond => { + return cond && typeof cond === 'object' && Object.keys(cond).length > 0; + }); + // Setze andConditions nur, wenn sie nicht leer sind + if (filteredConditions.length > 0) { + where[Op.and] = filteredConditions; + } } - // Wenn nur direkte Eigenschaften existieren (undConditions.length === 0), + // Wenn nur direkte Eigenschaften existieren (andConditions.length === 0), // bleiben sie in where (nichts zu tun, sie sind bereits dort) - - console.log(`[getCourses] Debug - Finale WHERE-Klausel:`, JSON.stringify(where, null, 2)); const courses = await VocabCourse.findAll({ where, @@ -666,17 +664,37 @@ export default class VocabService { // Lade Sprachnamen für alle Kurse const languageIds = [...new Set(coursesData.map(c => c.languageId))]; if (languageIds.length > 0) { - const [languages] = await sequelize.query( + const languages = await sequelize.query( `SELECT id, name FROM community.vocab_language WHERE id IN (:languageIds)`, { replacements: { languageIds }, type: sequelize.QueryTypes.SELECT } ); - const languageMap = new Map(languages.map(l => [l.id, l.name])); - coursesData.forEach(c => { - c.languageName = languageMap.get(c.languageId) || null; - }); + if (Array.isArray(languages)) { + const languageMap = new Map(languages.map(l => [l.id, l.name])); + coursesData.forEach(c => { + c.languageName = languageMap.get(c.languageId) || null; + }); + } + } + + // Lade Muttersprachen-Namen für alle Kurse + const nativeLanguageIds = [...new Set(coursesData.map(c => c.nativeLanguageId).filter(id => id !== null))]; + if (nativeLanguageIds.length > 0) { + const nativeLanguages = await sequelize.query( + `SELECT id, name FROM community.vocab_language WHERE id IN (:nativeLanguageIds)`, + { + replacements: { nativeLanguageIds }, + type: sequelize.QueryTypes.SELECT + } + ); + if (Array.isArray(nativeLanguages)) { + const nativeLanguageMap = new Map(nativeLanguages.map(l => [l.id, l.name])); + coursesData.forEach(c => { + c.nativeLanguageName = c.nativeLanguageId ? nativeLanguageMap.get(c.nativeLanguageId) || null : null; + }); + } } return coursesData;