Add locations (Filialen) as a grouping layer above resources
Test backoffice (smb-crm) / test (push) Successful in 1m46s

Enables multiple barbers/staff bookable at the same location and time
-- previously "resource" conflated "location" and "the thing that
can't double-book itself" into one row, so a Filiale could only ever
have exactly one bookable slot at once.

- New `locations` table; `resources.location_id` with a generic,
  idempotent backfill migration (any resource without a location gets
  one auto-created matching its name -- not a one-off for any single
  client, protects any future resource stuck in the old flat shape too)
- `resources`/`resource_hours`/services keep everything they already
  had (hours, min-notice, max-advance, buffer, the no-overlap
  constraint) scoped to resource_id, not location_id -- two barbers at
  one location must stay independently bookable at the same time
- booking_db.py: new locations CRUD mirroring the existing
  resources/services pattern; create_resource now requires a
  location_id, guarded the same way every other tenant check here is
  (get_location existence check, no real FK -- matches this schema's
  existing no-FK convention throughout)
- app.py: new POST /api/locations provisioning route; POST
  /api/resources now requires location_id
- owner_settings.py + settings.html: new self-service "add a Filiale"
  / "add a barber" UI -- there was previously no way to create a
  resource at all outside the CRM/n8n provisioning API
- public_booking.py + book.html: new Filiale picker (reuses the
  existing wireOptionGroup button-group pattern), filtering the
  Mitarbeiter picker to the selected location -- a single-location
  client sees no extra click, same as before Filialen existed
- owner_booking.py + agenda.html: the Filiale show/hide toggle and
  hide-cancelled toggle (shipped earlier this session) now key off
  location_id instead of resource_id, so hiding a Filiale hides every
  barber's bookings at it; manual-booking dropdown grouped by Filiale
- n8n/onboarding.json: default provisioning now creates a "Hauptfiliale"
  location before its resource (inert until re-imported into the live
  n8n instance)

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 03:02:56 +02:00
parent a56a238e81
commit 456ca3872f
19 changed files with 585 additions and 99 deletions
+73 -4
View File
@@ -26,6 +26,12 @@ class UnknownResource(Exception):
guards against a booking write smuggling in another tenant's resource."""
class UnknownLocation(Exception):
"""Raised when a location_id doesn't belong to the given client_id --
guards create_resource against a location_id smuggled in from another
tenant."""
def new_id(prefix):
return f"{prefix}-{int(time.time() * 1000)}-{secrets.token_hex(3)}"
@@ -38,17 +44,80 @@ def _list_active(table, client_id):
return cur.fetchall()
# ---- locations ----
def create_location(client_id, name, active=True):
location_id = new_id("LOC")
with db.connect() as conn, conn.cursor() as cur:
cur.execute(
"INSERT INTO locations (location_id, client_id, name, active) "
"VALUES (%s, %s, %s, %s) RETURNING *",
(location_id, client_id, name, active))
row = cur.fetchone()
conn.commit()
return row
def get_location(client_id, location_id):
with db.connect() as conn, conn.cursor() as cur:
cur.execute(
"SELECT * FROM locations WHERE client_id = %s AND location_id = %s",
(client_id, location_id))
return cur.fetchone()
def list_active_locations(client_id):
return _list_active("locations", client_id)
def list_locations(client_id):
"""All of client_id's locations, active or not -- for the owner settings
page, same reasoning as list_resources/list_services."""
with db.connect() as conn, conn.cursor() as cur:
cur.execute(
"SELECT * FROM locations WHERE client_id = %s ORDER BY name",
(client_id,))
return cur.fetchall()
_LOCATION_UPDATABLE = {"name", "active"}
def update_location(client_id, location_id, **fields):
"""Update a location's own fields, scoped to client_id. Returns the
updated row, or None if no such location exists for this client."""
bad = set(fields) - _LOCATION_UPDATABLE
if bad:
raise ValueError(f"not updatable: {', '.join(sorted(bad))}")
if not fields:
return get_location(client_id, location_id)
setsql = ", ".join(f"{c} = %s" for c in fields)
with db.connect() as conn, conn.cursor() as cur:
cur.execute(
f"UPDATE locations SET {setsql} WHERE client_id = %s AND location_id = %s "
"RETURNING *",
[*fields.values(), client_id, location_id])
row = cur.fetchone()
conn.commit()
return row
# ---- resources ----
def create_resource(client_id, name, active=True, min_notice_minutes=60,
def create_resource(client_id, location_id, name, active=True, min_notice_minutes=60,
max_advance_days=30, buffer_minutes=0):
"""location_id must be one of client_id's own locations -- resources
(individual bookable staff) always belong to a Filiale. Raises
UnknownLocation if it isn't."""
if get_location(client_id, location_id) is None:
raise UnknownLocation(f"no location {location_id} for client {client_id}")
resource_id = new_id("RS")
with db.connect() as conn, conn.cursor() as cur:
cur.execute(
"INSERT INTO resources (resource_id, client_id, name, active, "
"INSERT INTO resources (resource_id, client_id, location_id, name, active, "
"min_notice_minutes, max_advance_days, buffer_minutes) "
"VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING *",
(resource_id, client_id, name, active, min_notice_minutes,
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING *",
(resource_id, client_id, location_id, name, active, min_notice_minutes,
max_advance_days, buffer_minutes))
row = cur.fetchone()
conn.commit()