Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions internal/ent/hooks/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,23 @@ func HookInvite() ent.Hook {
return retValue, ErrInternalServerError
}

// check if the recipient already has an account so the invite link can route accordingly
recipientExists, err := m.Client().User.Query().
Where(user.EmailEqualFold(emailAddress)).
Exist(privacy.DecisionContext(ctx, privacy.Allow))
if err != nil {
logx.FromContext(ctx).Error().Err(err).Msg("error checking recipient account existence")

recipientExists = true
}

if err := sendSystemEmail(ctx, m.Client(), emaildef.InviteOp.Name(), emaildef.InviteRequest{
RecipientInfo: emaildef.RecipientInfo{Email: emailAddress},
InviterName: inviterName,
OrgName: orgName,
Role: string(role),
Token: tokenValue,
NewUser: !recipientExists,
}); err != nil {
logx.FromContext(ctx).Error().Err(err).Msg("error sending email to user")

Expand Down
8 changes: 7 additions & 1 deletion internal/integrations/definitions/email/system_emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ type InviteRequest struct {
Role string `json:"role,omitempty" jsonschema:"description=Invited role"`
// Token is the invite token appended to the invite URL
Token string `json:"token" jsonschema:"required,description=Invite token"`
// NewUser indicates the recipient has no existing account
NewUser bool `json:"newUser,omitempty" jsonschema:"description=Whether the recipient has no existing account"`
}

// InviteJoinedRequest is the input for the invite-accepted notification
Expand Down Expand Up @@ -320,7 +322,11 @@ var _ = RegisterEmailOperation(Operation[InviteRequest]{
return "Join Your Teammate " + req.InviterName + " on " + cfg.CompanyName + "!"
},
Build: func(cfg RuntimeEmailConfig, req InviteRequest) render.ContentBody {
inviteURL := tokenURL(cfg.ProductURL, "/invite", req.Token)
// append the recipient email and new-account hint so the invite page can route the recipient
inviteURL := tokenURL(cfg.ProductURL, "/invite", req.Token) + "&email=" + url.QueryEscape(req.Email)
if req.NewUser {
inviteURL += "&new=true"
}
inviteIntro := template.HTML("You're in - let's build trust without the busywork. "+html.EscapeString(req.InviterName)+" has invited you to collaborate in "+html.EscapeString(cfg.CompanyName)+", as part of the ") + //nolint:gosec // all user input is escaped via html.EscapeString
render.Bold(req.OrgName) + " organization"
if req.Role != "" {
Expand Down