@@ -5,11 +5,11 @@ defmodule Algora.Payments do
55
66 alias Algora.Accounts
77 alias Algora.Accounts.User
8+ alias Algora.Admin
89 alias Algora.Bounties
910 alias Algora.Bounties.Bounty
1011 alias Algora.Bounties.Claim
1112 alias Algora.Bounties.Tip
12- alias Algora.Contracts.Contract
1313 alias Algora.MoneyUtils
1414 alias Algora.Payments.Account
1515 alias Algora.Payments.Customer
@@ -617,7 +617,7 @@ defmodule Algora.Payments do
617617 { _ , txs } =
618618 Repo . update_all ( from ( t in Transaction , where: t . group_id == ^ group_id , select: t ) ,
619619 set: [
620- status: :succeeded ,
620+ status: :processing ,
621621 succeeded_at: DateTime . utc_now ( ) ,
622622 provider: "stripe" ,
623623 provider_id: charge_id ,
@@ -627,18 +627,61 @@ defmodule Algora.Payments do
627627 )
628628
629629 bounty_ids = txs |> Enum . map ( & & 1 . bounty_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
630+
631+ bounties =
632+ from ( b in Bounty ,
633+ where: b . id in ^ bounty_ids ,
634+ join: u in assoc ( b , :owner ) ,
635+ join: t in assoc ( b , :ticket ) ,
636+ select: % { b | ticket: t , owner: u }
637+ )
638+ |> Repo . all ( )
639+ |> Map . new ( & { & 1 . id , & 1 } )
640+
641+ { issue_bounty_ids , contract_bounty_ids } =
642+ Enum . split_with ( bounty_ids , fn id ->
643+ bounty = bounties [ id ]
644+ bounty && bounty . ticket . repository_id
645+ end )
646+
630647 tip_ids = txs |> Enum . map ( & & 1 . tip_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
631- contract_ids = txs |> Enum . map ( & & 1 . contract_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
632648 claim_ids = txs |> Enum . map ( & & 1 . claim_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
633649
634- Repo . update_all ( from ( b in Bounty , where: b . id in ^ bounty_ids ) , set: [ status: :paid ] )
650+ Repo . update_all ( from ( b in Bounty , where: b . id in ^ issue_bounty_ids ) , set: [ status: :paid ] )
635651 Repo . update_all ( from ( t in Tip , where: t . id in ^ tip_ids ) , set: [ status: :paid ] )
636- Repo . update_all ( from ( c in Contract , where: c . id in ^ contract_ids ) , set: [ status: :paid ] )
637652 # TODO: add and use a new "paid" status for claims
638653 Repo . update_all ( from ( c in Claim , where: c . id in ^ claim_ids ) , set: [ status: :approved ] )
639654
655+ auto_txs =
656+ Enum . filter ( txs , fn tx ->
657+ bounty = bounties [ tx . bounty_id ]
658+
659+ contract? = tx . bounty_id in contract_bounty_ids
660+
661+ if contract? do
662+ Admin . alert (
663+ "Contract payment received. URL: #{ AlgoraWeb.Endpoint . url ( ) } /#{ bounty . owner . handle } /contracts/#{ bounty . id } " ,
664+ :info
665+ )
666+ end
667+
668+ tx . type != :credit or not contract?
669+ end )
670+
671+ Repo . update_all (
672+ from ( t in Transaction , where: t . group_id == ^ group_id and t . id in ^ Enum . map ( auto_txs , & & 1 . id ) , select: t ) ,
673+ set: [
674+ status: :succeeded ,
675+ succeeded_at: DateTime . utc_now ( ) ,
676+ provider: "stripe" ,
677+ provider_id: charge_id ,
678+ provider_charge_id: charge_id ,
679+ provider_payment_intent_id: payment_intent_id
680+ ]
681+ )
682+
640683 activities_result =
641- txs
684+ auto_txs
642685 |> Enum . filter ( & ( & 1 . type == :credit ) )
643686 |> Enum . reduce_while ( :ok , fn tx , :ok ->
644687 case Repo . insert_activity ( tx , % { type: :transaction_succeeded , notify_users: [ tx . user_id ] } ) do
@@ -648,7 +691,7 @@ defmodule Algora.Payments do
648691 end )
649692
650693 jobs_result =
651- txs
694+ auto_txs
652695 |> Enum . filter ( & ( & 1 . type == :credit ) )
653696 |> Enum . reduce_while ( :ok , fn credit , :ok ->
654697 case fetch_active_account ( credit . user_id ) do
0 commit comments