Skip to content

Commit 8c5a607

Browse files
committed
refactor: update last_context logic to use all user contexts
- Replaced organization retrieval with user contexts in last_context function. - Simplified the logic for determining the last context by directly using the new context variable. - Ensured that the last_context is updated correctly based on the latest transactions and bounties.
1 parent 5a17944 commit 8c5a607

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

lib/algora/accounts/accounts.ex

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,13 @@ defmodule Algora.Accounts do
422422
def last_context(nil), do: "nil"
423423

424424
def last_context(%User{last_context: nil} = user) do
425-
orgs = Organizations.get_user_orgs(user)
425+
contexts = get_contexts(user)
426426

427427
last_debit_query =
428428
from(t in Transaction,
429429
join: u in assoc(t, :user),
430430
where: t.type == :debit,
431-
where: u.id in ^Enum.map(orgs, & &1.id),
431+
where: u.id in ^Enum.map(contexts, & &1.id),
432432
order_by: [desc: t.succeeded_at],
433433
limit: 1,
434434
select_merge: %{user: u}
@@ -437,28 +437,22 @@ defmodule Algora.Accounts do
437437
last_bounty_query =
438438
from(b in Bounty,
439439
join: c in assoc(b, :creator),
440-
where: c.id in ^Enum.map(orgs, & &1.id),
440+
where: c.id in ^Enum.map(contexts, & &1.id),
441441
order_by: [desc: b.inserted_at],
442442
limit: 1,
443443
select_merge: %{creator: c}
444444
)
445445

446-
last_sponsored_on_behalf_of =
446+
new_context =
447447
cond do
448-
last_debit = Repo.one(last_debit_query) -> last_debit.user
449-
last_bounty = Repo.one(last_bounty_query) -> last_bounty.owner
450-
true -> nil
448+
last_debit = Repo.one(last_debit_query) -> last_debit.user.handle
449+
last_bounty = Repo.one(last_bounty_query) -> last_bounty.owner.handle
450+
true -> default_context()
451451
end
452452

453-
last_context =
454-
case last_sponsored_on_behalf_of do
455-
%{type: :organization} -> last_sponsored_on_behalf_of.handle
456-
_ -> default_context()
457-
end
458-
459-
update_settings(user, %{last_context: last_context})
453+
update_settings(user, %{last_context: new_context})
460454

461-
last_context
455+
new_context
462456
end
463457

464458
def last_context(%User{last_context: last_context}), do: last_context

0 commit comments

Comments
 (0)