diff --git a/handlers/application/beneficiaries.go b/handlers/application/beneficiaries.go index 457e5a4..67fe788 100755 --- a/handlers/application/beneficiaries.go +++ b/handlers/application/beneficiaries.go @@ -23,6 +23,7 @@ import ( agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi" agendastorage "git.coopgo.io/coopgo-platform/agenda/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" "git.coopgo.io/coopgo-platform/groups-management/storage" mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" @@ -43,6 +44,67 @@ type BeneficiariesForm struct { Gender string `json:"gender"` } +type Event_Beneficiary interface { + Name() string + Date() time.Time + DateEnd() time.Time + Type() string + Db() string + ID() string + Icons() string + Status() int +} + +type Event struct { + IDVal string + NameVal string + DateVal time.Time + DateEndVal time.Time + TypeVal string + DbVal string + Deleted bool + IconSet string + StatusVal int +} + +func (e Event) Name() string { + return e.NameVal +} + +func (e Event) Date() time.Time { + return e.DateVal +} + +func (e Event) DateEnd() time.Time { + return e.DateEndVal +} + +func (e Event) Type() string { + return e.TypeVal +} + +func (e Event) ID() string { + return e.IDVal +} + +func (e Event) Db() string { + return e.DbVal +} + +func (e Event) Icons() string { + return e.IconSet +} + +func (e Event) Status() int { + return e.StatusVal +} + +func sortByDate(events []Event_Beneficiary) { + sort.Slice(events, func(i, j int) bool { + return events[i].Date().Before(events[j].Date()) + }) +} + func (h *ApplicationHandler) BeneficiariesList(w http.ResponseWriter, r *http.Request) { accounts, err := h.beneficiaries(r) @@ -161,7 +223,7 @@ func (h *ApplicationHandler) BeneficiaryDisplay(w http.ResponseWriter, r *http.R return } currentTime := time.Now().Truncate(24 * time.Hour) - if eventresp.Event.Startdate.AsTime().Equal(currentTime) || eventresp.Event.Startdate.AsTime().After(currentTime) { + if eventresp.Event.Enddate.AsTime().Equal(currentTime) || eventresp.Event.Enddate.AsTime().After(currentTime) { events = append(events, eventresp.Event.ToStorageType()) } } @@ -178,12 +240,79 @@ func (h *ApplicationHandler) BeneficiaryDisplay(w http.ResponseWriter, r *http.R return } - bookings := []any{} + bookings := []fleetsstorage.Booking{} for _, b := range bookingsresp.Bookings { bookings = append(bookings, b.ToStorageType()) } + var events_list []Event_Beneficiary + var status_event int + + for _, e := range events { + + if e.Startdate.After(time.Now()) { + status_event = 1 + } else if e.Startdate.Before(time.Now()) && e.Enddate.After(time.Now()) { + status_event = 2 + } else { + status_event = 3 + } + + event := Event{ + NameVal: e.Name, + DateVal: e.Startdate, + DateEndVal: e.Enddate, + TypeVal: e.Type, + IDVal: e.ID, + DbVal: "/app/agenda/", + IconSet: "calendar", + StatusVal: status_event, + } + + events_list = append(events_list, event) + } + + var status_booking int + for _, b := range bookings { + + currentTime := time.Now().Truncate(24 * time.Hour) + if b.Enddate.After(currentTime) || b.Enddate.Equal(currentTime) { + GetVehiculeRequest := &fleets.GetVehicleRequest{ + Vehicleid: b.Vehicleid, + } + + GetVehiculeResp, err := h.services.GRPC.Fleets.GetVehicle(context.Background(), GetVehiculeRequest) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + if b.Startdate.After(time.Now()) { + status_booking = 1 + } else if b.Startdate.Before(time.Now()) && b.Enddate.After(time.Now()) { + status_booking = 2 + } else { + status_booking = 3 + } + + event := Event{ + NameVal: GetVehiculeResp.Vehicle.ToStorageType().Data["name"].(string), + DateVal: b.Startdate, + DateEndVal: b.Enddate, + TypeVal: "Réservation de véhicule", + IDVal: b.ID, + DbVal: "/app/vehicles-management/bookings/", + IconSet: "vehicle", + StatusVal: status_booking, + } + + events_list = append(events_list, event) + } + } + sortByDate(events_list) + groupsrequest := &groupsmanagement.GetGroupsRequest{ Namespaces: []string{"parcoursmob_organizations"}, Member: beneficiaryID, @@ -202,7 +331,7 @@ func (h *ApplicationHandler) BeneficiaryDisplay(w http.ResponseWriter, r *http.R beneficiaries_file_types := h.config.GetStringSlice("modules.beneficiaries.documents_types") file_types_map := h.config.GetStringMapString("storage.files.file_types") - h.Renderer.BeneficiaryDisplay(w, r, resp.Account.ToStorageType(), bookings, organizations, beneficiaries_file_types, file_types_map, documents, events) + h.Renderer.BeneficiaryDisplay(w, r, resp.Account.ToStorageType(), bookings, organizations, beneficiaries_file_types, file_types_map, documents, events_list) } func (h *ApplicationHandler) BeneficiaryUpdate(w http.ResponseWriter, r *http.Request) { diff --git a/renderer/beneficiaries.go b/renderer/beneficiaries.go index dec0c7f..770587e 100755 --- a/renderer/beneficiaries.go +++ b/renderer/beneficiaries.go @@ -5,7 +5,7 @@ import ( "html/template" "net/http" - agendastorage "git.coopgo.io/coopgo-platform/agenda/storage" + fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage" mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage" ) @@ -53,7 +53,7 @@ type BeneficiariesDisplayState struct { Beneficiary any } -func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Request, beneficiary any, bookings []any, organizations []any, beneficiaries_file_types []string, file_types_map map[string]string, documents any, event []agendastorage.Event) { +func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Request, beneficiary any, bookings []fleetsstorage.Booking, organizations []any, beneficiaries_file_types []string, file_types_map map[string]string, documents any, event interface{}) { files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.display.files") state := NewState(r, renderer.ThemeConfig, beneficiariesMenu) state.ViewState = map[string]any{ @@ -65,7 +65,6 @@ func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Requ "organizations": organizations, "event": event, } - renderer.Render("beneficiaries_display", w, r, files, state) }