resolve commit and fix

This commit is contained in:
Arnaud Delcasse 2023-11-27 08:45:48 +01:00
commit c8f8f82535
6 changed files with 399 additions and 109 deletions

2
go.mod
View File

@ -2,7 +2,7 @@ module git.coopgo.io/coopgo-apps/parcoursmob
go 1.18
// replace git.coopgo.io/coopgo-platform/mobility-accounts => ../../coopgo-platform/mobility-accounts/
replace git.coopgo.io/coopgo-platform/mobility-accounts => ../../coopgo-platform/mobility-accounts/
// replace git.coopgo.io/coopgo-platform/groups-management => ../../coopgo-platform/groups-management/

View File

@ -45,8 +45,10 @@ func (h *ApplicationHandler) VehiclesManagementOverview(w http.ResponseWriter, r
vehicleBookings := []fleetsstorage.Booking{}
for _, b := range v.Bookings {
if b.Status() != fleetsstorage.StatusOld {
if deleted, ok := b.Data["Deleted"].(bool); !ok && !deleted {
bookings = append(bookings, b)
}
}
if b.Unavailableto.After(time.Now()) {
vehicleBookings = append(vehicleBookings, b)
}
@ -188,36 +190,10 @@ func (h *ApplicationHandler) VehiclesFleetDisplay(w http.ResponseWriter, r *http
w.WriteHeader(http.StatusInternalServerError)
return
}
// if len(resp.Vehicle.ToStorageType().Bookings) == 0 {
// fmt.Println("lol")
// }
// fmt.Println(resp.Vehicle.ToStorageType().Bookings)
h.Renderer.VehiclesFleetDisplay(w, r, resp.Vehicle.ToStorageType())
}
func (h *ApplicationHandler) VehiclesFleetUpdate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
vehicleid := vars["vehicleid"]
if r.Method == "POST" {
w.WriteHeader(http.StatusNotFound)
return
}
request := &fleets.GetVehicleRequest{
Vehicleid: vehicleid,
}
resp, err := h.services.GRPC.Fleets.GetVehicle(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.VehiclesFleetUpdate(w, r, resp.Vehicle.ToStorageType())
}
func filterVehicle(r *http.Request, v *fleets.Vehicle) bool {
g := r.Context().Value(identification.GroupKey)
if g == nil {
@ -491,3 +467,208 @@ func (h ApplicationHandler) VehiclesFleetMakeUnavailable(w http.ResponseWriter,
// fmt.Println(vehicles)
// h.Renderer.UnbookingVehicles(w, r, vehicles)
// }
func (h *ApplicationHandler) DeleteBooking(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookingid := vars["bookingid"]
request := &fleets.DeleteBookingRequest{
Id: bookingid,
}
_, err := h.services.GRPC.Fleets.DeleteBooking(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/app/vehicles-management/bookings/", http.StatusSeeOther)
}
func (h *ApplicationHandler) UnbookingVehicle(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 {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// now := time.Now()
// date := now.Format("2006-01-02")
date := "1970-01-01"
unavailableto, _ := time.Parse("2006-01-02", date)
unavailablefrom := unavailableto
current_group, err := h.currentGroup(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
current_user_token, current_user_claims, err := h.currentUser(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
booked_by_id := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["user"].GetStructValue().Fields["id"].GetStringValue()
booked_by_name := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["user"].GetStructValue().Fields["display_name"].GetStringValue()
booked_by_email := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["user"].GetStructValue().Fields["email"].GetStringValue()
booked_by_group_id := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["group"].GetStructValue().Fields["id"].GetStringValue()
booked_by_group_name := resp.Booking.Data.Fields["booked_by"].GetStructValue().Fields["group"].GetStructValue().Fields["name"].GetStringValue()
data := map[string]any{
"booked_by": map[string]any{
"user": map[string]any{
"id": booked_by_id,
"display_name": booked_by_name,
"email": booked_by_email,
},
"group": map[string]any{
"id": booked_by_group_id,
"name": booked_by_group_name,
},
},
"unbooked_by": map[string]any{
"user": map[string]any{
"id": current_user_token.Subject,
"display_name": current_user_claims["first_name"].(string) + " " + current_user_claims["last_name"].(string),
"email": current_user_claims["email"],
},
"group": map[string]any{
"id": current_group.ID,
"name": current_group.Data["name"],
},
},
"Deleted": true,
"motif": r.FormValue("motif"),
}
datapb, err := structpb.NewStruct(data)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Method == "POST" {
request := &fleets.UpdateBookingRequest{
Booking: &fleets.Booking{
Id: resp.Booking.Id,
Vehicleid: resp.Booking.Vehicleid,
Driver: resp.Booking.Driver,
Startdate: resp.Booking.Startdate,
Enddate: resp.Booking.Enddate,
Unavailablefrom: timestamppb.New(unavailablefrom),
Unavailableto: timestamppb.New(unavailableto),
Data: datapb,
},
}
_, err := h.services.GRPC.Fleets.UpdateBooking(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/app/vehicles-management/", http.StatusFound)
return
}
h.Renderer.UnbookingVehicle(w, r, resp.Booking.ToStorageType())
}
////////////////////////UpdateVehicle///////////////////////
func (h *ApplicationHandler) VehiclesFleetUpdate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
vehicleID := vars["vehicleid"]
request := &fleets.GetVehicleRequest{
Vehicleid: vehicleID,
}
resp, err := h.services.GRPC.Fleets.GetVehicle(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
namespaceV := resp.Vehicle.Namespace
//typeV := resp.Vehicle.Type
administratorsV := resp.Vehicle.Administrators
if r.Method == "POST" {
fmt.Print(r.FormValue("vehicle_type"))
if err := r.ParseForm(); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
dataMap := map[string]any{}
if v := r.FormValue("name"); v != "" {
dataMap["name"] = v
}
if v := r.FormValue("address"); v != "" {
var address map[string]any
err := json.Unmarshal([]byte(v), &address)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
dataMap["address"] = address
}
if v := r.FormValue("informations"); v != "" {
dataMap["informations"] = v
}
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 {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
request := &fleets.UpdateVehicleRequest{
Vehicle: &fleets.Vehicle{
Id: vehicleID,
Namespace: namespaceV,
Type: r.FormValue("type"),
Administrators: administratorsV,
Data: data.GetStructValue(),
},
}
resp, err := h.services.GRPC.Fleets.UpdateVehicle(context.TODO(), request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/app/vehicles-management/fleet/%s", resp.Vehicle.Id), http.StatusFound)
return
}
vehicles_types := h.config.GetStringSlice("modules.fleets.vehicle_types")
h.Renderer.VehiclesFleetUpdate(w, r, resp.Vehicle.ToStorageType(), vehicles_types)
}

View File

@ -13,14 +13,17 @@ import (
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
accountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"github.com/gorilla/mux"
"github.com/xuri/excelize/v2"
)
func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
func (h *ExportsHandler) Agenda(filter string) func(w http.ResponseWriter, r *http.Request) {
switch filter {
case "allEvents":
return func(w http.ResponseWriter, r *http.Request) {
resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"},
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
@ -67,15 +70,70 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
beneficiaries_map[ben.Id] = ben.ToStorageType()
}
/////////////// Generate file
f := h.generateExcel(events, groups, beneficiaries_map)
h.writeFileResponse(f, w)
}
case "oneEvent":
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventId := vars["eventid"]
resp, err := h.services.GRPC.Agenda.GetEvent(context.TODO(), &agenda.GetEventRequest{
Id: eventId,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
groupids := []string{}
beneficiaries_ids := []string{}
groupids = append(groupids, resp.Event.Owners...)
for _, subscriptions := range resp.Event.Subscriptions {
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
}
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids,
})
groups := map[string]groupsstorage.Group{}
if err == nil {
for _, g := range groupsresp.Groups {
groups[g.Id] = g.ToStorageType()
}
}
beneficiaries, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), &accounts.GetAccountsBatchRequest{
Accountids: beneficiaries_ids,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
beneficiaries_map := map[string]accountsstorage.Account{}
for _, ben := range beneficiaries.Accounts {
beneficiaries_map[ben.Id] = ben.ToStorageType()
}
f := h.generateExcel([]agendastorage.Event{resp.Event.ToStorageType()}, groups, beneficiaries_map)
h.writeFileResponse(f, w)
}
}
return nil
}
func (h *ExportsHandler) generateExcel(events []agendastorage.Event, groups map[string]groupsstorage.Group,
beneficiaries_map map[string]accountsstorage.Account) *excelize.File {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
f.SetCellValue("Sheet1", "A1", "Evénement")
f.SetCellValue("Sheet1", "B1", "Date de début")
f.SetCellValue("Sheet1", "C1", "Date de fin")
@ -133,10 +191,14 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
}
}
return f
}
func (h *ExportsHandler) writeFileResponse(file *excelize.File, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Expires", "0")
f.Write(w)
file.Write(w)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
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"
@ -18,7 +19,6 @@ func (h *ExportsHandler) Bookings(w http.ResponseWriter, r *http.Request) {
vehicles := map[string]fleetsstorage.Vehicle{}
bookings := []fleetsstorage.Booking{}
reequest := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
}
@ -93,8 +93,10 @@ func (h *ExportsHandler) Bookings(w http.ResponseWriter, r *http.Request) {
f.SetCellValue("Sheet1", "H1", "Fin de mise à disposition")
f.SetCellValue("Sheet1", "I1", "Début indisponibilité")
f.SetCellValue("Sheet1", "J1", "Fin indisponibilité")
f.SetCellValue("Sheet1", "K1", "Retrait par le gestionnaire")
f.SetCellValue("Sheet1", "L1", "Commentaire")
f.SetCellValue("Sheet1", "K1", "Véhicule retiré")
f.SetCellValue("Sheet1", "L1", "Commentaire - Retrait véhicule")
f.SetCellValue("Sheet1", "M1", "Réservation supprimée")
f.SetCellValue("Sheet1", "N1", "Motif de la suppression")
i := 2
for _, b := range bookings {
@ -106,14 +108,31 @@ func (h *ExportsHandler) Bookings(w http.ResponseWriter, r *http.Request) {
bookedby := ""
if v, ok := b.Data["booked_by"].(map[string]any); ok {
if v2, ok := v["group"].(map[string]any); ok {
if v3, ok := v2["name"].(string); ok {
if v2, ok := v["user"].(map[string]any); ok {
if v3, ok := v2["display_name"].(string); ok {
bookedby = v3
}
}
}
bookedbygroup := ""
if v4, ok := b.Data["booked_by"].(map[string]any); ok {
if v5, ok := v4["group"].(map[string]any); ok {
if v6, ok := v5["id"].(string); ok {
bookedbygroup = v6
}
}
}
// filter by group
g := r.Context().Value(identification.GroupKey)
group := g.(groupsstorage.Group)
if bookedbygroup != group.ID {
continue
}
beneficiary := beneficiaries_map[b.Driver]
adminunavailability := false
@ -121,6 +140,14 @@ func (h *ExportsHandler) Bookings(w http.ResponseWriter, r *http.Request) {
adminunavailability = true
}
deleted := ""
v, ok := b.Data["Deleted"]
fmt.Println(v)
fmt.Println(ok)
if b.Deleted || (ok && v.(bool)) {
deleted = "DELETED"
}
f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i), vehicle.Data["licence_plate"])
f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i), vehicle.Type)
f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i), admin.Data["name"])
@ -133,6 +160,8 @@ func (h *ExportsHandler) Bookings(w http.ResponseWriter, r *http.Request) {
f.SetCellValue("Sheet1", fmt.Sprintf("J%d", i), b.Unavailableto.Format("2006-01-02"))
f.SetCellValue("Sheet1", fmt.Sprintf("K%d", i), adminunavailability)
f.SetCellValue("Sheet1", fmt.Sprintf("L%d", i), b.Data["comment"])
f.SetCellValue("Sheet1", fmt.Sprintf("M%d", i), deleted)
f.SetCellValue("Sheet1", fmt.Sprintf("N%d", i), b.Data["motif"])
i = i + 1
}

View File

@ -108,6 +108,10 @@ func main() {
application.HandleFunc("/vehicles-management/bookings/", applicationHandler.VehiclesManagementBookingsList)
application.HandleFunc("/vehicles-management/bookings/{bookingid}", applicationHandler.VehicleManagementBookingDisplay)
application.HandleFunc("/vehicles-management/bookings/{bookingid}/change-vehicle", applicationHandler.VehicleManagementBookingChangeVehicle)
/////////////////////////////////////Remove booking vehicle/////////////////////////////////////////
application.HandleFunc("/vehicles-management/bookings/{bookingid}/delete", applicationHandler.UnbookingVehicle)
// application.HandleFunc("/vehicles-management/bookings/{bookingid}/delete", applicationHandler.DeleteBooking)
////////////////////////////////////////////////////////////////////////////////////////////////////
application.HandleFunc("/vehicles-management/bookings/{bookingid}/documents/{document}", applicationHandler.BookingDocumentDownload)
application.HandleFunc("/agenda/", applicationHandler.AgendaHome)
application.HandleFunc("/agenda/history", applicationHandler.AgendaHistory)
@ -163,7 +167,9 @@ func main() {
export := r.PathPrefix("/exports").Subrouter()
export.HandleFunc("/fleets/bookings", exportsHandler.Bookings)
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda)
export.HandleFunc("/fleets/bookings/{groupid}", exportsHandler.Bookings)
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda("allEvents"))
export.HandleFunc("/agenda/{eventid}", exportsHandler.Agenda("oneEvent"))
export.Use(idp.Middleware)
export.Use(idp.GroupsMiddleware)

View File

@ -53,11 +53,12 @@ func (renderer *Renderer) VehiclesFleetDisplay(w http.ResponseWriter, r *http.Re
renderer.Render("fleet display vehicle", w, r, files, state)
}
func (renderer *Renderer) VehiclesFleetUpdate(w http.ResponseWriter, r *http.Request, vehicle any) {
func (renderer *Renderer) VehiclesFleetUpdate(w http.ResponseWriter, r *http.Request, vehicle any, vehicle_types []string) {
files := renderer.ThemeConfig.GetStringSlice("views.vehicles_management.fleet_update.files")
state := NewState(r, renderer.ThemeConfig, vehiclesmanagementMenu)
state.ViewState = map[string]any{
"vehicle": vehicle,
"vehicle_types": vehicle_types,
}
renderer.Render("fleet display vehicle", w, r, files, state)
@ -78,3 +79,14 @@ 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) {
files := renderer.ThemeConfig.GetStringSlice("views.vehicles_management.delete_booking.files")
state := NewState(r, renderer.ThemeConfig, vehiclesmanagementMenu)
state.ViewState = map[string]any{
"booking": booking,
}
renderer.Render("vehicule unbooking", w, r, files, state)
}