diff --git a/api4/cloud.go b/api4/cloud.go index 55e61129da..0eb8f56635 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -7,6 +7,7 @@ import ( "bytes" "encoding/binary" "encoding/json" + "fmt" "io/ioutil" "net/http" "time" @@ -411,6 +412,16 @@ func handleCWSWebhook(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = appErr return } + case model.EventTypeTrialWillEnd: + endTimeStamp := event.SubscriptionTrialEndUnixTimeStamp + t := time.Unix(endTimeStamp, 0) + trialEndDate := fmt.Sprintf("%s %d, %d", t.Month(), t.Day(), t.Year()) + + if appErr := c.App.SendCloudTrialEndWarningEmail(trialEndDate, *c.App.Config().ServiceSettings.SiteURL); appErr != nil { + c.Err = appErr + return + } + default: c.Err = model.NewAppError("Api4.handleCWSWebhook", "api.cloud.cws_webhook_event_missing_error", nil, "", http.StatusNotFound) return diff --git a/app/app_iface.go b/app/app_iface.go index 0f1d9efcb8..91bf8e0925 100644 --- a/app/app_iface.go +++ b/app/app_iface.go @@ -940,6 +940,7 @@ type AppIface interface { SendAckToPushProxy(ack *model.PushNotificationAck) error SendAutoResponse(channel *model.Channel, receiver *model.User, post *model.Post) (bool, *model.AppError) SendAutoResponseIfNecessary(channel *model.Channel, sender *model.User, post *model.Post) (bool, *model.AppError) + SendCloudTrialEndWarningEmail(trialEndDate, siteURL string) *model.AppError SendEmailVerification(user *model.User, newEmail, redirect string) *model.AppError SendEphemeralPost(userID string, post *model.Post) *model.Post SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User, parentPostList *model.PostList, setOnline bool) ([]string, error) diff --git a/app/cloud.go b/app/cloud.go index 3cfe257294..10648fb835 100644 --- a/app/cloud.go +++ b/app/cloud.go @@ -175,3 +175,28 @@ func (a *App) SendNoCardPaymentFailedEmail() *model.AppError { } return nil } + +func (a *App) SendCloudTrialEndWarningEmail(trialEndDate, siteURL string) *model.AppError { + sysAdmins, e := a.getSysAdminsEmailRecipients() + if e != nil { + return e + } + + // we want to at least have one email sent out to an admin + countNotOks := 0 + + for admin := range sysAdmins { + err := a.Srv().EmailService.SendCloudTrialEndWarningEmail(sysAdmins[admin].Email, sysAdmins[admin].Username, trialEndDate, sysAdmins[admin].Locale, siteURL) + if err != nil { + a.Log().Error("Error sending trial ending warning to", mlog.String("email", sysAdmins[admin].Email), mlog.Err(err)) + countNotOks++ + } + } + + // if not even one admin got an email, we consider that this operation errored + if countNotOks == len(sysAdmins) { + return model.NewAppError("app.SendCloudTrialEndWarningEmail", "app.user.send_emails.app_error", nil, "", http.StatusInternalServerError) + } + + return nil +} diff --git a/app/email.go b/app/email.go index ce7dee2589..383dc42a06 100644 --- a/app/email.go +++ b/app/email.go @@ -290,6 +290,30 @@ func (es *EmailService) sendWelcomeEmail(userID string, email string, verified b return nil } +func (es *EmailService) SendCloudTrialEndWarningEmail(userEmail, userName, trialEndDate, locale, siteURL string) *model.AppError { + T := i18n.GetUserTranslations(locale) + subject := T("api.templates.cloud_trial_ending_email.subject") + + data := es.newEmailTemplateData(locale) + data.Props["Title"] = T("api.templates.cloud_trial_ending_email.title") + data.Props["SubTitle"] = T("api.templates.cloud_trial_ending_email.subtitle", map[string]interface{}{"Username": userName, "TrialEnd": trialEndDate}) + data.Props["SiteURL"] = siteURL + data.Props["ButtonURL"] = fmt.Sprintf("%s/admin_console/billing/subscription", siteURL) + data.Props["Button"] = T("api.templates.cloud_trial_ending_email.add_payment_method") + data.Props["QuestionTitle"] = T("api.templates.questions_footer.title") + data.Props["QuestionInfo"] = T("api.templates.questions_footer.info") + + body, err := es.srv.TemplatesContainer().RenderToString("cloud_trial_end_warning", data) + if err != nil { + return model.NewAppError("SendCloudTrialEndWarningEmail", "api.user.cloud_trial_ending_email.error", nil, err.Error(), http.StatusInternalServerError) + } + + if err := es.sendMail(userEmail, subject, body); err != nil { + return model.NewAppError("SendCloudTrialEndWarningEmail", "api.user.cloud_trial_ending_email.error", nil, err.Error(), http.StatusInternalServerError) + } + return nil +} + // SendCloudWelcomeEmail sends the cloud version of the welcome email func (es *EmailService) SendCloudWelcomeEmail(userEmail, locale, teamInviteID, workSpaceName, dns string) *model.AppError { T := i18n.GetUserTranslations(locale) diff --git a/app/opentracing/opentracing_layer.go b/app/opentracing/opentracing_layer.go index 970417d99d..97ce222537 100644 --- a/app/opentracing/opentracing_layer.go +++ b/app/opentracing/opentracing_layer.go @@ -13868,6 +13868,28 @@ func (a *OpenTracingAppLayer) SendAutoResponseIfNecessary(channel *model.Channel return resultVar0, resultVar1 } +func (a *OpenTracingAppLayer) SendCloudTrialEndWarningEmail(trialEndDate string, siteURL string) *model.AppError { + origCtx := a.ctx + span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SendCloudTrialEndWarningEmail") + + a.ctx = newCtx + a.app.Srv().Store.SetContext(newCtx) + defer func() { + a.app.Srv().Store.SetContext(origCtx) + a.ctx = origCtx + }() + + defer span.Finish() + resultVar0 := a.app.SendCloudTrialEndWarningEmail(trialEndDate, siteURL) + + if resultVar0 != nil { + span.LogFields(spanlog.Error(resultVar0)) + ext.Error.Set(span, true) + } + + return resultVar0 +} + func (a *OpenTracingAppLayer) SendEmailVerification(user *model.User, newEmail string, redirect string) *model.AppError { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SendEmailVerification") diff --git a/i18n/en.json b/i18n/en.json index 541dc895b5..20308c1605 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3126,6 +3126,22 @@ "id": "api.templates.at_limit_title", "translation": "You’ve reached the user limit for the free tier " }, + { + "id": "api.templates.cloud_trial_ending_email.add_payment_method", + "translation": "Add Payment method" + }, + { + "id": "api.templates.cloud_trial_ending_email.subject", + "translation": "Mattermost cloud trial ending" + }, + { + "id": "api.templates.cloud_trial_ending_email.subtitle", + "translation": "{{.Username}}, your 14-day trial of Mattermost Cloud Professional is ending in 3 days, on {{.TrialEnd}}. Please add your payment information to ensure your team can continue enjoying the benefits of Cloud Professional." + }, + { + "id": "api.templates.cloud_trial_ending_email.title", + "translation": "Your free 14-day trial of Mattermost is ending soon" + }, { "id": "api.templates.cloud_welcome_email.add_apps_info", "translation": "Add apps to your workspace" @@ -3192,7 +3208,7 @@ }, { "id": "api.templates.cloud_welcome_email.title", - "translation": "Congratulations - your {{.WorkSpace}} workspace is ready to go!" + "translation": "Your 14 day trial of the {{.WorkSpace}} workspace is ready to go!" }, { "id": "api.templates.copyright", @@ -3830,6 +3846,10 @@ "id": "api.user.check_user_password.invalid.app_error", "translation": "Login failed because of invalid password." }, + { + "id": "api.user.cloud_trial_ending_email.error", + "translation": "Failed to send trial ending warning email" + }, { "id": "api.user.complete_switch_with_oauth.blank_email.app_error", "translation": "Blank email." diff --git a/model/cloud.go b/model/cloud.go index 3a348d6dd6..8f3716d9c6 100644 --- a/model/cloud.go +++ b/model/cloud.go @@ -9,6 +9,7 @@ const ( EventTypeFailedPayment = "failed-payment" EventTypeFailedPaymentNoCard = "failed-payment-no-card" EventTypeSendAdminWelcomeEmail = "send-admin-welcome-email" + EventTypeTrialWillEnd = "trial-will-end" JoinLimitation = "join" InviteLimitation = "invite" ) @@ -95,6 +96,8 @@ type Subscription struct { DNS string `json:"dns"` IsPaidTier string `json:"is_paid_tier"` LastInvoice *Invoice `json:"last_invoice"` + IsFreeTrial string `json:"is_free_trial"` + TrialEndAt int64 `json:"trial_end_at"` } // GetWorkSpaceNameFromDNS returns the work space name. For example from test.mattermost.cloud.com, it returns test @@ -129,9 +132,10 @@ type InvoiceLineItem struct { } type CWSWebhookPayload struct { - Event string `json:"event"` - FailedPayment *FailedPayment `json:"failed_payment"` - CloudWorkspaceOwner *CloudWorkspaceOwner `json:"cloud_workspace_owner"` + Event string `json:"event"` + FailedPayment *FailedPayment `json:"failed_payment"` + CloudWorkspaceOwner *CloudWorkspaceOwner `json:"cloud_workspace_owner"` + SubscriptionTrialEndUnixTimeStamp int64 `json:"trial_end_time_stamp"` } type FailedPayment struct { @@ -147,4 +151,5 @@ type CloudWorkspaceOwner struct { type SubscriptionStats struct { RemainingSeats int `json:"remaining_seats"` IsPaidTier string `json:"is_paid_tier"` + IsFreeTrial string `json:"is_free_trial"` } diff --git a/templates/cloud_trial_end_warning.html b/templates/cloud_trial_end_warning.html new file mode 100644 index 0000000000..25c1c0f420 --- /dev/null +++ b/templates/cloud_trial_end_warning.html @@ -0,0 +1,442 @@ +{{define "cloud_trial_end_warning"}} + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ +
+ + + + + + +
+ +
+ + + + + + +
+ + + + + + +
+ +
+
+
+ +
+
+ +
+ + + + + + +
+ +
+ + + + + + + + + + + + +
+
{{.Props.Title}}
+
+
{{.Props.SubTitle}}
+
+ + + + +
+ + {{.Props.Button}} + +
+
+
+ +
+
+ +
+ + + + + + +
+ +
+ + + + + + +
+ + + + + + +
+ +
+
+
+ +
+
+ +
+ + + + + + +
+ +
+ + + + + + + + + +
+
{{.Props.QuestionTitle}}
+
+
{{.Props.QuestionInfo}} + + {{.Props.SupportEmail}} + +
+
+
+ +
+
+ +
+ + + + + + +
+ +
+ + + + + + +
+
{{.Props.Organization}} + {{.Props.FooterV2}} +
+
+
+ +
+
+ +
+
+ +
+ + + + +{{end}} diff --git a/templates/cloud_trial_end_warning.mjml b/templates/cloud_trial_end_warning.mjml new file mode 100644 index 0000000000..c6d529f926 --- /dev/null +++ b/templates/cloud_trial_end_warning.mjml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + {{.Props.Title}} + + + {{.Props.SubTitle}} + + + {{.Props.Button}} + + + + + + + + + + + + + + {{.Props.QuestionTitle}} + + + {{.Props.QuestionInfo}} + + {{.Props.SupportEmail}} + + + + + + + + + {{.Props.Organization}} + {{.Props.FooterV2}} + + + + + + + +