Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: users can upgrade their team plan trial to enterprise plan (#3322)

ecmadao37cdc5c2988a
diff --git a/api/plan.go b/api/plan.go
index 84a4d7b726..6ed0918833 100644
--- a/api/plan.go
+++ b/api/plan.go
@@ -30,6 +30,20 @@ func (p PlanType) String() string {
return ""
}
+// Priority returns the priority of the plan type.
+// Higher priority means the plan supports more features.
+func (p PlanType) Priority() int {
+ switch p {
+ case FREE:
+ return 1
+ case TEAM:
+ return 2
+ case ENTERPRISE:
+ return 3
+ }
+ return 0
+}
+
// FeatureType is the type of a feature.
type FeatureType string
diff --git a/frontend/src/components/FeatureModal.vue b/frontend/src/components/FeatureModal.vue
index 74a44ecb19..7593f3f3ce 100644
--- a/frontend/src/components/FeatureModal.vue
+++ b/frontend/src/components/FeatureModal.vue
@@ -22,20 +22,28 @@
</div>
<div class="mt-3">
<p class="whitespace-pre-wrap">
- {{
- $t("subscription.required-plan-with-trial", {
- requiredPlan: $t(
- `subscription.plan.${planTypeToString(requiredPlan)}.title`
- ),
- startTrial: $t("subscription.trial-for-plan", {
- plan: $t(
- `subscription.plan.${planTypeToString(
- subscriptionStore.trialingPlan
- )}.title`
- ),
- }).toLowerCase(),
- })
- }}*
+ <i18n-t keypath="subscription.required-plan-with-trial">
+ <template #requiredPlan>
+ <span class="font-bold text-accent">
+ {{
+ $t(
+ `subscription.plan.${planTypeToString(requiredPlan)}.title`
+ )
+ }}
+ </span>
+ </template>
+ <template #startTrial>
+ {{
+ $t("subscription.trial-for-plan", {
+ plan: $t(
+ `subscription.plan.${planTypeToString(
+ subscriptionStore.trialingPlan
+ )}.title`
+ ),
+ }).toLowerCase()
+ }}
+ </template>
+ </i18n-t>
</p>
</div>
<div class="mt-7 flex justify-end space-x-2">
diff --git a/frontend/src/store/modules/subscription.ts b/frontend/src/store/modules/subscription.ts
index 1304149971..99948445f5 100644
--- a/frontend/src/store/modules/subscription.ts
+++ b/frontend/src/store/modules/subscription.ts
@@ -59,7 +59,16 @@ export const useSubscriptionStore = defineStore("subscription", {
return daysBeforeExpire / total < 0.5;
},
canTrial(state): boolean {
- return !state.subscription || state.subscription.plan === PlanType.FREE;
+ if (!state.subscription) {
+ return true;
+ }
+ if (state.subscription.plan === PlanType.FREE) {
+ return true;
+ }
+ return (
+ state.subscription.trialing &&
+ state.subscription.plan < state.trialingPlan
+ );
},
},
actions: {
diff --git a/server/server.go b/server/server.go
index 9c63a25852..cde43680e0 100644
--- a/server/server.go
+++ b/server/server.go
@@ -411,13 +411,12 @@ func (s *Server) initMetricReporter(workspaceID string) {
func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
// initial branding
- _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
+ if _, _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: api.SystemBotID,
Name: api.SettingBrandingLogo,
Value: "",
Description: "The branding logo image in base64 string format.",
- })
- if err != nil {
+ }); err != nil {
return nil, err
}
@@ -428,7 +427,7 @@ func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to generate random JWT secret")
}
- authSetting, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
+ authSetting, _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: api.SystemBotID,
Name: api.SettingAuthSecret,
Value: value,
@@ -440,7 +439,7 @@ func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
conf.secret = authSetting.Value
// initial workspace
- workspaceSetting, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
+ workspaceSetting, _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: api.SystemBotID,
Name: api.SettingWorkspaceID,
Value: uuid.New().String(),
@@ -452,7 +451,7 @@ func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
conf.workspaceID = workspaceSetting.Value
// initial license
- if _, err = store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
+ if _, _, err = store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: api.SystemBotID,
Name: api.SettingEnterpriseLicense,
Value: "",
@@ -462,7 +461,7 @@ func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
}
// initial feishu app
- if _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
+ if _, _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: api.SystemBotID,
Name: api.SettingAppIM,
Value: "",
diff --git a/server/subscription.go b/server/subscription.go
index 637376b710..b2ec025015 100644
--- a/server/subscription.go
+++ b/server/subscription.go
@@ -69,21 +69,39 @@ func (s *Server) registerSubscriptionRoutes(g *echo.Group) {
OrgName: s.workspaceID,
}
+ upgradeTrial := s.subscription.Trialing && license.Plan.Priority() > s.subscription.Plan.Priority()
+ if upgradeTrial {
+ license.ExpiresTs = s.subscription.ExpiresTs
+ license.IssuedTs = s.subscription.StartedTs
+ }
+
value, err := json.Marshal(license)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal license").SetInternal(err)
}
ctx := c.Request().Context()
- if _, err := s.store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
+ _, created, err := s.store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: c.Get(getPrincipalIDContextKey()).(int),
Name: api.SettingEnterpriseTrial,
Value: string(value),
Description: "The trialing license.",
- }); err != nil {
+ })
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create license").SetInternal(err)
}
+ if created && upgradeTrial {
+ // We need to override SettingEnterpriseLicense with empty value so that we can get the valid free trial.
+ if _, err := s.store.PatchSetting(ctx, &api.SettingPatch{
+ UpdaterID: api.SystemBotID,
+ Name: api.SettingEnterpriseLicense,
+ Value: "",
+ }); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to remove license").SetInternal(err)
+ }
+ }
+
s.subscription = s.loadSubscription(ctx)
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
diff --git a/store/setting.go b/store/setting.go
index aa3325a1ff..efb32d34f7 100644
--- a/store/setting.go
+++ b/store/setting.go
@@ -48,16 +48,16 @@ func (raw *settingRaw) toSetting() *api.Setting {
}
// CreateSettingIfNotExist creates an instance of Setting.
-func (s *Store) CreateSettingIfNotExist(ctx context.Context, create *api.SettingCreate) (*api.Setting, error) {
- settingRaw, err := s.createSettingRawIfNotExist(ctx, create)
+func (s *Store) CreateSettingIfNotExist(ctx context.Context, create *api.SettingCreate) (*api.Setting, bool, error) {
+ settingRaw, created, err := s.createSettingRawIfNotExist(ctx, create)
if err != nil {
- return nil, errors.Wrapf(err, "failed to create Setting with SettingCreate[%+v]", create)
+ return nil, false, errors.Wrapf(err, "failed to create Setting with SettingCreate[%+v]", create)
}
setting, err := s.composeSetting(ctx, settingRaw)
if err != nil {
- return nil, errors.Wrapf(err, "failed to compose Setting with settingRaw[%+v]", settingRaw)
+ return nil, false, errors.Wrapf(err, "failed to compose Setting with settingRaw[%+v]", settingRaw)
}
- return setting, nil
+ return setting, created, nil
}
// FindSetting finds a list of Setting instances.
@@ -113,7 +113,8 @@ func (s *Store) composeSetting(ctx context.Context, raw *settingRaw) (*api.Setti
}
// createSettingRawIfNotExist creates a new setting only if the named setting does not exist.
-func (s *Store) createSettingRawIfNotExist(ctx context.Context, create *api.SettingCreate) (*settingRaw, error) {
+// The returned bool means the resource is created successfully.
+func (s *Store) createSettingRawIfNotExist(ctx context.Context, create *api.SettingCreate) (*settingRaw, bool, error) {
// We do a find followed by a create if NOT found. Though SQLite supports UPSERT ON CONFLICT DO NOTHING syntax, it doesn't
// support RETURNING in such case. So we have to use separate SELECT and INSERT anyway.
find := &api.SettingFind{
@@ -121,28 +122,28 @@ func (s *Store) createSettingRawIfNotExist(ctx context.Context, create *api.Sett
}
setting, err := s.getSettingRaw(ctx, find)
if err != nil {
- return nil, err
+ return nil, false, err
}
if setting == nil {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
- return nil, FormatError(err)
+ return nil, false, FormatError(err)
}
defer tx.Rollback()
setting, err := createSettingImpl(ctx, tx, create)
if err != nil {
- return nil, err
+ return nil, false, err
}
if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
+ return nil, false, FormatError(err)
}
- return setting, nil
+ return setting, true, nil
}
- return setting, nil
+ return setting, false, nil
}
// findSettingRaw retrieves a list of settings based on find.