Skip to content

Commit f15d31d

Browse files
committed
refactor: update generate_line_items function to include owner parameter
- Modified the generate_line_items function to accept an owner parameter, allowing for dynamic fee percentage calculation based on the owner's fee_pct. - Updated all calls to generate_line_items to pass the owner along with the amount, ensuring consistency across the module.
1 parent 9dfa0dc commit f15d31d

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

lib/algora/bounties/bounties.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,22 +707,22 @@ defmodule Algora.Bounties do
707707
end
708708

709709
@spec generate_line_items(
710-
%{amount: Money.t()},
710+
%{owner: User.t(), amount: Money.t()},
711711
opts :: [
712712
ticket_ref: %{owner: String.t(), repo: String.t(), number: integer()},
713713
claims: [Claim.t()],
714714
recipient: User.t()
715715
]
716716
) ::
717717
[LineItem.t()]
718-
def generate_line_items(%{amount: amount}, opts \\ []) do
718+
def generate_line_items(%{owner: owner, amount: amount}, opts \\ []) do
719719
ticket_ref = opts[:ticket_ref]
720720
recipient = opts[:recipient]
721721
claims = opts[:claims] || []
722722

723723
description = if(ticket_ref, do: "#{ticket_ref[:repo]}##{ticket_ref[:number]}")
724724

725-
platform_fee_pct = FeeTier.calculate_fee_percentage(Money.zero(:USD))
725+
platform_fee_pct = Decimal.div(owner.fee_pct, 100)
726726
transaction_fee_pct = Payments.get_transaction_fee_pct()
727727

728728
payouts =
@@ -779,7 +779,7 @@ defmodule Algora.Bounties do
779779
tx_group_id = Nanoid.generate()
780780

781781
line_items =
782-
generate_line_items(%{amount: amount},
782+
generate_line_items(%{owner: owner, amount: amount},
783783
ticket_ref: opts[:ticket_ref],
784784
recipient: opts[:recipient],
785785
claims: opts[:claims]
@@ -835,7 +835,7 @@ defmodule Algora.Bounties do
835835
tx_group_id = Nanoid.generate()
836836

837837
line_items =
838-
generate_line_items(%{amount: amount},
838+
generate_line_items(%{owner: owner, amount: amount},
839839
ticket_ref: opts[:ticket_ref],
840840
recipient: opts[:recipient],
841841
claims: opts[:claims]

lib/algora_web/live/claim_live.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ defmodule AlgoraWeb.ClaimLive do
255255

256256
defp assign_line_items(socket) do
257257
line_items =
258-
Bounties.generate_line_items(%{amount: calculate_final_amount(socket.assigns.reward_bounty_form.source)},
258+
Bounties.generate_line_items(
259+
%{
260+
owner: socket.assigns.selected_context,
261+
amount: calculate_final_amount(socket.assigns.reward_bounty_form.source)
262+
},
259263
ticket_ref: ticket_ref(socket),
260264
claims: socket.assigns.claims
261265
)

test/algora/bounties_test.exs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule Algora.BountiesTest do
5353

5454
assert {:ok, bounty} = Bounties.create_bounty(bounty_params, [])
5555

56-
assert bounty.visibility == :public
56+
assert bounty.visibility == :community
5757

5858
assert {:ok, claims} =
5959
Bounties.claim_bounty(
@@ -517,4 +517,70 @@ defmodule Algora.BountiesTest do
517517
refute Enum.any?(claims, &(&1.status == :cancelled))
518518
end
519519
end
520+
521+
describe "generate_line_items/2" do
522+
test "uses owner's fee percentage for platform fee" do
523+
owner = insert!(:user, fee_pct: 5)
524+
recipient = insert!(:user, provider_login: "recipient")
525+
amount = Money.new(10_000, :USD)
526+
527+
line_items =
528+
Bounties.generate_line_items(
529+
%{owner: owner, amount: amount},
530+
recipient: recipient
531+
)
532+
533+
platform_fee = Enum.find(line_items, &(&1.type == :fee and String.contains?(&1.title, "platform fee")))
534+
assert Money.equal?(platform_fee.amount, Money.new(500, :USD))
535+
assert platform_fee.title == "Algora platform fee (5%)"
536+
537+
payout = Enum.find(line_items, &(&1.type == :payout))
538+
assert Money.equal?(payout.amount, amount)
539+
assert payout.title == "Payment to @recipient"
540+
end
541+
542+
test "calculates line items correctly with claims" do
543+
owner = insert!(:user, fee_pct: 5)
544+
solver1 = insert!(:user, provider_login: "solver1")
545+
solver2 = insert!(:user, provider_login: "solver2")
546+
amount = Money.new(10_000, :USD)
547+
548+
claims = [
549+
build(:claim, user: solver1, group_share: Decimal.new("0.60")),
550+
build(:claim, user: solver2, group_share: Decimal.new("0.40"))
551+
]
552+
553+
line_items =
554+
Bounties.generate_line_items(
555+
%{owner: owner, amount: amount},
556+
claims: claims
557+
)
558+
559+
platform_fee = Enum.find(line_items, &(&1.type == :fee and String.contains?(&1.title, "platform fee")))
560+
assert Money.equal?(platform_fee.amount, Money.new(500, :USD))
561+
562+
[payout1, payout2] = Enum.filter(line_items, &(&1.type == :payout))
563+
assert Money.equal?(payout1.amount, Money.new(6000, :USD))
564+
assert payout1.title == "Payment to @solver1"
565+
assert Money.equal?(payout2.amount, Money.new(4000, :USD))
566+
assert payout2.title == "Payment to @solver2"
567+
end
568+
569+
test "includes ticket reference in description when provided" do
570+
owner = insert!(:user, fee_pct: 5)
571+
recipient = insert!(:user, provider_login: "recipient")
572+
amount = Money.new(10_000, :USD)
573+
ticket_ref = %{owner: "owner", repo: "repo", number: 123}
574+
575+
line_items =
576+
Bounties.generate_line_items(
577+
%{owner: owner, amount: amount},
578+
recipient: recipient,
579+
ticket_ref: ticket_ref
580+
)
581+
582+
payout = Enum.find(line_items, &(&1.type == :payout))
583+
assert payout.description == "repo#123"
584+
end
585+
end
520586
end

0 commit comments

Comments
 (0)