From aa13d60ca6be2becf1a8f487e20dd91eb399bd8c Mon Sep 17 00:00:00 2001 From: sbouaram Date: Tue, 25 Apr 2023 04:37:20 -0400 Subject: [PATCH] AdministrationGroupInviteMember --- handlers/application/administration.go | 274 +++++++++++++------------ 1 file changed, 138 insertions(+), 136 deletions(-) diff --git a/handlers/application/administration.go b/handlers/application/administration.go index 15b93a5..4151433 100755 --- a/handlers/application/administration.go +++ b/handlers/application/administration.go @@ -211,165 +211,167 @@ func (h *ApplicationHandler) AdministrationGroupDisplay(w http.ResponseWriter, r func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) groupid := vars["groupid"] + var ( + groupresp *groupsmanagement.GetGroupResponse + accountresp *accounts.GetAccountUsernameResponse + err error + ) + go func() { + groupresp, err = h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ + Id: groupid, + Namespace: "parcoursmob_organizations", + }) - groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ - Id: groupid, - Namespace: "parcoursmob_organizations", - }) - - if err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + }() r.ParseForm() - accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{ - Username: r.FormValue("username"), - Namespace: "parcoursmob", - }) + go func() { + accountresp, err = h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{ + Username: r.FormValue("username"), + Namespace: "parcoursmob", + }) + if err == nil { + // Account already exists : adding the existing account to admin list + account := accountresp.Account.ToStorageType() + account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid) + account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin") - if err == nil { - // Account already exists : adding the existing account to admin list - account := accountresp.Account.ToStorageType() - account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid) - account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin") + as, _ := accounts.AccountFromStorageType(&account) - as, _ := accounts.AccountFromStorageType(&account) + _, err = h.services.GRPC.MobilityAccounts.UpdateData( + context.TODO(), + &accounts.UpdateDataRequest{ + Account: as, + }, + ) - _, err = h.services.GRPC.MobilityAccounts.UpdateData( - context.TODO(), - &accounts.UpdateDataRequest{ - Account: as, - }, - ) - - fmt.Println(err) - - data := map[string]any{ - "group": groupresp.Group.ToStorageType().Data["name"], - } - - if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil { fmt.Println(err) + + data := map[string]any{ + "group": groupresp.Group.ToStorageType().Data["name"], + } + + if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil { + fmt.Println(err) + } + + http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound) + return + } else { + // Onboard now administrator + onboarding := map[string]any{ + "username": r.FormValue("username"), + "group": groupid, + "admin": true, + } + + b := make([]byte, 16) + if _, err := io.ReadFull(rand.Reader, b); err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + key := base64.RawURLEncoding.EncodeToString(b) + + h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL + + data := map[string]any{ + "group": groupresp.Group.ToStorageType().Data["name"], + "key": key, + } + + if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } } http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound) return - } else { - // Onboard now administrator - onboarding := map[string]any{ - "username": r.FormValue("username"), - "group": groupid, - "admin": true, - } - - b := make([]byte, 16) - if _, err := io.ReadFull(rand.Reader, b); err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - key := base64.RawURLEncoding.EncodeToString(b) - - h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL - - data := map[string]any{ - "group": groupresp.Group.ToStorageType().Data["name"], - "key": key, - } - - if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - } - - http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound) - return + }() } - func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) groupid := vars["groupid"] + var ( + group storage.Group + ) + groupCh := make(chan storage.Group) + go func() { + groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ + Id: groupid, + Namespace: "parcoursmob_organizations", + }) - groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ - Id: groupid, - Namespace: "parcoursmob_organizations", - }) - - if err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - - group := groupresp.Group.ToStorageType() - + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + group := groupresp.Group.ToStorageType() + groupCh <- group + }() r.ParseForm() + go func() { + group = <-groupCh + accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{ + Username: r.FormValue("username"), + Namespace: "parcoursmob", + }) + if err == nil { + account := accountresp.Account.ToStorageType() + account.Data["groups"] = append(account.Data["groups"].([]any), group.ID) - accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{ - Username: r.FormValue("username"), - Namespace: "parcoursmob", - }) + as, _ := accounts.AccountFromStorageType(&account) - if err == nil { - account := accountresp.Account.ToStorageType() - account.Data["groups"] = append(account.Data["groups"].([]any), group.ID) + _, err = h.services.GRPC.MobilityAccounts.UpdateData( + context.TODO(), + &accounts.UpdateDataRequest{ + Account: as, + }, + ) + data := map[string]any{ + "group": group.Data["name"], + } - as, _ := accounts.AccountFromStorageType(&account) + if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil { + } - _, err = h.services.GRPC.MobilityAccounts.UpdateData( - context.TODO(), - &accounts.UpdateDataRequest{ - Account: as, - }, - ) - - fmt.Println(err) - - data := map[string]any{ - "group": group.Data["name"], - } - - if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil { - fmt.Println(err) - } - - http.Redirect(w, r, "/app/group/settings", http.StatusFound) - return - } else { - // Onboard now administrator - onboarding := map[string]any{ - "username": r.FormValue("username"), - "group": group.ID, - "admin": false, - } - - b := make([]byte, 16) - if _, err := io.ReadFull(rand.Reader, b); err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) + http.Redirect(w, r, "/app/group/settings", http.StatusFound) return + } else { + // Onboard now administrator + onboarding := map[string]any{ + "username": r.FormValue("username"), + "group": group.ID, + "admin": false, + } + + b := make([]byte, 16) + if _, err := io.ReadFull(rand.Reader, b); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + key := base64.RawURLEncoding.EncodeToString(b) + + h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL + data := map[string]any{ + "group": group.Data["name"], + "key": key, + } + + if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } } - key := base64.RawURLEncoding.EncodeToString(b) - - h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL - - data := map[string]any{ - "group": group.Data["name"], - "key": key, - } - - if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - } - + }() http.Redirect(w, r, "/app/administration/groups/"+group.ID, http.StatusFound) return }