feat: gestion manuelle des statuts de réservation véhicule
This commit is contained in:
@@ -132,32 +132,38 @@ func (h *ApplicationHandler) GetVehiclesManagementBookingsList(ctx context.Conte
|
||||
if v, ok := b.Data["administrator_unavailability"].(bool); !ok || !v {
|
||||
// Apply status filter
|
||||
if status != "" {
|
||||
bookingStatus := b.Status()
|
||||
statusInt := 0
|
||||
|
||||
if b.Deleted {
|
||||
statusInt = -2 // Use -2 for cancelled to distinguish from terminated
|
||||
if h.config.GetString("modules.vehicles.status_management") == "manual" {
|
||||
if b.ManualStatus != status {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
statusInt = bookingStatus
|
||||
}
|
||||
bookingStatus := b.Status()
|
||||
statusInt := 0
|
||||
|
||||
// Map status string to int
|
||||
var filterStatusInt int
|
||||
switch status {
|
||||
case "FORTHCOMING":
|
||||
filterStatusInt = 1
|
||||
case "ONGOING":
|
||||
filterStatusInt = 0
|
||||
case "TERMINATED":
|
||||
filterStatusInt = -1
|
||||
case "CANCELLED":
|
||||
filterStatusInt = -2
|
||||
default:
|
||||
filterStatusInt = 999 // Invalid status, won't match anything
|
||||
}
|
||||
if b.Deleted {
|
||||
statusInt = -2 // Use -2 for cancelled to distinguish from terminated
|
||||
} else {
|
||||
statusInt = bookingStatus
|
||||
}
|
||||
|
||||
if statusInt != filterStatusInt {
|
||||
continue
|
||||
// Map status string to int
|
||||
var filterStatusInt int
|
||||
switch status {
|
||||
case "FORTHCOMING":
|
||||
filterStatusInt = 1
|
||||
case "ONGOING":
|
||||
filterStatusInt = 0
|
||||
case "TERMINATED":
|
||||
filterStatusInt = -1
|
||||
case "CANCELLED":
|
||||
filterStatusInt = -2
|
||||
default:
|
||||
filterStatusInt = 999 // Invalid status, won't match anything
|
||||
}
|
||||
|
||||
if statusInt != filterStatusInt {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -596,3 +602,67 @@ func (h *ApplicationHandler) UpdateVehicle(ctx context.Context, vehicleID, name,
|
||||
return updateResp.Vehicle.Id, nil
|
||||
}
|
||||
|
||||
// getStatusOptions extracts status options from Viper config, handling both
|
||||
// Go defaults ([]map[string]any) and YAML-parsed ([]interface{}) types.
|
||||
func getStatusOptions(raw interface{}) []map[string]any {
|
||||
if options, ok := raw.([]map[string]any); ok {
|
||||
return options
|
||||
}
|
||||
if rawSlice, ok := raw.([]interface{}); ok {
|
||||
result := make([]map[string]any, 0, len(rawSlice))
|
||||
for _, item := range rawSlice {
|
||||
if opt, ok := item.(map[string]any); ok {
|
||||
result = append(result, opt)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) UpdateBookingStatus(ctx context.Context, bookingID, newStatus, comment, userID string, userClaims map[string]any, currentGroup any) error {
|
||||
group := currentGroup.(storage.Group)
|
||||
|
||||
// Validate that newStatus is a valid status option
|
||||
options := getStatusOptions(h.config.Get("modules.vehicles.status_options"))
|
||||
validStatus := false
|
||||
for _, opt := range options {
|
||||
if name, ok := opt["name"].(string); ok && name == newStatus {
|
||||
validStatus = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !validStatus {
|
||||
return fmt.Errorf("invalid status: %s", newStatus)
|
||||
}
|
||||
|
||||
booking, err := h.services.GetBooking(bookingID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get booking: %w", err)
|
||||
}
|
||||
|
||||
oldStatus := booking.ManualStatus
|
||||
|
||||
booking.ManualStatus = newStatus
|
||||
booking.StatusHistory = append(booking.StatusHistory, fleetsstorage.StatusHistoryEntry{
|
||||
FromStatus: oldStatus,
|
||||
ToStatus: newStatus,
|
||||
UserID: userID,
|
||||
UserName: fmt.Sprintf("%s %s", userClaims["first_name"], userClaims["last_name"]),
|
||||
GroupID: group.ID,
|
||||
GroupName: fmt.Sprintf("%s", group.Data["name"]),
|
||||
Date: time.Now(),
|
||||
Comment: comment,
|
||||
})
|
||||
|
||||
b, err := fleets.BookingFromStorageType(&booking)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to convert booking: %w", err)
|
||||
}
|
||||
|
||||
_, err = h.services.GRPC.Fleets.UpdateBooking(ctx, &fleets.UpdateBookingRequest{
|
||||
Booking: b,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user