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
+24 -19
View File
@@ -13,10 +13,14 @@ def _dt(hour, minute=0):
return datetime(2026, 8, 3, hour, minute, tzinfo=timezone.utc)
def _loc(client_id):
return bdb.create_location(client_id, "Main")["location_id"]
# ---- resources / services ----
def test_create_and_get_resource():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
assert r["client_id"] == CLIENT_A
assert r["name"] == "Chair 1"
assert r["active"] is True
@@ -24,7 +28,7 @@ def test_create_and_get_resource():
def test_get_resource_is_tenant_scoped():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
assert bdb.get_resource(CLIENT_B, r["resource_id"]) is None
@@ -42,7 +46,7 @@ def test_get_service_is_tenant_scoped():
# ---- bookings: double-booking protection ----
def test_create_booking_succeeds():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "alice@example.com",
"Haircut", _dt(10), _dt(11))
assert b["status"] == "confirmed"
@@ -50,7 +54,7 @@ def test_create_booking_succeeds():
def test_overlapping_booking_same_resource_raises_conflict():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
with pytest.raises(bdb.BookingConflict):
@@ -63,7 +67,7 @@ def test_truly_concurrent_overlapping_inserts_only_one_wins():
overlapping slot -- not just a sequential second-request-fails check.
The Postgres EXCLUDE constraint (not app-level locking) is what has to
serialize this."""
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
start_barrier = threading.Barrier(2)
results = {}
@@ -92,7 +96,7 @@ def test_truly_concurrent_overlapping_inserts_only_one_wins():
def test_adjacent_non_overlapping_bookings_both_succeed():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
b2 = bdb.create_booking(CLIENT_A, r["resource_id"], "Bob", "b@x.com",
@@ -101,15 +105,15 @@ def test_adjacent_non_overlapping_bookings_both_succeed():
def test_create_booking_rejects_resource_from_another_client():
other = bdb.create_resource(CLIENT_B, "Chair 1")
other = bdb.create_resource(CLIENT_B, _loc(CLIENT_B), "Chair 1")
with pytest.raises(bdb.UnknownResource):
bdb.create_booking(CLIENT_A, other["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
def test_update_booking_rejects_moving_to_another_clients_resource():
r = bdb.create_resource(CLIENT_A, "Chair 1")
other = bdb.create_resource(CLIENT_B, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
other = bdb.create_resource(CLIENT_B, _loc(CLIENT_B), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
with pytest.raises(bdb.UnknownResource):
@@ -117,15 +121,16 @@ def test_update_booking_rejects_moving_to_another_clients_resource():
def test_create_pending_booking():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11), status="pending")
assert bdb.get_booking(CLIENT_A, b["booking_id"])["status"] == "pending"
def test_overlap_on_different_resource_succeeds():
r1 = bdb.create_resource(CLIENT_A, "Chair 1")
r2 = bdb.create_resource(CLIENT_A, "Chair 2")
loc = _loc(CLIENT_A)
r1 = bdb.create_resource(CLIENT_A, loc, "Chair 1")
r2 = bdb.create_resource(CLIENT_A, loc, "Chair 2")
bdb.create_booking(CLIENT_A, r1["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
b2 = bdb.create_booking(CLIENT_A, r2["resource_id"], "Bob", "b@x.com",
@@ -134,7 +139,7 @@ def test_overlap_on_different_resource_succeeds():
def test_reschedule_into_conflict_raises_and_leaves_original_untouched():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
b2 = bdb.create_booking(CLIENT_A, r["resource_id"], "Bob", "b@x.com",
@@ -147,7 +152,7 @@ def test_reschedule_into_conflict_raises_and_leaves_original_untouched():
def test_reschedule_to_free_slot_succeeds():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
updated = bdb.update_booking(CLIENT_A, b["booking_id"], start_time=_dt(14),
@@ -156,7 +161,7 @@ def test_reschedule_to_free_slot_succeeds():
def test_update_booking_rejects_non_updatable_field():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
with pytest.raises(ValueError):
@@ -166,14 +171,14 @@ def test_update_booking_rejects_non_updatable_field():
# ---- bookings: tenancy isolation ----
def test_get_booking_is_tenant_scoped_even_with_correct_id():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
assert bdb.get_booking(CLIENT_B, b["booking_id"]) is None
def test_update_booking_cannot_touch_other_clients_booking():
r = bdb.create_resource(CLIENT_A, "Chair 1")
r = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
b = bdb.create_booking(CLIENT_A, r["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
result = bdb.update_booking(CLIENT_B, b["booking_id"], status="cancelled")
@@ -182,8 +187,8 @@ def test_update_booking_cannot_touch_other_clients_booking():
def test_list_bookings_only_returns_own_client():
ra = bdb.create_resource(CLIENT_A, "Chair 1")
rb = bdb.create_resource(CLIENT_B, "Chair 1")
ra = bdb.create_resource(CLIENT_A, _loc(CLIENT_A), "Chair 1")
rb = bdb.create_resource(CLIENT_B, _loc(CLIENT_B), "Chair 1")
bdb.create_booking(CLIENT_A, ra["resource_id"], "Alice", "a@x.com",
"Haircut", _dt(10), _dt(11))
bdb.create_booking(CLIENT_B, rb["resource_id"], "Zoe", "z@x.com",