refactor: reduce nesting depth and improve code readability
All checks were successful
continuous-integration/drone/push Build is passing

- Replace Enum.map |> Enum.join with Enum.map_join for efficiency
- Extract helper functions to reduce nesting depth from 4 to 2
- Rename is_current_cycle? to current_cycle? following Elixir conventions
This commit is contained in:
Moritz 2025-12-15 11:50:08 +01:00
parent 06324d77c5
commit e9c53cc520
3 changed files with 146 additions and 114 deletions

View file

@ -37,29 +37,35 @@ defmodule Mv.MembershipFees.Changes.ValidateSameInterval do
current_type_id = get_current_type_id(changeset)
new_type_id = get_new_type_id(changeset)
# If no current type, allow any change (first assignment)
if is_nil(current_type_id) do
changeset
else
# If new type is nil, that's allowed (removing type)
if is_nil(new_type_id) do
cond do
# If no current type, allow any change (first assignment)
is_nil(current_type_id) ->
changeset
else
# Both types exist - validate intervals match
case get_intervals(current_type_id, new_type_id) do
{:ok, current_interval, new_interval} ->
if current_interval == new_interval do
changeset
else
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
# If new type is nil, that's allowed (removing type)
is_nil(new_type_id) ->
changeset
# Both types exist - validate intervals match
true ->
validate_intervals_match(changeset, current_type_id, new_type_id)
end
end
# Validates that intervals match when both types exist
defp validate_intervals_match(changeset, current_type_id, new_type_id) do
case get_intervals(current_type_id, new_type_id) do
{:ok, current_interval, new_interval} ->
if current_interval == new_interval do
changeset
else
add_interval_mismatch_error(changeset, current_interval, new_interval)
end
end
{:error, _reason} ->
# If we can't load the types, allow the change (fail open)
# The database constraint will catch invalid foreign keys
changeset
end
end