edit handlers and renderer for delete a booking vehicle

This commit is contained in:
Nicolas CARON 2023-05-05 15:14:56 +02:00
parent 8f20a12bc5
commit 31e5eadc73
2 changed files with 64 additions and 142 deletions

View File

@ -494,8 +494,11 @@ func (h ApplicationHandler) VehiclesFleetMakeUnavailable(w http.ResponseWriter,
func (h *ApplicationHandler) UnbookingVehicle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookingid := vars["bookingid"]
request := &fleets.GetBookingRequest{
Bookingid: bookingid,
}
booking, err := h.services.GetBooking(bookingid)
resp, err := h.services.GRPC.Fleets.GetBooking(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
@ -503,106 +506,31 @@ func (h ApplicationHandler) VehiclesFleetMakeUnavailable(w http.ResponseWriter,
}
if r.Method == "POST" {
r.ParseForm()
newbooking, _ := fleets.BookingFromStorageType(&booking)
startdate := r.FormValue("startdate")
if startdate != "" {
newstartdate, _ := time.Parse("2006-01-02", startdate)
newbooking.Startdate = timestamppb.New(newstartdate)
if newstartdate.Before(newbooking.Unavailablefrom.AsTime()) {
newbooking.Unavailablefrom = timestamppb.New(newstartdate)
}
}
enddate := r.FormValue("enddate")
if enddate != "" {
newenddate, _ := time.Parse("2006-01-02", enddate)
newbooking.Enddate = timestamppb.New(newenddate)
if newenddate.After(newbooking.Unavailableto.AsTime()) || newenddate.Equal(newbooking.Unavailableto.AsTime()) {
newbooking.Unavailableto = timestamppb.New(newenddate.Add(24 * time.Hour))
}
}
unavailablefrom := r.FormValue("unavailablefrom")
if unavailablefrom != "" {
newunavailablefrom, _ := time.Parse("2006-01-02", unavailablefrom)
newbooking.Unavailablefrom = timestamppb.New(newunavailablefrom)
}
unavailableto := r.FormValue("unavailableto")
if unavailableto != "" {
newunavailableto, _ := time.Parse("2006-01-02", unavailableto)
newbooking.Unavailableto = timestamppb.New(newunavailableto)
}
request := &fleets.UpdateBookingRequest{
Booking: newbooking,
Booking: &fleets.Booking{
Id: resp.Booking.Id,
Vehicleid: resp.Booking.Vehicleid,
Driver: resp.Booking.Driver,
Startdate: resp.Booking.Startdate,
Enddate: resp.Booking.Enddate,
Unavailablefrom: resp.Booking.Unavailablefrom,
Unavailableto: resp.Booking.Unavailableto,
Data: resp.Booking.Data,
Deleted: true,
},
}
_, err := h.services.GRPC.Fleets.UpdateBooking(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
booking = newbooking.ToStorageType()
}
beneficiary := mobilityaccountsstorage.Account{}
if booking.Driver != "" {
beneficiaryrequest := &mobilityaccounts.GetAccountRequest{
Id: booking.Driver,
}
beneficiaryresp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), beneficiaryrequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
http.Redirect(w, r, "/app/vehicles-management/", http.StatusFound)
return
}
beneficiary = beneficiaryresp.Account.ToStorageType()
}
grouprequest := &groupsmanagement.GetGroupRequest{
Id: booking.Vehicle.Administrators[0],
}
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), grouprequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
alternativerequest := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
Types: []string{booking.Vehicle.Type},
Administrators: booking.Vehicle.Administrators,
AvailabilityFrom: timestamppb.New(booking.Startdate),
AvailabilityTo: timestamppb.New(booking.Enddate.Add(24 * time.Hour)),
}
alternativeresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), alternativerequest)
if err != nil {
fmt.Println(err)
}
alternatives := []any{}
for _, a := range alternativeresp.Vehicles {
alternatives = append(alternatives, a.ToStorageType())
}
documents := h.filestorage.List(filestorage.PREFIX_BOOKINGS + "/" + bookingid)
file_types_map := h.config.GetStringMapString("storage.files.file_types")
h.Renderer.UnbookingVehicle(w, r, booking, booking.Vehicle, beneficiary, groupresp.Group.ToStorageType(), documents, file_types_map, alternatives)
h.Renderer.UnbookingVehicle(w, r, bookingid)
}

View File

@ -79,19 +79,13 @@ func (renderer *Renderer) VehicleManagementBookingDisplay(w http.ResponseWriter,
renderer.Render("vehicles search", w, r, files, state)
}
func (renderer *Renderer) UnbookingVehicle(w http.ResponseWriter, r *http.Request, booking any, vehicle any, beneficiary any, group any, documents []filestorage.FileInfo, file_types_map map[string]string, alternative_vehicles []any) {
func (renderer *Renderer) UnbookingVehicle(w http.ResponseWriter, r *http.Request, booking string) {
files := renderer.ThemeConfig.GetStringSlice("views.vehicles_management.delete_booking.files")
state := NewState(r, renderer.ThemeConfig, vehiclesmanagementMenu)
state.ViewState = map[string]any{
"booking": booking,
"vehicle": vehicle,
"beneficiary": beneficiary,
"group": group,
"documents": documents,
"file_types_map": file_types_map,
"alternative_vehicles": alternative_vehicles,
}
renderer.Render("vehicles search", w, r, files, state)
renderer.Render("vehicule unbooking", w, r, files, state)
}