Skip to content

Commit

Permalink
fix: modify GetUserCourses to filter out other user's courses changin…
Browse files Browse the repository at this point in the history
…g by left joins to inner joins
  • Loading branch information
carddev81 committed Oct 29, 2024
1 parent 0e96a2e commit bc1edfd
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions backend/src/database/user_catalogue.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ func (db *DB) GetUserCourses(userId uint, order string, orderBy string, search s
ELSE (SELECT COUNT(m.id) * 100.0 / c.total_progress_milestones) END
END as course_progress,
a.total_time`, userId, userId).
Joins("LEFT JOIN provider_platforms pp ON c.provider_platform_id = pp.id").
Joins("JOIN milestones as m ON m.course_id = c.id AND m.user_id = ?", userId).
Joins("LEFT JOIN favorites f ON f.course_id = c.id AND f.user_id = ?", userId).
Joins("LEFT JOIN outcomes o ON o.course_id = c.id AND o.user_id = ?", userId).
Joins(`LEFT JOIN activities a ON a.id = (
SELECT id FROM activities
WHERE course_id = c.id AND user_id = ?
ORDER BY created_at DESC
LIMIT 1
)`, userId).
Joins("JOIN provider_platforms pp ON c.provider_platform_id = pp.id").
Joins("JOIN milestones as m ON m.course_id = c.id and m.user_id = ?", userId).
Joins(`JOIN (select id, course_id, user_id, total_time, row_number() over (PARTITION BY course_id, user_id ORDER BY created_at DESC) AS RN
from activities
) a on a.course_id = c.id
and a.user_id = m.user_id
and a.RN = 1`).
Joins("LEFT JOIN favorites f ON f.course_id = c.id AND f.user_id = m.user_id").
Joins("LEFT JOIN outcomes o ON o.course_id = c.id AND o.user_id = m.user_id").
Where("c.deleted_at IS NULL").
Where("pp.deleted_at IS NULL")

Expand Down

0 comments on commit bc1edfd

Please sign in to comment.