Skip to content

FIX: User directory for solutions should update when value changes from positive value to zero #372

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

Merged
merged 3 commits into from
Jun 11, 2025
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
13 changes: 8 additions & 5 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ def self.skip_db?
end

query = <<~SQL
UPDATE directory_items di
SET solutions = 0
WHERE di.period_type = :period_type AND di.solutions IS NOT NULL;

WITH x AS (
SELECT p.user_id, COUNT(DISTINCT st.id) AS solutions
FROM discourse_solved_solved_topics AS st
Expand All @@ -310,11 +314,10 @@ def self.skip_db?
GROUP BY p.user_id
)
UPDATE directory_items di
SET solutions = x.solutions
FROM x
WHERE x.user_id = di.user_id
AND di.period_type = :period_type
AND di.solutions <> x.solutions
SET solutions = x.solutions
FROM x
WHERE x.user_id = di.user_id
AND di.period_type = :period_type;
SQL

add_directory_column("solutions", query:)
Expand Down
27 changes: 27 additions & 0 deletions spec/models/directory_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,32 @@
).solutions,
).to eq(1)
end

context "when refreshing across dates" do
it "updates the user's solution count from 1 to 0" do
freeze_time 40.days.ago
DiscourseSolved.accept_answer!(topic_post1, Discourse.system_user)

DirectoryItem.refresh!

expect(
DirectoryItem.find_by(
user_id: user.id,
period_type: DirectoryItem.period_types[:monthly],
).solutions,
).to eq(1)

unfreeze_time

DirectoryItem.refresh!

expect(
DirectoryItem.find_by(
user_id: user.id,
period_type: DirectoryItem.period_types[:monthly],
).solutions,
).to eq(0)
end
end
end
end