From 9363212e907c35d016420ec9178463b3e624b778 Mon Sep 17 00:00:00 2001 From: Brandon Norsworthy Date: Sun, 22 Sep 2024 22:05:27 -0500 Subject: [PATCH 1/2] resolves #89 --- services/quest.service.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/quest.service.ts b/services/quest.service.ts index 2f0ad14..401f28a 100644 --- a/services/quest.service.ts +++ b/services/quest.service.ts @@ -112,11 +112,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 From 6b0f63864d23fe5e166c0db977a31aca3998ca2a Mon Sep 17 00:00:00 2001 From: Brandon Norsworthy Date: Sun, 22 Sep 2024 22:12:30 -0500 Subject: [PATCH 2/2] fix empty filter returning no quests --- services/category.service.ts | 12 +++++++++--- services/quest.service.ts | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) 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 401f28a..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);