Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/algora/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Algora.Accounts do

alias Algora.Accounts.Identity
alias Algora.Accounts.User
alias Algora.Bounties.Bounty
alias Algora.Organizations
alias Algora.Payments.Transaction
alias Algora.Repo

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

def last_context(%{last_context: nil} = user) do
orgs = Organizations.get_user_orgs(user)

last_debit_query =
from(t in Transaction,
join: u in assoc(t, :user),
where: t.type == :debit,
where: u.id in ^Enum.map(orgs, & &1.id),
order_by: [desc: t.succeeded_at],
limit: 1
)

last_bounty_query =
from(b in Bounty,
join: c in assoc(b, :creator),
where: c.id in ^Enum.map(orgs, & &1.id),
order_by: [desc: b.created_at],
limit: 1
)

last_sponsored_on_behalf_of =
cond do
last_debit = Repo.one(last_debit_query) -> last_debit.user
last_bounty = Repo.one(last_bounty_query) -> last_bounty.owner
true -> nil
end

case last_sponsored_on_behalf_of do
%{type: :organization} -> last_sponsored_on_behalf_of.handle
_ -> default_context()
end
end

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

def default_context, do: "personal"

defp get_flag(user), do: Algora.Misc.CountryEmojis.get(user.country, "🌎")

# TODO: implement this
Expand Down
4 changes: 0 additions & 4 deletions lib/algora/accounts/schemas/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,4 @@ defmodule Algora.Accounts.User do
def url(%{handle: handle, type: :organization}), do: "#{Endpoint.url()}/org/#{handle}"
def url(%{handle: handle}) when is_binary(handle), do: "#{Endpoint.url()}/org/#{handle}"
def url(%{provider_login: handle}), do: "https://github.com/#{handle}"

def last_context(%{last_context: last_context}), do: last_context || default_context()

def default_context, do: "personal"
end
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ defmodule AlgoraWeb.InstallationCallbackController do
end
end

defp redirect_url(conn), do: ~p"/org/#{User.last_context(conn.assigns.current_user)}"
defp redirect_url(conn), do: ~p"/org/#{Accounts.last_context(conn.assigns.current_user)}"
end
4 changes: 2 additions & 2 deletions lib/algora_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ defmodule AlgoraWeb.UserAuth do
def signed_in_path_from_context(org_handle), do: ~p"/org/#{org_handle}"

def signed_in_path(%User{} = user) do
signed_in_path_from_context(User.last_context(user))
signed_in_path_from_context(Accounts.last_context(user))
end

def signed_in_path(conn) do
signed_in_path_from_context(get_session(conn, :last_context) || User.default_context())
signed_in_path_from_context(get_session(conn, :last_context) || Accounts.default_context())
end

defp login_code_ttl, do: 3600
Expand Down
4 changes: 2 additions & 2 deletions lib/algora_web/live/payment/success_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule AlgoraWeb.Payment.SuccessLive do
@moduledoc false
use AlgoraWeb, :live_view

alias Algora.Accounts.User
alias Algora.Accounts

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

current_user ->
to =
case User.last_context(current_user) do
case Accounts.last_context(current_user) do
"personal" -> ~p"/user/transactions"
org_handle -> ~p"/org/#{org_handle}/transactions"
end
Expand Down
2 changes: 1 addition & 1 deletion scripts/database_migration.exs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ defmodule DatabaseMigration do
"linkedin_url" => nil,
"og_title" => nil,
"og_image_url" => nil,
"last_context" => row["handle"],
"last_context" => nil,
"need_avatar" => nil,
"inserted_at" => row["created_at"],
"updated_at" => row["updated_at"],
Expand Down