Skip to content

Commit a5b014d

Browse files
committed
add admin fn to release payment
1 parent 65af5cc commit a5b014d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

lib/algora/admin/admin.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Algora.Admin do
1212
alias Algora.Github
1313
alias Algora.Parser
1414
alias Algora.Payments
15+
alias Algora.Payments.Transaction
1516
alias Algora.Repo
1617
alias Algora.Util
1718
alias Algora.Workspace
@@ -21,6 +22,48 @@ defmodule Algora.Admin do
2122

2223
require Logger
2324

25+
def release_payment(tx_id) do
26+
Repo.transact(fn ->
27+
{_, [tx]} =
28+
Repo.update_all(from(t in Transaction, where: t.id == ^tx_id, select: t),
29+
set: [status: :succeeded, succeeded_at: DateTime.utc_now()]
30+
)
31+
32+
Repo.update_all(from(b in Bounty, where: b.id == ^tx.bounty_id), set: [status: :paid])
33+
34+
activities_result = Repo.insert_activity(tx, %{type: :transaction_succeeded, notify_users: [tx.user_id]})
35+
36+
jobs_result =
37+
case Payments.fetch_active_account(tx.user_id) do
38+
{:ok, _account} ->
39+
%{credit_id: tx.id}
40+
|> Payments.Jobs.ExecutePendingTransfer.new()
41+
|> Oban.insert()
42+
43+
{:error, :no_active_account} ->
44+
Logger.warning("No active account for user #{tx.user_id}")
45+
46+
%{credit_id: tx.id}
47+
|> Bounties.Jobs.PromptPayoutConnect.new()
48+
|> Oban.insert()
49+
end
50+
51+
with {:ok, _} <- activities_result,
52+
{:ok, _} <- jobs_result do
53+
Payments.broadcast()
54+
{:ok, nil}
55+
else
56+
{:error, reason} ->
57+
Logger.error("Failed to update transactions: #{inspect(reason)}")
58+
{:error, :failed_to_update_transactions}
59+
60+
error ->
61+
Logger.error("Failed to update transactions: #{inspect(error)}")
62+
{:error, :failed_to_update_transactions}
63+
end
64+
end)
65+
end
66+
2467
def refresh_bounty(url) do
2568
with %{owner: owner, repo: repo, number: number} <- parse_ticket_url(url),
2669
{:ok, ticket} <- Workspace.ensure_ticket(token_for(owner), owner, repo, number) do

0 commit comments

Comments
 (0)