Skip to content

Commit

Permalink
fix(Actions): compute_reputation query (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Aug 15, 2021
1 parent a18cddf commit 5e31b48
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions apps/cf/lib/actions/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ defmodule CF.Actions do

@spec compute_reputation!(%User{}) :: integer
def compute_reputation!(user) do
DB.Schema.User
|> where([u], u.id == ^user.id)
|> join(:inner, [u], action in UserAction, on: action.user_id == u.id)
|> join(:inner, [u, _], action in UserAction, on: action.target_user_id == u.id)
DB.Schema.UserAction
|> where([a], a.user_id == ^user.id or a.target_user_id == ^user.id)
|> select(
[_, actions_authored, actions_targeted],
coalesce(sum(actions_authored.author_reputation_change), 0) +
coalesce(sum(actions_targeted.target_reputation_change), 0)
[a],
coalesce(
sum(
fragment(
"CASE WHEN user_id = ? THEN author_reputation_change ELSE target_reputation_change END",
^user.id
)
),
0
)
)
|> DB.Repo.one!()
end
Expand Down

0 comments on commit 5e31b48

Please sign in to comment.