feat(backend): add done-for-day summary with DailySummary, week aggregation, exceptional hours support
Introduce DoneForDay state and DailySummary struct in current-next endpoint. Adds computeAggregateSummary for daily/weekly revenue, tips, duration, customer stats, and new booking services. Adds isDayOpen, findWeekSummaryRange, and getClosingTime with exceptional hours lookup. Scopes all current/next queries to today's date range. Ultraworked with Sisyphus Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -10,6 +10,9 @@ package admin
|
||||
// - GetTodayAppointmentsHandler: GET /api/admin/today/appointments - Get today's bookings
|
||||
// - GetPendingApprovalsHandler: GET /api/admin/today/pending-approvals - Get pending bookings
|
||||
// - Auto-status transitions: Silent background updates on GET requests
|
||||
// - Closed day summary: done_for_day=true, summary_scope="week", total_bookings excludes cancelled
|
||||
// - Week summary on tomorrow-closed: day summary + week_summary when tomorrow is closed
|
||||
// - Exceptional hours: Exceptional closed day via groups + applications overrides defaults
|
||||
//
|
||||
// Authentication: All endpoints require admin role (403 for non-admins).
|
||||
//
|
||||
@@ -107,10 +110,12 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
func TestAdminToday_CurrentNext_ClosingTime(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
// Seed working hours for today (query uses current weekday)
|
||||
// Seed working hours for today (DB uses 0=Monday, 6=Sunday)
|
||||
todayWeekday := int(time.Now().Weekday())
|
||||
if todayWeekday == 0 {
|
||||
todayWeekday = 7
|
||||
todayWeekday = 6
|
||||
} else {
|
||||
todayWeekday -= 1
|
||||
}
|
||||
_, err := db.DB.Exec(context.Background(), `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
@@ -596,6 +601,353 @@ func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Week Summary Tests — covering "Closed today" and "Tomorrow is closed" states
|
||||
// =============================================================================
|
||||
|
||||
// TestAdminToday_ClosedDay_Summary verifies that on a closed day:
|
||||
// - done_for_day = true
|
||||
// - summary_scope = "week" (closed day summary)
|
||||
// - total_bookings counts non-cancelled bookings, excluding cancelled/no_show
|
||||
// - The range includes bookings from both the closed day and prior open days
|
||||
func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
yesterdayStart := todayStart.AddDate(0, 0, -1)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(ctx, `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
// Seed working hours: today is CLOSED, all other days OPEN
|
||||
todayWeekday := int(now.Weekday())
|
||||
if todayWeekday == 0 {
|
||||
todayWeekday = 6
|
||||
} else {
|
||||
todayWeekday -= 1
|
||||
}
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '00:00', '00:00', false)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '00:00', end_time = '00:00', is_open = false
|
||||
`, todayWeekday)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed today as closed: %v", err)
|
||||
}
|
||||
// Mark all other weekdays as open
|
||||
for wd := 0; wd <= 6; wd++ {
|
||||
if wd != todayWeekday {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '17:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '17:00', is_open = true
|
||||
`, wd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed weekday %d as open: %v", wd, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create bookings:
|
||||
// - Yesterday: 1 completed
|
||||
// - Today: 2 completed, 1 client_cancelled, 1 no_show
|
||||
// total_bookings should count: 1 (yesterday) + 2 (today completed) = 3
|
||||
// NOT counting: client_cancelled, no_show
|
||||
yesterdayBookings := []struct {
|
||||
startTime time.Time
|
||||
status string
|
||||
}{
|
||||
{yesterdayStart.Add(9 * time.Hour), "completed"},
|
||||
}
|
||||
todayBookings := []struct {
|
||||
startTime time.Time
|
||||
status string
|
||||
}{
|
||||
{todayStart.Add(9 * time.Hour), "completed"},
|
||||
{todayStart.Add(10 * time.Hour), "completed"},
|
||||
{todayStart.Add(11 * time.Hour), "client_cancelled"},
|
||||
{todayStart.Add(12 * time.Hour), "no_show"},
|
||||
}
|
||||
|
||||
for _, b := range yesterdayBookings {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
`, userID, b.startTime, b.status)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create yesterday %s booking: %v", b.status, err)
|
||||
}
|
||||
}
|
||||
for _, b := range todayBookings {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
`, userID, b.startTime, b.status)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create today %s booking: %v", b.status, err)
|
||||
}
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var response today.CurrentNextResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if response.DoneForDay == nil || !*response.DoneForDay {
|
||||
t.Error("expected done_for_day = true on a closed day")
|
||||
}
|
||||
if response.Summary == nil {
|
||||
t.Fatal("expected summary on a closed day")
|
||||
}
|
||||
if response.Summary.SummaryScope != "week" {
|
||||
t.Errorf("expected summary_scope 'week' for closed day, got '%s'", response.Summary.SummaryScope)
|
||||
}
|
||||
if response.Summary.TotalBookings != 3 {
|
||||
t.Errorf("expected total_bookings = 3 (1 yesterday + 2 today completed, excluding cancelled/no_show), got %d", response.Summary.TotalBookings)
|
||||
}
|
||||
if response.Summary.CustomersServed != 1 {
|
||||
t.Errorf("expected customers_served = 1 (all non-cancelled bookings are by same distinct user), got %d", response.Summary.CustomersServed)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminToday_WeekSummary_TomorrowClosed verifies that when today is open
|
||||
// and all current+next appointments are done, but tomorrow is closed:
|
||||
// - summary_scope = "day" (today's summary)
|
||||
// - week_summary is present with summary_scope = "week"
|
||||
func TestAdminToday_WeekSummary_TomorrowClosed(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(ctx, `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
// Compute weekdays
|
||||
sundayGo := int(time.Sunday)
|
||||
todayWeekday := int(now.Weekday())
|
||||
if todayWeekday == 0 {
|
||||
todayWeekday = 6
|
||||
} else {
|
||||
todayWeekday -= 1
|
||||
}
|
||||
tomorrowWeekday := (todayWeekday + 1) % 7
|
||||
|
||||
// Mark today as OPEN, tomorrow as CLOSED
|
||||
for wd := 0; wd <= 6; wd++ {
|
||||
isOpen := true
|
||||
startTime := "09:00"
|
||||
endTime := "17:00"
|
||||
if wd == tomorrowWeekday {
|
||||
isOpen = false
|
||||
startTime = "00:00"
|
||||
endTime = "00:00"
|
||||
}
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = $2, end_time = $3, is_open = $4
|
||||
`, wd, startTime, endTime, isOpen)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed working_hours weekday %d: %v", wd, err)
|
||||
}
|
||||
}
|
||||
_ = sundayGo // unused but kept for clarity
|
||||
|
||||
// Create a completed booking for today (so we're done-for-day but today is open)
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, 'completed', NOW())
|
||||
`, userID, todayStart.Add(9*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create completed booking: %v", err)
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var response today.CurrentNextResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if response.DoneForDay == nil || !*response.DoneForDay {
|
||||
t.Error("expected done_for_day = true (no upcoming bookings)")
|
||||
}
|
||||
if response.Summary == nil {
|
||||
t.Fatal("expected daily summary")
|
||||
}
|
||||
if response.Summary.SummaryScope != "day" {
|
||||
t.Errorf("expected summary_scope 'day' for open day, got '%s'", response.Summary.SummaryScope)
|
||||
}
|
||||
if response.WeekSummary == nil {
|
||||
t.Fatal("expected week_summary when tomorrow is closed")
|
||||
}
|
||||
if response.WeekSummary.SummaryScope != "week" {
|
||||
t.Errorf("expected week_summary summary_scope 'week', got '%s'", response.WeekSummary.SummaryScope)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminToday_ExceptionalHours_ClosedDay verifies that exceptional hours
|
||||
// (via exceptional_working_hours + groups + applications) correctly make today
|
||||
// a closed day, even when default working_hours says today is open.
|
||||
// This tests the column name fix: monday_week_start → week_start.
|
||||
func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
|
||||
// Compute today's weekday (our system: 0=Monday, 6=Sunday)
|
||||
todayWeekday := int(now.Weekday())
|
||||
if todayWeekday == 0 {
|
||||
todayWeekday = 6
|
||||
} else {
|
||||
todayWeekday -= 1
|
||||
}
|
||||
|
||||
// Seed DEFAULT working_hours: today is OPEN (this should be overridden by exceptional hours)
|
||||
_, err := db.DB.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '17:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '17:00', is_open = true
|
||||
`, todayWeekday)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed default working_hours: %v", err)
|
||||
}
|
||||
|
||||
// Make all other weekdays open too
|
||||
for wd := 0; wd <= 6; wd++ {
|
||||
if wd != todayWeekday {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '17:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '17:00', is_open = true
|
||||
`, wd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed default working_hours weekday %d: %v", wd, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now seed EXCEPTIONAL hours making today CLOSED.
|
||||
// Need: group → hours → application with week_start = Monday of this week
|
||||
weekday := now.Weekday()
|
||||
daysSinceMonday := int(weekday) - 1
|
||||
if daysSinceMonday < 0 {
|
||||
daysSinceMonday = 6
|
||||
}
|
||||
monday := now.AddDate(0, 0, -daysSinceMonday)
|
||||
mondayStr := monday.Format("2006-01-02")
|
||||
|
||||
var groupID int
|
||||
err = db.DB.QueryRow(ctx, `
|
||||
INSERT INTO exceptional_working_hours_groups (name, description)
|
||||
VALUES ('Test Closure', 'Exceptional closure for test')
|
||||
RETURNING id
|
||||
`).Scan(&groupID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create exceptional hours group: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO exceptional_working_hours (group_id, weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, $2, '00:00', '00:00', false)
|
||||
`, groupID, todayWeekday)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed exceptional hours: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO exceptional_group_applications (group_id, week_start)
|
||||
VALUES ($1, $2::date)
|
||||
`, groupID, mondayStr)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed exceptional group application: %v", err)
|
||||
}
|
||||
|
||||
// Create a completed booking on today (to populate summary)
|
||||
var userID string
|
||||
err = db.DB.QueryRow(ctx, `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, 'completed', NOW())
|
||||
`, userID, todayStart.Add(9*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
|
||||
// Call the handler — with exceptional hours making today closed,
|
||||
// it should use the closed-day branch (summary_scope = "week")
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var response today.CurrentNextResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if response.DoneForDay == nil || !*response.DoneForDay {
|
||||
t.Error("expected done_for_day = true (exceptional closed day)")
|
||||
}
|
||||
if response.Summary == nil {
|
||||
t.Fatal("expected summary on exceptional closed day")
|
||||
}
|
||||
// summary_scope should be "week" because today is a closed day via exceptional hours
|
||||
if response.Summary.SummaryScope != "week" {
|
||||
t.Errorf("expected summary_scope 'week' for exceptional closed day, got '%s'", response.Summary.SummaryScope)
|
||||
}
|
||||
if response.ClosingTime != nil {
|
||||
t.Errorf("expected no closing_time on closed day, got '%s'", *response.ClosingTime)
|
||||
}
|
||||
if response.Summary.TotalBookings != 1 {
|
||||
t.Errorf("expected total_bookings = 1 (completed booking), got %d", response.Summary.TotalBookings)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminNotifications_List is skipped (WIP) - tests that an admin
|
||||
// can list all their notifications.
|
||||
func TestAdminNotifications_List(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user