towerops/lib/towerops_web/live/schedule_live/form.ex
Graham McIntire 0cd2ed3567
feat: add on-call schedules and escalation policies
Built-in PagerDuty-equivalent system for on-call scheduling and alert
escalation. Users can now manage schedules, rotation layers, overrides,
and escalation policies directly in the app alongside PagerDuty.

- On-call schedules with rotation layers (daily/weekly/custom), member
  management, and temporary overrides
- Escalation policies with ordered rules, timeout-based escalation,
  and user/schedule targets
- Automatic escalation via Oban worker with configurable repeat count
- Email notifications via Swoosh for on-call alerts
- Resolver computes who's on-call from layer stacking and overrides
- AlertNotificationWorker integration: starts escalation alongside
  PagerDuty, acknowledges/resolves incidents on alert state changes
- Device and organization schemas support escalation_policy_id
- Escalation policy picker on device form
- Schedules nav item with tabbed index (schedules + escalation policies)
- Full CRUD UI for schedules, layers, members, overrides, rules, targets
- 62 LiveView tests, 56 context/schema/resolver/escalation tests
- 26 E2E Playwright tests for smoke and critical path coverage
2026-03-11 12:32:54 -05:00

83 lines
2.3 KiB
Elixir

defmodule ToweropsWeb.ScheduleLive.Form do
@moduledoc false
use ToweropsWeb, :live_view
alias Towerops.OnCall
alias Towerops.OnCall.Schedule
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :active_page, "schedules")}
end
@impl true
def handle_params(params, _url, socket) do
case socket.assigns.live_action do
:new ->
schedule = %Schedule{}
changeset = Schedule.changeset(schedule, %{})
{:noreply,
socket
|> assign(:page_title, t("New Schedule"))
|> assign(:schedule, schedule)
|> assign(:form, to_form(changeset))}
:edit ->
schedule = OnCall.get_schedule!(params["id"])
changeset = Schedule.changeset(schedule, %{})
{:noreply,
socket
|> assign(:page_title, t("Edit Schedule"))
|> assign(:schedule, schedule)
|> assign(:form, to_form(changeset))}
end
end
@impl true
def handle_event("validate", %{"schedule" => params}, socket) do
changeset =
socket.assigns.schedule
|> Schedule.changeset(params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :form, to_form(changeset))}
end
def handle_event("save", %{"schedule" => params}, socket) do
org_id = socket.assigns.current_scope.organization.id
params = Map.put(params, "organization_id", org_id)
case socket.assigns.live_action do
:new -> save_schedule(socket, :create, params)
:edit -> save_schedule(socket, :update, params)
end
end
defp save_schedule(socket, :create, params) do
case OnCall.create_schedule(params) do
{:ok, schedule} ->
{:noreply,
socket
|> put_flash(:info, t("Schedule created"))
|> push_navigate(to: ~p"/schedules/#{schedule.id}")}
{:error, changeset} ->
{:noreply, assign(socket, :form, to_form(changeset))}
end
end
defp save_schedule(socket, :update, params) do
case OnCall.update_schedule(socket.assigns.schedule, params) do
{:ok, schedule} ->
{:noreply,
socket
|> put_flash(:info, t("Schedule updated"))
|> push_navigate(to: ~p"/schedules/#{schedule.id}")}
{:error, changeset} ->
{:noreply, assign(socket, :form, to_form(changeset))}
end
end
end