diff --git a/services/category.service.ts b/services/category.service.ts index 5a56fb7..bb5dd4c 100644 --- a/services/category.service.ts +++ b/services/category.service.ts @@ -16,16 +16,22 @@ export default { FROM categories c JOIN quests q ON q.category_id = c.id WHERE - ($1::INTEGER[] IS NULL OR c.id = ANY($1)) + ($1::BOOLEAN IS TRUE OR c.id = ANY($2::INTEGER[])) AND c.deleted_by IS NULL AND q.id NOT IN ( SELECT UNNEST(completed_quests) FROM users - WHERE id = $2 + WHERE id = $3 ) AND q.soft_deleted IS NOT TRUE `; - const values = [categoryFilters, userId]; + + // Determine if categoryFilters is empty + const isCategoryFiltersEmpty = !categoryFilters || categoryFilters.length === 0; + + // If categoryFilters is empty, set it to null, otherwise use the array + const values = [isCategoryFiltersEmpty, categoryFilters, userId]; + const categories = await executeQuery(query, values); return categories || []; diff --git a/services/quest.service.ts b/services/quest.service.ts index 2f0ad14..8aa1fef 100644 --- a/services/quest.service.ts +++ b/services/quest.service.ts @@ -94,6 +94,7 @@ export default { async getRandomQuest(userId: number): Promise { // 1. Fetch user preferences const { categoryFilters, completedQuests } = await userService.getUserPreferences(userId); + console.log(categoryFilters, completedQuests); // 2. Fetch available categories based on user filters const availableCategories = await categoryService.getAvailableCategories(userId, categoryFilters); @@ -112,11 +113,14 @@ export default { quests.description, quests.objectives, quests.image_url, - categories.name AS category + categories.name AS category, + su.username FROM quests JOIN categories ON quests.category_id = categories.id + LEFT JOIN + users su on su.id = quests.suggested_by WHERE quests.id NOT IN (SELECT UNNEST($1::INTEGER[])) AND category_id = $2