Skip to content

Commit 6658ea6

Browse files
committed
refactor: add wrapper money type to set defaults
1 parent 5bc4ef3 commit 6658ea6

File tree

9 files changed

+50
-26
lines changed

9 files changed

+50
-26
lines changed

lib/algora/accounts/schemas/user.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ defmodule Algora.Accounts.User do
88
alias Algora.Contracts.Contract
99
alias Algora.MoneyUtils
1010
alias Algora.Organizations.Member
11+
alias Algora.Types.Money
1112
alias Algora.Workspace.Installation
12-
alias Money.Ecto.Composite.Type, as: MoneyType
1313

1414
@type t() :: %__MODULE__{}
1515

@@ -42,11 +42,11 @@ defmodule Algora.Accounts.User do
4242
field :max_open_attempts, :integer, default: 3
4343
field :manual_assignment, :boolean, default: false
4444

45-
field :hourly_rate_min, MoneyType, no_fraction_if_integer: true
46-
field :hourly_rate_max, MoneyType, no_fraction_if_integer: true
45+
field :hourly_rate_min, Money
46+
field :hourly_rate_max, Money
4747
field :hours_per_week, :integer
4848

49-
field :total_earned, MoneyType, no_fraction_if_integer: true, virtual: true
49+
field :total_earned, Money, virtual: true
5050
field :completed_bounties_count, :integer, virtual: true
5151
field :contributed_projects_count, :integer, virtual: true
5252

lib/algora/bounties/schemas/bounty.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Algora.Bounties.Bounty do
99
@type t() :: %__MODULE__{}
1010

1111
schema "bounties" do
12-
field :amount, Money.Ecto.Composite.Type, no_fraction_if_integer: true
12+
field :amount, Algora.Types.Money
1313
field :status, Ecto.Enum, values: [:open, :cancelled, :paid]
1414

1515
belongs_to :ticket, Algora.Workspace.Ticket

lib/algora/bounties/schemas/tip.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Algora.Bounties.Tip do
77
@type t() :: %__MODULE__{}
88

99
schema "tips" do
10-
field :amount, Money.Ecto.Composite.Type, no_fraction_if_integer: true
10+
field :amount, Algora.Types.Money
1111
field :status, Ecto.Enum, values: [:open, :cancelled, :paid]
1212

1313
belongs_to :ticket, Algora.Workspace.Ticket

lib/algora/bounties/ticket_view.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule Algora.Bounties.TicketView do
1515
field :url, :string
1616

1717
# Computed/aggregated fields
18-
field :total_bounty_amount, Money.Ecto.Composite.Type
18+
field :total_bounty_amount, Algora.Types.Money
1919
field :bounty_count, :integer
2020

2121
# Original associations

lib/algora/contracts/schemas/contract.ex

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@ defmodule Algora.Contracts.Contract do
55
alias Algora.Accounts.User
66
alias Algora.Contracts.Contract
77
alias Algora.MoneyUtils
8-
alias Money.Ecto.Composite.Type, as: MoneyType
98

109
@type t() :: %__MODULE__{}
1110

1211
schema "contracts" do
1312
field :status, Ecto.Enum, values: [:draft, :active, :paid, :cancelled, :disputed]
1413
field :sequence_number, :integer, default: 1
15-
field :hourly_rate, MoneyType, no_fraction_if_integer: true
16-
field :hourly_rate_min, MoneyType, no_fraction_if_integer: true
17-
field :hourly_rate_max, MoneyType, no_fraction_if_integer: true
14+
field :hourly_rate, Algora.Types.Money
15+
field :hourly_rate_min, Algora.Types.Money
16+
field :hourly_rate_max, Algora.Types.Money
1817
field :hours_per_week, :integer
1918
field :start_date, :utc_datetime_usec
2019
field :end_date, :utc_datetime_usec
2120

