PhoenixKitBookings.Services (PhoenixKitBookings v0.1.1)

Copy Markdown View Source

CRUD for bookable services and their availability rules.

Every mutation logs an Activity entry (actor via opts[:actor_uuid], passed down from the LiveView layer) and broadcasts {:bookings_service_changed, service_uuid} on services_topic/0 so admin lists and public pickers stay live.

Summary

Functions

Adds one availability rule. service_uuid is stamped explicitly — never cast from attrs.

Counts services per status — powers the admin tabs.

Non-trashed services owned by a user (the self-service cap check).

Creates a service. opts[:owner_uuid] stamps the owning user (nil = a site-wide service) — deliberately an option, never cast from attrs.

Permanent delete — Trash view only. Cascades to availability rules and bookings (DB ON DELETE CASCADE).

Deletes a unit; its past bookings keep history via ON DELETE SET NULL.

The service's effective capacity: the active-unit count when any units exist (unit-tracked service), else the pooled seats.

Fetches an ACTIVE service by public slug (the public booking path).

Fetches a service by UUID; nil on miss or forged id.

Active units only — the capacity-defining set.

Active services for the public listing.

Rules of a service, overrides first, then weekly rules.

Lists services. Trashed rows are excluded unless status: "trashed" or include_trashed: true is passed. Options: :status, :include_trashed, :owner_uuid (only services owned by that user — Policy.visible_services/2 uses this for base-permission holders).

All units of a service, active first, then by name.

PubSub topic announcing service config changes.

Controlled status flip (active / inactive).

Soft-delete (workspace status-sentinel convention).

A unit's display name; nil for a deleted/unknown unit.

Unit names keyed by uuid — batch lookup for admin lists.

Functions

add_rule(service, attrs, opts \\ [])

Adds one availability rule. service_uuid is stamped explicitly — never cast from attrs.

add_unit(service, attrs, opts \\ [])

count_by_status()

Counts services per status — powers the admin tabs.

count_owned(owner_uuid)

Non-trashed services owned by a user (the self-service cap check).

create_service(attrs, opts \\ [])

Creates a service. opts[:owner_uuid] stamps the owning user (nil = a site-wide service) — deliberately an option, never cast from attrs.

delete_rule(rule, opts \\ [])

delete_service(service, opts \\ [])

Permanent delete — Trash view only. Cascades to availability rules and bookings (DB ON DELETE CASCADE).

delete_unit(unit, opts \\ [])

Deletes a unit; its past bookings keep history via ON DELETE SET NULL.

effective_seats(service)

The service's effective capacity: the active-unit count when any units exist (unit-tracked service), else the pooled seats.

get_active_service_by_slug(slug)

Fetches an ACTIVE service by public slug (the public booking path).

get_service(uuid)

Fetches a service by UUID; nil on miss or forged id.

get_service!(uuid)

list_active_units(service_uuid)

Active units only — the capacity-defining set.

list_public_services()

Active services for the public listing.

list_rules(service_uuid)

Rules of a service, overrides first, then weekly rules.

list_services(opts \\ [])

Lists services. Trashed rows are excluded unless status: "trashed" or include_trashed: true is passed. Options: :status, :include_trashed, :owner_uuid (only services owned by that user — Policy.visible_services/2 uses this for base-permission holders).

list_units(service_uuid)

All units of a service, active first, then by name.

restore_service(service, opts \\ [])

services_topic()

PubSub topic announcing service config changes.

set_status(service, status, opts \\ [])

Controlled status flip (active / inactive).

set_unit_active(unit, active?, opts \\ [])

trash_service(service, opts \\ [])

Soft-delete (workspace status-sentinel convention).

unit_name(unit_uuid)

A unit's display name; nil for a deleted/unknown unit.

unit_names(uuids)

Unit names keyed by uuid — batch lookup for admin lists.

update_service(service, attrs, opts \\ [])