Three deferred followups from the chunk-4 plan, all together: A. Restrictions editor - Resolver: time_of_week support (weekday set + time-of-day window), in addition to the existing time_of_day. - New OnCall.update_layer_restrictions/2 + clear_layer_restrictions/1. - schedule_live/show: per-layer Restrict on-call shifts form (Always on call / Time of day / Time of week with Mon-Sun checkboxes), changes patched live and persisted via the resolver. B. Drag-and-drop reorder - New OnCall.reorder_layers/2 + reorder_escalation_rules/2 (atomic transaction; validates id count + membership). - assets/js/sortable_list.ts: minimal HTML5 drag-and-drop hook (no external dep; mirrors the existing DeviceListReorder pattern), registered as SortableList in app.ts. - schedule_live/show + escalation_policy_live/show: layer/level cards now carry data-id + draggable + a visible drag handle. Up/down arrow buttons remain as a fallback for users on assistive tech. C. Unified new-schedule UX - schedule_live/show pre-opens the Add Layer form when the schedule has zero layers, so the new-schedule -> add-layers flow lands ready to act on. Two existing tests updated to seed a layer first; two new tests verify the auto-open behaviour both ways. Full suite 10,231 / 0 failures. Format/dialyzer clean.
146 lines
4.6 KiB
Elixir
146 lines
4.6 KiB
Elixir
defmodule ToweropsWeb.EscalationPolicyLive.Show do
|
|
@moduledoc false
|
|
use ToweropsWeb, :live_view
|
|
|
|
alias Towerops.OnCall
|
|
alias Towerops.Organizations
|
|
|
|
@impl true
|
|
def mount(%{"id" => id}, _session, socket) do
|
|
org_id = socket.assigns.current_scope.organization.id
|
|
policy = OnCall.get_escalation_policy!(id, org_id)
|
|
members = Organizations.list_organization_members(org_id)
|
|
users = Enum.map(members, & &1.user)
|
|
schedules = OnCall.list_schedules(org_id)
|
|
services = OnCall.list_devices_using_policy(policy.id, org_id)
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, policy.name)
|
|
|> assign(:active_page, "schedules")
|
|
|> assign(:policy, policy)
|
|
|> assign(:org_users, users)
|
|
|> assign(:org_schedules, schedules)
|
|
|> assign(:services, services)
|
|
|> assign(:show_add_rule, false)}
|
|
end
|
|
|
|
@doc false
|
|
def handoff_mode_label("when_in_use_by_service"), do: "when in use by a service"
|
|
def handoff_mode_label("always"), do: "always"
|
|
def handoff_mode_label("never"), do: "never"
|
|
def handoff_mode_label(_), do: "when in use by a service"
|
|
|
|
@impl true
|
|
def handle_event("delete", _params, socket) do
|
|
case OnCall.delete_escalation_policy(socket.assigns.policy) do
|
|
{:ok, _} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, t("Escalation policy deleted"))
|
|
|> push_navigate(to: ~p"/schedules?tab=escalation-policies")}
|
|
|
|
{:error, _} ->
|
|
{:noreply, put_flash(socket, :error, t("Unable to delete escalation policy"))}
|
|
end
|
|
end
|
|
|
|
def handle_event("toggle_add_rule", _params, socket) do
|
|
{:noreply, assign(socket, :show_add_rule, !socket.assigns.show_add_rule)}
|
|
end
|
|
|
|
def handle_event("save_rule", %{"timeout_minutes" => timeout}, socket) do
|
|
policy = socket.assigns.policy
|
|
next_position = length(policy.rules)
|
|
|
|
case OnCall.create_escalation_rule(%{
|
|
escalation_policy_id: policy.id,
|
|
position: next_position,
|
|
timeout_minutes: timeout
|
|
}) do
|
|
{:ok, _} ->
|
|
{:noreply,
|
|
socket
|
|
|> assign(:show_add_rule, false)
|
|
|> reload_policy()}
|
|
|
|
{:error, _} ->
|
|
{:noreply, put_flash(socket, :error, t("Unable to add rule"))}
|
|
end
|
|
end
|
|
|
|
def handle_event("delete_rule", %{"id" => rule_id}, socket) do
|
|
rule = Enum.find(socket.assigns.policy.rules, &(&1.id == rule_id))
|
|
|
|
if rule do
|
|
case OnCall.delete_escalation_rule(rule) do
|
|
{:ok, _} -> {:noreply, reload_policy(socket)}
|
|
{:error, _} -> {:noreply, put_flash(socket, :error, t("Unable to delete rule"))}
|
|
end
|
|
else
|
|
{:noreply, socket}
|
|
end
|
|
end
|
|
|
|
def handle_event("add_target", %{"rule_id" => rule_id, "target_type" => target_type} = params, socket) do
|
|
attrs = %{
|
|
escalation_rule_id: rule_id,
|
|
target_type: target_type
|
|
}
|
|
|
|
attrs =
|
|
case target_type do
|
|
"user" -> Map.put(attrs, :user_id, params["target_id"])
|
|
"schedule" -> Map.put(attrs, :schedule_id, params["target_id"])
|
|
end
|
|
|
|
case OnCall.create_escalation_target(attrs) do
|
|
{:ok, _} -> {:noreply, reload_policy(socket)}
|
|
{:error, _} -> {:noreply, put_flash(socket, :error, t("Unable to add target"))}
|
|
end
|
|
end
|
|
|
|
def handle_event("reorder_rules", %{"ordered_ids" => ordered_ids}, socket) when is_list(ordered_ids) do
|
|
case OnCall.reorder_escalation_rules(socket.assigns.policy.id, ordered_ids) do
|
|
:ok -> {:noreply, reload_policy(socket)}
|
|
{:error, _} -> {:noreply, put_flash(socket, :error, t("Unable to reorder levels"))}
|
|
end
|
|
end
|
|
|
|
def handle_event("move_rule", %{"id" => id, "direction" => direction}, socket) when direction in ["up", "down"] do
|
|
rule = Enum.find(socket.assigns.policy.rules, &(&1.id == id))
|
|
|
|
if rule do
|
|
{:ok, _} = OnCall.move_escalation_rule(rule, String.to_existing_atom(direction))
|
|
{:noreply, reload_policy(socket)}
|
|
else
|
|
{:noreply, socket}
|
|
end
|
|
end
|
|
|
|
def handle_event("delete_target", %{"id" => target_id}, socket) do
|
|
target =
|
|
socket.assigns.policy.rules
|
|
|> Enum.flat_map(& &1.targets)
|
|
|> Enum.find(&(&1.id == target_id))
|
|
|
|
if target do
|
|
case OnCall.delete_escalation_target(target) do
|
|
{:ok, _} -> {:noreply, reload_policy(socket)}
|
|
{:error, _} -> {:noreply, put_flash(socket, :error, t("Unable to delete target"))}
|
|
end
|
|
else
|
|
{:noreply, socket}
|
|
end
|
|
end
|
|
|
|
defp reload_policy(socket) do
|
|
org_id = socket.assigns.current_scope.organization.id
|
|
policy = OnCall.get_escalation_policy!(socket.assigns.policy.id)
|
|
services = OnCall.list_devices_using_policy(policy.id, org_id)
|
|
|
|
socket
|
|
|> assign(:policy, policy)
|
|
|> assign(:services, services)
|
|
end
|
|
end
|