From bcddbc0d5816bada2740187baa5179d7f548da57 Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Wed, 12 Apr 2023 10:56:36 +0200 Subject: [PATCH] Bookings list in admin --- handlers/application/administration.go | 63 ++++++++++++++++++++++++++ main.go | 1 + renderer/administration.go | 13 ++++++ 3 files changed, 77 insertions(+) diff --git a/handlers/application/administration.go b/handlers/application/administration.go index 7aed121..748ea06 100644 --- a/handlers/application/administration.go +++ b/handlers/application/administration.go @@ -447,6 +447,69 @@ func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Req h.Renderer.AdminStatVehicles(w, r, vehicles, bookings, groups) } +func (h ApplicationHandler) AdminStatBookings(w http.ResponseWriter, r *http.Request) { + + vehicles := map[string]fleetsstorage.Vehicle{} + bookings := []fleetsstorage.Booking{} + + reequest := &fleets.GetVehiclesRequest{ + Namespaces: []string{"parcoursmob"}, + } + reesp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), reequest) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + beneficiaries_ids := []string{} + + for _, vehicle := range reesp.Vehicles { + + v := vehicle.ToStorageType() + + for _, b := range v.Bookings { + bookings = append(bookings, b) + beneficiaries_ids = append(beneficiaries_ids, b.Driver) + } + + vehicles[v.ID] = v + + } + + groups := map[string]any{} + + admingroups, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), &groupsmanagement.GetGroupsRequest{ + Namespaces: []string{"parcoursmob_organizations"}, + }) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + for _, g := range admingroups.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]any{} + for _, ben := range beneficiaries.Accounts { + beneficiaries_map[ben.Id] = ben.ToStorageType() + } + + sort.Sort(sorting.BookingsByStartdate(bookings)) + h.Renderer.AdminStatBookings(w, r, vehicles, bookings, groups, beneficiaries_map) +} + func (h *ApplicationHandler) members() ([]*accounts.Account, error) { resp, err := h.services.GRPC.MobilityAccounts.GetAccounts(context.TODO(), &accounts.GetAccountsRequest{ Namespaces: []string{"parcoursmob"}, diff --git a/main.go b/main.go index 2420d8f..6a06f4e 100644 --- a/main.go +++ b/main.go @@ -150,6 +150,7 @@ func main() { appAdmin.HandleFunc("/groups/{groupid}/invite-member", applicationHandler.AdministrationGroupInviteMember) //add statistiques appAdmin.HandleFunc("/stats/vehicles", applicationHandler.AdminStatVehicles) + appAdmin.HandleFunc("/stats/bookings", applicationHandler.AdminStatBookings) appAdmin.HandleFunc("/stats/beneficaires", applicationHandler.AdminStatBeneficaires) appAdmin.HandleFunc("/stats/events", applicationHandler.AdminStatEvents) diff --git a/renderer/administration.go b/renderer/administration.go index fe89393..aaa41ff 100644 --- a/renderer/administration.go +++ b/renderer/administration.go @@ -99,3 +99,16 @@ func (renderer *Renderer) AdminStatVehicles(w http.ResponseWriter, r *http.Reque renderer.Render("vehicles_state", w, r, files, state) } + +func (renderer *Renderer) AdminStatBookings(w http.ResponseWriter, r *http.Request, vehicles map[string]fleetsstorage.Vehicle, bookings []fleetsstorage.Booking, admingroups map[string]any, beneficiaries map[string]any) { + files := renderer.ThemeConfig.GetStringSlice("views.administration.bookings_list.files") + state := NewState(r, renderer.ThemeConfig, administrationMenu) + state.ViewState = map[string]any{ + "vehicles_map": vehicles, + "bookings": bookings, + "admingroups": admingroups, + "beneficiaries_map": beneficiaries, + } + + renderer.Render("bookings_stats", w, r, files, state) +}