Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 4d20d83

Browse files
authored
FIX: User directory for solutions should update when value changes from positive value to zero (#372)
# bug If John created a post which is a solution in March, the user directory would show that John solution = 1. In April, if John has not solved a topic, he would still have solution = 1 in the user directory. # fix reset solutions to 0 before updating
1 parent 94f0c5a commit 4d20d83

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

plugin.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ def self.skip_db?
290290
end
291291

292292
query = <<~SQL
293+
UPDATE directory_items di
294+
SET solutions = 0
295+
WHERE di.period_type = :period_type AND di.solutions IS NOT NULL;
296+
293297
WITH x AS (
294298
SELECT p.user_id, COUNT(DISTINCT st.id) AS solutions
295299
FROM discourse_solved_solved_topics AS st
@@ -310,11 +314,10 @@ def self.skip_db?
310314
GROUP BY p.user_id
311315
)
312316
UPDATE directory_items di
313-
SET solutions = x.solutions
314-
FROM x
315-
WHERE x.user_id = di.user_id
316-
AND di.period_type = :period_type
317-
AND di.solutions <> x.solutions
317+
SET solutions = x.solutions
318+
FROM x
319+
WHERE x.user_id = di.user_id
320+
AND di.period_type = :period_type;
318321
SQL
319322

320323
add_directory_column("solutions", query:)

spec/models/directory_item_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,32 @@
101101
).solutions,
102102
).to eq(1)
103103
end
104+
105+
context "when refreshing across dates" do
106+
it "updates the user's solution count from 1 to 0" do
107+
freeze_time 40.days.ago
108+
DiscourseSolved.accept_answer!(topic_post1, Discourse.system_user)
109+
110+
DirectoryItem.refresh!
111+
112+
expect(
113+
DirectoryItem.find_by(
114+
user_id: user.id,
115+
period_type: DirectoryItem.period_types[:monthly],
116+
).solutions,
117+
).to eq(1)
118+
119+
unfreeze_time
120+
121+
DirectoryItem.refresh!
122+
123+
expect(
124+
DirectoryItem.find_by(
125+
user_id: user.id,
126+
period_type: DirectoryItem.period_types[:monthly],
127+
).solutions,
128+
).to eq(0)
129+
end
130+
end
104131
end
105132
end

0 commit comments

Comments
 (0)