towerops/lib/towerops/on_call/layer.ex
Graham McIntire a4e08b2aca Major codebase cleanup: DRY schemas, remove dead config, consolidate modules
- Created Towerops.Schema base macro (95+ schemas updated to use it)
- Created Towerops.Snmp.Reading macro (7 reading schemas consolidated)
- Created Towerops.Gaiia.BaseSchema macro (4 Gaiia schemas consolidated)
- Created Towerops.SyncLog shared module (UISP + Preseem merge)
- Created Towerops.LogFilters (3 log filter modules merged into 1)
- Created Towerops.OnCall.ChangesetHelpers (9 on-call schemas simplified)
- Created API v1 ResourceController shared helpers (7 controllers)
- Extracted ConnectionHelpers.format_connection_result (2 LiveViews)
- Removed 5 dead config keys (scopes, mib_dirs, stripe_meter_id, etc.)
- Fixed 2 pre-existing broken tests
- All 13,219 tests pass, Credo clean (0 issues)
2026-06-16 15:29:22 -05:00

47 lines
1.5 KiB
Elixir

defmodule Towerops.OnCall.Layer do
@moduledoc """
Schema for a rotation layer within an on-call schedule.
Layers stack — higher position layers override lower layers where they have coverage.
"""
use Towerops.Schema
import Towerops.OnCall.ChangesetHelpers
alias Towerops.OnCall.LayerMember
alias Towerops.OnCall.Schedule
schema "on_call_layers" do
field :name, :string
field :position, :integer, default: 0
field :rotation_type, :string
field :rotation_interval, :integer, default: 1
field :handoff_time, :time
field :handoff_day, :integer
field :start_date, :utc_datetime
field :restriction_type, :string
field :restrictions, :map
belongs_to :schedule, Schedule
has_many :members, LayerMember, preload_order: [asc: :position]
timestamps(type: :utc_datetime)
end
@valid_rotation_types ~w(daily weekly custom)
@valid_restriction_types ~w(time_of_day time_of_week)
@required_fields ~w(name position rotation_type rotation_interval handoff_time start_date schedule_id)a
@optional_fields ~w(handoff_day restriction_type restrictions)a
def changeset(layer, attrs) do
layer
|> required_first(attrs, @required_fields, @optional_fields)
|> validate_inclusion(:rotation_type, @valid_rotation_types)
|> validate_inclusion(:restriction_type, @valid_restriction_types)
|> validate_number(:rotation_interval, greater_than: 0)
|> validate_number(:handoff_day, greater_than_or_equal_to: 0, less_than_or_equal_to: 6)
|> foreign_key_constraint(:schedule_id)
end
end