Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #36

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions services/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [];
Expand Down
6 changes: 5 additions & 1 deletion services/quest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default {
async getRandomQuest(userId: number): Promise<Quest | null> {
// 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);
Expand All @@ -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
Expand Down