From fc8268990e541ba9d798e2e0c0555e965c61efa3 Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Mon, 5 Dec 2022 14:34:51 -0500 Subject: [PATCH] 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"`