Skip to content

Commit 78a05b4

Browse files
committed
feat: add last_context logic to determine user's default org context (#70)
1 parent 9efe5fb commit 78a05b4

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

lib/algora/accounts/accounts.ex

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ defmodule Algora.Accounts do
55

66
alias Algora.Accounts.Identity
77
alias Algora.Accounts.User
8+
alias Algora.Bounties.Bounty
9+
alias Algora.Organizations
810
alias Algora.Payments.Transaction
911
alias Algora.Repo
1012

@@ -367,6 +369,43 @@ defmodule Algora.Accounts do
367369
{:ok, Repo.preload(user, :identities, force: true)}
368370
end
369371

372+
def last_context(%{last_context: nil} = user) do
373+
orgs = Organizations.get_user_orgs(user)
374+
375+
last_debit_query =
376+
from(t in Transaction,
377+
join: u in assoc(t, :user),
378+
where: t.type == :debit,
379+
where: u.id in ^Enum.map(orgs, & &1.id),
380+
order_by: [desc: t.succeeded_at],
381+
limit: 1
382+
)
383+
384+
last_bounty_query =
385+
from(b in Bounty,
386+
join: c in assoc(b, :creator),
387+
where: c.id in ^Enum.map(orgs, & &1.id),
388+
order_by: [desc: b.created_at],
389+
limit: 1
390+
)
391+
392+
last_sponsored_on_behalf_of =
393+
cond do
394+
last_debit = Repo.one(last_debit_query) -> last_debit.user
395+
last_bounty = Repo.one(last_bounty_query) -> last_bounty.owner
396+
true -> nil
397+
end
398+
399+
case last_sponsored_on_behalf_of do
400+
%{type: :organization} -> last_sponsored_on_behalf_of.handle
401+
_ -> default_context()
402+
end
403+
end
404+
405+
def last_context(%{last_context: last_context}), do: last_context
406+
407+
def default_context, do: "personal"
408+
370409
defp get_flag(user), do: Algora.Misc.CountryEmojis.get(user.country, "🌎")
371410

372411
# TODO: implement this

lib/algora/accounts/schemas/user.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,4 @@ defmodule Algora.Accounts.User do
327327
def url(%{handle: handle, type: :organization}), do: "#{Endpoint.url()}/org/#{handle}"
328328
def url(%{handle: handle}) when is_binary(handle), do: "#{Endpoint.url()}/org/#{handle}"
329329
def url(%{provider_login: handle}), do: "https://github.com/#{handle}"
330-
331-
def last_context(%{last_context: last_context}), do: last_context || default_context()
332-
333-
def default_context, do: "personal"
334330
end

lib/algora_web/controllers/installation_callback_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ defmodule AlgoraWeb.InstallationCallbackController do
7878
end
7979
end
8080

81-
defp redirect_url(conn), do: ~p"/org/#{User.last_context(conn.assigns.current_user)}"
81+
defp redirect_url(conn), do: ~p"/org/#{Accounts.last_context(conn.assigns.current_user)}"
8282
end

lib/algora_web/controllers/user_auth.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ defmodule AlgoraWeb.UserAuth do
190190
def signed_in_path_from_context(org_handle), do: ~p"/org/#{org_handle}"
191191

192192
def signed_in_path(%User{} = user) do
193-
signed_in_path_from_context(User.last_context(user))
193+
signed_in_path_from_context(Accounts.last_context(user))
194194
end
195195

196196
def signed_in_path(conn) do
197-
signed_in_path_from_context(get_session(conn, :last_context) || User.default_context())
197+
signed_in_path_from_context(get_session(conn, :last_context) || Accounts.default_context())
198198
end
199199

200200
defp login_code_ttl, do: 3600

lib/algora_web/live/payment/success_live.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule AlgoraWeb.Payment.SuccessLive do
22
@moduledoc false
33
use AlgoraWeb, :live_view
44

5-
alias Algora.Accounts.User
5+
alias Algora.Accounts
66

77
def mount(_params, _session, socket) do
88
socket =
@@ -12,7 +12,7 @@ defmodule AlgoraWeb.Payment.SuccessLive do
1212

1313
current_user ->
1414
to =
15-
case User.last_context(current_user) do
15+
case Accounts.last_context(current_user) do
1616
"personal" -> ~p"/user/transactions"
1717
org_handle -> ~p"/org/#{org_handle}/transactions"
1818
end

scripts/database_migration.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ defmodule DatabaseMigration do
197197
"linkedin_url" => nil,
198198
"og_title" => nil,
199199
"og_image_url" => nil,
200-
"last_context" => row["handle"],
200+
"last_context" => nil,
201201
"need_avatar" => nil,
202202
"inserted_at" => row["created_at"],
203203
"updated_at" => row["updated_at"],

0 commit comments

Comments
 (0)