@@ -548,29 +548,49 @@ defmodule Algora.Bounties do
548548
549549 @ spec create_tip (
550550 % { creator: User . t ( ) , owner: User . t ( ) , recipient: User . t ( ) , amount: Money . t ( ) } ,
551- opts :: [ ticket_ref: % { owner: String . t ( ) , repo: String . t ( ) , number: integer ( ) } ]
551+ opts :: [ ticket_ref: % { owner: String . t ( ) , repo: String . t ( ) , number: integer ( ) } , installation_id: integer ( ) ]
552552 ) ::
553553 { :ok , String . t ( ) } | { :error , atom ( ) }
554554 def create_tip ( % { creator: creator , owner: owner , recipient: recipient , amount: amount } , opts \\ [ ] ) do
555- changeset =
556- Tip . changeset ( % Tip { } , % {
557- amount: amount ,
558- owner_id: owner . id ,
559- creator_id: creator . id ,
560- recipient_id: recipient . id
561- } )
555+ token_res =
556+ if opts [ :installation_id ] do
557+ Github . get_installation_token ( opts [ :installation_id ] )
558+ else
559+ case Accounts . get_access_token ( creator ) do
560+ { :ok , token } -> { :ok , token }
561+ { :error , _reason } -> { :ok , nil }
562+ end
563+ end
562564
563- activity_attrs =
564- % {
565- type: :tip_awarded ,
566- notify_users: [ recipient . id ]
567- }
565+ ticket_ref = opts [ :ticket_ref ]
566+
567+ ticket_res =
568+ if ticket_ref do
569+ with { :ok , token } <- token_res do
570+ Workspace . ensure_ticket ( token , ticket_ref [ :owner ] , ticket_ref [ :repo ] , ticket_ref [ :number ] )
571+ end
572+ else
573+ { :ok , nil }
574+ end
568575
569576 Repo . transact ( fn ->
570- with { :ok , tip } <- Repo . insert_with_activity ( changeset , activity_attrs ) do
577+ with { :ok , ticket } <- ticket_res ,
578+ { :ok , tip } <-
579+ % Tip { }
580+ |> Tip . changeset ( % {
581+ amount: amount ,
582+ owner_id: owner . id ,
583+ creator_id: creator . id ,
584+ recipient_id: recipient . id ,
585+ ticket_id: if ( ticket , do: ticket . id )
586+ } )
587+ |> Repo . insert_with_activity ( % {
588+ type: :tip_awarded ,
589+ notify_users: [ recipient . id ]
590+ } ) do
571591 create_payment_session (
572592 % { owner: owner , amount: amount , description: "Tip payment for OSS contributions" } ,
573- ticket_ref: opts [ : ticket_ref] ,
593+ ticket_ref: ticket_ref ,
574594 tip_id: tip . id ,
575595 recipient: recipient
576596 )
@@ -622,29 +642,31 @@ defmodule Algora.Bounties do
622642 platform_fee_pct = FeeTier . calculate_fee_percentage ( Money . zero ( :USD ) )
623643 transaction_fee_pct = Payments . get_transaction_fee_pct ( )
624644
625- if recipient do
626- [
627- % LineItem {
628- amount: amount ,
629- title: "Payment to @#{ recipient . provider_login } " ,
630- description: description ,
631- image: recipient . avatar_url ,
632- type: :payout
633- }
634- ]
635- else
636- [ ]
637- end ++
638- Enum . map ( claims , fn claim ->
639- % LineItem {
640- # TODO: ensure shares are normalized
641- amount: Money . mult! ( amount , claim . group_share ) ,
642- title: "Payment to @#{ claim . user . provider_login } " ,
643- description: description ,
644- image: claim . user . avatar_url ,
645- type: :payout
646- }
647- end ) ++
645+ payouts =
646+ if recipient do
647+ [
648+ % LineItem {
649+ amount: amount ,
650+ title: "Payment to @#{ recipient . provider_login } " ,
651+ description: description ,
652+ image: recipient . avatar_url ,
653+ type: :payout
654+ }
655+ ]
656+ else
657+ Enum . map ( claims , fn claim ->
658+ % LineItem {
659+ # TODO: ensure shares are normalized
660+ amount: Money . mult! ( amount , claim . group_share ) ,
661+ title: "Payment to @#{ claim . user . provider_login } " ,
662+ description: description ,
663+ image: claim . user . avatar_url ,
664+ type: :payout
665+ }
666+ end )
667+ end
668+
669+ payouts ++
648670 [
649671 % LineItem {
650672 amount: Money . mult! ( amount , platform_fee_pct ) ,
@@ -698,6 +720,7 @@ defmodule Algora.Bounties do
698720 create_transaction_pairs ( % {
699721 claims: opts [ :claims ] || [ ] ,
700722 tip_id: opts [ :tip_id ] ,
723+ recipient_id: if ( opts [ :recipient ] , do: opts [ :recipient ] . id ) ,
701724 bounty_id: opts [ :bounty_id ] ,
702725 claim_id: nil ,
703726 amount: amount ,
@@ -755,6 +778,7 @@ defmodule Algora.Bounties do
755778 create_transaction_pairs ( % {
756779 claims: opts [ :claims ] || [ ] ,
757780 tip_id: opts [ :tip_id ] ,
781+ recipient_id: if ( opts [ :recipient ] , do: opts [ :recipient ] . id ) ,
758782 bounty_id: opts [ :bounty_id ] ,
759783 claim_id: nil ,
760784 amount: amount ,
0 commit comments