mirror of
https://github.com/Dvorinka/Bookra.git
synced 2026-10-28 23:51:17 +00:00
feat(reports): capacity-based utilization + report CSVs
Utilization now measures booked minutes against each member's availability-rule capacity in the tenant timezone, replacing the confirmed-share proxy. StaffMetric exposes booked/capacity minutes. New endpoints: GET /v1/export/staff-report.csv (reuses StaffMetrics) and GET /v1/export/service-report.csv (per-service bookings/cancelled/ revenue, class bookings rolled up). Export buttons on the staff performance panel and services page. 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
2464aa45fa
commit
7b9080eb1c
13 files changed
+541
-33
No files matched your search
@@ -8,12 +8,17 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bookra/apps/backend/internal/bookings"
|
||||
"bookra/apps/backend/internal/db"
|
||||
"bookra/apps/backend/internal/domain"
|
||||
)
|
||||
|
||||
var demoPrincipal = domain.Principal{Subject: "demo-owner", Role: "owner"}
|
||||
|
||||
func newTestService(repo *db.MemoryRepository) *Service {
|
||||
return NewService(repo, bookings.NewService(repo, nil, "http://localhost:3000"))
|
||||
}
|
||||
|
||||
const demoTenantID = "5d6b3551-0a3e-4b86-bdf0-e9df20a47148"
|
||||
|
||||
func readCSV(t *testing.T, buf *bytes.Buffer) [][]string {
|
||||
@@ -50,7 +55,7 @@ func TestWriteBookingsCSVResolvesLabelsAndEscapes(t *testing.T) {
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
svc := NewService(repo)
|
||||
svc := newTestService(repo)
|
||||
from := time.Now().Add(-time.Hour)
|
||||
to := time.Now().Add(72 * time.Hour)
|
||||
if err := svc.WriteBookingsCSV(context.Background(), demoPrincipal, &buf, from, to); err != nil {
|
||||
@@ -96,7 +101,7 @@ func TestWriteBookingsCSVRangeExcludesOutOfWindow(t *testing.T) {
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
svc := NewService(repo)
|
||||
svc := newTestService(repo)
|
||||
from := time.Now().Add(-time.Hour)
|
||||
to := time.Now().Add(24 * time.Hour) // far-future booking outside
|
||||
if err := svc.WriteBookingsCSV(context.Background(), demoPrincipal, &buf, from, to); err != nil {
|
||||
@@ -112,7 +117,7 @@ func TestWriteBookingsCSVRangeExcludesOutOfWindow(t *testing.T) {
|
||||
func TestWriteCustomersCSVPagesAndStrips(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
var buf bytes.Buffer
|
||||
svc := NewService(repo)
|
||||
svc := newTestService(repo)
|
||||
if err := svc.WriteCustomersCSV(context.Background(), demoPrincipal, &buf); err != nil {
|
||||
t.Fatalf("write csv: %v", err)
|
||||
}
|
||||
@@ -136,7 +141,7 @@ func TestWriteCustomersCSVPagesAndStrips(t *testing.T) {
|
||||
|
||||
func TestExportRequiresMembership(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
svc := NewService(repo)
|
||||
svc := newTestService(repo)
|
||||
var buf bytes.Buffer
|
||||
err := svc.WriteCustomersCSV(context.Background(), domain.Principal{Subject: "nobody"}, &buf)
|
||||
if err != ErrTenantMembership {
|
||||
@@ -147,3 +152,96 @@ func TestExportRequiresMembership(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestWriteStaffReportCSV(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
serviceID := "d5d76a61-3d49-467c-8dd4-bf61ee754e39"
|
||||
staffID := "6936c444-c7d0-4a7d-b596-a9b72d2f4fc0"
|
||||
now := time.Now().UTC()
|
||||
seed := func(status string, offsetHours int) {
|
||||
_, err := repo.CreateBooking(context.Background(), db.CreateBookingParams{
|
||||
TenantID: demoTenantID,
|
||||
ServiceID: &serviceID,
|
||||
StaffID: &staffID,
|
||||
CustomerName: "Report Customer",
|
||||
CustomerEmail: "[email protected]",
|
||||
StartsAt: now.Add(time.Duration(offsetHours) * time.Hour),
|
||||
EndsAt: now.Add(time.Duration(offsetHours+1) * time.Hour),
|
||||
Status: status,
|
||||
Reference: "BK-REP-" + status,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("seed booking: %v", err)
|
||||
}
|
||||
}
|
||||
seed("confirmed", -5)
|
||||
seed("confirmed", -20)
|
||||
seed("cancelled", -30)
|
||||
|
||||
var buf bytes.Buffer
|
||||
svc := newTestService(repo)
|
||||
if err := svc.WriteStaffReportCSV(context.Background(), demoPrincipal, &buf, 30); err != nil {
|
||||
t.Fatalf("staff report csv: %v", err)
|
||||
}
|
||||
records := readCSV(t, &buf)
|
||||
if len(records) != 2 {
|
||||
t.Fatalf("expected header + 1 staff row, got %d rows", len(records))
|
||||
}
|
||||
row := records[1]
|
||||
if row[0] != "Demo Stylist" {
|
||||
t.Fatalf("expected staff name, got %q", row[0])
|
||||
}
|
||||
if row[1] != "3" || row[2] != "2" || row[3] != "1" {
|
||||
t.Fatalf("expected bookings=3 completed=2 cancelled=1, got %v", row[1:4])
|
||||
}
|
||||
if row[4] != "120" {
|
||||
t.Fatalf("expected 120 booked minutes, got %q", row[4])
|
||||
}
|
||||
if row[7] != "240000" {
|
||||
t.Fatalf("expected 240000 revenue cents, got %q", row[7])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteServiceReportCSV(t *testing.T) {
|
||||
repo := db.NewMemoryRepository()
|
||||
serviceID := "d5d76a61-3d49-467c-8dd4-bf61ee754e39"
|
||||
sessionID := "4bf74c12-44dd-45ca-86bb-b104f16f2435"
|
||||
now := time.Now().UTC()
|
||||
seed := func(service *string, session *string, status string, ref string) {
|
||||
_, err := repo.CreateBooking(context.Background(), db.CreateBookingParams{
|
||||
TenantID: demoTenantID,
|
||||
ServiceID: service,
|
||||
ClassSessionID: session,
|
||||
CustomerName: "Svc Customer",
|
||||
CustomerEmail: "[email protected]",
|
||||
StartsAt: now.Add(-5 * time.Hour),
|
||||
EndsAt: now.Add(-4 * time.Hour),
|
||||
Status: status,
|
||||
Reference: ref,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("seed booking: %v", err)
|
||||
}
|
||||
}
|
||||
seed(&serviceID, nil, "confirmed", "BK-SVC-1")
|
||||
seed(&serviceID, nil, "cancelled", "BK-SVC-2")
|
||||
seed(nil, &sessionID, "confirmed", "BK-SVC-3")
|
||||
|
||||
var buf bytes.Buffer
|
||||
svc := newTestService(repo)
|
||||
if err := svc.WriteServiceReportCSV(context.Background(), demoPrincipal, &buf, 30); err != nil {
|
||||
t.Fatalf("service report csv: %v", err)
|
||||
}
|
||||
records := readCSV(t, &buf)
|
||||
if len(records) != 3 {
|
||||
t.Fatalf("expected header + service row + class row, got %d rows", len(records))
|
||||
}
|
||||
svcRow := records[1]
|
||||
if svcRow[0] != "Signature treatment" || svcRow[1] != "2" || svcRow[2] != "1" || svcRow[3] != "120000" {
|
||||
t.Fatalf("unexpected service row: %v", svcRow)
|
||||
}
|
||||
classRow := records[2]
|
||||
if classRow[0] != "class sessions" || classRow[1] != "1" {
|
||||
t.Fatalf("unexpected class row: %v", classRow)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user