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
This commit is contained in:
Natalie Tay 2025-06-11 10:46:36 +08:00 committed by GitHub
parent 94f0c5a315
commit 4d20d83374
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 5 deletions

View File

@ -290,6 +290,10 @@ after_initialize do
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
@ -313,8 +317,7 @@ after_initialize do
SET solutions = x.solutions
FROM x
WHERE x.user_id = di.user_id
AND di.period_type = :period_type
AND di.solutions <> x.solutions
AND di.period_type = :period_type;
SQL
add_directory_column("solutions", query:)

View File

@ -101,5 +101,32 @@ describe DirectoryItem, type: :model do
).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