From fc8268990e541ba9d798e2e0c0555e965c61efa3 Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Mon, 5 Dec 2022 14:34:51 -0500 Subject: [PATCH 1/4] Add check for subscription change to product with sku cloud-starter. --- api4/cloud.go | 18 ++++++++++++++++-- model/cloud.go | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/api4/cloud.go b/api4/cloud.go index 173d975279..0dd56c3055 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -7,6 +7,7 @@ import ( "bytes" "encoding/binary" "encoding/json" + "fmt" "io" "net/http" "time" @@ -96,6 +97,8 @@ func getSubscription(c *Context, w http.ResponseWriter, r *http.Request) { } func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) { + userId := c.AppContext.Session().UserId + if !c.App.Channels().License().IsCloud() { c.Err = model.NewAppError("Api4.changeSubscription", "api.cloud.license_error", nil, "", http.StatusInternalServerError) return @@ -118,13 +121,13 @@ func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) { return } - currentSubscription, appErr := c.App.Cloud().GetSubscription(c.AppContext.Session().UserId) + currentSubscription, appErr := c.App.Cloud().GetSubscription(userId) if appErr != nil { c.Err = model.NewAppError("Api4.changeSubscription", "api.cloud.app_error", nil, "", http.StatusInternalServerError).Wrap(appErr) return } - changedSub, err := c.App.Cloud().ChangeSubscription(c.AppContext.Session().UserId, currentSubscription.ID, subscriptionChange) + changedSub, err := c.App.Cloud().ChangeSubscription(userId, currentSubscription.ID, subscriptionChange) if err != nil { c.Err = model.NewAppError("Api4.changeSubscription", "api.cloud.app_error", nil, "", http.StatusInternalServerError).Wrap(err) return @@ -136,6 +139,17 @@ func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) { return } + changedProduct, err := c.App.Cloud().GetCloudProduct(userId, changedSub.ProductID) + if err != nil { + c.Err = model.NewAppError("Api4.changeSubscription", "api_cloud.app_error", nil, "", http.StatusInternalServerError).Wrap(err) + } + + starterSku := fmt.Sprintf("%s-%s", model.SubscriptionFamilyCloud, model.ProductSkuStarter) + if changedProduct.SKU != starterSku { + w.Write(json) + return + } + // Log failures for purchase confirmation email, but don't show an error to the user so as not to confuse them // At this point, the upgrade is complete. if appErr := c.App.SendUpgradeConfirmationEmail(); appErr != nil { diff --git a/model/cloud.go b/model/cloud.go index 2985c929ea..d4e7d614e0 100644 --- a/model/cloud.go +++ b/model/cloud.go @@ -40,6 +40,14 @@ const ( SubscriptionFamilyOnPrem = SubscriptionFamily("on-prem") ) +type ProductSku string + +const ( + ProductSkuStarter = ProductSku("starter") + ProductSkuProfessional = ProductSku("professional") + ProductSkuEnterprise = ProductSku("enterprise") +) + // Product model represents a product on the cloud system. type Product struct { ID string `json:"id"` From 352eedf7c1bd90b88fc05a7a36e39a26c86acafa Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Mon, 5 Dec 2022 14:53:38 -0500 Subject: [PATCH 2/4] Fix app error id. --- api4/cloud.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api4/cloud.go b/api4/cloud.go index 0dd56c3055..7f4a013047 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -141,7 +141,7 @@ func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) { changedProduct, err := c.App.Cloud().GetCloudProduct(userId, changedSub.ProductID) if err != nil { - c.Err = model.NewAppError("Api4.changeSubscription", "api_cloud.app_error", nil, "", http.StatusInternalServerError).Wrap(err) + c.Err = model.NewAppError("Api4.changeSubscription", "api.cloud.app_error", nil, "", http.StatusInternalServerError).Wrap(err) } starterSku := fmt.Sprintf("%s-%s", model.SubscriptionFamilyCloud, model.ProductSkuStarter) From 47b81256594124ef48f51e6ca00bf9462edde783 Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Mon, 5 Dec 2022 15:28:36 -0500 Subject: [PATCH 3/4] Fix check for starter sku. --- api4/cloud.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api4/cloud.go b/api4/cloud.go index cf48f44d7f..20d9687946 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -145,7 +145,7 @@ func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) { } starterSku := fmt.Sprintf("%s-%s", model.SubscriptionFamilyCloud, model.ProductSkuStarter) - if product.SKU != starterSku { + if product.SKU == starterSku { w.Write(json) return } From 82d9f2580764f6b3bec911b39dd4df822b05ad77 Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Tue, 6 Dec 2022 13:03:00 -0500 Subject: [PATCH 4/4] Replase product skus with their full names. --- api4/cloud.go | 4 +--- model/cloud.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/api4/cloud.go b/api4/cloud.go index 20d9687946..d7a265e24f 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -7,7 +7,6 @@ import ( "bytes" "encoding/binary" "encoding/json" - "fmt" "io" "net/http" "time" @@ -144,8 +143,7 @@ func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) { c.Logger.Error("Error finding the new cloud product", mlog.Err(err)) } - starterSku := fmt.Sprintf("%s-%s", model.SubscriptionFamilyCloud, model.ProductSkuStarter) - if product.SKU == starterSku { + if product.SKU == string(model.SkuCloudStarter) { w.Write(json) return } diff --git a/model/cloud.go b/model/cloud.go index d4e7d614e0..4494b33dea 100644 --- a/model/cloud.go +++ b/model/cloud.go @@ -43,9 +43,15 @@ const ( type ProductSku string const ( - ProductSkuStarter = ProductSku("starter") - ProductSkuProfessional = ProductSku("professional") - ProductSkuEnterprise = ProductSku("enterprise") + SkuStarterGov = ProductSku("starter-gov") + SkuProfessionalGov = ProductSku("professional-gov") + SkuEnterpriseGov = ProductSku("enterprise-gov") + SkuStarter = ProductSku("starter") + SkuProfessional = ProductSku("professional") + SkuEnterprise = ProductSku("enterprise") + SkuCloudStarter = ProductSku("cloud-starter") + SkuCloudProfessional = ProductSku("cloud-professional") + SkuCloudEnterprise = ProductSku("cloud-enterprise") ) // Product model represents a product on the cloud system.