fix: implement fail-closed behavior in ValidateSameInterval

Change validation to fail closed instead of fail open when types cannot
be loaded. This prevents inconsistent data states and provides clearer
error messages to users.
This commit is contained in:
Moritz 2025-12-15 12:21:22 +01:00
parent 1e5f84fd88
commit 032db2a4ba
Signed by: moritz
GPG key ID: 1020A035E5DD0824

View file

@ -62,10 +62,10 @@ defmodule Mv.MembershipFees.Changes.ValidateSameInterval do
add_interval_mismatch_error(changeset, current_interval, new_interval) add_interval_mismatch_error(changeset, current_interval, new_interval)
end end
{:error, _reason} -> {:error, reason} ->
# If we can't load the types, allow the change (fail open) # Fail closed: If we can't load the types, reject the change
# The database constraint will catch invalid foreign keys # This prevents inconsistent data states
changeset add_type_validation_error(changeset, reason)
end end
end end
@ -114,6 +114,17 @@ defmodule Mv.MembershipFees.Changes.ValidateSameInterval do
) )
end end
# Add validation error when types cannot be loaded
defp add_type_validation_error(changeset, reason) do
message = "Could not validate membership fee type intervals: type not found"
Ash.Changeset.add_error(
changeset,
field: :membership_fee_type_id,
message: message
)
end
# Format interval atom to human-readable string # Format interval atom to human-readable string
defp format_interval(:monthly), do: "monthly" defp format_interval(:monthly), do: "monthly"
defp format_interval(:quarterly), do: "quarterly" defp format_interval(:quarterly), do: "quarterly"