Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invitation emails #360

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions server/action/organisation/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,6 +71,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"))

Expand Down Expand Up @@ -95,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()
Expand Down Expand Up @@ -133,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
}
Expand Down Expand Up @@ -217,17 +229,43 @@ 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()
}
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) {
Expand Down