Files management for bookings

This commit is contained in:
2022-11-01 17:06:12 +01:00
parent cb36a7c8dd
commit 72c9ca9635
12 changed files with 509 additions and 169 deletions

View File

@@ -10,6 +10,7 @@ import (
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
@@ -303,5 +304,8 @@ func (h ApplicationHandler) VehicleManagementBookingDisplay(w http.ResponseWrite
return
}
h.Renderer.VehicleManagementBookingDisplay(w, r, booking, booking.Vehicle, beneficiaryresp.Account.ToStorageType(), groupresp.Group.ToStorageType())
documents := h.filestorage.List(filestorage.PREFIX_BOOKINGS + "/" + bookingid)
file_types_map := h.config.GetStringMapString("storage.files.file_types")
h.Renderer.VehicleManagementBookingDisplay(w, r, booking, booking.Vehicle, beneficiaryresp.Account.ToStorageType(), groupresp.Group.ToStorageType(), documents, file_types_map)
}

View File

@@ -3,16 +3,20 @@ package application
import (
"context"
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
"git.coopgo.io/coopgo-platform/groups-management/storage"
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"github.com/coreos/go-oidc"
"github.com/google/uuid"
"github.com/gorilla/mux"
@@ -23,7 +27,9 @@ import (
func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var beneficiary any
var beneficiary mobilityaccountsstorage.Account
beneficiarydocuments := []filestorage.FileInfo{}
vehicles := []any{}
searched := false
@@ -65,6 +71,8 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
vehicles = append(vehicles, v)
}
}
beneficiarydocuments = h.filestorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + beneficiary.ID)
}
accounts, err := h.beneficiaries(r)
@@ -76,7 +84,10 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
sort.Sort(sorting.BeneficiariesByName(accounts))
h.Renderer.VehiclesSearch(w, r, accounts, searched, vehicles, beneficiary, r.FormValue("startdate"), r.FormValue("enddate"))
mandatory_documents := h.config.GetStringSlice("modules.fleets.booking_documents.mandatory")
file_types_map := h.config.GetStringMapString("storage.files.file_types")
h.Renderer.VehiclesSearch(w, r, accounts, searched, vehicles, beneficiary, r.FormValue("startdate"), r.FormValue("enddate"), mandatory_documents, file_types_map, beneficiarydocuments)
}
func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
@@ -111,7 +122,7 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
vehicleid := vars["vehicleid"]
beneficiaryid := vars["beneficiaryid"]
r.ParseForm()
r.ParseMultipartForm(10 * 1024 * 1024)
start := r.FormValue("startdate")
end := r.FormValue("enddate")
@@ -153,6 +164,42 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
Booking: booking,
}
fmt.Println(r.FormFile("doc-identity_proof"))
for _, v := range h.config.GetStringSlice("modules.fleets.booking_documents.mandatory") {
existing_file := r.FormValue("type-" + v)
if existing_file == "" {
file, header, err := r.FormFile("doc-" + v)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Document manquant : " + v))
return
}
defer file.Close()
fileid := uuid.NewString()
metadata := map[string]string{
"type": v,
"name": header.Filename,
}
if err := h.filestorage.Put(file, filestorage.PREFIX_BOOKINGS, fmt.Sprintf("%s/%s_%s", booking.Id, fileid, header.Filename), header.Size, metadata); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
} else {
path := strings.Split(existing_file, "/")
if err := h.filestorage.Copy(existing_file, fmt.Sprintf("%s/%s/%s", filestorage.PREFIX_BOOKINGS, booking.Id, path[len(path)-1])); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
_, err = h.services.GRPC.Fleets.CreateBooking(context.TODO(), request)
if err != nil {
fmt.Println(err)
@@ -202,7 +249,10 @@ func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http
return
}
h.Renderer.VehicleBookingDisplay(w, r, booking, booking.Vehicle, beneficiaryresp.Account.ToStorageType(), groupresp.Group.ToStorageType())
documents := h.filestorage.List(filestorage.PREFIX_BOOKINGS + "/" + bookingid)
file_types_map := h.config.GetStringMapString("storage.files.file_types")
h.Renderer.VehicleBookingDisplay(w, r, booking, booking.Vehicle, beneficiaryresp.Account.ToStorageType(), groupresp.Group.ToStorageType(), documents, file_types_map)
}
func (h ApplicationHandler) VehiclesBookingsList(w http.ResponseWriter, r *http.Request) {
@@ -222,3 +272,28 @@ func (h ApplicationHandler) VehiclesBookingsList(w http.ResponseWriter, r *http.
h.Renderer.VehicleBookingsList(w, r, bookings)
}
func (h *ApplicationHandler) BookingDocumentDownload(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookingid := vars["bookingid"]
document := vars["document"]
fmt.Println(fmt.Sprintf("%s/%s", bookingid, document))
file, info, err := h.filestorage.Get(filestorage.PREFIX_BOOKINGS, fmt.Sprintf("%s/%s", bookingid, document))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", info.ContentType)
if _, err = io.Copy(w, file); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", bookingid), http.StatusFound)
}