2022-08-12 12:53:54 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-11-01 16:06:12 +00:00
|
|
|
"io"
|
2022-08-12 12:53:54 +00:00
|
|
|
"net/http"
|
2022-11-01 10:32:13 +00:00
|
|
|
"sort"
|
2022-11-01 16:06:12 +00:00
|
|
|
"strings"
|
2022-08-12 12:53:54 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
2022-11-01 10:32:13 +00:00
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
|
2022-11-01 16:06:12 +00:00
|
|
|
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
2022-08-12 12:53:54 +00:00
|
|
|
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
|
2022-12-05 19:06:22 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/fleets/storage"
|
2022-09-05 05:25:05 +00:00
|
|
|
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
2022-12-05 19:06:22 +00:00
|
|
|
groupsmanagementstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
2022-08-12 12:53:54 +00:00
|
|
|
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
2022-11-01 16:06:12 +00:00
|
|
|
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
2022-08-12 12:53:54 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gorilla/mux"
|
2024-11-11 18:50:17 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-09-05 05:25:05 +00:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2022-08-12 12:53:54 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
2023-04-20 16:14:00 +00:00
|
|
|
fmt.Println("invoked")
|
2022-11-01 16:06:12 +00:00
|
|
|
var beneficiary mobilityaccountsstorage.Account
|
|
|
|
|
|
|
|
beneficiarydocuments := []filestorage.FileInfo{}
|
2022-08-12 12:53:54 +00:00
|
|
|
|
|
|
|
vehicles := []any{}
|
|
|
|
searched := false
|
|
|
|
start := r.FormValue("startdate")
|
|
|
|
end := r.FormValue("enddate")
|
|
|
|
|
|
|
|
startdate, _ := time.Parse("2006-01-02", start)
|
|
|
|
enddate, _ := time.Parse("2006-01-02", end)
|
2022-11-07 00:24:16 +00:00
|
|
|
automatic := (r.FormValue("automatic") == "on")
|
|
|
|
|
|
|
|
administrators := []string{}
|
2022-08-12 12:53:54 +00:00
|
|
|
|
|
|
|
if r.FormValue("beneficiaryid") != "" {
|
|
|
|
// Handler form
|
|
|
|
searched = true
|
|
|
|
|
|
|
|
requestbeneficiary := &mobilityaccounts.GetAccountRequest{
|
|
|
|
Id: r.FormValue("beneficiaryid"),
|
|
|
|
}
|
|
|
|
|
|
|
|
respbeneficiary, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), requestbeneficiary)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-08-12 12:53:54 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
beneficiary = respbeneficiary.Account.ToStorageType()
|
|
|
|
|
|
|
|
request := &fleets.GetVehiclesRequest{
|
2022-11-07 00:24:16 +00:00
|
|
|
Namespaces: []string{"parcoursmob"},
|
|
|
|
AvailabilityFrom: timestamppb.New(startdate),
|
|
|
|
AvailabilityTo: timestamppb.New(enddate),
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FormValue("type") != "" {
|
|
|
|
request.Types = []string{r.FormValue("type")}
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
2022-11-07 00:24:16 +00:00
|
|
|
|
2022-08-12 12:53:54 +00:00
|
|
|
resp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), request)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-08-12 12:53:54 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vehicle := range resp.Vehicles {
|
|
|
|
v := vehicle.ToStorageType()
|
2022-11-07 00:24:16 +00:00
|
|
|
|
|
|
|
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])
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
2022-11-07 00:24:16 +00:00
|
|
|
|
|
|
|
vehicles = append(vehicles, v)
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
2022-11-01 16:06:12 +00:00
|
|
|
|
|
|
|
beneficiarydocuments = h.filestorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + beneficiary.ID)
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
accounts, err := h.beneficiaries(r)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-08-12 12:53:54 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 00:24:16 +00:00
|
|
|
groups := map[string]any{}
|
|
|
|
|
|
|
|
if len(administrators) > 0 {
|
|
|
|
admingroups, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
|
|
|
|
Groupids: administrators,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-07 00:24:16 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-08-12 12:53:54 +00:00
|
|
|
|
2022-11-07 00:24:16 +00:00
|
|
|
for _, g := range admingroups.Groups {
|
|
|
|
groups[g.Id] = g.ToStorageType()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-11-01 10:32:13 +00:00
|
|
|
sort.Sort(sorting.BeneficiariesByName(accounts))
|
|
|
|
|
2022-11-01 16:06:12 +00:00
|
|
|
mandatory_documents := h.config.GetStringSlice("modules.fleets.booking_documents.mandatory")
|
|
|
|
file_types_map := h.config.GetStringMapString("storage.files.file_types")
|
2022-11-07 00:24:16 +00:00
|
|
|
vehicles_types := h.config.GetStringSlice("modules.fleets.vehicle_types")
|
2022-11-01 16:06:12 +00:00
|
|
|
|
2022-11-07 00:24:16 +00:00
|
|
|
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)
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
|
2023-01-17 07:31:07 +00:00
|
|
|
fmt.Println("Book")
|
2022-12-05 19:06:22 +00:00
|
|
|
current_group, err := h.currentGroup(r)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-09-05 05:25:05 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 19:06:22 +00:00
|
|
|
current_user_token, current_user_claims, err := h.currentUser(r)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-09-05 05:25:05 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-08-12 12:53:54 +00:00
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
vehicleid := vars["vehicleid"]
|
|
|
|
beneficiaryid := vars["beneficiaryid"]
|
|
|
|
|
2022-11-07 00:24:16 +00:00
|
|
|
vehicle, err := h.services.GRPC.Fleets.GetVehicle(context.TODO(), &fleets.GetVehicleRequest{
|
|
|
|
Vehicleid: vehicleid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-07 00:24:16 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("Vehicle not found"))
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-17 07:31:07 +00:00
|
|
|
r.ParseMultipartForm(100 * 1024 * 1024)
|
2022-08-12 12:53:54 +00:00
|
|
|
|
|
|
|
start := r.FormValue("startdate")
|
|
|
|
end := r.FormValue("enddate")
|
|
|
|
|
|
|
|
startdate, _ := time.Parse("2006-01-02", start)
|
|
|
|
enddate, _ := time.Parse("2006-01-02", end)
|
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
data := map[string]any{
|
|
|
|
"booked_by": map[string]any{
|
|
|
|
"user": map[string]any{
|
|
|
|
"id": current_user_token.Subject,
|
2023-01-17 07:31:07 +00:00
|
|
|
"display_name": fmt.Sprintf("%s %s", current_user_claims["first_name"], current_user_claims["last_name"]),
|
2022-09-05 05:25:05 +00:00
|
|
|
},
|
|
|
|
"group": map[string]any{
|
|
|
|
"id": current_group.ID,
|
|
|
|
"name": current_group.Data["name"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
datapb, err := structpb.NewStruct(data)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-09-05 05:25:05 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-12 12:53:54 +00:00
|
|
|
booking := &fleets.Booking{
|
|
|
|
Id: uuid.NewString(),
|
|
|
|
Vehicleid: vehicleid,
|
|
|
|
Driver: beneficiaryid,
|
|
|
|
Startdate: timestamppb.New(startdate),
|
|
|
|
Enddate: timestamppb.New(enddate),
|
|
|
|
Unavailablefrom: timestamppb.New(startdate),
|
|
|
|
Unavailableto: timestamppb.New(enddate.Add(72 * time.Hour)),
|
2022-09-05 05:25:05 +00:00
|
|
|
Data: datapb,
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request := &fleets.CreateBookingRequest{
|
|
|
|
Booking: booking,
|
|
|
|
}
|
|
|
|
|
2022-11-01 16:06:12 +00:00
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-01 16:06:12 +00:00
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-01 16:06:12 +00:00
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-01 16:06:12 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
_, err = h.services.GRPC.Fleets.CreateBooking(context.TODO(), request)
|
2022-08-12 12:53:54 +00:00
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-08-12 12:53:54 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-07 00:24:16 +00:00
|
|
|
//NOTIFY GROUP MEMBERS
|
|
|
|
members, _, err := h.groupmembers(vehicle.Vehicle.Administrators[0])
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-07 00:24:16 +00:00
|
|
|
} else {
|
|
|
|
for _, m := range members {
|
2023-01-17 07:31:07 +00:00
|
|
|
if email, ok := m.Data["email"].(string); ok {
|
|
|
|
h.emailing.Send("fleets.bookings.creation_admin_alert", email, map[string]string{
|
|
|
|
"bookingid": booking.Id,
|
|
|
|
})
|
|
|
|
}
|
2022-11-07 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 12:53:54 +00:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", booking.Id), http.StatusFound)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bookingid := vars["bookingid"]
|
|
|
|
|
|
|
|
request := &fleets.GetBookingRequest{
|
|
|
|
Bookingid: bookingid,
|
|
|
|
}
|
|
|
|
resp, err := h.services.GRPC.Fleets.GetBooking(context.TODO(), request)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-08-12 12:53:54 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
booking := resp.Booking.ToStorageType()
|
|
|
|
|
|
|
|
beneficiaryrequest := &mobilityaccounts.GetAccountRequest{
|
|
|
|
Id: booking.Driver,
|
|
|
|
}
|
|
|
|
|
|
|
|
beneficiaryresp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), beneficiaryrequest)
|
|
|
|
if err != nil {
|
2023-01-17 07:31:07 +00:00
|
|
|
beneficiaryresp = &mobilityaccounts.GetAccountResponse{
|
|
|
|
Account: &mobilityaccounts.Account{},
|
|
|
|
}
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
grouprequest := &groupsmanagement.GetGroupRequest{
|
|
|
|
Id: booking.Vehicle.Administrators[0],
|
|
|
|
}
|
2022-08-12 12:53:54 +00:00
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), grouprequest)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-09-05 05:25:05 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-01 16:06:12 +00:00
|
|
|
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)
|
2022-09-05 05:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h ApplicationHandler) VehiclesBookingsList(w http.ResponseWriter, r *http.Request) {
|
2022-12-05 19:06:22 +00:00
|
|
|
|
|
|
|
g := r.Context().Value(identification.GroupKey)
|
|
|
|
if g == nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
group := g.(groupsmanagementstorage.Group)
|
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
request := &fleets.GetBookingsRequest{}
|
|
|
|
resp, err := h.services.GRPC.Fleets.GetBookings(context.TODO(), request)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-09-05 05:25:05 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 19:06:22 +00:00
|
|
|
bookings := []storage.Booking{}
|
2022-09-05 05:25:05 +00:00
|
|
|
|
|
|
|
for _, b := range resp.Bookings {
|
2022-12-05 19:06:22 +00:00
|
|
|
booking := b.ToStorageType()
|
|
|
|
if b1, ok := booking.Data["booked_by"].(map[string]any); ok {
|
|
|
|
if b2, ok := b1["group"].(map[string]any); ok {
|
|
|
|
if b2["id"] == group.ID {
|
|
|
|
bookings = append(bookings, booking)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:25:05 +00:00
|
|
|
}
|
2022-08-12 12:53:54 +00:00
|
|
|
|
2022-12-05 19:06:22 +00:00
|
|
|
sort.Sort(sorting.BookingsByStartdate(bookings))
|
|
|
|
|
2023-01-17 07:31:07 +00:00
|
|
|
vehicles, _ := h.services.GetVehiclesMap()
|
|
|
|
groups, _ := h.services.GetGroupsMap()
|
|
|
|
|
2023-12-08 12:29:25 +00:00
|
|
|
// fmt.Println("bookings : ", bookings)
|
|
|
|
// fmt.Println("vehicles : ", vehicles)
|
|
|
|
// fmt.Println("groups : ", groups)
|
2023-01-17 07:31:07 +00:00
|
|
|
h.Renderer.VehicleBookingsList(w, r, bookings, vehicles, groups)
|
2022-08-12 12:53:54 +00:00
|
|
|
}
|
2022-11-01 16:06:12 +00:00
|
|
|
|
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-01 16:06:12 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", info.ContentType)
|
|
|
|
if _, err = io.Copy(w, file); err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-11-01 16:06:12 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", bookingid), http.StatusFound)
|
|
|
|
|
|
|
|
}
|