From 12a8a359ab35cde6995dfea2db4ff0797a7f8993 Mon Sep 17 00:00:00 2001 From: soukainna Date: Wed, 7 Dec 2022 11:12:43 +0100 Subject: [PATCH 1/5] create groups journeys --- go.mod | 2 +- handlers/application/journeys.go | 352 +++++++++++++++++++++++++++++++ main.go | 8 +- renderer/journeys.go | 129 ++++++++++- 4 files changed, 488 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7f6a52c..3282b9d 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 // replace git.coopgo.io/coopgo-platform/mobility-accounts => ../../coopgo-platform/mobility-accounts/ -// replace git.coopgo.io/coopgo-platform/groups-management => ../../coopgo-platform/groups-management/ +//replace git.coopgo.io/coopgo-platform/groups-management => ../../coopgo-platform/groups-management/ // replace git.coopgo.io/coopgo-platform/fleets => ../../coopgo-platform/fleets/ diff --git a/handlers/application/journeys.go b/handlers/application/journeys.go index 108e537..27571a9 100644 --- a/handlers/application/journeys.go +++ b/handlers/application/journeys.go @@ -5,14 +5,35 @@ import ( "encoding/json" "fmt" "net/http" + "sort" + "strings" "time" fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi" + groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" + groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage" + mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" + "github.com/google/uuid" + "github.com/gorilla/mux" geojson "github.com/paulmach/go.geojson" "gitlab.scity.coop/maas/navitia-golang" "gitlab.scity.coop/maas/navitia-golang/types" + "google.golang.org/protobuf/types/known/structpb" ) +var Depart any +var Arrive any + +// type Typep string + +// const ( +// rec Typep = "rec" +// pon Typep = "pon" +// ) + +// var departdate string +// var departTime string + func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) { r.ParseForm() @@ -144,3 +165,334 @@ func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Reque h.Renderer.JourneysSearch(w, r, carpoolresults, journeys, vehicles, searched, departuregeo, destinationgeo, departuredate, departuretime) } + +type GroupsModule []groupstorage.Group + +func (a GroupsModule) Len() int { return len(a) } +func (a GroupsModule) Less(i, j int) bool { + return strings.Compare(a[i].Data["name"].(string), a[j].Data["name"].(string)) < 0 +} +func (a GroupsModule) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func (h *ApplicationHandler) GroupsGestion(w http.ResponseWriter, r *http.Request) { + + request := &groupsmanagement.GetGroupsRequest{ + Namespaces: []string{"parcoursmob_groups_covoiturage"}, + } + + resp, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + var groups = []groupstorage.Group{} + + for _, group := range resp.Groups { + g := group.ToStorageType() + groups = append(groups, g) + } + ////////////////// + + cacheid := uuid.NewString() + + ///////////////// + sort.Sort(GroupsModule(groups)) + h.cache.PutWithTTL(cacheid, groups, 1*time.Hour) + + h.Renderer.GroupsGestion(w, r, groups, cacheid) +} +func filterAcc(r *http.Request, a *mobilityaccounts.Account) bool { + searchFilter, ok := r.URL.Query()["search"] + + if ok && len(searchFilter[0]) > 0 { + name := a.Data.AsMap()["first_name"].(string) + " " + a.Data.AsMap()["last_name"].(string) + if !strings.Contains(strings.ToLower(name), strings.ToLower(searchFilter[0])) { + return false + } + } + + return true +} + +func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) { + + var beneficiary any + var ( + departurgeo *geojson.Feature + dstinationgeo *geojson.Feature + ) + searched := false + // var accounts = []any{} + // cacheid := uuid.NewString() + // accountsBeneficaire, err := h.beneficiaries(r) + // if err != nil { + // fmt.Println(err) + // w.WriteHeader(http.StatusBadRequest) + // return + // } + 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 { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + beneficiary = respbeneficiary.Account.ToStorageType() + + if r.Method == "POST" { + fmt.Println("herrre") + /////////////////////////////////////////// + departure := r.FormValue("departure") + destination := r.FormValue("destination") + + if departure != "" && destination != "" { + + var err error + + departurgeo, err = geojson.UnmarshalFeature([]byte(departure)) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + dstinationgeo, err = geojson.UnmarshalFeature([]byte(destination)) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + } + ////////////////////////////////////////////////////////////////////////////////////////////////////// + if r.FormValue("departure") != "" { + var a any + json.Unmarshal([]byte(r.FormValue("departure")), &a) + + Depart = a + } + if r.FormValue("destination") != "" { + var a any + json.Unmarshal([]byte(r.FormValue("destination")), &a) + + Arrive = a + } + r.ParseForm() + + ///////////////// + // locTime, errTime := time.LoadLocation("Europe/Paris") + // if errTime != nil { + // fmt.Println("Loading timezone location Europe/Paris error : ") + // fmt.Println("Missing zones in container ? ") + // panic(errTime) + // } + + // departdate := r.FormValue("departuredate") + // departTime := r.FormValue("departuretime") + //departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime) + + //////////////// + if r.FormValue("name") == "" { + + fmt.Println("invalid name") + w.WriteHeader(http.StatusBadRequest) + return + } + // if r.FormValue("driver") == "" { + + // fmt.Println("invalid name") + // w.WriteHeader(http.StatusBadRequest) + // return + // } + if r.FormValue("number") == "" { + + fmt.Println("invalid number of personne") + w.WriteHeader(http.StatusBadRequest) + return + } + // planType := map[string]any{ + // "rrecurrent": r.FormValue("recurrent") == "on", + // "pponctuelle": r.FormValue("ponctuelle") == "on", + // } + planDays := map[string]any{ + "lundi": r.FormValue("lundi") == "on", + "mardi": r.FormValue("mardi") == "on", + "mercredi": r.FormValue("mercredi") == "on", + "jeudi": r.FormValue("jeudi") == "on", + "vendredi": r.FormValue("vendredi") == "on", + "samedi": r.FormValue("samedi") == "on", + "dimanche": r.FormValue("dimanche") == "on", + } + // var rec bool = r.FormValue("recurrent") + // var ponc bool + groupidd := uuid.NewString() + fmt.Println("herrr&e") + dataMap := map[string]any{ + "name": r.FormValue("name"), + "number": r.FormValue("number"), + "driver_first_name": respbeneficiary.Account.ToStorageType().Data["first_name"], + "driver_last_name": respbeneficiary.Account.ToStorageType().Data["last_name"], + "depart": Depart, + "arrive": Arrive, + "departdate": r.FormValue("departdate"), + "date": r.FormValue("date"), + "enddate": r.FormValue("enddate"), + "departtime": r.FormValue("departtime"), + "time": r.FormValue("time"), + // "ponctdate": r.FormValue("ponctdate"), + //"planType": planType, + "planDays": planDays, + "recurrent": r.FormValue("recurrent"), + "pontuelle": r.FormValue("ponctuelle"), + // "r": rec, + // "p": pon, + } + fmt.Println(dataMap["ponctuelle"]) + data, err := structpb.NewValue(dataMap) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + request_organization := &groupsmanagement.AddGroupRequest{ + Group: &groupsmanagement.Group{ + Id: groupidd, + Namespace: "parcoursmob_groups_covoiturage", + Data: data.GetStructValue(), + }, + } + + _, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_organization) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + ////////////////////////////////////////////////// + // request_organizatio := &groupsmanagement.AddGroupMemberRequest{ + // Group: &groupsmanagement.GroupMember{ + // Id: groupidd, + // //Namespace: "parcoursmob_groups_covoiturage", + // Data: data.GetStructValue(), + // }, + // } + + // _, err = h.services.GRPC.GroupsManagement.AddGroupMember(context.TODO(), request_organizatio) + // if err != nil { + // fmt.Println(err) + // w.WriteHeader(http.StatusInternalServerError) + // return + // } + //////////////////////////////////////////////// + http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", request_organization.Group.ToStorageType().ID), http.StatusFound) + + //http.Redirect(w, r, fmt.Print("/app/journeys"), http.StatusFound) + return + } + } + accountsBeneficaire, err := h.beneficiaries(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + h.Renderer.CreateGroup(w, r, Depart, Arrive, searched, beneficiary, accountsBeneficaire, departurgeo, dstinationgeo) + +} + +func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + groupid := vars["groupid"] + + request := &groupsmanagement.GetGroupRequest{ + Id: groupid, + } + + resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + var accounts = []any{} + + requesst := &mobilityaccounts.GetAccountsBatchRequest{ + Accountids: resp.Group.Members, + } + + ressp, _ := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), requesst) + // if err != nil { + // return err + // } + for _, account := range ressp.Accounts { + if filterAcc(r, account) { + a := account.ToStorageType() + accounts = append(accounts, a) + } + } + + cacheid := uuid.NewString() + h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour) + r.ParseForm() + + var beneficiary any + + searched := false + + // if r.Method == "POST" { + 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 { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + beneficiary = respbeneficiary.Account.ToStorageType() + + subscribe := &groupsmanagement.SubscribeRequest{ + Groupid: resp.Group.ToStorageType().ID, + Memberid: respbeneficiary.Account.Id, + } + + _, err = h.services.GRPC.GroupsManagement.Subscribe(context.TODO(), subscribe) + + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", resp.Group.ToStorageType().ID), http.StatusFound) + return + } + + accountsBeneficaire, err := h.beneficiaries(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + //h.Renderer.BeneficaireSearch(w, r, accounts, searched, beneficiary, resp.Group.ToStorageType()) + h.Renderer.DisplayGroupCovoiturage(w, r, resp.Group.ToStorageType().ID, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire) +} diff --git a/main.go b/main.go index 8fe2c90..082d486 100644 --- a/main.go +++ b/main.go @@ -107,14 +107,20 @@ func main() { application.HandleFunc("/group/settings", applicationHandler.GroupSettingsDisplay) application.HandleFunc("/group/settings/invite-member", applicationHandler.GroupSettingsInviteMember) + /****************************Groupe Déplacement ************************************/ + application.HandleFunc("/journeys/groups_covoiturage", applicationHandler.GroupsGestion) + application.HandleFunc("/journeys/groups_covoiturage/create", applicationHandler.CreateGroup) + application.HandleFunc("/journeys/groups_covoiturage/create/{groupid}", applicationHandler.DisplayGroupCovoiturage) + /****************************************************************/ + /********************Code Supprt Emailing************************/ application.HandleFunc("/support/", applicationHandler.SupportSend) /*********************** CODE GROUP **************************/ + appGroup := application.PathPrefix("/group_module").Subrouter() appGroup.HandleFunc("/", applicationHandler.Groups) appGroup.HandleFunc("/groups", applicationHandler.CreateGroupModule) appGroup.HandleFunc("/groups/{groupid}", applicationHandler.DisplayGroupModule) - /****************************************************************/ //TODO Subrouters with middlewares checking security for each module ? application.Use(idp.Middleware) diff --git a/renderer/journeys.go b/renderer/journeys.go index c488f4a..e4ea288 100644 --- a/renderer/journeys.go +++ b/renderer/journeys.go @@ -1,9 +1,41 @@ package renderer -import "net/http" +import ( + "encoding/json" + "html/template" + "net/http" + + groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage" + mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage" +) const journeysMenu = "journeys" +type BeneficiariesCovoiturage struct { + Group string `json:"group"` + Count int `json:"count"` + CacheId string `json:"cache_id"` + Beneficiaries []any `json:"beneficiaries"` +} +type BeneficiariesCovoiturageA struct { + //Group string `json:"group"` + Count int `json:"count"` + CacheId string `json:"cache_id"` + Beneficiaries []any `json:"beneficiaries"` +} + +func (s BeneficiariesCovoiturage) JSON() template.JS { + result, _ := json.Marshal(s) + return template.JS(result) +} + +func (s BeneficiariesCovoiturage) JSONWithLimits(a int, b int) template.JS { + if b < len(s.Beneficiaries) { + s.Beneficiaries = s.Beneficiaries[a:b] + } + return s.JSON() +} + func (renderer *Renderer) JourneysSearch(w http.ResponseWriter, r *http.Request, carpools any, transitjourneys any, vehicles []any, searched bool, departure any, destination any, departuredate string, departuretime string) { files := renderer.ThemeConfig.GetStringSlice("views.journeys.search.files") state := NewState(r, renderer.ThemeConfig, journeysMenu) @@ -20,3 +52,98 @@ func (renderer *Renderer) JourneysSearch(w http.ResponseWriter, r *http.Request, renderer.Render("journeys", w, r, files, state) } + +type BeneficiariesListstate struct { + Count int `json:"count"` + CacheId string `json:"cache_id"` + Beneficiaries []groupstorage.Group `json:"beneficiaries"` +} + +func (s BeneficiariesListstate) JSON() template.JS { + result, _ := json.Marshal(s) + return template.JS(result) +} + +func (s BeneficiariesListstate) JSONWithLimits(a int, b int) template.JS { + if b < len(s.Beneficiaries) { + s.Beneficiaries = s.Beneficiaries[a:b] + } + return s.JSON() +} +func (renderer *Renderer) GroupsGestion(w http.ResponseWriter, r *http.Request, groups []groupstorage.Group, cacheid string) { + files := renderer.ThemeConfig.GetStringSlice("views.journeys.list.files") + state := NewState(r, renderer.ThemeConfig, journeysMenu) + // state.ViewState = map[string]any{ + // "groups": groups, + // } + + //state := NewState(r, renderer.ThemeConfig, beneficiariesMenu) + state.ViewState = BeneficiariesListstate{ + Count: len(groups), + CacheId: cacheid, + Beneficiaries: groups, + } + + renderer.Render("journeys", w, r, files, state) +} + +func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, depart any, arrive any, searched bool, beneficiary any, beneficiaries []mobilityaccountsstorage.Account, departure any, destination any) { + files := renderer.ThemeConfig.GetStringSlice("views.journeys.create.files") + state := NewState(r, renderer.ThemeConfig, journeysMenu) + // state.ViewState = map[string]any{ + // "departure": depart, + // "destination": arrive, + // //"beneficiary": beneficiary, + // //"accounts": accounts, + // // "cacheid": cacheid, + // // "searched": searched, + // } + viewstate := map[string]any{ + "deeparture": depart, + "deestination": arrive, + "beneficiaries": beneficiaries, + "searched": searched, + "departure": departure, + "destination": destination, + // "departuredate": departuredate, + // "departuretime": departuretime, + } + + if searched { + viewstate["search"] = map[string]any{ + "beneficiary": beneficiary, + } + + } + + state.ViewState = viewstate + renderer.Render("journeys", w, r, files, state) +} + +func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account) { + files := renderer.ThemeConfig.GetStringSlice("views.journeys.display.files") + state := NewState(r, renderer.ThemeConfig, journeysMenu) + + viewstate := map[string]any{ + "beneficiaries": beneficiaries, + "searched": searched, + "group": group, + "list": BeneficiariesCovoiturage{ + Group: groupid, + Count: len(accounts), + CacheId: cacheid, + Beneficiaries: accounts, + }, + } + + if searched { + viewstate["search"] = map[string]any{ + "beneficiary": beneficiary, + } + + } + + state.ViewState = viewstate + + renderer.Render("journeys", w, r, files, state) +} From a2b644d548eeaa14f1ba0a805a5b743435dc60bc Mon Sep 17 00:00:00 2001 From: soukainna Date: Wed, 4 Jan 2023 11:55:18 +0100 Subject: [PATCH 2/5] add more information about beneficaire in groupCovoiturage --- handlers/application/journeys.go | 128 +++++++++++++------------------ renderer/journeys.go | 19 +---- 2 files changed, 57 insertions(+), 90 deletions(-) diff --git a/handlers/application/journeys.go b/handlers/application/journeys.go index 27571a9..d95d040 100644 --- a/handlers/application/journeys.go +++ b/handlers/application/journeys.go @@ -24,16 +24,6 @@ import ( var Depart any var Arrive any -// type Typep string - -// const ( -// rec Typep = "rec" -// pon Typep = "pon" -// ) - -// var departdate string -// var departTime string - func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) { r.ParseForm() @@ -224,16 +214,9 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) dstinationgeo *geojson.Feature ) searched := false - // var accounts = []any{} - // cacheid := uuid.NewString() - // accountsBeneficaire, err := h.beneficiaries(r) - // if err != nil { - // fmt.Println(err) - // w.WriteHeader(http.StatusBadRequest) - // return - // } + if r.FormValue("beneficiaryid") != "" { - // Handler form + searched = true requestbeneficiary := &mobilityaccounts.GetAccountRequest{ @@ -250,8 +233,6 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) beneficiary = respbeneficiary.Account.ToStorageType() if r.Method == "POST" { - fmt.Println("herrre") - /////////////////////////////////////////// departure := r.FormValue("departure") destination := r.FormValue("destination") @@ -288,41 +269,20 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) } r.ParseForm() - ///////////////// - // locTime, errTime := time.LoadLocation("Europe/Paris") - // if errTime != nil { - // fmt.Println("Loading timezone location Europe/Paris error : ") - // fmt.Println("Missing zones in container ? ") - // panic(errTime) - // } - - // departdate := r.FormValue("departuredate") - // departTime := r.FormValue("departuretime") - //departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime) - - //////////////// if r.FormValue("name") == "" { fmt.Println("invalid name") w.WriteHeader(http.StatusBadRequest) return } - // if r.FormValue("driver") == "" { - // fmt.Println("invalid name") - // w.WriteHeader(http.StatusBadRequest) - // return - // } if r.FormValue("number") == "" { fmt.Println("invalid number of personne") w.WriteHeader(http.StatusBadRequest) return } - // planType := map[string]any{ - // "rrecurrent": r.FormValue("recurrent") == "on", - // "pponctuelle": r.FormValue("ponctuelle") == "on", - // } + planDays := map[string]any{ "lundi": r.FormValue("lundi") == "on", "mardi": r.FormValue("mardi") == "on", @@ -332,10 +292,9 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) "samedi": r.FormValue("samedi") == "on", "dimanche": r.FormValue("dimanche") == "on", } - // var rec bool = r.FormValue("recurrent") - // var ponc bool + groupidd := uuid.NewString() - fmt.Println("herrr&e") + dataMap := map[string]any{ "name": r.FormValue("name"), "number": r.FormValue("number"), @@ -348,15 +307,12 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) "enddate": r.FormValue("enddate"), "departtime": r.FormValue("departtime"), "time": r.FormValue("time"), - // "ponctdate": r.FormValue("ponctdate"), - //"planType": planType, + "planDays": planDays, "recurrent": r.FormValue("recurrent"), "pontuelle": r.FormValue("ponctuelle"), - // "r": rec, - // "p": pon, } - fmt.Println(dataMap["ponctuelle"]) + data, err := structpb.NewValue(dataMap) if err != nil { fmt.Println(err) @@ -378,25 +334,9 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusInternalServerError) return } - ////////////////////////////////////////////////// - // request_organizatio := &groupsmanagement.AddGroupMemberRequest{ - // Group: &groupsmanagement.GroupMember{ - // Id: groupidd, - // //Namespace: "parcoursmob_groups_covoiturage", - // Data: data.GetStructValue(), - // }, - // } - // _, err = h.services.GRPC.GroupsManagement.AddGroupMember(context.TODO(), request_organizatio) - // if err != nil { - // fmt.Println(err) - // w.WriteHeader(http.StatusInternalServerError) - // return - // } - //////////////////////////////////////////////// http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", request_organization.Group.ToStorageType().ID), http.StatusFound) - //http.Redirect(w, r, fmt.Print("/app/journeys"), http.StatusFound) return } } @@ -433,9 +373,7 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h } ressp, _ := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), requesst) - // if err != nil { - // return err - // } + for _, account := range ressp.Accounts { if filterAcc(r, account) { a := account.ToStorageType() @@ -451,9 +389,8 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h searched := false - // if r.Method == "POST" { if r.FormValue("beneficiaryid") != "" { - // Handler form + searched = true requestbeneficiary := &mobilityaccounts.GetAccountRequest{ @@ -482,6 +419,50 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h return } + ////////////////////////////////////////////////////////////////////////////////////////////////////// + if r.FormValue("departure") != "" { + var a any + json.Unmarshal([]byte(r.FormValue("departure")), &a) + + Depart = a + } + if r.FormValue("destination") != "" { + var a any + json.Unmarshal([]byte(r.FormValue("destination")), &a) + + Arrive = a + } + r.ParseForm() + dataMap := map[string]any{ + + "depart": Depart, + "arrive": Arrive, + } + + data, err := structpb.NewValue(dataMap) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + ////////////////////////////////////////////// + request_organizatio := &groupsmanagement.SubscribeMemberRequest{ + Group: &groupsmanagement.GroupMember{ + Memberid: respbeneficiary.Account.Id, + Groupid: resp.Group.ToStorageType().ID, + Data: data.GetStructValue(), + }, + } + + _, err = h.services.GRPC.GroupsManagement.SubscribeMember(context.TODO(), request_organizatio) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + //////////////////////////////////////////////// + http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", resp.Group.ToStorageType().ID), http.StatusFound) return } @@ -493,6 +474,5 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h return } - //h.Renderer.BeneficaireSearch(w, r, accounts, searched, beneficiary, resp.Group.ToStorageType()) - h.Renderer.DisplayGroupCovoiturage(w, r, resp.Group.ToStorageType().ID, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire) + h.Renderer.DisplayGroupCovoiturage(w, r, resp.Group.ToStorageType().ID, Depart, Arrive, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire) } diff --git a/renderer/journeys.go b/renderer/journeys.go index e4ea288..693f1e4 100644 --- a/renderer/journeys.go +++ b/renderer/journeys.go @@ -18,7 +18,6 @@ type BeneficiariesCovoiturage struct { Beneficiaries []any `json:"beneficiaries"` } type BeneficiariesCovoiturageA struct { - //Group string `json:"group"` Count int `json:"count"` CacheId string `json:"cache_id"` Beneficiaries []any `json:"beneficiaries"` @@ -73,11 +72,7 @@ func (s BeneficiariesListstate) JSONWithLimits(a int, b int) template.JS { func (renderer *Renderer) GroupsGestion(w http.ResponseWriter, r *http.Request, groups []groupstorage.Group, cacheid string) { files := renderer.ThemeConfig.GetStringSlice("views.journeys.list.files") state := NewState(r, renderer.ThemeConfig, journeysMenu) - // state.ViewState = map[string]any{ - // "groups": groups, - // } - //state := NewState(r, renderer.ThemeConfig, beneficiariesMenu) state.ViewState = BeneficiariesListstate{ Count: len(groups), CacheId: cacheid, @@ -90,14 +85,6 @@ func (renderer *Renderer) GroupsGestion(w http.ResponseWriter, r *http.Request, func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, depart any, arrive any, searched bool, beneficiary any, beneficiaries []mobilityaccountsstorage.Account, departure any, destination any) { files := renderer.ThemeConfig.GetStringSlice("views.journeys.create.files") state := NewState(r, renderer.ThemeConfig, journeysMenu) - // state.ViewState = map[string]any{ - // "departure": depart, - // "destination": arrive, - // //"beneficiary": beneficiary, - // //"accounts": accounts, - // // "cacheid": cacheid, - // // "searched": searched, - // } viewstate := map[string]any{ "deeparture": depart, "deestination": arrive, @@ -105,8 +92,6 @@ func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, de "searched": searched, "departure": departure, "destination": destination, - // "departuredate": departuredate, - // "departuretime": departuretime, } if searched { @@ -120,7 +105,7 @@ func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, de renderer.Render("journeys", w, r, files, state) } -func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account) { +func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, depart any, arrive any, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account) { files := renderer.ThemeConfig.GetStringSlice("views.journeys.display.files") state := NewState(r, renderer.ThemeConfig, journeysMenu) @@ -128,6 +113,8 @@ func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http "beneficiaries": beneficiaries, "searched": searched, "group": group, + "deeparture": depart, + "deestination": arrive, "list": BeneficiariesCovoiturage{ Group: groupid, Count: len(accounts), From e03f31d3f433654efcd979663a7da0efcfd3a069 Mon Sep 17 00:00:00 2001 From: soukainna Date: Wed, 4 Jan 2023 16:12:49 +0100 Subject: [PATCH 3/5] fix some err --- handlers/application/journeys.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/handlers/application/journeys.go b/handlers/application/journeys.go index d95d040..d46cf23 100644 --- a/handlers/application/journeys.go +++ b/handlers/application/journeys.go @@ -418,8 +418,7 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h w.WriteHeader(http.StatusInternalServerError) return } - - ////////////////////////////////////////////////////////////////////////////////////////////////////// + /*******************Code to store more information about mermbers groupscovoiturage**************/ if r.FormValue("departure") != "" { var a any json.Unmarshal([]byte(r.FormValue("departure")), &a) @@ -446,7 +445,6 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h return } - ////////////////////////////////////////////// request_organizatio := &groupsmanagement.SubscribeMemberRequest{ Group: &groupsmanagement.GroupMember{ Memberid: respbeneficiary.Account.Id, @@ -461,7 +459,7 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h w.WriteHeader(http.StatusInternalServerError) return } - //////////////////////////////////////////////// + /******************************************************************************************************/ http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", resp.Group.ToStorageType().ID), http.StatusFound) return From 774a3c2301176125b2f236db7beb40d2a1fb21ef Mon Sep 17 00:00:00 2001 From: soukainna Date: Tue, 17 Jan 2023 14:45:09 +0100 Subject: [PATCH 4/5] add address od members and draw position map --- go.mod | 2 +- handlers/application/journeys.go | 107 ++++++++++++++++++++++++++++--- main.go | 1 + renderer/journeys.go | 18 +++++- 4 files changed, 116 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index e7d8047..66b43ee 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 // replace git.coopgo.io/coopgo-platform/mobility-accounts => ../../coopgo-platform/mobility-accounts/ -//replace git.coopgo.io/coopgo-platform/groups-management => ../../coopgo-platform/groups-management/ +replace git.coopgo.io/coopgo-platform/groups-management => ../../coopgo-platform/groups-management/ // replace git.coopgo.io/coopgo-platform/fleets => ../../coopgo-platform/fleets/ diff --git a/handlers/application/journeys.go b/handlers/application/journeys.go index d46cf23..3aa2055 100644 --- a/handlers/application/journeys.go +++ b/handlers/application/journeys.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "sort" + "strconv" "strings" "time" @@ -183,11 +184,9 @@ func (h *ApplicationHandler) GroupsGestion(w http.ResponseWriter, r *http.Reques g := group.ToStorageType() groups = append(groups, g) } - ////////////////// cacheid := uuid.NewString() - ///////////////// sort.Sort(GroupsModule(groups)) h.cache.PutWithTTL(cacheid, groups, 1*time.Hour) @@ -254,7 +253,6 @@ func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) return } } - ////////////////////////////////////////////////////////////////////////////////////////////////////// if r.FormValue("departure") != "" { var a any json.Unmarshal([]byte(r.FormValue("departure")), &a) @@ -386,7 +384,6 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h r.ParseForm() var beneficiary any - searched := false if r.FormValue("beneficiaryid") != "" { @@ -437,7 +434,7 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h "depart": Depart, "arrive": Arrive, } - + id := uuid.NewString() data, err := structpb.NewValue(dataMap) if err != nil { fmt.Println(err) @@ -445,26 +442,46 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h return } - request_organizatio := &groupsmanagement.SubscribeMemberRequest{ + request_organizatio := &groupsmanagement.AddGroupMemberRequest{ Group: &groupsmanagement.GroupMember{ + Id: id, Memberid: respbeneficiary.Account.Id, Groupid: resp.Group.ToStorageType().ID, Data: data.GetStructValue(), }, } - _, err = h.services.GRPC.GroupsManagement.SubscribeMember(context.TODO(), request_organizatio) + _, err = h.services.GRPC.GroupsManagement.AddGroupMember(context.TODO(), request_organizatio) if err != nil { fmt.Println(err) w.WriteHeader(http.StatusInternalServerError) return } - /******************************************************************************************************/ http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", resp.Group.ToStorageType().ID), http.StatusFound) return } + //////////find all groups to store the adresse passenger/////// + grp := &groupsmanagement.GetGroupsBatchMemberRequest{ + Groupids: []string{resp.Group.ToStorageType().ID}, + } + s, err := h.services.GRPC.GroupsManagement.GetGroupsBatchMember(context.TODO(), grp) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + groups := map[string]any{} + + if err == nil { + for _, g := range s.Groups { + groups[g.Memberid] = g.ToStorageType() + } + } + + var number string = strconv.Itoa(len(resp.Group.Members)) + ///////////////////// accountsBeneficaire, err := h.beneficiaries(r) if err != nil { fmt.Println(err) @@ -472,5 +489,77 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h return } - h.Renderer.DisplayGroupCovoiturage(w, r, resp.Group.ToStorageType().ID, Depart, Arrive, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire) + h.Renderer.DisplayGroupCovoiturage(w, r, number, resp.Group.ToStorageType().ID, Depart, Arrive, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire, groups) +} + +func (h *ApplicationHandler) UpdateGroupCovoiturage(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + id := vars["id"] + groupid := vars["groupid"] + memberid := vars["memberid"] + + if r.Method == "POST" { + + //////////get groupid covoiturage////////// + request := &groupsmanagement.GetGroupRequest{ + Id: groupid, + } + + resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + //////////////////////////get group member//////////////////////////////// + + reequest := &groupsmanagement.GetGroupMemberRequest{ + Id: id, + } + + ressp, err := h.services.GRPC.GroupsManagement.GetGroupMember(context.TODO(), reequest) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + req := &groupsmanagement.UnsubscribeMemberRequest{ + Id: ressp.Group.Id, + } + + _, errr := h.services.GRPC.GroupsManagement.UnsubscribeMember(context.TODO(), req) + if errr != nil { + fmt.Println(errr) + w.WriteHeader(http.StatusInternalServerError) + return + } + + members := resp.Group.Members + for i := 0; i < len(members); i++ { + if members[i] == memberid { + members = append(members[:i], members[(i+1):]...) + resp.Group.Members = members + reequest := &groupsmanagement.UnsubscribeRequest{ + Groupid: resp.Group.ToStorageType().ID, + Memberid: memberid, + } + + _, err := h.services.GRPC.GroupsManagement.Unsubscribe(context.TODO(), reequest) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + } + + } + http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", groupid), http.StatusFound) + /* + I must add "return" to resolve the err + http: superfluous response.WriteHeader call from git.coopgo.io/coopgo-apps/parcoursmob/renderer.(*Renderer).Render (renderer.go:50) + */ + return + } + h.Renderer.UpdateGroupCovoiturage(w, r, groupid, memberid) } diff --git a/main.go b/main.go index a787c85..f57873e 100644 --- a/main.go +++ b/main.go @@ -112,6 +112,7 @@ func main() { application.HandleFunc("/journeys/groups_covoiturage", applicationHandler.GroupsGestion) application.HandleFunc("/journeys/groups_covoiturage/create", applicationHandler.CreateGroup) application.HandleFunc("/journeys/groups_covoiturage/create/{groupid}", applicationHandler.DisplayGroupCovoiturage) + application.HandleFunc("/journeys/groups_covoiturage/create/{id}/{groupid}/{memberid}", applicationHandler.UpdateGroupCovoiturage) /****************************************************************/ /********************Code Supprt Emailing************************/ diff --git a/renderer/journeys.go b/renderer/journeys.go index 693f1e4..accf061 100644 --- a/renderer/journeys.go +++ b/renderer/journeys.go @@ -105,7 +105,7 @@ func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, de renderer.Render("journeys", w, r, files, state) } -func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, depart any, arrive any, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account) { +func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, number string, groupid string, depart any, arrive any, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account, groups map[string]any) { files := renderer.ThemeConfig.GetStringSlice("views.journeys.display.files") state := NewState(r, renderer.ThemeConfig, journeysMenu) @@ -115,6 +115,9 @@ func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http "group": group, "deeparture": depart, "deestination": arrive, + "groups": groups, + "ben": accounts, + "number": number, "list": BeneficiariesCovoiturage{ Group: groupid, Count: len(accounts), @@ -129,8 +132,19 @@ func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http } } - state.ViewState = viewstate renderer.Render("journeys", w, r, files, state) } + +func (renderer *Renderer) UpdateGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, memberid string) { + files := renderer.ThemeConfig.GetStringSlice("views.journeys.update.files") + state := NewState(r, renderer.ThemeConfig, journeysMenu) + + state.ViewState = map[string]any{ + "groupid": groupid, + "memberid": memberid, + } + renderer.Render("journeys", w, r, files, state) + +} From 40cf6012a6d90a82746f4cb802e51d5e98441cb2 Mon Sep 17 00:00:00 2001 From: soukainna Date: Mon, 23 Jan 2023 10:15:23 +0100 Subject: [PATCH 5/5] optimize code --- handlers/application/journeys.go | 35 +++++++++++++++++--------------- services/groupsmanagement.go | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/handlers/application/journeys.go b/handlers/application/journeys.go index 3aa2055..c8f143b 100644 --- a/handlers/application/journeys.go +++ b/handlers/application/journeys.go @@ -462,24 +462,27 @@ func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *h return } //////////find all groups to store the adresse passenger/////// - grp := &groupsmanagement.GetGroupsBatchMemberRequest{ + // grp := &groupsmanagement.GetGroupsBatchMemberRequest{ - Groupids: []string{resp.Group.ToStorageType().ID}, - } - s, err := h.services.GRPC.GroupsManagement.GetGroupsBatchMember(context.TODO(), grp) - if err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - groups := map[string]any{} - - if err == nil { - for _, g := range s.Groups { - groups[g.Memberid] = g.ToStorageType() - } - } + // Groupids: []string{resp.Group.ToStorageType().ID}, + // } + // s, err := h.services.GRPC.GroupsManagement.GetGroupsBatchMember(context.TODO(), grp) + // if err != nil { + // fmt.Println(err) + // w.WriteHeader(http.StatusInternalServerError) + // return + // } + // groups := map[string]any{} + // if err == nil { + // for _, g := range s.Groups { + // groups[g.Memberid] = g.ToStorageType() + // } + // } + //////////find all groups to store the adresse passenger/////// + ///////////try to optimise the code //////////////////////////// + groups, _ := h.services.GetGroupsMemberMap(resp.Group.ToStorageType().ID) + //fmt.Println(groups) var number string = strconv.Itoa(len(resp.Group.Members)) ///////////////////// accountsBeneficaire, err := h.beneficiaries(r) diff --git a/services/groupsmanagement.go b/services/groupsmanagement.go index f5bf047..08647b7 100644 --- a/services/groupsmanagement.go +++ b/services/groupsmanagement.go @@ -1,7 +1,10 @@ package services import ( + "context" + groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" + "git.coopgo.io/coopgo-platform/groups-management/storage" "google.golang.org/grpc" ) @@ -21,3 +24,35 @@ func NewGroupsManagementService(groupsManagementDial string) (*GroupsManagementS GroupsManagementClient: client, }, nil } + +func (s *ServicesHandler) GetGroupsMap() (groups map[string]storage.Group, err error) { + groups = map[string]storage.Group{} + + request := &groupsmanagement.GetGroupsRequest{ + Namespaces: []string{"parcoursmob_organizations"}, + } + resp, err := s.GRPC.GroupsManagement.GetGroups(context.TODO(), request) + if err == nil { + for _, group := range resp.Groups { + groups[group.Id] = group.ToStorageType() + } + } + + return +} + +////////////////////////////////optimize the code////////////////////////////////////// +func (s *ServicesHandler) GetGroupsMemberMap(id string) (groups map[string]any, err error) { + groups = map[string]any{} + + request := &groupsmanagement.GetGroupsBatchMemberRequest{ + Groupids: []string{id}, + } + resp, err := s.GRPC.GroupsManagement.GetGroupsBatchMember(context.TODO(), request) + if err == nil { + for _, group := range resp.Groups { + groups[group.Memberid] = group.ToStorageType() + } + } + return +}