diff --git a/handlers/application/vehicles-management.go b/handlers/application/vehicles-management.go index 2e71e06..b61f436 100644 --- a/handlers/application/vehicles-management.go +++ b/handlers/application/vehicles-management.go @@ -45,7 +45,9 @@ func (h *ApplicationHandler) VehiclesManagementOverview(w http.ResponseWriter, r vehicleBookings := []fleetsstorage.Booking{} for _, b := range v.Bookings { if b.Status() != fleetsstorage.StatusOld { - bookings = append(bookings, b) + if deleted, ok := b.Data["Deleted"].(bool); !ok && !deleted { + bookings = append(bookings, b) + } } if b.Unavailableto.After(time.Now()) { vehicleBookings = append(vehicleBookings, b) @@ -505,6 +507,63 @@ func (h *ApplicationHandler) UnbookingVehicle(w http.ResponseWriter, r *http.Req w.WriteHeader(http.StatusInternalServerError) return } + now := time.Now() + date := now.Format("2006-01-02") + unavailableto, _ := time.Parse("2006-01-02", date) + + current_group, err := h.currentGroup(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + current_user_token, current_user_claims, err := h.currentUser(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + booked_by_id := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["user"].GetStructValue().Fields["id"].GetStringValue() + booked_by_name := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["user"].GetStructValue().Fields["display_name"].GetStringValue() + booked_by_email := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["user"].GetStructValue().Fields["email"].GetStringValue() + booked_by_group_id := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["group"].GetStructValue().Fields["id"].GetStringValue() + booked_by_group_name := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["group"].GetStructValue().Fields["name"].GetStringValue() + + data := map[string]any{ + "booked_by": map[string]any{ + "user": map[string]any{ + "id": booked_by_id, + "display_name": booked_by_name, + "email": booked_by_email, + }, + "group": map[string]any{ + "id": booked_by_group_id, + "name": booked_by_group_name, + }, + }, + "unbooked_by": map[string]any{ + "user": map[string]any{ + "id": current_user_token.Subject, + "display_name": current_user_claims["first_name"].(string) + " " + current_user_claims["last_name"].(string), + "email": current_user_claims["email"], + }, + "group": map[string]any{ + "id": current_group.ID, + "name": current_group.Data["name"], + }, + }, + "Deleted": true, + "motif": r.FormValue("motif"), + } + + datapb, err := structpb.NewStruct(data) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } if r.Method == "POST" { @@ -516,9 +575,8 @@ func (h *ApplicationHandler) UnbookingVehicle(w http.ResponseWriter, r *http.Req Startdate: resp.Booking.Startdate, Enddate: resp.Booking.Enddate, Unavailablefrom: resp.Booking.Unavailablefrom, - Unavailableto: timestamppb.New(time.Now().Add(-48 * time.Hour)), - Data: resp.Booking.Data, - Deleted: true, + Unavailableto: timestamppb.New(unavailableto), + Data: datapb, }, }