22-
field :amount_credited, MoneyType, virtual: true, no_fraction_if_integer: true
23-
field :amount_debited, MoneyType, virtual: true, no_fraction_if_integer: true
21+
field :amount_credited, Algora.Types.Money, virtual: true
22+
field :amount_debited, Algora.Types.Money, virtual: true
2423

25-
field :total_charged, MoneyType, virtual: true, no_fraction_if_integer: true
26-
field :total_credited, MoneyType, virtual: true, no_fraction_if_integer: true
27-
field :total_debited, MoneyType, virtual: true, no_fraction_if_integer: true
28-
field :total_deposited, MoneyType, virtual: true, no_fraction_if_integer: true
29-
field :total_transferred, MoneyType, virtual: true, no_fraction_if_integer: true
30-
field :total_withdrawn, MoneyType, virtual: true, no_fraction_if_integer: true
24+
field :total_charged, Algora.Types.Money, virtual: true
25+
field :total_credited, Algora.Types.Money, virtual: true
26+
field :total_debited, Algora.Types.Money, virtual: true
27+
field :total_deposited, Algora.Types.Money, virtual: true
28+
field :total_transferred, Algora.Types.Money, virtual: true
29+
field :total_withdrawn, Algora.Types.Money, virtual: true
3130

3231
belongs_to :original_contract, Contract
3332
has_many :renewals, Contract, foreign_key: :original_contract_id

lib/algora/payments/schemas/platform_transaction.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Algora.Payments.PlatformTransaction do
1111
field :provider_meta, :map
1212

1313
field :succeeded_at, :utc_datetime_usec
14-
field :amount, Money.Ecto.Composite.Type
14+
field :amount, Algora.Types.Money
1515
field :type, :string
1616
field :reporting_category, :string
1717

lib/algora/payments/schemas/transaction.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Algora.Payments.Transaction do
33
use Algora.Schema
44

55
alias Algora.Contracts.Contract
6-
alias Money.Ecto.Composite.Type, as: MoneyType
6+
alias Algora.Types.Money
77

88
@type t() :: %__MODULE__{}
99

@@ -21,10 +21,10 @@ defmodule Algora.Payments.Transaction do
2121
field :provider_balance_transaction_id, :string
2222
field :provider_meta, :map
2323

24-
field :gross_amount, MoneyType, no_fraction_if_integer: true
25-
field :net_amount, MoneyType, no_fraction_if_integer: true
26-
field :total_fee, MoneyType, no_fraction_if_integer: true
27-
field :provider_fee, MoneyType, no_fraction_if_integer: true
24+
field :gross_amount, Money
25+
field :net_amount, Money
26+
field :total_fee, Money
27+
field :provider_fee, Money
2828
field :line_items, {:array, :map}
2929

3030
field :type, Ecto.Enum, values: @transaction_types

lib/algora/schema.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Algora.Schema do
22
@moduledoc false
33
defmacro __using__(_) do
44
quote do
5-
use Ecto.Schema
5+
use TypedEctoSchema
66

77
import Ecto.Changeset
88
import Ecto.Query

lib/algora/shared/types/money.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule Algora.Types.Money do
2+
@moduledoc false
3+
4+
use Ecto.ParameterizedType
5+
6+
alias Money.Ecto.Composite.Type
7+
8+
@type t :: Money.t()
9+
10+
defdelegate type(params), to: Type
11+
defdelegate cast_type(opts), to: Type
12+
defdelegate load(tuple, loader, params), to: Type
13+
defdelegate dump(money, dumper, params), to: Type
14+
defdelegate cast(money), to: Type
15+
defdelegate cast(money, params), to: Type
16+
defdelegate embed_as(term), to: Type
17+
defdelegate embed_as(term, params), to: Type
18+
defdelegate equal?(money1, money2), to: Type
19+
20+
def init(opts) do
21+
opts
22+
|> Type.init()
23+
|> Keyword.put(:no_fraction_if_integer, true)
24+
end
25+
end

0 commit comments

Comments
 (0)