mirror of
https://github.com/Dvorinka/Bookra.git
synced 2026-10-28 15:51:15 +00:00
feat(availability): per-staff working hours and class/session management
- Working-hours repo accepts nullable staff scope (pgx + memory); staff rules live in availability_rules and drive public slot generation - Protected routes for staff working hours, class templates, and class sessions with tenant-ownership checks and input validation - Sessions derive endsAt/capacity from templates; list resolves location names and live booked counts; template delete cascades sessions, session delete preserves bookings via SET NULL (mirrored in memory) - Zones page: staff-scope selector on working hours, new Classes tab with template/session CRUD, CS+EN strings, mobile tab wrapping - Normalize availability_rules times to HH:MM in the service layer Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
d9aa29dbcb
commit
0405106587
12 files changed
+2069
-58
No files matched your search
@@ -4,11 +4,15 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bookra/apps/backend/internal/bookings"
|
||||
"bookra/apps/backend/internal/db"
|
||||
"bookra/apps/backend/internal/domain"
|
||||
)
|
||||
|
||||
func boolPtr(v bool) *bool { return &v }
|
||||
|
||||
var owner = domain.Principal{Subject: "demo-owner", Email: "[email protected]", Role: "owner"}
|
||||
|
||||
type stubBilling struct {
|
||||
@@ -259,3 +263,287 @@ func TestStaffNotFoundAndValidation(t *testing.T) {
|
||||
t.Fatalf("expected ErrTenantMembership, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
const demoStaffID = "6936c444-c7d0-4a7d-b596-a9b72d2f4fc0"
|
||||
|
||||
func TestListStaffWorkingHours(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
|
||||
hours, err := svc.ListStaffWorkingHours(context.Background(), owner, demoStaffID)
|
||||
if err != nil {
|
||||
t.Fatalf("list staff working hours: %v", err)
|
||||
}
|
||||
if len(hours) != 5 {
|
||||
t.Fatalf("expected 5 seeded weekday rules, got %d", len(hours))
|
||||
}
|
||||
for _, wh := range hours {
|
||||
if wh.Open != "09:00" || wh.Close != "17:00" {
|
||||
t.Fatalf("day %d: expected 09:00-17:00, got %s-%s", wh.DayOfWeek, wh.Open, wh.Close)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStaffWorkingHours(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
err := svc.UpdateStaffWorkingHours(ctx, owner, demoStaffID, 1, domain.UpdateWorkingHoursRequest{
|
||||
IsOpen: boolPtr(true),
|
||||
Open: "08:00",
|
||||
Close: "12:00",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("update staff hours: %v", err)
|
||||
}
|
||||
|
||||
hours, err := svc.ListStaffWorkingHours(ctx, owner, demoStaffID)
|
||||
if err != nil {
|
||||
t.Fatalf("list staff hours: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, wh := range hours {
|
||||
if wh.DayOfWeek == 1 {
|
||||
found = true
|
||||
if wh.Open != "08:00" || wh.Close != "12:00" {
|
||||
t.Fatalf("expected Monday 08:00-12:00, got %s-%s", wh.Open, wh.Close)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("Monday rule missing after update")
|
||||
}
|
||||
|
||||
// Closing a day removes the rule.
|
||||
err = svc.UpdateStaffWorkingHours(ctx, owner, demoStaffID, 2, domain.UpdateWorkingHoursRequest{
|
||||
IsOpen: boolPtr(false),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("close staff day: %v", err)
|
||||
}
|
||||
hours, _ = svc.ListStaffWorkingHours(ctx, owner, demoStaffID)
|
||||
for _, wh := range hours {
|
||||
if wh.DayOfWeek == 2 {
|
||||
t.Fatal("Tuesday rule should have been removed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaffWorkingHoursRejectsForeignStaff(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
missing := "00000000-0000-0000-0000-000000000099"
|
||||
if _, err := svc.ListStaffWorkingHours(ctx, owner, missing); !errors.Is(err, ErrStaffNotFound) {
|
||||
t.Fatalf("expected ErrStaffNotFound, got %v", err)
|
||||
}
|
||||
err := svc.UpdateStaffWorkingHours(ctx, owner, missing, 1, domain.UpdateWorkingHoursRequest{
|
||||
IsOpen: boolPtr(true), Open: "09:00", Close: "17:00",
|
||||
})
|
||||
if !errors.Is(err, ErrStaffNotFound) {
|
||||
t.Fatalf("expected ErrStaffNotFound, got %v", err)
|
||||
}
|
||||
|
||||
err = svc.UpdateStaffWorkingHours(ctx, owner, demoStaffID, 1, domain.UpdateWorkingHoursRequest{
|
||||
IsOpen: boolPtr(true), Open: "9am", Close: "17:00",
|
||||
})
|
||||
if !errors.Is(err, ErrInvalidBooking) {
|
||||
t.Fatalf("expected ErrInvalidBooking for bad time, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassTemplateCRUD(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
templates, err := svc.ListClassTemplates(ctx, owner)
|
||||
if err != nil {
|
||||
t.Fatalf("list templates: %v", err)
|
||||
}
|
||||
if len(templates) != 1 || templates[0].Title != "Small group mobility class" {
|
||||
t.Fatalf("expected seeded template, got %+v", templates)
|
||||
}
|
||||
|
||||
created, err := svc.CreateClassTemplate(ctx, owner, domain.UpsertClassTemplateRequest{
|
||||
Title: " Yoga Basics ",
|
||||
DurationMinutes: 45,
|
||||
Capacity: 8,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create template: %v", err)
|
||||
}
|
||||
if created.Title != "Yoga Basics" {
|
||||
t.Fatalf("expected trimmed title, got %q", created.Title)
|
||||
}
|
||||
|
||||
if err := svc.DeleteClassTemplate(ctx, owner, created.ID); err != nil {
|
||||
t.Fatalf("delete template: %v", err)
|
||||
}
|
||||
templates, _ = svc.ListClassTemplates(ctx, owner)
|
||||
if len(templates) != 1 {
|
||||
t.Fatalf("expected 1 template after delete, got %d", len(templates))
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassTemplateValidation(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
cases := []domain.UpsertClassTemplateRequest{
|
||||
{Title: " ", DurationMinutes: 60, Capacity: 4},
|
||||
{Title: "X", DurationMinutes: 0, Capacity: 4},
|
||||
{Title: "X", DurationMinutes: 60, Capacity: 0},
|
||||
}
|
||||
for i, req := range cases {
|
||||
if _, err := svc.CreateClassTemplate(ctx, owner, req); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("case %d: expected ErrInvalidInput, got %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
missing := "00000000-0000-0000-0000-000000000099"
|
||||
if err := svc.DeleteClassTemplate(ctx, owner, missing); !errors.Is(err, ErrClassTemplateNotFound) {
|
||||
t.Fatalf("expected ErrClassTemplateNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteClassTemplateCascadesSessions(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
if err := svc.DeleteClassTemplate(ctx, owner, "d13fe5fd-727f-4d69-bfd8-47f1b92a2cf7"); err != nil {
|
||||
t.Fatalf("delete template: %v", err)
|
||||
}
|
||||
sessions, err := svc.ListClassSessions(ctx, owner)
|
||||
if err != nil {
|
||||
t.Fatalf("list sessions: %v", err)
|
||||
}
|
||||
if len(sessions) != 0 {
|
||||
t.Fatalf("expected sessions to cascade-delete, got %d", len(sessions))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListClassSessionsEnrichment(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
|
||||
sessions, err := svc.ListClassSessions(context.Background(), owner)
|
||||
if err != nil {
|
||||
t.Fatalf("list sessions: %v", err)
|
||||
}
|
||||
if len(sessions) != 1 {
|
||||
t.Fatalf("expected 1 seeded session, got %d", len(sessions))
|
||||
}
|
||||
s := sessions[0]
|
||||
if s.TemplateTitle != "Small group mobility class" {
|
||||
t.Fatalf("expected template title, got %q", s.TemplateTitle)
|
||||
}
|
||||
if s.LocationName != "Main Studio" {
|
||||
t.Fatalf("expected location name, got %q", s.LocationName)
|
||||
}
|
||||
if s.Capacity != 4 || s.BookedCount != 0 {
|
||||
t.Fatalf("expected capacity 4 booked 0, got %d/%d", s.Capacity, s.BookedCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateClassSession(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
startsAt := time.Now().Add(72 * time.Hour).UTC().Format(time.RFC3339)
|
||||
cap := 6
|
||||
created, err := svc.CreateClassSession(ctx, owner, domain.CreateClassSessionRequest{
|
||||
TemplateID: "d13fe5fd-727f-4d69-bfd8-47f1b92a2cf7",
|
||||
StartsAt: startsAt,
|
||||
Capacity: &cap,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create session: %v", err)
|
||||
}
|
||||
if created.Capacity != 6 {
|
||||
t.Fatalf("expected override capacity 6, got %d", created.Capacity)
|
||||
}
|
||||
if !created.EndsAt.Equal(created.StartsAt.Add(60 * time.Minute)) {
|
||||
t.Fatalf("expected end = start + 60m, got %s -> %s", created.StartsAt, created.EndsAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateClassSessionValidation(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
future := time.Now().Add(48 * time.Hour).UTC().Format(time.RFC3339)
|
||||
past := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
|
||||
template := "d13fe5fd-727f-4d69-bfd8-47f1b92a2cf7"
|
||||
badLocation := "00000000-0000-0000-0000-000000000099"
|
||||
badCap := 0
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req domain.CreateClassSessionRequest
|
||||
want error
|
||||
}{
|
||||
{"missing template", domain.CreateClassSessionRequest{TemplateID: "00000000-0000-0000-0000-000000000099", StartsAt: future}, ErrClassTemplateNotFound},
|
||||
{"bad start format", domain.CreateClassSessionRequest{TemplateID: template, StartsAt: "tomorrow"}, ErrInvalidInput},
|
||||
{"past start", domain.CreateClassSessionRequest{TemplateID: template, StartsAt: past}, ErrInvalidInput},
|
||||
{"foreign location", domain.CreateClassSessionRequest{TemplateID: template, StartsAt: future, LocationID: &badLocation}, ErrLocationNotFound},
|
||||
{"zero capacity", domain.CreateClassSessionRequest{TemplateID: template, StartsAt: future, Capacity: &badCap}, ErrInvalidInput},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if _, err := svc.CreateClassSession(ctx, owner, tc.req); !errors.Is(err, tc.want) {
|
||||
t.Fatalf("%s: expected %v, got %v", tc.name, tc.want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteClassSessionPreservesBooking(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := newCatalogService(repo)
|
||||
ctx := context.Background()
|
||||
|
||||
// Book the seeded class session via the public booking service.
|
||||
bookingSvc := bookings.NewService(repo, nil, "")
|
||||
sessionID := "4bf74c12-44dd-45ca-86bb-b104f16f2435"
|
||||
session, err := repo.GetClassSessionByID(ctx, sessionID)
|
||||
if err != nil {
|
||||
t.Fatalf("get seeded session: %v", err)
|
||||
}
|
||||
_, err = bookingSvc.Create(ctx, domain.CreateBookingRequest{
|
||||
TenantSlug: "studio-atelier",
|
||||
CustomerName: "Kate",
|
||||
CustomerEmail: "[email protected]",
|
||||
BookingMode: "class",
|
||||
ClassSessionID: &sessionID,
|
||||
StartsAt: session.StartsAt.UTC().Format(time.RFC3339),
|
||||
EndsAt: session.EndsAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("book class session: %v", err)
|
||||
}
|
||||
|
||||
sessions, _ := svc.ListClassSessions(ctx, owner)
|
||||
if sessions[0].BookedCount != 1 {
|
||||
t.Fatalf("expected bookedCount 1, got %d", sessions[0].BookedCount)
|
||||
}
|
||||
|
||||
if err := svc.DeleteClassSession(ctx, owner, sessionID); err != nil {
|
||||
t.Fatalf("delete session: %v", err)
|
||||
}
|
||||
|
||||
// The booking survives but no longer references the session.
|
||||
bookingsList, err := repo.ListBookingsByTenantBetween(ctx, sessions[0].TenantID, time.Now().Add(-time.Hour), time.Now().Add(200*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("list bookings: %v", err)
|
||||
}
|
||||
if len(bookingsList) != 1 {
|
||||
t.Fatalf("expected booking to survive, got %d", len(bookingsList))
|
||||
}
|
||||
if bookingsList[0].ClassSessionID != nil {
|
||||
t.Fatal("expected booking class_session_id to be nulled")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user