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

This commit is contained in:
Nat 2025-06-06 23:32:03 +08:00
parent ae01ad30c3
commit c859e41650
No known key found for this signature in database
GPG Key ID: 4938B35D927EC773
2 changed files with 45 additions and 17 deletions

View File

@ -291,23 +291,24 @@ after_initialize do
query = <<~SQL query = <<~SQL
WITH x AS ( WITH x AS (
SELECT p.user_id, COUNT(DISTINCT st.id) AS solutions SELECT users.id AS user_id,
FROM discourse_solved_solved_topics AS st COUNT(DISTINCT CASE WHEN
JOIN posts AS p st.topic_id IS NOT NULL AND
ON p.id = st.answer_post_id t.id IS NOT NULL AND
AND COALESCE(st.created_at, :since) > :since t.archetype <> 'private_message' AND
AND p.deleted_at IS NULL t.deleted_at IS NULL AND
JOIN topics AS t p.deleted_at IS NULL
ON t.id = st.topic_id THEN st.topic_id ELSE NULL END) AS solutions
AND t.archetype <> 'private_message' FROM users
AND t.deleted_at IS NULL LEFT JOIN posts p ON p.user_id = users.id
JOIN users AS u LEFT JOIN discourse_solved_solved_topics st
ON u.id = p.user_id ON st.answer_post_id = p.id
WHERE u.id > 0 AND st.created_at >= :since
AND u.active LEFT JOIN topics t ON t.id = st.topic_id
AND u.silenced_till IS NULL WHERE users.active
AND u.suspended_till IS NULL AND users.silenced_till IS NULL
GROUP BY p.user_id AND users.suspended_till IS NULL
GROUP BY users.id
) )
UPDATE directory_items di UPDATE directory_items di
SET solutions = x.solutions SET solutions = x.solutions

View File

@ -101,5 +101,32 @@ describe DirectoryItem, type: :model do
).solutions, ).solutions,
).to eq(1) ).to eq(1)
end 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
end end