fix edit requests
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// UserCancelBookingHandler allows an authenticated user to cancel a booking they own.
|
||||
@@ -726,8 +727,17 @@ func DeleteEditRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Use transaction to delete edit request and associated admin notification
|
||||
tx, err := db.DB.Begin(r.Context())
|
||||
if err != nil {
|
||||
log.Printf("Failed to start transaction: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(r.Context())
|
||||
|
||||
// Delete the edit request for this booking
|
||||
res, err := db.DB.Exec(r.Context(), `
|
||||
res, err := tx.Exec(r.Context(), `
|
||||
DELETE FROM booking_edit_requests
|
||||
WHERE booking_id = $1 AND requested_by = $2
|
||||
`, bookingID, userID)
|
||||
@@ -743,6 +753,23 @@ func DeleteEditRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the admin notification for this edit request
|
||||
_, err = tx.Exec(r.Context(), `
|
||||
DELETE FROM admin_notifications
|
||||
WHERE booking_id = $1 AND reason = 'edit_request' AND user_id = $2
|
||||
`, bookingID, userID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to delete admin notification for booking %s: %v", bookingID, err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit(r.Context()); err != nil {
|
||||
log.Printf("Failed to commit delete edit request: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -951,14 +978,14 @@ func AdminListEditRequestsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var origStartTime time.Time
|
||||
var bookingStatus string
|
||||
var userName string
|
||||
var newServicesJSON []byte
|
||||
var newServices []string
|
||||
|
||||
err := rows.Scan(
|
||||
&req.ID,
|
||||
&req.BookingID,
|
||||
&req.RequestedBy,
|
||||
&req.NewStartTime,
|
||||
&newServicesJSON,
|
||||
pq.Array(&newServices),
|
||||
&req.Notes,
|
||||
&req.HasOverrides,
|
||||
&req.UpdatedAt,
|
||||
@@ -971,12 +998,7 @@ func AdminListEditRequestsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Unmarshal JSON services
|
||||
if len(newServicesJSON) > 0 {
|
||||
if err := json.Unmarshal(newServicesJSON, &req.NewServices); err != nil {
|
||||
log.Printf("Failed to unmarshal new_services: %v", err)
|
||||
}
|
||||
}
|
||||
req.NewServices = newServices
|
||||
|
||||
req.Booking = &Booking{
|
||||
ID: req.BookingID,
|
||||
@@ -1021,14 +1043,14 @@ func AdminApproveEditRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the edit request
|
||||
var bookingID string
|
||||
var newStartTime *time.Time
|
||||
var newServicesJSON []byte
|
||||
var newServices []string
|
||||
var notes *string
|
||||
var hasOverrides bool
|
||||
err = tx.QueryRow(r.Context(), `
|
||||
SELECT booking_id, new_start_time, new_services, notes, has_overrides
|
||||
FROM booking_edit_requests
|
||||
WHERE id = $1
|
||||
`, requestID).Scan(&bookingID, &newStartTime, &newServicesJSON, ¬es, &hasOverrides)
|
||||
`, requestID).Scan(&bookingID, &newStartTime, pq.Array(&newServices), ¬es, &hasOverrides)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
http.Error(w, "Edit request not found", http.StatusNotFound)
|
||||
@@ -1039,16 +1061,6 @@ func AdminApproveEditRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Parse new_services
|
||||
var newServices []string
|
||||
if len(newServicesJSON) > 0 {
|
||||
if err := json.Unmarshal(newServicesJSON, &newServices); err != nil {
|
||||
log.Printf("Failed to unmarshal new_services: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If new_services provided and has_overrides is true, block with error
|
||||
if len(newServices) > 0 && hasOverrides {
|
||||
http.Error(w, "Cannot change services on a booking that has overrides. Please update services manually.", http.StatusForbidden)
|
||||
|
||||
Reference in New Issue
Block a user