feat: implement full CRUD for membership fee types with settings UI
- Add interval immutability and deletion prevention validations - Add settings validation for default_membership_fee_type_id - Create MembershipFeeSettingsLive for admin UI with form handling - Add comprehensive test coverage (unit, integration, settings)
This commit is contained in:
parent
82897d5cd3
commit
da1fd3da73
7 changed files with 754 additions and 2 deletions
|
|
@ -36,7 +36,7 @@ defmodule Mv.MembershipFees.MembershipFeeType do
|
|||
end
|
||||
|
||||
actions do
|
||||
defaults [:read, :destroy]
|
||||
defaults [:read]
|
||||
|
||||
create :create do
|
||||
primary? true
|
||||
|
|
@ -45,10 +45,69 @@ defmodule Mv.MembershipFees.MembershipFeeType do
|
|||
|
||||
update :update do
|
||||
primary? true
|
||||
require_atomic? false
|
||||
# Note: interval is NOT in accept list - it's immutable after creation
|
||||
# Immutability validation will be added in a future issue
|
||||
accept [:name, :amount, :description]
|
||||
end
|
||||
|
||||
destroy :destroy do
|
||||
primary? true
|
||||
require_atomic? false
|
||||
end
|
||||
end
|
||||
|
||||
validations do
|
||||
# Prevent interval changes after creation
|
||||
validate fn changeset, _context ->
|
||||
if Ash.Changeset.changing_attribute?(changeset, :interval) do
|
||||
case changeset.data do
|
||||
nil -> :ok # Creating new resource, interval can be set
|
||||
_existing -> {:error, field: :interval, message: "Interval cannot be changed after creation"}
|
||||
end
|
||||
else
|
||||
:ok
|
||||
end
|
||||
end, on: [:update]
|
||||
|
||||
# Prevent deletion if assigned to members
|
||||
validate fn changeset, _context ->
|
||||
if changeset.action_type == :destroy do
|
||||
require Ash.Query
|
||||
|
||||
member_count =
|
||||
Mv.Membership.Member
|
||||
|> Ash.Query.filter(membership_fee_type_id == ^changeset.data.id)
|
||||
|> Ash.count!()
|
||||
|
||||
if member_count > 0 do
|
||||
{:error, message: "Cannot delete membership fee type: #{member_count} member(s) are assigned to it"}
|
||||
else
|
||||
:ok
|
||||
end
|
||||
else
|
||||
:ok
|
||||
end
|
||||
end, on: [:destroy]
|
||||
|
||||
# Prevent deletion if cycles exist
|
||||
validate fn changeset, _context ->
|
||||
if changeset.action_type == :destroy do
|
||||
require Ash.Query
|
||||
|
||||
cycle_count =
|
||||
Mv.MembershipFees.MembershipFeeCycle
|
||||
|> Ash.Query.filter(membership_fee_type_id == ^changeset.data.id)
|
||||
|> Ash.count!()
|
||||
|
||||
if cycle_count > 0 do
|
||||
{:error, message: "Cannot delete membership fee type: #{cycle_count} cycle(s) reference it"}
|
||||
else
|
||||
:ok
|
||||
end
|
||||
else
|
||||
:ok
|
||||
end
|
||||
end, on: [:destroy]
|
||||
end
|
||||
|
||||
attributes do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue