mirror of
https://github.com/discourse/discourse-solved.git
synced 2025-07-01 19:32:11 +00:00
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:
parent
94f0c5a315
commit
4d20d83374
13
plugin.rb
13
plugin.rb
@ -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
|
||||
@ -310,11 +314,10 @@ after_initialize do
|
||||
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:)
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user