From d46f62d92a0844c8c52e60549f193d633915e0e0 Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Thu, 21 Nov 2024 23:11:59 +0100 Subject: [PATCH] fix issues in groups members management --- handlers/api/calendars.go | 1 - handlers/application/administration.go | 258 +++++++++++-------------- handlers/application/vehicles.go | 7 +- main.go | 5 +- services/groupsmanagement.go | 14 ++ 5 files changed, 136 insertions(+), 149 deletions(-) diff --git a/handlers/api/calendars.go b/handlers/api/calendars.go index 4e9367d..715ed34 100644 --- a/handlers/api/calendars.go +++ b/handlers/api/calendars.go @@ -100,7 +100,6 @@ func (h *APIHandler) CalendarOrganizations(w http.ResponseWriter, r *http.Reques for _, e := range events { for _, g := range e.Owners { - log.Debug().Str("groupid", groupid).Str("g", g).Msg("check identical") if g == groupid { filteredEvents = append(filteredEvents, e) continue diff --git a/handlers/application/administration.go b/handlers/application/administration.go index 4bc9827..a92b016 100755 --- a/handlers/application/administration.go +++ b/handlers/application/administration.go @@ -224,162 +224,141 @@ func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWrite 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 { + log.Error().Err(err).Msg("Issue in Groups management service - GetGroup") + w.WriteHeader(http.StatusInternalServerError) + return + } + r.ParseForm() - if err != nil { - log.Error().Err(err).Msg("Issue in Groups management service - GetGroup") + accountresp, err = h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{ + Username: r.FormValue("username"), + Namespace: "parcoursmob", + }) + if err == nil { + log.Print("account exists") + // 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) + + if _, err = h.services.GRPC.MobilityAccounts.UpdateData(context.TODO(), &accounts.UpdateDataRequest{Account: as}); err != nil { + log.Error().Err(err).Msg("could not set groups to user account") w.WriteHeader(http.StatusInternalServerError) return } - }() - r.ParseForm() - 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") - - as, _ := accounts.AccountFromStorageType(&account) - - _, err = h.services.GRPC.MobilityAccounts.UpdateData( - context.TODO(), - &accounts.UpdateDataRequest{ - Account: as, - }, - ) - - data := map[string]any{ - "group": groupresp.Group.ToStorageType().Data["name"], - } - - if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil { - log.Error().Err(err).Msg("Cannot send email") - } - - 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 { - log.Error().Err(err).Msg("Issue creating random bytes") - 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 { - log.Error().Err(err).Msg("Cannot send email") - w.WriteHeader(http.StatusInternalServerError) - return - } + data := map[string]any{ + "group": groupresp.Group.ToStorageType().Data["name"], } - http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound) - return - }() + if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil { + log.Error().Err(err).Msg("Cannot send email") + } + + } 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 { + log.Error().Err(err).Msg("Issue creating random bytes") + 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 { + log.Error().Err(err).Msg("Cannot send email") + w.WriteHeader(http.StatusInternalServerError) + return + } + } + + http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound) } + 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", - }) + group, err := h.services.GetGroup(groupid) + if err != nil { + log.Error().Err(err).Msg("Issue in Groups management service - GetGroup") + w.WriteHeader(http.StatusInternalServerError) + return + } + r.ParseForm() + 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) - if err != nil { - log.Error().Err(err).Msg("Issue in Groups management service - GetGroup") + as, _ := accounts.AccountFromStorageType(&account) + + _, err = h.services.GRPC.MobilityAccounts.UpdateData( + context.TODO(), + &accounts.UpdateDataRequest{ + Account: as, + }, + ) + data := map[string]any{ + "group": group.Data["name"], + } + + if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil { + log.Error().Err(err).Msg("error sending email onboarding.existing_member") + } + + // http.Redirect(w, r, "/app/group/settings", http.StatusFound) + // return + } else { + // Onboard new 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 } - 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) + key := base64.RawURLEncoding.EncodeToString(b) - as, _ := accounts.AccountFromStorageType(&account) - - _, err = h.services.GRPC.MobilityAccounts.UpdateData( - context.TODO(), - &accounts.UpdateDataRequest{ - Account: as, - }, - ) - data := map[string]any{ - "group": group.Data["name"], - } - - if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil { - } - - 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 - } + 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 { + log.Error().Err(err).Msg("error sending email onboarding.new_member") + w.WriteHeader(http.StatusInternalServerError) + return + } + } http.Redirect(w, r, "/app/administration/groups/"+group.ID, http.StatusFound) - return } func filteVehicle(r *http.Request, v *fleets.Vehicle) bool { @@ -400,7 +379,6 @@ func filteVehicle(r *http.Request, v *fleets.Vehicle) bool { } func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Request) { - bookings := []fleetsstorage.Booking{} administrators := []string{} reequest := &fleets.GetVehiclesRequest{ @@ -462,7 +440,6 @@ func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Req } func (h ApplicationHandler) AdminStatBookings(w http.ResponseWriter, r *http.Request) { - vehicles := map[string]fleetsstorage.Vehicle{} bookings := []fleetsstorage.Booking{} @@ -578,7 +555,6 @@ func (h ApplicationHandler) AdminStatEvents(w http.ResponseWriter, r *http.Reque resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{ Namespaces: []string{"parcoursmob_dispositifs"}, }) - if err != nil { log.Error().Err(err).Msg("Issue in Agenda service - GetEvents") w.WriteHeader(http.StatusInternalServerError) diff --git a/handlers/application/vehicles.go b/handlers/application/vehicles.go index 8e96355..1217446 100755 --- a/handlers/application/vehicles.go +++ b/handlers/application/vehicles.go @@ -42,7 +42,7 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques administrators := []string{} - if r.FormValue("beneficiaryid") != "" { + if r.FormValue("beneficiaryid") != "" && startdate.After(time.Now()) && enddate.After(startdate) { // Handler form searched = true @@ -247,7 +247,7 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) { return } - //NOTIFY GROUP MEMBERS + // NOTIFY GROUP MEMBERS members, _, err := h.groupmembers(vehicle.Vehicle.Administrators[0]) if err != nil { log.Error().Err(err).Msg("") @@ -262,7 +262,6 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) { } http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", booking.Id), http.StatusFound) - } func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http.Request) { @@ -310,7 +309,6 @@ func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http } func (h ApplicationHandler) VehiclesBookingsList(w http.ResponseWriter, r *http.Request) { - g := r.Context().Value(identification.GroupKey) if g == nil { w.WriteHeader(http.StatusBadRequest) @@ -371,5 +369,4 @@ func (h *ApplicationHandler) BookingDocumentDownload(w http.ResponseWriter, r *h } http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", bookingid), http.StatusFound) - } diff --git a/main.go b/main.go index f1b8dc8..709034d 100755 --- a/main.go +++ b/main.go @@ -20,11 +20,12 @@ import ( ) func main() { - // zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix cfg, err := ReadConfig() if err != nil { - panic(err) + log.Fatal().Err(err).Msg("cannot read config") + return } var ( diff --git a/services/groupsmanagement.go b/services/groupsmanagement.go index d51563e..7fbdba7 100755 --- a/services/groupsmanagement.go +++ b/services/groupsmanagement.go @@ -57,6 +57,20 @@ func (s *ServicesHandler) GetGroupsMemberMap(id string) (groups map[string]any, return } +func (s *ServicesHandler) GetGroup(groupid string) (*storage.Group, error) { + groupresp, err := s.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ + Id: groupid, + Namespace: "parcoursmob_organizations", + }) + if err != nil { + return nil, err + } + + group := groupresp.Group.ToStorageType() + + return &group, nil +} + // Enriched types type GroupsManagementGroup struct {