Fix overrides

This commit is contained in:
2025-10-21 00:30:50 +01:00
parent ea0d042793
commit dec2dfb7fe
+60 -22
View File
@@ -80,9 +80,15 @@ type ProgressBookingRequest struct {
// ConfirmBookingRequest represents the request payload for confirming a booking // ConfirmBookingRequest represents the request payload for confirming a booking
type ConfirmBookingRequest struct { type ConfirmBookingRequest struct {
ServiceOverrides []ServiceOverride `json:"service_overrides,omitempty"`
Notes *string `json:"notes,omitempty"`
}
// ServiceOverride represents override values for a specific service in a booking
type ServiceOverride struct {
ServiceID string `json:"service_id" validate:"required"`
OverridePrice *float64 `json:"override_price,omitempty"` OverridePrice *float64 `json:"override_price,omitempty"`
OverrideDurationMinutes *int `json:"override_duration_minutes,omitempty"` OverrideDurationMinutes *int `json:"override_duration_minutes,omitempty"`
Notes *string `json:"notes,omitempty"`
} }
// DeleteBookingRequest represents the request payload for deleting a booking with payment // DeleteBookingRequest represents the request payload for deleting a booking with payment
@@ -594,13 +600,16 @@ func ConfirmBookingHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if req.OverridePrice != nil && *req.OverridePrice < 0 { // Validate override values
http.Error(w, "Override price cannot be negative", http.StatusBadRequest) for _, override := range req.ServiceOverrides {
return if override.OverridePrice != nil && *override.OverridePrice < 0 {
} http.Error(w, "Override price cannot be negative", http.StatusBadRequest)
if req.OverrideDurationMinutes != nil && *req.OverrideDurationMinutes <= 0 { return
http.Error(w, "Override duration must be positive", http.StatusBadRequest) }
return if override.OverrideDurationMinutes != nil && *override.OverrideDurationMinutes <= 0 {
http.Error(w, "Override duration must be positive", http.StatusBadRequest)
return
}
} }
tx, err := db.DB.Begin(r.Context()) tx, err := db.DB.Begin(r.Context())
@@ -640,29 +649,58 @@ func ConfirmBookingHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Booking not found or already confirmed", http.StatusNotFound) http.Error(w, "Booking not found or already confirmed", http.StatusNotFound)
return return
} }
log.Printf("Failed to confirm booking %s: %v", bookingID, err)
http.Error(w, "Internal server error", http.StatusInternalServerError) http.Error(w, "Internal server error", http.StatusInternalServerError)
return return
} }
// Update booking services with overrides if provided // Update service overrides individually
if req.OverridePrice != nil || req.OverrideDurationMinutes != nil { if len(req.ServiceOverrides) > 0 {
serviceQuery := ` // First, verify all service IDs belong to this booking
UPDATE booking_services serviceCheckQuery := `
SET override_price = COALESCE($1, override_price), SELECT COUNT(*) FROM booking_services
override_duration_minutes = COALESCE($2, override_duration_minutes) WHERE booking_id = $1 AND service_id = ANY($2)
WHERE booking_id = $3
` `
_, err := tx.Exec(r.Context(), serviceIDs := make([]string, len(req.ServiceOverrides))
serviceQuery, for i, override := range req.ServiceOverrides {
req.OverridePrice, serviceIDs[i] = override.ServiceID
req.OverrideDurationMinutes, }
bookingID,
) var count int
err = tx.QueryRow(r.Context(), serviceCheckQuery, bookingID, serviceIDs).Scan(&count)
if err != nil { if err != nil {
log.Printf("Failed to update service overrides for booking %s: %v", bookingID, err) log.Printf("Failed to verify services for booking %s: %v", bookingID, err)
http.Error(w, "Internal server error", http.StatusInternalServerError) http.Error(w, "Internal server error", http.StatusInternalServerError)
return return
} }
if count != len(req.ServiceOverrides) {
http.Error(w, "One or more service IDs do not belong to this booking", http.StatusBadRequest)
return
}
// Update each service override
serviceUpdateQuery := `
UPDATE booking_services
SET override_price = $1,
override_duration_minutes = $2
WHERE booking_id = $3 AND service_id = $4
`
for _, override := range req.ServiceOverrides {
_, err := tx.Exec(r.Context(),
serviceUpdateQuery,
override.OverridePrice,
override.OverrideDurationMinutes,
bookingID,
override.ServiceID,
)
if err != nil {
log.Printf("Failed to update service override for booking %s, service %s: %v",
bookingID, override.ServiceID, err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
} }
if err := tx.Commit(r.Context()); err != nil { if err := tx.Commit(r.Context()); err != nil {