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)
end
{:error, _reason} ->
# If we can't load the types, allow the change (fail open)
# The database constraint will catch invalid foreign keys
changeset
{:error, reason} ->
# Fail closed: If we can't load the types, reject the change
# This prevents inconsistent data states
add_type_validation_error(changeset, reason)
end
end
@ -114,6 +114,17 @@ defmodule Mv.MembershipFees.Changes.ValidateSameInterval do
)
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
defp format_interval(:monthly), do: "monthly"
defp format_interval(:quarterly), do: "quarterly"