From 100245975a7ca788ed2812704f6a363e23ac6b85 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Fri, 6 Mar 2026 14:44:33 -0600 Subject: [PATCH] refactor: reduce complexity in global pricing validation Extract validation logic into separate functions to reduce: - Cyclomatic complexity from 12 to acceptable levels - Nesting depth from 3 to 2 Improves code maintainability and passes Credo checks. --- lib/towerops_web/live/admin/org_live/index.ex | 73 ++++++++++--------- .../live/admin/org_live/index.html.heex | 4 +- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/lib/towerops_web/live/admin/org_live/index.ex b/lib/towerops_web/live/admin/org_live/index.ex index cf1b47e0..2e86e527 100644 --- a/lib/towerops_web/live/admin/org_live/index.ex +++ b/lib/towerops_web/live/admin/org_live/index.ex @@ -211,47 +211,52 @@ defmodule ToweropsWeb.Admin.OrgLive.Index do end defp validate_global_pricing_params(params) do - errors = [] + [] + |> validate_free_devices(params) + |> validate_price_per_device(params) + end - errors = - case Map.get(params, "default_free_devices") do - nil -> - errors + defp validate_free_devices(errors, params) do + case Map.get(params, "default_free_devices") do + nil -> errors + value -> validate_free_devices_value(errors, value) + end + end - value -> - case Integer.parse(value) do - {int, _} when int > 0 and int < 10_000 -> - errors + defp validate_free_devices_value(errors, value) do + case Integer.parse(value) do + {int, _} when int > 0 and int < 10_000 -> + errors - _ -> - [ - {:default_free_devices, {"must be greater than 0 and less than 10,000", []}} - | errors - ] - end - end + _ -> + [{:default_free_devices, {"must be greater than 0 and less than 10,000", []}} | errors] + end + end - errors = - case Map.get(params, "default_price_per_device") do - nil -> - errors + defp validate_price_per_device(errors, params) do + case Map.get(params, "default_price_per_device") do + nil -> errors + value -> validate_price_per_device_value(errors, value) + end + end - value -> - case Decimal.parse(value) do - {decimal, _} -> - if Decimal.compare(decimal, "0.01") in [:gt, :eq] and - Decimal.compare(decimal, "999.99") in [:lt, :eq] do - errors - else - [{:default_price_per_device, {"must be between 0.01 and 999.99", []}} | errors] - end + defp validate_price_per_device_value(errors, value) do + case Decimal.parse(value) do + {decimal, _} -> validate_price_range(errors, decimal) + :error -> [{:default_price_per_device, {"must be a valid decimal", []}} | errors] + end + end - :error -> - [{:default_price_per_device, {"must be a valid decimal", []}} | errors] - end - end + defp validate_price_range(errors, decimal) do + valid_range? = + Decimal.compare(decimal, "0.01") in [:gt, :eq] and + Decimal.compare(decimal, "999.99") in [:lt, :eq] - errors + if valid_range? do + errors + else + [{:default_price_per_device, {"must be between 0.01 and 999.99", []}} | errors] + end end defp count_active_subscriptions do diff --git a/lib/towerops_web/live/admin/org_live/index.html.heex b/lib/towerops_web/live/admin/org_live/index.html.heex index 9ff86c4f..3d9a1304 100644 --- a/lib/towerops_web/live/admin/org_live/index.html.heex +++ b/lib/towerops_web/live/admin/org_live/index.html.heex @@ -4,8 +4,8 @@

{t("All Organizations")}

{t_admin("Manage organizations")}

- - + +