mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-04-25 03:39:38 -05:00
fix: add back SendTemplateEmail methods, fix postmark emails (#2910)
This commit is contained in:
@@ -68,6 +68,10 @@ type EmailService interface {
|
||||
SendWorkflowRunFailedAlerts(ctx context.Context, emails []string, data WorkflowRunsFailedEmailData) error
|
||||
SendExpiringTokenEmail(ctx context.Context, emails []string, data ExpiringTokenEmailData) error
|
||||
SendTenantResourceLimitAlert(ctx context.Context, emails []string, data ResourceLimitAlertData) error
|
||||
|
||||
// Used for extending the email provider for sending additional templated emails
|
||||
SendTemplateEmail(ctx context.Context, to, templateAlias string, templateModelData interface{}, bccSupport bool) error
|
||||
SendTemplateEmailBCC(ctx context.Context, bcc, templateAlias string, templateModelData interface{}, bccSupport bool) error
|
||||
}
|
||||
|
||||
type NoOpService struct{}
|
||||
@@ -91,3 +95,11 @@ func (s *NoOpService) SendExpiringTokenEmail(ctx context.Context, emails []strin
|
||||
func (s *NoOpService) SendTenantResourceLimitAlert(ctx context.Context, emails []string, data ResourceLimitAlertData) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NoOpService) SendTemplateEmail(ctx context.Context, to, templateAlias string, templateModelData interface{}, bccSupport bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NoOpService) SendTemplateEmailBCC(ctx context.Context, bcc, templateAlias string, templateModelData interface{}, bccSupport bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -28,12 +28,7 @@ func NewPostmarkClient(serverKey, fromEmail, fromName, supportEmail string) *Pos
|
||||
Timeout: time.Minute,
|
||||
}
|
||||
|
||||
return &PostmarkClient{
|
||||
serverKey: serverKey,
|
||||
fromEmail: fromEmail,
|
||||
fromName: fromName, supportEmail: supportEmail,
|
||||
httpClient: httpClient,
|
||||
}
|
||||
return &PostmarkClient{serverKey, fromEmail, fromName, supportEmail, httpClient}
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -47,48 +42,58 @@ func (c *PostmarkClient) IsValid() bool {
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) SendTenantInviteEmail(ctx context.Context, to string, data email.TenantInviteEmailData) error {
|
||||
return c.sendRequest(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", c.fromName, c.fromEmail),
|
||||
To: to,
|
||||
TemplateAlias: email.UserInviteTemplate,
|
||||
TemplateModel: data,
|
||||
})
|
||||
return c.SendTemplateEmail(ctx, to, email.UserInviteTemplate, data, false)
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) SendWorkflowRunFailedAlerts(ctx context.Context, emails []string, data email.WorkflowRunsFailedEmailData) error {
|
||||
return c.sendRequest(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", c.fromName, c.fromEmail),
|
||||
Bcc: strings.Join(emails, ","),
|
||||
TemplateAlias: email.WorkflowRunsFailedTemplate,
|
||||
TemplateModel: data,
|
||||
})
|
||||
return c.SendTemplateEmailBCC(ctx, strings.Join(emails, ","), email.WorkflowRunsFailedTemplate, data, false)
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) SendExpiringTokenEmail(ctx context.Context, emails []string, data email.ExpiringTokenEmailData) error {
|
||||
return c.sendRequest(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", c.fromName, c.fromEmail),
|
||||
Bcc: strings.Join(emails, ","),
|
||||
TemplateAlias: email.TokenAlertExpiringTemplate,
|
||||
TemplateModel: data,
|
||||
})
|
||||
return c.SendTemplateEmailBCC(ctx, strings.Join(emails, ","), email.TokenAlertExpiringTemplate, data, false)
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) SendTenantResourceLimitAlert(ctx context.Context, emails []string, data email.ResourceLimitAlertData) error {
|
||||
return c.sendRequest(ctx, &email.SendEmailFromTemplateRequest{
|
||||
return c.SendTemplateEmailBCC(ctx, strings.Join(emails, ","), email.ResourceLimitAlertTemplate, data, true)
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) SendTemplateEmail(ctx context.Context, to, templateAlias string, templateModelData interface{}, bccSupport bool) error {
|
||||
var bcc string
|
||||
|
||||
if bccSupport {
|
||||
bcc = c.supportEmail
|
||||
}
|
||||
|
||||
return c.sendPostmarkEmail(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", c.fromName, c.fromEmail),
|
||||
Bcc: strings.Join(append(emails, c.supportEmail), ","),
|
||||
TemplateAlias: email.ResourceLimitAlertTemplate,
|
||||
TemplateModel: data,
|
||||
To: to,
|
||||
Bcc: bcc,
|
||||
TemplateAlias: templateAlias,
|
||||
TemplateModel: templateModelData,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) sendRequest(ctx context.Context, data *email.SendEmailFromTemplateRequest) error {
|
||||
func (c *PostmarkClient) SendTemplateEmailBCC(ctx context.Context, bcc, templateAlias string, templateModelData interface{}, bccSupport bool) error {
|
||||
|
||||
if bccSupport {
|
||||
bcc = fmt.Sprintf("%s,%s", bcc, c.supportEmail)
|
||||
}
|
||||
|
||||
return c.sendPostmarkEmail(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", c.fromName, c.fromEmail),
|
||||
Bcc: bcc,
|
||||
TemplateAlias: templateAlias,
|
||||
TemplateModel: templateModelData,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *PostmarkClient) sendPostmarkEmail(ctx context.Context, data *email.SendEmailFromTemplateRequest) error {
|
||||
reqURL, err := url.Parse(postmarkAPIURL)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
reqURL.Path = postmarkAPIURL
|
||||
reqURL.Path = postmarkEmailPath
|
||||
|
||||
strData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
|
||||
@@ -104,6 +104,35 @@ func (s *SMTPService) SendTenantResourceLimitAlert(ctx context.Context, emails [
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SMTPService) SendTemplateEmail(ctx context.Context, to, templateAlias string, templateModelData interface{}, bccSupport bool) error {
|
||||
var bcc string
|
||||
|
||||
if bccSupport {
|
||||
bcc = s.supportEmail
|
||||
}
|
||||
|
||||
return s.sendRequest(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", s.fromName, s.fromEmail),
|
||||
To: to,
|
||||
Bcc: bcc,
|
||||
TemplateAlias: templateAlias,
|
||||
TemplateModel: templateModelData,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SMTPService) SendTemplateEmailBCC(ctx context.Context, bcc, templateAlias string, templateModelData interface{}, bccSupport bool) error {
|
||||
if bccSupport {
|
||||
bcc = fmt.Sprintf("%s,%s", bcc, s.supportEmail)
|
||||
}
|
||||
|
||||
return s.sendRequest(ctx, &email.SendEmailFromTemplateRequest{
|
||||
From: fmt.Sprintf("%s <%s>", s.fromName, s.fromEmail),
|
||||
Bcc: bcc,
|
||||
TemplateAlias: templateAlias,
|
||||
TemplateModel: templateModelData,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SMTPService) sendRequest(ctx context.Context, req *email.SendEmailFromTemplateRequest) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user