Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: add trial notifications

goenning03c8d23298c8
diff --git a/app/cmd/server.go b/app/cmd/server.go
index 9fb87da2a..b5140bdd9 100644
--- a/app/cmd/server.go
+++ b/app/cmd/server.go
@@ -57,9 +57,19 @@ func startJobs(ctx context.Context) {
c := cron.New()
_ = c.AddJob(jobs.NewJob(ctx, "PurgeExpiredNotificationsJob", jobs.PurgeExpiredNotificationsJobHandler{}))
_ = c.AddJob(jobs.NewJob(ctx, "EmailSupressionJob", jobs.EmailSupressionJobHandler{}))
+
if env.IsBillingEnabled() {
_ = c.AddJob(jobs.NewJob(ctx, "LockExpiredTenantsJob", jobs.LockExpiredTenantsJobHandler{}))
+ _ = c.AddJob(jobs.NewJob(ctx, "TrialNotification7DaysJob", jobs.TrialNotificationJobHandler{
+ Days: 7,
+ TemplateName: "trial_7days",
+ }))
+ _ = c.AddJob(jobs.NewJob(ctx, "TrialNotification1DayJob", jobs.TrialNotificationJobHandler{
+ Days: 1,
+ TemplateName: "trial_1day",
+ }))
}
+
c.Start()
}
diff --git a/app/jobs/trial_notification_job.go b/app/jobs/trial_notification_job.go
new file mode 100644
index 000000000..4106023b1
--- /dev/null
+++ b/app/jobs/trial_notification_job.go
@@ -0,0 +1,63 @@
+package jobs
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/query"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/web"
+)
+
+type TrialNotificationJobHandler struct {
+ Days int
+ TemplateName string
+}
+
+func (e TrialNotificationJobHandler) Schedule() string {
+ return "0 0 7 * * *" // every day at 7:00 AM
+}
+
+func (e TrialNotificationJobHandler) Run(ctx Context) error {
+
+ if ctx.LastSuccessfulRun == nil {
+ // this is the first run, we can skip it
+ return nil
+ }
+
+ date := time.Now().AddDate(0, 0, e.Days)
+ q := &query.GetTrialingTenantContacts{
+ TrialExpiresOn: date,
+ }
+
+ if err := bus.Dispatch(ctx, q); err != nil {
+ return err
+ }
+
+ to := make([]dto.Recipient, 0)
+ for _, contact := range q.Contacts {
+ to = append(to, dto.NewRecipient(contact.Name, contact.Email, dto.Props{
+ "url": fmt.Sprintf("https://%s.%s", contact.Tenant.Subdomain, env.Config.HostDomain),
+ "name": contact.Name,
+ }))
+ }
+
+ if len(to) > 0 {
+ bus.Publish(ctx, &cmd.SendMail{
+ From: dto.Recipient{
+ Name: "Guilherme",
+ Address: "[email protected]",
+ },
+ To: to,
+ TemplateName: e.TemplateName,
+ Props: dto.Props{
+ "logo": web.LogoURL(ctx),
+ },
+ })
+ }
+
+ return nil
+}
diff --git a/app/models/entity/tenant.go b/app/models/entity/tenant.go
index 28387fe64..dcb89bc16 100644
--- a/app/models/entity/tenant.go
+++ b/app/models/entity/tenant.go
@@ -2,7 +2,7 @@ package entity
import "github.com/getfider/fider/app/models/enum"
-//Tenant represents a tenant
+// Tenant represents a tenant
type Tenant struct {
ID int `json:"id"`
Name string `json:"name"`
@@ -26,3 +26,10 @@ func (t *Tenant) IsDisabled() bool {
return t.Status == enum.TenantDisabled
}
+
+// TenantContact is a reference to an administrator account
+type TenantContact struct {
+ Name string `json:"name"`
+ Email string `json:"email"`
+ Subdomain string `json:"subdomain"`
+}
diff --git a/app/models/query/tenant.go b/app/models/query/tenant.go
index b7e501bb0..f6061b76d 100644
--- a/app/models/query/tenant.go
+++ b/app/models/query/tenant.go
@@ -1,6 +1,8 @@
package query
import (
+ "time"
+
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/enum"
)
@@ -8,12 +10,14 @@ import (
type IsCNAMEAvailable struct {
CNAME string
+ // Output
Result bool
}
type IsSubdomainAvailable struct {
Subdomain string
+ // Output
Result bool
}
@@ -21,15 +25,26 @@ type GetVerificationByKey struct {
Kind enum.EmailVerificationKind
Key string
+ // Output
Result *entity.EmailVerification
}
type GetFirstTenant struct {
+
+ // Output
Result *entity.Tenant
}
type GetTenantByDomain struct {
Domain string
+ // Output
Result *entity.Tenant
}
+
+type GetTrialingTenantContacts struct {
+ TrialExpiresOn time.Time
+
+ // Output
+ Contacts []*entity.User
+}
diff --git a/app/services/sqlstore/postgres/billing.go b/app/services/sqlstore/postgres/billing.go
index 8519e7efe..8a7546b7d 100644
--- a/app/services/sqlstore/postgres/billing.go
+++ b/app/services/sqlstore/postgres/billing.go
@@ -120,3 +120,33 @@ func lockExpiredTenants(ctx context.Context, c *cmd.LockExpiredTenants) error {
return nil
})
}
+
+func getTrialingTenantContacts(ctx context.Context, q *query.GetTrialingTenantContacts) error {
+ return using(ctx, func(trx *dbx.Trx, _ *entity.Tenant, _ *entity.User) error {
+ var users []*dbUser
+ err := trx.Select(&users, `
+ SELECT
+ u.name,
+ u.email,
+ u.role,
+ u.status,
+ t.subdomain as tenant_subdomain
+ FROM tenants_billing tb
+ INNER JOIN tenants t
+ ON t.id = tb.tenant_id
+ INNER JOIN users u
+ ON u.tenant_id = tb.tenant_id
+ AND u.role = $1
+ WHERE date(trial_ends_at) = date($2)
+ AND tb.status = $3`, enum.RoleAdministrator, q.TrialExpiresOn, enum.BillingTrial)
+ if err != nil {
+ return errors.Wrap(err, "failed to get trialing tenant contacts")
+ }
+
+ q.Contacts = make([]*entity.User, len(users))
+ for i, user := range users {
+ q.Contacts[i] = user.toModel(ctx)
+ }
+ return nil
+ })
+}
diff --git a/app/services/sqlstore/postgres/postgres.go b/app/services/sqlstore/postgres/postgres.go
index 00fb82953..38be8f6f3 100644
--- a/app/services/sqlstore/postgres/postgres.go
+++ b/app/services/sqlstore/postgres/postgres.go
@@ -130,6 +130,7 @@ func (s Service) Init() {
bus.AddHandler(activateBillingSubscription)
bus.AddHandler(cancelBillingSubscription)
bus.AddHandler(lockExpiredTenants)
+ bus.AddHandler(getTrialingTenantContacts)
bus.AddHandler(setSystemSettings)
bus.AddHandler(getSystemSettings)
diff --git a/app/services/sqlstore/postgres/user.go b/app/services/sqlstore/postgres/user.go
index 80866038a..cab24ebf5 100644
--- a/app/services/sqlstore/postgres/user.go
+++ b/app/services/sqlstore/postgres/user.go
@@ -38,7 +38,12 @@ func (u *dbUser) toModel(ctx context.Context) *entity.User {
return nil
}
+ avatarURL := ""
avatarType := enum.AvatarType(u.AvatarType.Int64)
+ if u.AvatarType.Valid {
+ avatarURL = buildAvatarURL(ctx, avatarType, int(u.ID.Int64), u.Name.String, u.AvatarBlobKey.String)
+ }
+
user := &entity.User{
ID: int(u.ID.Int64),
Name: u.Name.String,
@@ -49,7 +54,7 @@ func (u *dbUser) toModel(ctx context.Context) *entity.User {
Status: enum.UserStatus(u.Status.Int64),
AvatarType: avatarType,
AvatarBlobKey: u.AvatarBlobKey.String,
- AvatarURL: buildAvatarURL(ctx, avatarType, int(u.ID.Int64), u.Name.String, u.AvatarBlobKey.String),
+ AvatarURL: avatarURL,
}
for i, p := range u.Providers {
diff --git a/public/pages/Administration/pages/ManageBilling.page.tsx b/public/pages/Administration/pages/ManageBilling.page.tsx
index b309778f8..5bfc72f4a 100644
--- a/public/pages/Administration/pages/ManageBilling.page.tsx
+++ b/public/pages/Administration/pages/ManageBilling.page.tsx
@@ -97,7 +97,7 @@ const TrialInformation = (props: ManageBillingPageProps) => {
<p>
Your account is currently on a trial until{" "}
<strong>
- <Moment locale={fider.currentLocale} format="full" date={props.trialEndsAt} />
+ <Moment locale={fider.currentLocale} format="date" date={props.trialEndsAt} />
</strong>
. <br />
Subscribe before the end of your trial to avoid a service interruption.
diff --git a/views/email/trial_1day.html b/views/email/trial_1day.html
new file mode 100644
index 000000000..7133c3a76
--- /dev/null
+++ b/views/email/trial_1day.html
@@ -0,0 +1,14 @@
+{{define "subject"}}{{ .url }} is about to expire: Update Now!{{end}}
+
+{{define "body"}}
+<tr>
+ <td>
+ <h2 style="color:#1c262d">Hey {{ .name }},</h2>
+ <p style="color:#1c262d">Just a gently reminder that your Fider site <a href="{{ .url }}">{{ .url }}</a> will expire tomorrow and you won’t be able to access your site anymore.</p>
+ <p style="color:#1c262d">You can subscribe from the <a href="{{ .url }}/billing">Billing</a> page of your site.</p>
+ <p style="color:#1c262d">Let me know if you have any questions or need any assistance!</p>
+ <p></p>
+ <p style="color:#1c262d"> - Guilherme</p>
+ </td>
+</tr>
+{{end}}
\ No newline at end of file
diff --git a/views/email/trial_7days.html b/views/email/trial_7days.html
new file mode 100644
index 000000000..c588eaa23
--- /dev/null
+++ b/views/email/trial_7days.html
@@ -0,0 +1,16 @@
+{{define "subject"}}{{ .url }} expires in 7 days!{{end}}
+
+{{define "body"}}
+<tr>
+ <td>
+ <h2 style="color:#1c262d">Hey {{ .name }},</h2>
+ <p style="color:#1c262d">It’s been a fun journey, but unfortunately, we’re coming to the end of your trial period. We’d love to keep this going!</p>
+ <p style="color:#1c262d">In order to keep <a href="{{ .url }}">{{ .url }}</a> online, you’ll need to subscribe to our paid plan.</p>
+ <p style="color:#1c262d">Otherwise, your trial will end in 7 days and you won’t be able to access your site anymore.</p>
+ <p style="color:#1c262d">You can subscribe from the <a href="{{ .url }}/billing">Billing</a> page of your site.</p>
+ <p style="color:#1c262d">Let me know if you have any questions or need any assistance!</p>
+ <p></p>
+ <p style="color:#1c262d"> - Guilherme</p>
+ </td>
+</tr>
+{{end}}
\ No newline at end of file