test(membership-fees): share create_fee_type and create_cycle fixtures
Replace the create_fee_type/create_cycle helpers duplicated across 18/8 membership-fee test files with a single shared definition in Mv.Fixtures, reconciling the divergent local signatures (including the reversed argument order) into one superset so behavior is unchanged.
This commit is contained in:
parent
9aa5bdb6a7
commit
18fb954f73
19 changed files with 279 additions and 513 deletions
|
|
@ -335,4 +335,82 @@ defmodule Mv.Fixtures do
|
|||
{:ok, request} = Membership.confirm_join_request(token, actor: nil)
|
||||
request
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a membership fee type with default or custom attributes.
|
||||
|
||||
Defaults: a unique `name`, `amount` 50.00, `interval` :yearly.
|
||||
|
||||
## Parameters
|
||||
- `attrs` - Map of attributes to override defaults (e.g. `%{interval: :monthly}`).
|
||||
- `actor` - the authorization actor; defaults to the system actor when omitted/nil.
|
||||
|
||||
## Returns
|
||||
- MembershipFeeType struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_fee_type(%{interval: :monthly})
|
||||
%Mv.MembershipFees.MembershipFeeType{interval: :monthly, ...}
|
||||
|
||||
iex> create_fee_type(%{amount: Decimal.new("10.00")}, admin)
|
||||
%Mv.MembershipFees.MembershipFeeType{...}
|
||||
"""
|
||||
def create_fee_type(attrs \\ %{}, actor \\ nil) do
|
||||
actor = actor || SystemActor.get_system_actor()
|
||||
|
||||
default_attrs = %{
|
||||
name: "Test Fee Type #{System.unique_integer([:positive])}",
|
||||
amount: Decimal.new("50.00"),
|
||||
interval: :yearly
|
||||
}
|
||||
|
||||
Mv.MembershipFees.MembershipFeeType
|
||||
|> Ash.Changeset.for_create(:create, Map.merge(default_attrs, attrs))
|
||||
|> Ash.create!(actor: actor)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a membership fee cycle for the given member and fee type.
|
||||
|
||||
Defaults: `cycle_start` ~D[2024-01-01], `amount` 50.00, `status` :unpaid,
|
||||
with `member_id`/`membership_fee_type_id` derived from the passed structs.
|
||||
|
||||
## Parameters
|
||||
- `member` - the Member struct the cycle belongs to.
|
||||
- `fee_type` - the MembershipFeeType struct the cycle references.
|
||||
- `attrs` - Map overriding the cycle defaults (e.g. `%{cycle_start: ~D[2023-01-01], status: :paid}`).
|
||||
A reserved `:replace_existing` key (truthy) deletes any pre-existing cycles for the member
|
||||
before creating the new one (used where auto-generated cycles would otherwise conflict);
|
||||
it is stripped from the attrs and never passed to the create action. Defaults to absent/false.
|
||||
- `actor` - the authorization actor; defaults to the system actor when omitted/nil.
|
||||
|
||||
## Returns
|
||||
- MembershipFeeCycle struct.
|
||||
"""
|
||||
def create_cycle(member, fee_type, attrs \\ %{}, actor \\ nil) do
|
||||
actor = actor || SystemActor.get_system_actor()
|
||||
{replace_existing, attrs} = Map.pop(attrs, :replace_existing, false)
|
||||
|
||||
if replace_existing do
|
||||
require Ash.Query
|
||||
|
||||
Mv.MembershipFees.MembershipFeeCycle
|
||||
|> Ash.Query.filter(member_id == ^member.id)
|
||||
|> Ash.read!(actor: actor)
|
||||
|> Enum.each(&Ash.destroy!(&1, actor: actor))
|
||||
end
|
||||
|
||||
default_attrs = %{
|
||||
cycle_start: ~D[2024-01-01],
|
||||
amount: Decimal.new("50.00"),
|
||||
member_id: member.id,
|
||||
membership_fee_type_id: fee_type.id,
|
||||
status: :unpaid
|
||||
}
|
||||
|
||||
Mv.MembershipFees.MembershipFeeCycle
|
||||
|> Ash.Changeset.for_create(:create, Map.merge(default_attrs, attrs))
|
||||
|> Ash.create!(actor: actor)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue