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

Include the email template in the event message #3756

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
43 changes: 21 additions & 22 deletions internal/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ type Service interface {

// MailEventPayload is the event payload for sending an invitation email
type MailEventPayload struct {
Code string `json:"code"`
ProjectDisplay string `json:"project"`
SponsorDisplay string `json:"sponsor"`
Role string `json:"role"`
Email string `json:"email"`
Address string `json:"email"`
Subject string `json:"subject"`
BodyHTML string `json:"body_html"`
BodyText string `json:"body_text"`
}

// MailEventHandler is the email event handler
Expand Down Expand Up @@ -77,7 +76,7 @@ func (m *MailEventHandler) handlerInviteEmail(msg *message.Message) error {
}

// Send the email
return m.client.SendEmail(msgCtx, e.Email, e.getEmailSubject(), e.getEmailBodyHTML(), e.getEmailBodyText())
return m.client.SendEmail(msgCtx, e.Address, e.Subject, e.BodyHTML, e.BodyText)
}

// NewMessage creates a new message for sending an invitation email
Expand All @@ -87,13 +86,13 @@ func NewMessage(inviteeEmail, code, role, projectDisplay, sponsorDisplay string)
if err != nil {
return nil, fmt.Errorf("error generating UUID: %w", err)
}

// Create the payload
payload, err := json.Marshal(MailEventPayload{
Code: code,
ProjectDisplay: projectDisplay,
SponsorDisplay: sponsorDisplay,
Role: role,
Email: inviteeEmail,
Address: inviteeEmail,
Subject: getEmailSubject(projectDisplay),
BodyHTML: getEmailBodyHTML(code, sponsorDisplay, projectDisplay, role, inviteeEmail),
BodyText: getEmailBodyText(code, sponsorDisplay, projectDisplay, role),
})
if err != nil {
return nil, fmt.Errorf("error marshalling payload for email event: %w", err)
Expand All @@ -103,7 +102,7 @@ func NewMessage(inviteeEmail, code, role, projectDisplay, sponsorDisplay string)
}

// getBodyHTML returns the HTML body for the email based on the message payload
func (e *MailEventPayload) getEmailBodyHTML() string {
func getEmailBodyHTML(code, sponsor, project, role, inviteeEmail string) string {
data := struct {
AdminName string
OrganizationName string
Expand All @@ -115,16 +114,16 @@ func (e *MailEventPayload) getEmailBodyHTML() string {
SignInURL string
RoleName string
}{
AdminName: e.SponsorDisplay,
OrganizationName: e.ProjectDisplay,
AdminName: sponsor,
OrganizationName: project,
// TODO: Determine the correct environment for the invite URL and the rest of the URLs
InvitationURL: fmt.Sprintf("https://cloud.minder.com/join/%s", e.Code),
RecipientEmail: e.Email,
InvitationURL: fmt.Sprintf("https://cloud.minder.com/join/%s", code),
RecipientEmail: inviteeEmail,
MinderURL: "https://cloud.minder.com",
TermsURL: "https://cloud.minder.com/terms",
PrivacyURL: "https://cloud.minder.com/privacy",
SignInURL: "https://cloud.minder.com",
RoleName: e.Role,
RoleName: role,
}

// TODO: Load the email template from elsewhere
Expand All @@ -134,7 +133,7 @@ func (e *MailEventPayload) getEmailBodyHTML() string {
if err != nil {
// TODO: Log the error
// Default to the text body
return e.getEmailBodyText()
return getEmailBodyText(code, sponsor, project, role)
}
// Execute the template
var b strings.Builder
Expand All @@ -145,12 +144,12 @@ func (e *MailEventPayload) getEmailBodyHTML() string {
}

// getEmailBodyText returns the text body for the email based on the message payload
func (e *MailEventPayload) getEmailBodyText() string {
func getEmailBodyText(code, sponsor, project, role string) string {
return fmt.Sprintf("You have been invited to join %s as a %s by %s. Use code %s to accept the invitation.",
e.ProjectDisplay, e.Role, e.SponsorDisplay, e.Code)
project, role, sponsor, code)
}

// getEmailSubject returns the subject for the email based on the message payload
func (e *MailEventPayload) getEmailSubject() string {
return fmt.Sprintf("You have been invited to join the %s organisation in Minder", e.ProjectDisplay)
func getEmailSubject(project string) string {
return fmt.Sprintf("You have been invited to join the %s organisation in Minder", project)
}