|
| 1 | +defmodule Algora.Bounties.Jobs.PromptPayoutConnect do |
| 2 | + @moduledoc false |
| 3 | + use Oban.Worker, queue: :prompt_payout_connect |
| 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.Installation |
| 12 | + alias Algora.Workspace.Ticket |
| 13 | + |
| 14 | + require Logger |
| 15 | + |
| 16 | + # TODO: confirm url |
| 17 | + @onboarding_url "https://console.algora.io/solve" |
| 18 | + |
| 19 | + @impl Oban.Worker |
| 20 | + def perform(%Oban.Job{args: %{"credit_id" => credit_id}}) do |
| 21 | + with {:ok, ticket} <- |
| 22 | + Repo.fetch_one( |
| 23 | + from t in Ticket, |
| 24 | + left_join: bounty in assoc(t, :bounties), |
| 25 | + left_join: tip in assoc(t, :tips), |
| 26 | + left_join: tx in Transaction, |
| 27 | + on: tx.bounty_id == bounty.id or tx.tip_id == tip.id, |
| 28 | + join: repo in assoc(t, :repository), |
| 29 | + join: user in assoc(repo, :user), |
| 30 | + where: tx.id == ^credit_id, |
| 31 | + select_merge: %{ |
| 32 | + repository: %{repo | user: user} |
| 33 | + } |
| 34 | + ), |
| 35 | + ticket_ref = %{ |
| 36 | + owner: ticket.repository.user.provider_login, |
| 37 | + repo: ticket.repository.name, |
| 38 | + number: ticket.number |
| 39 | + }, |
| 40 | + {:ok, transaction} <- |
| 41 | + Repo.fetch_one( |
| 42 | + from tx in Transaction, |
| 43 | + join: user in assoc(tx, :user), |
| 44 | + left_join: linked_tx in Transaction, |
| 45 | + on: linked_tx.id == tx.linked_transaction_id, |
| 46 | + left_join: sender in assoc(linked_tx, :user), |
| 47 | + where: tx.id == ^credit_id, |
| 48 | + select_merge: %{ |
| 49 | + user: user, |
| 50 | + linked_transaction: %{linked_tx | user: sender} |
| 51 | + } |
| 52 | + ) do |
| 53 | + installation = Repo.get_by(Installation, provider_user_id: ticket.repository.user.id) |
| 54 | + |
| 55 | + reward_type = |
| 56 | + cond do |
| 57 | + transaction.tip_id -> "tip" |
| 58 | + transaction.bounty_id -> "bounty" |
| 59 | + transaction.contract_id -> "contract" |
| 60 | + true -> raise "Unknown transaction type" |
| 61 | + end |
| 62 | + |
| 63 | + body = |
| 64 | + "@#{transaction.user.provider_login}: You've been awarded a **#{transaction.net_amount}** #{reward_type} #{if transaction.linked_transaction, do: "by **#{transaction.linked_transaction.user.name}**", else: ""}! 👉 [Complete your Algora onboarding](#{@onboarding_url}) to collect the #{reward_type}." |
| 65 | + |
| 66 | + do_perform(ticket_ref, body, installation) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp do_perform(ticket_ref, body, nil) do |
| 71 | + Github.try_without_installation(&Github.create_issue_comment/5, [ |
| 72 | + ticket_ref.owner, |
| 73 | + ticket_ref.repo, |
| 74 | + ticket_ref.number, |
| 75 | + body |
| 76 | + ]) |
| 77 | + end |
| 78 | + |
| 79 | + defp do_perform(ticket_ref, body, installation) do |
| 80 | + with {:ok, token} <- Github.get_installation_token(installation.provider_id) do |
| 81 | + Github.create_issue_comment(token, ticket_ref.owner, ticket_ref.repo, ticket_ref.number, body) |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments