From fa065f2a436086fb92b4ec04731c847399695fef Mon Sep 17 00:00:00 2001 From: soukainna Date: Wed, 22 Feb 2023 11:02:37 +0100 Subject: [PATCH] add member page with organisations --- handlers/application/members.go | 89 ++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/handlers/application/members.go b/handlers/application/members.go index 586b4e4..ae4c5c3 100644 --- a/handlers/application/members.go +++ b/handlers/application/members.go @@ -6,9 +6,12 @@ import ( "fmt" "net/http" "strings" + "time" formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators" + groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" + "github.com/google/uuid" "google.golang.org/protobuf/types/known/structpb" ) @@ -36,8 +39,34 @@ func (h *ApplicationHandler) MemberDisplay(w http.ResponseWriter, r *http.Reques w.WriteHeader(http.StatusInternalServerError) return } + //////////////////////////////////add organisations///////////////////////////////////////////////// - h.Renderer.MemberDisplay(w, r, resp.Account.ToStorageType()) + var allIds []string + for _, v := range resp.Account.ToStorageType().Data["groups"].([]any) { + s := fmt.Sprintf("%v", v) + if !(strings.Contains(s, "admin")) { + allIds = append(allIds, s) + } + } + reques := &groupsmanagement.GetGroupsBatchRequest{ + Groupids: allIds, + } + + res, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), reques) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + var groupsName []string + + for _, group := range res.Groups { + g := fmt.Sprintf("%v", group.ToStorageType().Data["name"]) + groupsName = append(groupsName, g) + } + + h.Renderer.MemberDisplay(w, r, resp.Account.ToStorageType(), groupsName) } func (h *ApplicationHandler) MemberUpdate(w http.ResponseWriter, r *http.Request) { @@ -76,7 +105,7 @@ func (h *ApplicationHandler) MemberUpdate(w http.ResponseWriter, r *http.Request return } - http.Redirect(w, r, fmt.Sprintf("/app/profile/%s", resp.Account.Id), http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("/app/members/%s", resp.Account.Id), http.StatusFound) return } @@ -126,3 +155,59 @@ func parseUserForm(r *http.Request) (map[string]any, error) { return dataMap, nil } + +func (h *ApplicationHandler) MembersList(w http.ResponseWriter, r *http.Request) { + + accounts, err := h.services.GetAccounts() + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + var groupsName []string + + for _, v := range accounts { + adminid := v.ID + request := &mobilityaccounts.GetAccountRequest{ + Id: adminid, + } + + resp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + //////////////////////////////////add organisations///////////////////////////////////////////////// + + var allIds []string + for _, v := range resp.Account.ToStorageType().Data["groups"].([]any) { + s := fmt.Sprintf("%v", v) + if !(strings.Contains(s, "admin")) { + allIds = append(allIds, s) + } + + } + + reques := &groupsmanagement.GetGroupsBatchRequest{ + Groupids: allIds, + } + + res, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), reques) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + g := "" + for _, group := range res.Groups { + g += fmt.Sprintf("%v", group.ToStorageType().Data["name"]) + " " + } + groupsName = append(groupsName, g) + + } + cacheid := uuid.NewString() + h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour) + + h.Renderer.MembersList(w, r, accounts, cacheid, groupsName) +}