feat(backend): update notifications handler
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -27,6 +27,7 @@ import (
|
||||
|
||||
"crussell/db"
|
||||
"crussell/mw"
|
||||
"crussell/testutils/fixtures"
|
||||
"crussell/testutils/jwt"
|
||||
"crussell/testutils/testdb"
|
||||
|
||||
@@ -280,14 +281,6 @@ func TestNotifications_ListPagination(t *testing.T) {
|
||||
if resp.Total != 25 {
|
||||
t.Errorf("expected total 25, got %d", resp.Total)
|
||||
}
|
||||
|
||||
if resp.Page != 1 {
|
||||
t.Errorf("expected page 1, got %d", resp.Page)
|
||||
}
|
||||
|
||||
if resp.PerPage != 10 {
|
||||
t.Errorf("expected per_page 10, got %d", resp.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotifications_ListExcludesAcknowledged tests that acknowledged notifications are not returned
|
||||
@@ -451,13 +444,13 @@ func TestNotifications_AcknowledgeInvalidID(t *testing.T) {
|
||||
handler := http.HandlerFunc(AcknowledgeNotification)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
id string
|
||||
expectCode int
|
||||
name string
|
||||
id string
|
||||
expectCode int
|
||||
}{
|
||||
{"non_numeric_id", "abc", http.StatusBadRequest}, // Invalid format
|
||||
{"negative_id", "-1", http.StatusBadRequest}, // Invalid (non-positive)
|
||||
{"zero_id", "0", http.StatusBadRequest}, // Invalid (non-positive)
|
||||
{"negative_id", "-1", http.StatusBadRequest}, // Invalid (non-positive)
|
||||
{"zero_id", "0", http.StatusBadRequest}, // Invalid (non-positive)
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -584,3 +577,176 @@ func TestNotifications_WithBookingReference(t *testing.T) {
|
||||
|
||||
// Ensure test compilation - import pgxpool to avoid unused import
|
||||
var _ = func() *pgxpool.Pool { return nil }
|
||||
|
||||
// =============================================================================
|
||||
// AcknowledgePendingBookingNotification Tests
|
||||
// =============================================================================
|
||||
|
||||
func TestAcknowledgePendingBookingNotification_Success(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
userID, err := fixtures.CreateTestUser(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteUser(db.DB, userID) })
|
||||
|
||||
serviceID, err := fixtures.CreateTestService(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteService(db.DB, serviceID) })
|
||||
|
||||
bookingID, err := fixtures.CreateTestBookingAtTime(db.DB, userID, serviceID, time.Now().Add(24*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteBooking(db.DB, bookingID) })
|
||||
|
||||
// Create a pending notification for this booking
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO admin_notifications (booking_id, reason, acknowledged_at)
|
||||
VALUES ($1, 'pending_booking', NULL)
|
||||
`, bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create notification: %v", err)
|
||||
}
|
||||
|
||||
tx, err := db.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to begin tx: %v", err)
|
||||
}
|
||||
|
||||
err = AcknowledgePendingBookingNotification(tx, ctx, bookingID)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
t.Fatalf("AcknowledgePendingBookingNotification failed: %v", err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
t.Fatalf("failed to commit tx: %v", err)
|
||||
}
|
||||
|
||||
// Verify notification was acknowledged
|
||||
var acknowledgedAt *time.Time
|
||||
err = db.DB.QueryRow(ctx,
|
||||
"SELECT acknowledged_at FROM admin_notifications WHERE booking_id = $1 AND reason = 'pending_booking'",
|
||||
bookingID).Scan(&acknowledgedAt)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query notification: %v", err)
|
||||
}
|
||||
if acknowledgedAt == nil {
|
||||
t.Error("expected acknowledged_at to be set, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcknowledgePendingBookingNotification_Idempotent(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
userID, err := fixtures.CreateTestUser(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteUser(db.DB, userID) })
|
||||
|
||||
serviceID, err := fixtures.CreateTestService(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteService(db.DB, serviceID) })
|
||||
|
||||
bookingID, err := fixtures.CreateTestBookingAtTime(db.DB, userID, serviceID, time.Now().Add(24*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteBooking(db.DB, bookingID) })
|
||||
|
||||
// Notification already acknowledged
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
INSERT INTO admin_notifications (booking_id, reason, acknowledged_at)
|
||||
VALUES ($1, 'pending_booking', NOW())
|
||||
`, bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create acknowledged notification: %v", err)
|
||||
}
|
||||
|
||||
tx, err := db.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to begin tx: %v", err)
|
||||
}
|
||||
|
||||
// Calling again on already acknowledged should not error
|
||||
err = AcknowledgePendingBookingNotification(tx, ctx, bookingID)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
t.Fatalf("AcknowledgePendingBookingNotification should not error when already acknowledged: %v", err)
|
||||
}
|
||||
tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func TestAcknowledgePendingBookingNotification_NoNotification(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
userID, err := fixtures.CreateTestUser(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteUser(db.DB, userID) })
|
||||
|
||||
serviceID, err := fixtures.CreateTestService(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteService(db.DB, serviceID) })
|
||||
|
||||
bookingID, err := fixtures.CreateTestBookingAtTime(db.DB, userID, serviceID, time.Now().Add(24*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteBooking(db.DB, bookingID) })
|
||||
|
||||
tx, err := db.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to begin tx: %v", err)
|
||||
}
|
||||
|
||||
// No notification exists - should not error
|
||||
err = AcknowledgePendingBookingNotification(tx, ctx, bookingID)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
t.Fatalf("AcknowledgePendingBookingNotification should not error when no notification exists: %v", err)
|
||||
}
|
||||
tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func TestAcknowledgePendingBookingNotification_NonTxCaller(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
ctx := context.Background()
|
||||
userID, err := fixtures.CreateTestUser(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteUser(db.DB, userID) })
|
||||
|
||||
serviceID, err := fixtures.CreateTestService(db.DB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteService(db.DB, serviceID) })
|
||||
|
||||
bookingID, err := fixtures.CreateTestBookingAtTime(db.DB, userID, serviceID, time.Now().Add(24*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { fixtures.DeleteBooking(db.DB, bookingID) })
|
||||
|
||||
// Call with a plain struct (not a tx) - should log warning and not error
|
||||
err = AcknowledgePendingBookingNotification("not-a-tx", ctx, bookingID)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error for non-tx caller, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user