Skip to content

Commit 9efbdf7

Browse files
committed
stash: fee tiers for expert contract payment calculations
- Introduced separate fee tiers for community and expert contracts, enhancing fee calculation logic. - Updated the `calculate_fee_data` function to preload client data and handle fee calculations based on the contract type. - Adjusted the fee tier retrieval methods to support both community and expert tiers. - Modified the UI to conditionally display fee tier information based on the available data.
1 parent a2f2383 commit 9efbdf7

File tree

4 files changed

+186
-167
lines changed

4 files changed

+186
-167
lines changed

lib/algora/contracts/contracts.ex

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ defmodule Algora.Contracts do
6060
end
6161

6262
def calculate_fee_data(contract) do
63-
total_paid = Payments.get_total_paid(contract.client_id, contract.contractor_id)
64-
fee_tiers = FeeTier.all()
65-
current_fee = FeeTier.calculate_fee_percentage(total_paid)
63+
contract = Repo.preload(contract, :client)
64+
65+
# TODO: implement sliding scale for expert contracts
66+
# fee_tiers = FeeTier.all(:expert)
67+
# total_paid = Payments.get_total_paid(contract.client_id, contract.contractor_id)
68+
# progress: FeeTier.calculate_progress(total_paid)
69+
# current_fee = FeeTier.calculate_fee_percentage(total_paid)
70+
71+
fee_tiers = FeeTier.all(:community)
72+
total_paid = Money.zero(:USD)
73+
progress = Decimal.new(0)
74+
current_fee = Decimal.div(contract.client.fee_pct, 100)
6675

6776
%{
6877
total_paid: total_paid,
6978
fee_tiers: fee_tiers,
7079
current_fee: current_fee,
7180
transaction_fee: Payments.get_transaction_fee_pct(),
7281
total_fee: Decimal.add(current_fee, Payments.get_transaction_fee_pct()),
73-
progress: FeeTier.calculate_progress(total_paid)
82+
progress: progress
7483
}
7584
end
7685

lib/algora/shared/fee_tier.ex

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ defmodule Algora.FeeTier do
33
Defines the fee tiers and helper functions for calculating fees based on payment volume.
44
"""
55

6-
@tiers [
6+
@community_tiers [
7+
%{
8+
threshold: Money.new!(0, :USD, no_fraction_if_integer: true),
9+
fee: Decimal.new("0.09"),
10+
progress: Decimal.new("0.00")
11+
}
12+
]
13+
14+
@expert_tiers [
715
%{
816
threshold: Money.new!(0, :USD, no_fraction_if_integer: true),
917
fee: Decimal.new("0.19"),
@@ -26,22 +34,23 @@ defmodule Algora.FeeTier do
2634
}
2735
]
2836

29-
def all, do: @tiers
37+
def all(:community), do: @community_tiers
38+
def all(:expert), do: @expert_tiers
3039

3140
def calculate_fee_percentage(total_paid) do
3241
# Find the highest tier where total_paid is greater than or equal to the threshold
3342
tier =
34-
@tiers
43+
@expert_tiers
3544
|> Enum.reverse()
36-
|> Enum.find(List.first(@tiers), &threshold_met?(total_paid, &1))
45+
|> Enum.find(List.first(@expert_tiers), &threshold_met?(total_paid, &1))
3746

3847
tier.fee
3948
end
4049

4150
def calculate_progress(total_paid) do
4251
tier = find_current_tier(total_paid)
4352
prev_tier = find_previous_tier(tier)
44-
next_threshold = if tier, do: tier.threshold, else: List.last(@tiers).threshold
53+
next_threshold = if tier, do: tier.threshold, else: List.last(@expert_tiers).threshold
4554

4655
case {prev_tier, tier} do
4756
{nil, _tier} ->
@@ -67,14 +76,14 @@ defmodule Algora.FeeTier do
6776
end
6877

6978
defp find_current_tier(total_paid) do
70-
Enum.find(@tiers, &(Money.compare!(total_paid, &1.threshold) == :lt))
79+
Enum.find(@expert_tiers, &(Money.compare!(total_paid, &1.threshold) == :lt))
7180
end
7281

73-
defp find_previous_tier(nil), do: List.last(@tiers)
82+
defp find_previous_tier(nil), do: List.last(@expert_tiers)
7483

7584
defp find_previous_tier(current_tier) do
76-
index = Enum.find_index(@tiers, &(&1 == current_tier))
77-
if index > 0, do: Enum.at(@tiers, index - 1)
85+
index = Enum.find_index(@expert_tiers, &(&1 == current_tier))
86+
if index > 0, do: Enum.at(@expert_tiers, index - 1)
7887
end
7988

8089
defp percentage_of(amount, total) do
@@ -92,6 +101,6 @@ defmodule Algora.FeeTier do
92101
end
93102

94103
def first_threshold_met?(total_paid) do
95-
threshold_met?(total_paid, Enum.at(@tiers, 1))
104+
threshold_met?(total_paid, Enum.at(@expert_tiers, 1))
96105
end
97106
end

lib/algora_web/live/contract/modals/release_renew_drawer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ defmodule AlgoraWeb.Contract.Modals.ReleaseRenewDrawer do
8484
</.card_content>
8585
</.card>
8686
</div>
87-
<div class="flex flex-col gap-8">
87+
<div :if={length(@fee_data.fee_tiers) > 1} class="flex flex-col gap-8">
8888
<div>
8989
<h3 class="mb-6 text-lg font-semibold">Algora Fee Tier</h3>
9090
<div class="space-y-2">

0 commit comments

Comments
 (0)