Skip to content

Commit 3097633

Browse files
committed
add job to notify transfer
1 parent a30ded2 commit 3097633

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

.iex.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ alias Algora.Payments.Customer
1818
alias Algora.Payments.PaymentMethod
1919
alias Algora.Payments.Transaction
2020
alias Algora.Repo
21+
alias Algora.Workspace.Ticket
2122

2223
IEx.configure(inspect: [charlists: :as_lists, limit: :infinity], auto_reload: true)
2324

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ config :algora, Oban,
3737
prompt_payout_connect: 10,
3838
transfers: 1,
3939
activity_notifier: 1,
40-
activity_mailer: 1
40+
activity_mailer: 1,
41+
notify_transfer: 100
4142
]
4243

4344
# Configures the mailer
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
defmodule Algora.Bounties.Jobs.NotifyTransfer do
2+
@moduledoc false
3+
use Oban.Worker, queue: :notify_transfer
4+
5+
import Ecto.Query
6+
7+
alias Algora.Bounties.Ticket
8+
alias Algora.Github
9+
alias Algora.Payments.Transaction
10+
alias Algora.Repo
11+
alias Algora.Workspace.Ticket
12+
13+
require Logger
14+
15+
@impl Oban.Worker
16+
def perform(%Oban.Job{args: %{"transaction_id" => transaction_id}}) do
17+
with {:ok, ticket} <-
18+
Repo.fetch_one(
19+
from t in Ticket,
20+
left_join: bounty in assoc(t, :bounties),
21+
left_join: tip in assoc(t, :tips),
22+
left_join: tx in Transaction,
23+
on: tx.bounty_id == bounty.id or tx.tip_id == tip.id,
24+
join: repo in assoc(t, :repository),
25+
join: user in assoc(repo, :user),
26+
where: tx.id == ^transaction_id,
27+
select_merge: %{
28+
repository: %{repo | user: user}
29+
}
30+
),
31+
ticket_ref = %{
32+
owner: ticket.repository.user.provider_login,
33+
repo: ticket.repository.name,
34+
number: ticket.number
35+
},
36+
{:ok, transaction} <-
37+
Repo.fetch_one(
38+
from tx in Transaction,
39+
join: user in assoc(tx, :user),
40+
where: tx.id == ^transaction_id,
41+
where: tx.type == :transfer,
42+
select_merge: %{user: user}
43+
) do
44+
installation = Repo.get(Installation, connected_user_id: ticket.repository.user.id)
45+
body = "🎉🎈 @#{transaction.user.provider_login} has been awarded **#{transaction.net_amount}**! 🎈🎊"
46+
47+
do_perform(ticket_ref, body, installation)
48+
end
49+
end
50+
51+
defp do_perform(ticket_ref, body, nil) do
52+
if Github.pat_enabled() do
53+
Github.create_issue_comment(
54+
Github.pat(),
55+
ticket_ref["owner"],
56+
ticket_ref["repo"],
57+
ticket_ref["number"],
58+
body
59+
)
60+
else
61+
Logger.info("""
62+
Github.create_issue_comment(Github.pat(), "#{ticket_ref["owner"]}", "#{ticket_ref["repo"]}", #{ticket_ref["number"]},
63+
\"\"\"
64+
#{body}
65+
\"\"\")
66+
""")
67+
end
68+
end
69+
70+
defp do_perform(ticket_ref, body, installation) do
71+
{:ok, token} = Github.get_installation_token(installation.id)
72+
Github.create_issue_comment(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"], body)
73+
end
74+
end

lib/algora/workspace/schemas/ticket.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule Algora.Workspace.Ticket do
1919

2020
belongs_to :repository, Algora.Workspace.Repository
2121
has_many :bounties, Algora.Bounties.Bounty
22+
has_many :tips, Algora.Bounties.Tip
2223

2324
has_many :activities, {"ticket_activities", Activity}, foreign_key: :assoc_id
2425

lib/algora_web/controllers/webhooks/stripe_controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ defmodule AlgoraWeb.Webhooks.StripeController do
8282
data: %{object: %Stripe.Transfer{metadata: %{"version" => @metadata_version}} = transfer}
8383
}) do
8484
with {:ok, transaction} <- Repo.fetch_by(Transaction, provider: "stripe", provider_id: transfer.id),
85-
{:ok, _transaction} <- maybe_update_transaction(transaction, transfer) do
86-
# TODO: notify user
85+
{:ok, _transaction} <- maybe_update_transaction(transaction, transfer),
86+
{:ok, _job} <- Oban.insert(Bounties.Jobs.NotifyTransfer.new(%{transaction_id: transaction.id})) do
8787
Payments.broadcast()
8888
{:ok, nil}
8989
else

test/algora_web/controllers/webhooks/stripe_controller_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ defmodule AlgoraWeb.Webhooks.StripeControllerTest do
130130
updated_tx = Repo.get(Transaction, transaction.id)
131131
assert updated_tx.status == :succeeded
132132
assert updated_tx.succeeded_at != nil
133+
134+
assert_enqueued(worker: Bounties.Jobs.NotifyTransfer, args: %{transaction_id: transaction.id})
133135
end
134136
end
135137

0 commit comments

Comments
 (0)