From 249b8cc16249d3620e7bdc3715f478a4604b460f Mon Sep 17 00:00:00 2001 From: surajmn1 Date: Fri, 17 Mar 2023 18:31:57 +0530 Subject: [PATCH 1/2] add disable param which can disable emails sent from kavach for invitations --- server/action/organisation/user/create.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/server/action/organisation/user/create.go b/server/action/organisation/user/create.go index 4897775a..ddb0c636 100644 --- a/server/action/organisation/user/create.go +++ b/server/action/organisation/user/create.go @@ -62,6 +62,8 @@ func create(w http.ResponseWriter, r *http.Request) { errorx.Render(w, errorx.Parser(errorx.InternalServerError())) return } + disableInvite := r.URL.Query().Get("disable_invite") + var currentUID int currentUID, err = strconv.Atoi(r.Header.Get("X-User")) @@ -217,13 +219,16 @@ func create(w http.ResponseWriter, r *http.Request) { // } else { // receiver.ActionURL = return_to // } - err = email.SendmailwithSendGrid(receiver) - if err != nil { - // tx.Rollback() - loggerx.Error(err) - // errorx.Render(w, errorx.Parser(errorx.InternalServerError())) - // return + if disableInvite != "true" { + err = email.SendmailwithSendGrid(receiver) + if err != nil { + // tx.Rollback() + loggerx.Error(err) + // errorx.Render(w, errorx.Parser(errorx.InternalServerError())) + // return + } } + } tx.Commit() } From 61b7b4c6c3afd489c928c15ce857970ff5abb191 Mon Sep 17 00:00:00 2001 From: surajmn1 Date: Fri, 17 Mar 2023 23:01:00 +0530 Subject: [PATCH 2/2] add invitations sent and failed invites in response --- server/action/organisation/user/create.go | 37 +++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/server/action/organisation/user/create.go b/server/action/organisation/user/create.go index ddb0c636..5acf520c 100644 --- a/server/action/organisation/user/create.go +++ b/server/action/organisation/user/create.go @@ -32,6 +32,15 @@ type invite struct { Email string `json:"email" validate:"required"` Role string `json:"role" validate:"required"` } +type FailedInvite struct { + Email string `json:"email"` + Message string `json:"message"` +} + +type Response struct { + FailedInvites []FailedInvite `json:"failed_invites"` + Invitations []invite `json:"invitations"` +} // create - Create organisation user // @Summary Create organisation user @@ -97,6 +106,7 @@ func create(w http.ResponseWriter, r *http.Request) { return } } + inviteeCounts := make(map[string]int64) for _, user := range req.Users { tx := model.DB.WithContext(context.WithValue(r.Context(), userContext, currentUID)).Begin() @@ -135,8 +145,8 @@ func create(w http.ResponseWriter, r *http.Request) { loggerx.Error(err) return } - if invitationCount > 0 { + inviteeCounts[invitee.Email]++ loggerx.Error(err) continue } @@ -232,7 +242,30 @@ func create(w http.ResponseWriter, r *http.Request) { } tx.Commit() } - renderx.JSON(w, http.StatusOK, nil) + var failedInvites []FailedInvite + var invitations []invite + for _, user := range req.Users { + if count, exists := inviteeCounts[user.Email]; exists && count >= 1 { + failedInvites = append(failedInvites, FailedInvite{ + Email: user.Email, + Message: "invite already exists", + }) + } else { + invitations = append(invitations, user) + } + } + var resp Response + if len(failedInvites) > 0 { + resp.FailedInvites = failedInvites + } else { + resp.FailedInvites = []FailedInvite{} + } + if len(invitations) > 0 { + resp.Invitations = invitations + } else { + resp.Invitations = []invite{} + } + renderx.JSON(w, http.StatusOK, resp) } func decodeURLIfNeeded(urlString string) (string, error) {