Improve vehicles management

This commit is contained in:
2022-11-07 01:24:16 +01:00
parent dc53589e7d
commit 58f5a23d1d
15 changed files with 261 additions and 64 deletions

View File

@@ -59,15 +59,11 @@ func (h APIHandler) CacheExport(w http.ResponseWriter, r *http.Request) {
//fmt.Println(data)
for _, v := range data {
fmt.Println(v)
fm := map[string]any{}
flatten("", v.(map[string]any), fm)
fmt.Println(fm)
flatmaps = append(flatmaps, fm)
}
fmt.Println(flatmaps)
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=export-%s.csv", cacheid))
c := csv.NewWriter(w)

View File

@@ -14,6 +14,7 @@ import (
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"github.com/google/uuid"
"github.com/gorilla/mux"
"google.golang.org/protobuf/types/known/structpb"
@@ -129,28 +130,35 @@ func (h *ApplicationHandler) AdministrationGroupDisplay(w http.ResponseWriter, r
return
}
members, err := h.members()
// members, err := h.members()
// if err != nil {
// if err != nil {
// fmt.Println(err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// }
// groupmembers := []any{}
// admins := []any{}
// for _, m := range members {
// mm := m.ToStorageType()
// for _, g := range mm.Data["groups"].([]any) {
// if g.(string) == groupid {
// groupmembers = append(groupmembers, mm)
// }
// if g.(string) == groupid+":admin" {
// admins = append(admins, mm)
// }
// }
// }
groupmembers, admins, err := h.groupmembers(groupid)
if err != nil {
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
groupmembers := []any{}
admins := []any{}
for _, m := range members {
mm := m.ToStorageType()
for _, g := range mm.Data["groups"].([]any) {
if g.(string) == groupid {
groupmembers = append(groupmembers, mm)
}
if g.(string) == groupid+":admin" {
admins = append(admins, mm)
}
}
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.AdministrationGroupDisplay(w, r, resp.Group.ToStorageType(), groupmembers, admins)
@@ -332,3 +340,30 @@ func (h *ApplicationHandler) members() ([]*accounts.Account, error) {
return resp.Accounts, nil
}
func (h *ApplicationHandler) groupmembers(groupid string) (groupmembers []mobilityaccountsstorage.Account, admins []mobilityaccountsstorage.Account, err error) {
members, err := h.members()
if err != nil {
if err != nil {
fmt.Println(err)
return nil, nil, err
}
}
groupmembers = []mobilityaccountsstorage.Account{}
admins = []mobilityaccountsstorage.Account{}
for _, m := range members {
mm := m.ToStorageType()
for _, g := range mm.Data["groups"].([]any) {
if g.(string) == groupid {
groupmembers = append(groupmembers, mm)
}
if g.(string) == groupid+":admin" {
admins = append(admins, mm)
}
}
}
return groupmembers, admins, err
}

View File

@@ -89,8 +89,6 @@ func (h *ApplicationHandler) AgendaCreateEvent(w http.ResponseWriter, r *http.Re
return
}
fmt.Println(eventForm)
data, _ := structpb.NewStruct(map[string]any{
"address": eventForm.Address,
})

View File

@@ -66,7 +66,6 @@ func (h *ApplicationHandler) BeneficiaryCreate(w http.ResponseWriter, r *http.Re
}
group := g.(storage.Group)
fmt.Println(group)
if r.Method == "POST" {

View File

@@ -26,7 +26,6 @@ func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Reque
departuredate := r.FormValue("departuredate")
departuretime := r.FormValue("departuretime")
departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime)
fmt.Println(departuredatetime)
departure := r.FormValue("departure")
destination := r.FormValue("destination")
@@ -70,7 +69,7 @@ func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Println(session)
request := navitia.JourneyRequest{
From: types.ID(fmt.Sprintf("%f", departuregeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", departuregeo.Geometry.Point[1])),
To: types.ID(fmt.Sprintf("%f", destinationgeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", destinationgeo.Geometry.Point[1])),
@@ -124,8 +123,6 @@ func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Reque
carpoolresults = []any{}
}
fmt.Println(carpoolresults)
// Vehicles
vehiclerequest := &fleets.GetVehiclesRequest{

View File

@@ -16,6 +16,8 @@ import (
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"
"google.golang.org/protobuf/types/known/structpb"
@@ -123,6 +125,10 @@ func (h *ApplicationHandler) VehiclesFleetAdd(w http.ResponseWriter, r *http.Req
if v := r.FormValue("licence_plate"); v != "" {
dataMap["licence_plate"] = v
}
if v := r.FormValue("automatic"); v != "" {
fmt.Println(v)
dataMap["automatic"] = (v == "on")
}
data, err := structpb.NewValue(dataMap)
if err != nil {
@@ -154,7 +160,9 @@ func (h *ApplicationHandler) VehiclesFleetAdd(w http.ResponseWriter, r *http.Req
http.Redirect(w, r, fmt.Sprintf("/app/vehicles-management/fleet/%s", vehicle.Id), http.StatusFound)
return
}
h.Renderer.VehiclesFleetAdd(w, r)
vehicles_types := h.config.GetStringSlice("modules.fleets.vehicle_types")
h.Renderer.VehiclesFleetAdd(w, r, vehicles_types)
}
func (h *ApplicationHandler) VehiclesFleetDisplay(w http.ResponseWriter, r *http.Request) {
@@ -282,15 +290,21 @@ func (h ApplicationHandler) VehicleManagementBookingDisplay(w http.ResponseWrite
booking = newbooking.ToStorageType()
}
beneficiaryrequest := &mobilityaccounts.GetAccountRequest{
Id: booking.Driver,
}
beneficiary := mobilityaccountsstorage.Account{}
beneficiaryresp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), beneficiaryrequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
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)
return
}
beneficiary = beneficiaryresp.Account.ToStorageType()
}
grouprequest := &groupsmanagement.GetGroupRequest{
@@ -307,5 +321,88 @@ func (h ApplicationHandler) VehicleManagementBookingDisplay(w http.ResponseWrite
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)
h.Renderer.VehicleManagementBookingDisplay(w, r, booking, booking.Vehicle, beneficiary, groupresp.Group.ToStorageType(), documents, file_types_map)
}
func (h ApplicationHandler) VehiclesFleetMakeUnavailable(w http.ResponseWriter, r *http.Request) { // Get Group
g := r.Context().Value(identification.GroupKey)
if g == nil {
fmt.Println("no current group")
w.WriteHeader(http.StatusInternalServerError)
return
}
current_group := g.(storage.Group)
// Get current user ID
u := r.Context().Value(identification.IdtokenKey)
if u == nil {
fmt.Println("no current user")
w.WriteHeader(http.StatusInternalServerError)
return
}
current_user_token := u.(*oidc.IDToken)
// Get current user claims
c := r.Context().Value(identification.ClaimsKey)
if c == nil {
fmt.Println("no current user claims")
w.WriteHeader(http.StatusInternalServerError)
return
}
current_user_claims := c.(map[string]any)
vars := mux.Vars(r)
vehicleid := vars["vehicleid"]
r.ParseForm()
start := r.FormValue("unavailablefrom")
end := r.FormValue("unavailableto")
comment := r.FormValue("comment")
unavailablefrom, _ := time.Parse("2006-01-02", start)
unavailableto, _ := time.Parse("2006-01-02", end)
data := map[string]any{
"comment": comment,
"administrator_unavailability": true,
"booked_by": map[string]any{
"user": map[string]any{
"id": current_user_token.Subject,
"display_name": current_user_claims["display_name"],
},
"group": map[string]any{
"id": current_group.ID,
"name": current_group.Data["name"],
},
},
}
datapb, err := structpb.NewStruct(data)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
booking := &fleets.Booking{
Id: uuid.NewString(),
Vehicleid: vehicleid,
Unavailablefrom: timestamppb.New(unavailablefrom),
Unavailableto: timestamppb.New(unavailableto),
Data: datapb,
}
request := &fleets.CreateBookingRequest{
Booking: booking,
}
_, err = h.services.GRPC.Fleets.CreateBooking(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
http.Redirect(w, r, fmt.Sprintf("/app/vehicles-management/fleet/%s", vehicleid), http.StatusFound)
}

View File

@@ -38,6 +38,9 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
startdate, _ := time.Parse("2006-01-02", start)
enddate, _ := time.Parse("2006-01-02", end)
automatic := (r.FormValue("automatic") == "on")
administrators := []string{}
if r.FormValue("beneficiaryid") != "" {
// Handler form
@@ -57,8 +60,15 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
beneficiary = respbeneficiary.Account.ToStorageType()
request := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
Namespaces: []string{"parcoursmob"},
AvailabilityFrom: timestamppb.New(startdate),
AvailabilityTo: timestamppb.New(enddate),
}
if r.FormValue("type") != "" {
request.Types = []string{r.FormValue("type")}
}
resp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), request)
if err != nil {
fmt.Println(err)
@@ -67,9 +77,27 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
for _, vehicle := range resp.Vehicles {
v := vehicle.ToStorageType()
if v.Free(startdate, enddate) {
vehicles = append(vehicles, v)
if r.FormValue("type") == "Voiture" && automatic {
fmt.Println(v.Data["automatic"])
if auto, ok := v.Data["automatic"].(bool); !ok || !auto {
fmt.Println(v.Data["automatic"])
continue
}
}
adminfound := false
for _, a := range administrators {
if a == v.Administrators[0] {
adminfound = true
break
}
}
if !adminfound {
administrators = append(administrators, v.Administrators[0])
}
vehicles = append(vehicles, v)
}
beneficiarydocuments = h.filestorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + beneficiary.ID)
@@ -81,13 +109,30 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
w.WriteHeader(http.StatusBadRequest)
return
}
groups := map[string]any{}
if len(administrators) > 0 {
admingroups, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: administrators,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, g := range admingroups.Groups {
groups[g.Id] = g.ToStorageType()
}
}
sort.Sort(sorting.BeneficiariesByName(accounts))
mandatory_documents := h.config.GetStringSlice("modules.fleets.booking_documents.mandatory")
file_types_map := h.config.GetStringMapString("storage.files.file_types")
vehicles_types := h.config.GetStringSlice("modules.fleets.vehicle_types")
h.Renderer.VehiclesSearch(w, r, accounts, searched, vehicles, beneficiary, r.FormValue("startdate"), r.FormValue("enddate"), mandatory_documents, file_types_map, beneficiarydocuments)
h.Renderer.VehiclesSearch(w, r, accounts, searched, vehicles, beneficiary, r.FormValue("startdate"), r.FormValue("enddate"), mandatory_documents, file_types_map, beneficiarydocuments, r.FormValue("type"), automatic, vehicles_types, groups)
}
func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
@@ -122,6 +167,17 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
vehicleid := vars["vehicleid"]
beneficiaryid := vars["beneficiaryid"]
vehicle, err := h.services.GRPC.Fleets.GetVehicle(context.TODO(), &fleets.GetVehicleRequest{
Vehicleid: vehicleid,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Vehicle not found"))
w.Write([]byte(err.Error()))
return
}
r.ParseMultipartForm(10 * 1024 * 1024)
start := r.FormValue("startdate")
@@ -207,6 +263,18 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
return
}
//NOTIFY GROUP MEMBERS
members, _, err := h.groupmembers(vehicle.Vehicle.Administrators[0])
if err != nil {
fmt.Println(err)
} else {
for _, m := range members {
h.emailing.Send("fleets.bookings.creation_admin_alert", m.Data["email"].(string), map[string]string{
"bookingid": booking.Id,
})
}
}
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", booking.Id), http.StatusFound)
}