Refactor VocabService to improve boolean parameter handling and enhance debugging
- Updated VocabService to convert string parameters for course retrieval into booleans, ensuring accurate filtering based on user input. - Added detailed debug logging to track the state of query conditions and the final WHERE clause, aiding in troubleshooting and performance analysis. - Improved comments for clarity on the logic and implications of the changes made.
This commit is contained in:
@@ -563,20 +563,24 @@ export default class VocabService {
|
||||
async getCourses(hashedUserId, { includePublic = true, includeOwn = true, languageId, nativeLanguageId, search } = {}) {
|
||||
const user = await this._getUserByHashedId(hashedUserId);
|
||||
|
||||
// Konvertiere String-Parameter zu Booleans
|
||||
const includePublicBool = includePublic === 'true' || includePublic === true;
|
||||
const includeOwnBool = includeOwn === 'true' || includeOwn === true;
|
||||
|
||||
const where = {};
|
||||
const andConditions = [];
|
||||
|
||||
// Zugriffsbedingungen
|
||||
if (includeOwn && includePublic) {
|
||||
if (includeOwnBool && includePublicBool) {
|
||||
andConditions.push({
|
||||
[Op.or]: [
|
||||
{ ownerUserId: user.id },
|
||||
{ isPublic: true }
|
||||
]
|
||||
});
|
||||
} else if (includeOwn) {
|
||||
} else if (includeOwnBool) {
|
||||
where.ownerUserId = user.id;
|
||||
} else if (includePublic) {
|
||||
} else if (includePublicBool) {
|
||||
where.isPublic = true;
|
||||
}
|
||||
|
||||
@@ -608,9 +612,19 @@ 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
|
||||
});
|
||||
|
||||
if (andConditions.length > 0) {
|
||||
// Wenn where bereits direkte Eigenschaften hat, füge sie zu andConditions hinzu
|
||||
const directWhereProps = Object.keys(where).filter(key => key !== Op.and && key !== Op.or);
|
||||
if (directWhereProps.length > 0) {
|
||||
const directWhere = {};
|
||||
for (const key of directWhereProps) {
|
||||
@@ -618,9 +632,15 @@ 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));
|
||||
}
|
||||
// Wenn nur direkte Eigenschaften existieren (undConditions.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,
|
||||
@@ -633,9 +653,12 @@ export default class VocabService {
|
||||
languageId,
|
||||
nativeLanguageId,
|
||||
search,
|
||||
where: JSON.stringify(where, null, 2),
|
||||
includePublic,
|
||||
includeOwn
|
||||
whereBefore: JSON.stringify(where, null, 2),
|
||||
includePublic: includePublicBool,
|
||||
includeOwn: includeOwnBool,
|
||||
andConditionsLength: andConditions.length,
|
||||
directWherePropsBefore: Object.keys(where).filter(key => key !== Op.and && key !== Op.or),
|
||||
whereAfter: JSON.stringify(where, null, 2)
|
||||
});
|
||||
|
||||
const coursesData = courses.map(c => c.get({ plain: true }));
|
||||
|
||||
Reference in New Issue
Block a user