Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: create trial record (#710)

Guilherme Oenninge01f064135ea
diff --git a/app/handlers/billing.go b/app/handlers/billing.go
index 1affe0762..93137e066 100644
--- a/app/handlers/billing.go
+++ b/app/handlers/billing.go
@@ -8,7 +8,7 @@ import (
// BillingPage is the billing settings page
func BillingPage() web.HandlerFunc {
return func(c web.Context) error {
- if !env.IsBillingEnabled() {
+ if !env.IsBillingEnabled() || c.Tenant().Billing == nil {
return c.Redirect(c.BaseURL())
}
diff --git a/app/models/identity.go b/app/models/identity.go
index 00b55ddca..f28ce1301 100644
--- a/app/models/identity.go
+++ b/app/models/identity.go
@@ -9,16 +9,17 @@ import (
//Tenant represents a tenant
type Tenant struct {
- ID int `json:"id"`
- Name string `json:"name"`
- Subdomain string `json:"subdomain"`
- Invitation string `json:"invitation"`
- WelcomeMessage string `json:"welcomeMessage"`
- CNAME string `json:"cname"`
- Status int `json:"status"`
- IsPrivate bool `json:"isPrivate"`
- LogoBlobKey string `json:"logoBlobKey"`
- CustomCSS string `json:"-"`
+ ID int `json:"id"`
+ Name string `json:"name"`
+ Subdomain string `json:"subdomain"`
+ Invitation string `json:"invitation"`
+ WelcomeMessage string `json:"welcomeMessage"`
+ CNAME string `json:"cname"`
+ Status int `json:"status"`
+ IsPrivate bool `json:"isPrivate"`
+ LogoBlobKey string `json:"logoBlobKey"`
+ Billing *TenantBilling `json:"billing,omitempty"`
+ CustomCSS string `json:"-"`
}
var (
@@ -30,6 +31,11 @@ var (
TenantLocked = 3
)
+//TenantBilling has all the billing information of given tenant
+type TenantBilling struct {
+ TrialEndsAt time.Time `json:"trialEndsAt"`
+}
+
//Upload represents a file that has been uploaded to Fider
type Upload struct {
ContentType string `db:"content_type"`
diff --git a/app/storage/postgres/tenant.go b/app/storage/postgres/tenant.go
index 2ff2dc9ee..c0e646a51 100644
--- a/app/storage/postgres/tenant.go
+++ b/app/storage/postgres/tenant.go
@@ -11,16 +11,17 @@ import (
)
type dbTenant struct {
- ID int `db:"id"`
- Name string `db:"name"`
- Subdomain string `db:"subdomain"`
- CNAME string `db:"cname"`
- Invitation string `db:"invitation"`
- WelcomeMessage string `db:"welcome_message"`
- Status int `db:"status"`
- IsPrivate bool `db:"is_private"`
- LogoBlobKey string `db:"logo_bkey"`
- CustomCSS string `db:"custom_css"`
+ ID int `db:"id"`
+ Name string `db:"name"`
+ Subdomain string `db:"subdomain"`
+ CNAME string `db:"cname"`
+ Invitation string `db:"invitation"`
+ WelcomeMessage string `db:"welcome_message"`
+ Status int `db:"status"`
+ IsPrivate bool `db:"is_private"`
+ LogoBlobKey string `db:"logo_bkey"`
+ CustomCSS string `db:"custom_css"`
+ Billing *dbTenantBilling `db:"billing"`
}
func (t *dbTenant) toModel() *models.Tenant {
@@ -28,7 +29,7 @@ func (t *dbTenant) toModel() *models.Tenant {
return nil
}
- return &models.Tenant{
+ tenant := &models.Tenant{
ID: t.ID,
Name: t.Name,
Subdomain: t.Subdomain,
@@ -40,6 +41,18 @@ func (t *dbTenant) toModel() *models.Tenant {
LogoBlobKey: t.LogoBlobKey,
CustomCSS: t.CustomCSS,
}
+
+ if t.Billing != nil && t.Billing.TrialEndsAt.Valid {
+ tenant.Billing = &models.TenantBilling{
+ TrialEndsAt: t.Billing.TrialEndsAt.Time,
+ }
+ }
+
+ return tenant
+}
+
+type dbTenantBilling struct {
+ TrialEndsAt dbx.NullTime `db:"trial_ends_at"`
}
type dbEmailVerification struct {
@@ -141,15 +154,27 @@ func (s *TenantStorage) Current() *models.Tenant {
// Add given tenant to tenant list
func (s *TenantStorage) Add(name string, subdomain string, status int) (*models.Tenant, error) {
+ now := time.Now()
+
var id int
err := s.trx.Get(&id,
`INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css, logo_bkey)
VALUES ($1, $2, $3, '', '', '', $4, false, '', '')
- RETURNING id`, name, subdomain, time.Now(), status)
+ RETURNING id`, name, subdomain, now, status)
if err != nil {
return nil, err
}
+ if env.IsBillingEnabled() {
+ _, err = s.trx.Execute(
+ `INSERT INTO tenants_billing (tenant_id, trial_ends_at) VALUES ($1, $2)`,
+ id, now.Add(30*24*time.Hour),
+ )
+ if err != nil {
+ return nil, err
+ }
+ }
+
return s.GetByDomain(subdomain)
}
@@ -157,7 +182,14 @@ func (s *TenantStorage) Add(name string, subdomain string, status int) (*models.
func (s *TenantStorage) First() (*models.Tenant, error) {
tenant := dbTenant{}
- err := s.trx.Get(&tenant, "SELECT id, name, subdomain, cname, invitation, welcome_message, status, is_private, logo_bkey, custom_css FROM tenants ORDER BY id LIMIT 1")
+ err := s.trx.Get(&tenant, `
+ SELECT t.id, t.name, t.subdomain, t.cname, t.invitation, t.welcome_message, t.status, t.is_private, t.logo_bkey, t.custom_css,
+ tb.trial_ends_at AS billing_trial_ends_at
+ FROM tenants t
+ LEFT JOIN tenants_billing tb
+ ON tb.tenant_id = t.id
+ ORDER BY t.id LIMIT 1
+ `)
if err != nil {
return nil, errors.Wrap(err, "failed to get first tenant")
}
@@ -169,7 +201,15 @@ func (s *TenantStorage) First() (*models.Tenant, error) {
func (s *TenantStorage) GetByDomain(domain string) (*models.Tenant, error) {
tenant := dbTenant{}
- err := s.trx.Get(&tenant, "SELECT id, name, subdomain, cname, invitation, welcome_message, status, is_private, logo_bkey, custom_css FROM tenants WHERE subdomain = $1 OR subdomain = $2 OR cname = $3 ORDER BY cname DESC", env.Subdomain(domain), domain, domain)
+ err := s.trx.Get(&tenant, `
+ SELECT t.id, t.name, t.subdomain, t.cname, t.invitation, t.welcome_message, t.status, t.is_private, t.logo_bkey, t.custom_css,
+ tb.trial_ends_at AS billing_trial_ends_at
+ FROM tenants t
+ LEFT JOIN tenants_billing tb
+ ON tb.tenant_id = t.id
+ WHERE t.subdomain = $1 OR t.subdomain = $2 OR t.cname = $3
+ ORDER BY t.cname DESC
+ `, env.Subdomain(domain), domain, domain)
if err != nil {
return nil, errors.Wrap(err, "failed to get tenant with domain '%s'", domain)
}
diff --git a/app/storage/postgres/tenant_test.go b/app/storage/postgres/tenant_test.go
index 97af1dfb9..418655711 100644
--- a/app/storage/postgres/tenant_test.go
+++ b/app/storage/postgres/tenant_test.go
@@ -37,6 +37,28 @@ func TestTenantStorage_Add_Activate(t *testing.T) {
Expect(tenant.Subdomain).Equals("mydomain")
Expect(tenant.Status).Equals(models.TenantActive)
Expect(tenant.IsPrivate).IsFalse()
+ Expect(tenant.Billing).IsNil()
+}
+
+func TestTenantStorage_Add_WithBillingEnabled(t *testing.T) {
+ SetupDatabaseTest(t)
+ defer TeardownDatabaseTest()
+
+ env.Config.Stripe.SecretKey = "sk_1"
+ env.Config.Stripe.PublicKey = "pk_1"
+ tenant, err := tenants.Add("My Domain Inc.", "mydomain", models.TenantPending)
+
+ Expect(err).IsNil()
+ Expect(tenant).IsNotNil()
+
+ tenant, err = tenants.GetByDomain("mydomain")
+ Expect(err).IsNil()
+ Expect(tenant.Name).Equals("My Domain Inc.")
+ Expect(tenant.Subdomain).Equals("mydomain")
+ Expect(tenant.Status).Equals(models.TenantPending)
+ Expect(tenant.IsPrivate).IsFalse()
+ Expect(tenant.Billing).IsNotNil()
+ Expect(tenant.Billing.TrialEndsAt).TemporarilySimilar(time.Now().Add(30*24*time.Hour), 5*time.Second)
}
func TestTenantStorage_SingleTenant_Add(t *testing.T) {
diff --git a/migrations/201901042021_create_tenants_billing.sql b/migrations/201901042021_create_tenants_billing.sql
new file mode 100644
index 000000000..4c71bbd18
--- /dev/null
+++ b/migrations/201901042021_create_tenants_billing.sql
@@ -0,0 +1,10 @@
+create table if not exists tenants_billing (
+ tenant_id int not null,
+ trial_ends_at timestamptz not null,
+ subscription_ends_at timestamptz null,
+ stripe_customer_id varchar(255) null,
+ stripe_subscription_id varchar(255) null,
+ stripe_plan_id varchar(255) null,
+ primary key (tenant_id),
+ foreign key (tenant_id) references tenants(id)
+);
\ No newline at end of file
diff --git a/public/models/identity.ts b/public/models/identity.ts
index b18d38721..6dca2df9d 100644
--- a/public/models/identity.ts
+++ b/public/models/identity.ts
@@ -8,6 +8,9 @@ export interface Tenant {
status: TenantStatus;
isPrivate: boolean;
logoBlobKey: string;
+ billing?: {
+ trialEndsAt: string;
+ };
}
export enum TenantStatus {
diff --git a/public/pages/Administration/components/SideMenu.tsx b/public/pages/Administration/components/SideMenu.tsx
index 149107446..23ee84766 100644
--- a/public/pages/Administration/components/SideMenu.tsx
+++ b/public/pages/Administration/components/SideMenu.tsx
@@ -64,7 +64,7 @@ export const SideMenu = (props: SiteMenuProps) => {
<SideMenuItem name="advanced" title="Advanced" href="/admin/advanced" isActive={activeItem === "advanced"} />
{Fider.session.user.isAdministrator && (
<>
- {Fider.isBillingEnabled() && (
+ {Fider.isBillingEnabled() && !!Fider.session.tenant.billing && (
<SideMenuItem name="billing" title="Billing" href="/admin/billing" isActive={activeItem === "billing"} />
)}
<SideMenuItem name="export" title="Export" href="/admin/export" isActive={activeItem === "export"} />