Skip to content

Commit 0b7a0c2

Browse files
authored
feat: improve contract work flow (#129)
1 parent cc6aa5a commit 0b7a0c2

File tree

7 files changed

+422
-51
lines changed

7 files changed

+422
-51
lines changed

lib/algora/bounties/bounties.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,12 @@ defmodule Algora.Bounties do
429429

430430
@spec notify_bounty(%{owner: User.t(), bounty: Bounty.t()}, opts :: []) ::
431431
{:ok, nil} | {:error, atom()}
432-
def notify_bounty(%{owner: _owner, bounty: bounty}, _opts) do
433-
Algora.Admin.alert("Notify bounty: #{inspect(bounty)}", :error)
432+
def notify_bounty(%{owner: owner, bounty: bounty}, _opts) do
433+
Algora.Admin.alert(
434+
"New contract offer: #{AlgoraWeb.Endpoint.url()}/#{owner.handle}/contracts/#{bounty.id}",
435+
:critical
436+
)
437+
434438
{:ok, nil}
435439
end
436440

@@ -906,11 +910,9 @@ defmodule Algora.Bounties do
906910
end
907911
end
908912

909-
def calculate_contract_amount(amount), do: Money.mult!(amount, Decimal.new("1.13"))
910-
911913
def final_contract_amount(:marketplace, amount), do: amount
912914

913-
def final_contract_amount(:bring_your_own, amount), do: calculate_contract_amount(amount)
915+
def final_contract_amount(:bring_your_own, amount), do: Money.mult!(amount, Decimal.new("1.13"))
914916

915917
@spec create_payment_session(
916918
%{owner: User.t(), amount: Money.t(), description: String.t()},

lib/algora/payments/payments.ex

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Algora.Payments do
99
alias Algora.Bounties
1010
alias Algora.Bounties.Bounty
1111
alias Algora.Bounties.Claim
12+
alias Algora.Bounties.Jobs.PromptPayoutConnect
1213
alias Algora.Bounties.Tip
1314
alias Algora.Jobs.JobPosting
1415
alias Algora.MoneyUtils
@@ -737,7 +738,7 @@ defmodule Algora.Payments do
737738

738739
{:error, :no_active_account} ->
739740
case %{credit_id: credit.id}
740-
|> Bounties.Jobs.PromptPayoutConnect.new()
741+
|> PromptPayoutConnect.new()
741742
|> Oban.insert() do
742743
{:ok, _job} -> {:cont, :ok}
743744
error -> {:halt, error}
@@ -761,4 +762,81 @@ defmodule Algora.Payments do
761762
end
762763
end)
763764
end
765+
766+
def process_release(
767+
%Stripe.Charge{id: charge_id, captured: true, payment_intent: payment_intent_id},
768+
group_id,
769+
amount,
770+
recipient
771+
) do
772+
Repo.transact(fn ->
773+
tx = Repo.get_by(Transaction, group_id: group_id, type: :charge, status: :succeeded)
774+
775+
user = Repo.get_by(User, id: tx.user_id)
776+
bounty = Repo.get_by(Bounty, id: tx.bounty_id)
777+
778+
Algora.Admin.alert(
779+
"Release #{amount} escrow to #{recipient.handle} for #{AlgoraWeb.Endpoint.url()}/#{user.handle}/contracts/#{bounty.id}",
780+
:critical
781+
)
782+
783+
debit_id = Nanoid.generate()
784+
credit_id = Nanoid.generate()
785+
786+
with {:ok, debit0} <- Repo.fetch_by(Transaction, group_id: group_id, type: :debit, status: :requires_release),
787+
{:ok, _} <-
788+
debit0
789+
|> change(%{
790+
net_amount: Money.sub!(debit0.net_amount, amount),
791+
gross_amount: Money.sub!(debit0.gross_amount, amount)
792+
})
793+
|> Repo.update(),
794+
{:ok, credit0} <- Repo.fetch_by(Transaction, group_id: group_id, type: :credit, status: :requires_release),
795+
{:ok, _} <-
796+
credit0
797+
|> change(%{
798+
net_amount: Money.add!(credit0.net_amount, amount),
799+
gross_amount: Money.add!(credit0.gross_amount, amount)
800+
})
801+
|> Repo.update(),
802+
{:ok, _debit} <-
803+
Repo.insert(%Transaction{
804+
id: debit_id,
805+
provider: "stripe",
806+
provider_id: charge_id,
807+
provider_charge_id: charge_id,
808+
provider_payment_intent_id: payment_intent_id,
809+
type: :debit,
810+
status: :succeeded,
811+
succeeded_at: DateTime.utc_now(),
812+
bounty_id: tx.bounty_id,
813+
user_id: tx.user_id,
814+
gross_amount: amount,
815+
net_amount: amount,
816+
total_fee: Money.zero(:USD),
817+
linked_transaction_id: credit_id,
818+
group_id: group_id
819+
}),
820+
{:ok, _credit} <-
821+
Repo.insert(%Transaction{
822+
id: credit_id,
823+
provider: "stripe",
824+
provider_id: charge_id,
825+
provider_charge_id: charge_id,
826+
provider_payment_intent_id: payment_intent_id,
827+
type: :credit,
828+
status: :initialized,
829+
succeeded_at: DateTime.utc_now(),
830+
bounty_id: tx.bounty_id,
831+
user_id: recipient.id,
832+
gross_amount: amount,
833+
net_amount: amount,
834+
total_fee: Money.zero(:USD),
835+
linked_transaction_id: debit_id,
836+
group_id: group_id
837+
}) do
838+
{:ok, nil}
839+
end
840+
end)
841+
end
764842
end

lib/algora/settings/settings.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,16 @@ defmodule Algora.Settings do
127127
projects = Accounts.list_contributed_projects(user, limit: 2)
128128
avatar_url = profile["avatar_url"] || user.avatar_url
129129
hourly_rate = match["hourly_rate"] || profile["hourly_rate"]
130+
hours_per_week = match["hours_per_week"] || profile["hours_per_week"] || user.hours_per_week
130131

131132
[
132133
%{
133134
user: %{user | avatar_url: avatar_url},
134135
projects: projects,
135136
badge_variant: match["badge_variant"],
136137
badge_text: match["badge_text"],
137-
hourly_rate: if(hourly_rate, do: Money.new(:USD, hourly_rate, no_fraction_if_integer: true))
138+
hourly_rate: if(hourly_rate, do: Money.new(:USD, hourly_rate, no_fraction_if_integer: true)),
139+
hours_per_week: hours_per_week
138140
}
139141
]
140142
else

lib/algora_web/forms/contract_form.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ defmodule AlgoraWeb.Forms.ContractForm do
225225
</div>
226226
</dt>
227227
<dd class="font-display font-semibold tabular-nums text-lg">
228-
{Money.to_string!(
229-
Bounties.calculate_contract_amount(get_change(@form.source, :amount))
230-
)}
228+
{Money.to_string!(get_change(@form.source, :amount))}
231229
</dd>
232230
</div>
233231
</dl>

0 commit comments

Comments
 (0)