lot of new functionalities
This commit is contained in:
161
servers/web/application/vehicles.go
Normal file
161
servers/web/application/vehicles.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/core/utils/identification"
|
||||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *Handler) VehiclesSearchHTTPHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
// Extract parameters
|
||||
beneficiaryID := r.FormValue("beneficiaryid")
|
||||
startDate := r.FormValue("startdate")
|
||||
endDate := r.FormValue("enddate")
|
||||
vehicleType := r.FormValue("type")
|
||||
automatic := r.FormValue("automatic") == "on"
|
||||
|
||||
// Call business logic
|
||||
result, err := h.applicationHandler.SearchVehicles(r.Context(), beneficiaryID, startDate, endDate, vehicleType, automatic)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error searching vehicles")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render response
|
||||
h.renderer.VehiclesSearch(w, r, result.Beneficiaries, result.Searched, result.Vehicles, result.Beneficiary,
|
||||
result.StartDate, result.EndDate, result.MandatoryDocuments, result.FileTypesMap,
|
||||
result.BeneficiaryDocuments, result.VehicleType, result.Automatic, result.VehicleTypes, result.Groups)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) BookVehicleHTTPHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Extract URL parameters
|
||||
vars := mux.Vars(r)
|
||||
vehicleID := vars["vehicleid"]
|
||||
beneficiaryID := vars["beneficiaryid"]
|
||||
|
||||
// Extract user context
|
||||
currentUserToken := r.Context().Value(identification.IdtokenKey).(*oidc.IDToken)
|
||||
currentUserClaims := r.Context().Value(identification.ClaimsKey).(map[string]any)
|
||||
currentGroup := r.Context().Value(identification.GroupKey)
|
||||
|
||||
// Parse multipart form
|
||||
if err := r.ParseMultipartForm(100 * 1024 * 1024); err != nil {
|
||||
log.Error().Err(err).Msg("error parsing multipart form")
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract form data
|
||||
startDate := r.FormValue("startdate")
|
||||
endDate := r.FormValue("enddate")
|
||||
|
||||
// Extract documents
|
||||
documents := make(map[string]io.Reader)
|
||||
documentHeaders := make(map[string]string)
|
||||
existingDocs := make(map[string]string)
|
||||
|
||||
// Get mandatory document types from config
|
||||
mandatoryDocTypes := h.cfg.GetStringSlice("modules.fleets.booking_documents.mandatory")
|
||||
|
||||
for _, docType := range mandatoryDocTypes {
|
||||
existingFile := r.FormValue("type-" + docType)
|
||||
if existingFile != "" {
|
||||
existingDocs[docType] = existingFile
|
||||
} else {
|
||||
file, header, err := r.FormFile("doc-" + docType)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("missing required document: " + docType)
|
||||
http.Error(w, "Document manquant : "+docType, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
documents[docType] = file
|
||||
documentHeaders[docType] = header.Filename
|
||||
}
|
||||
}
|
||||
|
||||
// Call business logic
|
||||
result, err := h.applicationHandler.BookVehicle(r.Context(), vehicleID, beneficiaryID, startDate, endDate,
|
||||
documents, documentHeaders, existingDocs, currentUserToken.Subject, currentUserClaims, currentGroup)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error booking vehicle")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to booking details
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", result.BookingID), http.StatusFound)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) VehicleBookingDisplayHTTPHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
bookingID := vars["bookingid"]
|
||||
|
||||
result, err := h.applicationHandler.GetVehicleBookingDetails(r.Context(), bookingID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error getting booking details")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
h.renderer.VehicleBookingDisplay(w, r, result.Booking, result.Vehicle, result.Beneficiary,
|
||||
result.Group, result.Documents, result.FileTypesMap)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) VehiclesBookingsListHTTPHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Get group from context
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
|
||||
result, err := h.applicationHandler.GetVehicleBookingsList(r.Context(), group.ID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error getting bookings list")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
h.renderer.VehicleBookingsList(w, r, result.Bookings, result.VehiclesMap, result.GroupsMap)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) BookingDocumentDownloadHTTPHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
bookingID := vars["bookingid"]
|
||||
document := vars["document"]
|
||||
|
||||
fileReader, contentType, err := h.applicationHandler.GetBookingDocument(r.Context(), bookingID, document)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error getting booking document")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
if _, err = io.Copy(w, fileReader); err != nil {
|
||||
log.Error().Err(err).Msg("error writing document to response")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user