Skip to content

Commit d307d3d

Browse files
committed
feat: add user retrieval by provider ID in Workspace
- Introduced `ensure_user_by_provider_id` function to fetch or create a user based on GitHub provider ID. - Updated `BountyLive` to utilize the new function for user management during bounty sharing, enhancing user experience and data handling.
1 parent 21ea058 commit d307d3d

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lib/algora/workspace/workspace.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ defmodule Algora.Workspace do
200200
end
201201
end
202202

203+
def ensure_user_by_provider_id(token, provider_id) do
204+
case Repo.get_by(User, provider: "github", provider_id: provider_id) do
205+
%User{} = user -> {:ok, user}
206+
nil -> create_user_from_github(token, provider_id)
207+
end
208+
end
209+
203210
def sync_user(user, repository, owner, repo) do
204211
github_user = repository["owner"]
205212

lib/algora_web/live/bounty_live.ex

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,18 @@ defmodule AlgoraWeb.BountyLive do
181181

182182
case apply_action(changeset, :save) do
183183
{:ok, data} ->
184-
shared_with = Enum.uniq(bounty.shared_with ++ [data.github_handle])
185-
186-
case bounty
187-
|> Bounty.settings_changeset(%{shared_with: shared_with})
188-
|> Repo.update() do
189-
{:ok, _} ->
190-
{:noreply,
191-
socket
192-
|> put_flash(:info, "Bounty shared!")
193-
|> assign_exclusives(shared_with)
194-
|> close_drawers()}
184+
with {:ok, token} <- Accounts.get_access_token(socket.assigns.current_user),
185+
{:ok, user} <- Workspace.ensure_user(token, data.github_handle),
186+
shared_with = Enum.uniq(bounty.shared_with ++ [user.provider_id]),
187+
{:ok, _} <- bounty |> Bounty.settings_changeset(%{shared_with: shared_with}) |> Repo.update() do
188+
{:noreply,
189+
socket
190+
|> put_flash(:info, "Bounty shared!")
191+
|> assign_exclusives(shared_with)
192+
|> close_drawers()}
193+
else
194+
nil ->
195+
{:noreply, put_flash(socket, :error, "User not found")}
195196

196197
{:error, reason} ->
197198
Logger.error("Failed to share bounty: #{inspect(reason)}")
@@ -691,9 +692,9 @@ defmodule AlgoraWeb.BountyLive do
691692

692693
defp assign_exclusives(socket, shared_with) do
693694
exclusives =
694-
Enum.flat_map(shared_with, fn github_handle ->
695+
Enum.flat_map(shared_with, fn provider_id ->
695696
with {:ok, token} <- Accounts.get_access_token(socket.assigns.current_user),
696-
{:ok, user} <- Workspace.ensure_user(token, github_handle) do
697+
{:ok, user} <- Workspace.ensure_user_by_provider_id(token, provider_id) do
697698
[user]
698699
else
699700
_ -> []

0 commit comments

Comments
 (0)