From 524a00ea3b2b7d658a54cabb0f6eadc83f0884d9 Mon Sep 17 00:00:00 2001 From: Patrick Pfeiffer Date: Mon, 3 Jun 2024 12:42:41 +0200 Subject: [PATCH] (BIDS-3049) wip --- db/frontend.go | 60 ++++++++++++++++++++++----------------------- handlers/auth.go | 20 +++++++-------- handlers/stripe.go | 22 +++++++++-------- utils/products.go | 61 +++++++++++++++++++++++++++++++--------------- 4 files changed, 94 insertions(+), 69 deletions(-) diff --git a/db/frontend.go b/db/frontend.go index a7ce580289..4fa1c70d19 100644 --- a/db/frontend.go +++ b/db/frontend.go @@ -78,16 +78,16 @@ func GetUserIdByApiKey(apiKey string) (*types.UserWithPremium, error) { from users_app_subscriptions WHERE user_id = users.id AND active = true order by CASE product_id - WHEN 'whale' THEN 1 - WHEN 'goldfish' THEN 2 - WHEN 'plankton' THEN 3 - WHEN 'orca' THEN 1 - WHEN 'dolphin' THEN 2 - WHEN 'guppy' THEN 3 - WHEN 'orca.yearly' THEN 1 - WHEN 'dolphin.yearly' THEN 2 - WHEN 'guppy.yearly' THEN 3 - ELSE 4 -- For any other product_id values + WHEN 'orca.yearly' THEN 1 + WHEN 'dolphin.yearly' THEN 2 + WHEN 'guppy.yearly' THEN 3 + WHEN 'orca' THEN 4 + WHEN 'dolphin' THEN 5 + WHEN 'guppy' THEN 6 + WHEN 'whale' THEN 7 + WHEN 'goldfish' THEN 8 + WHEN 'plankton' THEN 9 + ELSE 10 -- For any other product_id values END, id desc limit 1 ) FROM users WHERE api_key = $1`, apiKey) @@ -457,16 +457,16 @@ func GetUserPremiumPackage(userID uint64) (PremiumResult, error) { from users_app_subscriptions WHERE user_id = $1 AND active = true order by CASE product_id - WHEN 'whale' THEN 1 - WHEN 'goldfish' THEN 2 - WHEN 'plankton' THEN 3 - WHEN 'orca' THEN 1 - WHEN 'dolphin' THEN 2 - WHEN 'guppy' THEN 3 - WHEN 'orca.yearly' THEN 1 - WHEN 'dolphin.yearly' THEN 2 - WHEN 'guppy.yearly' THEN 3 - ELSE 4 -- For any other product_id values + WHEN 'orca.yearly' THEN 1 + WHEN 'dolphin.yearly' THEN 2 + WHEN 'guppy.yearly' THEN 3 + WHEN 'orca' THEN 4 + WHEN 'dolphin' THEN 5 + WHEN 'guppy' THEN 6 + WHEN 'whale' THEN 7 + WHEN 'goldfish' THEN 8 + WHEN 'plankton' THEN 9 + ELSE 10 -- For any other product_id values END, id desc`, userID, ) @@ -482,16 +482,16 @@ func GetUserPremiumSubscription(id uint64) (types.UserPremiumSubscription, error ORDER BY active desc, CASE product_id - WHEN 'whale' THEN 1 - WHEN 'goldfish' THEN 2 - WHEN 'plankton' THEN 3 - WHEN 'orca' THEN 1 - WHEN 'dolphin' THEN 2 - WHEN 'guppy' THEN 3 - WHEN 'orca.yearly' THEN 1 - WHEN 'dolphin.yearly' THEN 2 - WHEN 'guppy.yearly' THEN 3 - ELSE 4 -- For any other product_id values + WHEN 'orca.yearly' THEN 1 + WHEN 'dolphin.yearly' THEN 2 + WHEN 'guppy.yearly' THEN 3 + WHEN 'orca' THEN 4 + WHEN 'dolphin' THEN 5 + WHEN 'guppy' THEN 6 + WHEN 'whale' THEN 7 + WHEN 'goldfish' THEN 8 + WHEN 'plankton' THEN 9 + ELSE 10 -- For any other product_id values END, id desc LIMIT 1`, id) diff --git a/handlers/auth.go b/handlers/auth.go index ea9ca2d603..8c7010fc3b 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -249,16 +249,16 @@ func LoginPost(w http.ResponseWriter, r *http.Request) { left join users on users.id = user_id WHERE users.email = $1 AND active = true ORDER BY CASE product_id - WHEN 'whale' THEN 1 - WHEN 'goldfish' THEN 2 - WHEN 'plankton' THEN 3 - WHEN 'orca' THEN 1 - WHEN 'dolphin' THEN 2 - WHEN 'guppy' THEN 3 - WHEN 'orca.yearly' THEN 1 - WHEN 'dolphin.yearly' THEN 2 - WHEN 'guppy.yearly' THEN 3 - ELSE 4 -- For any other product_id values + WHEN 'orca.yearly' THEN 1 + WHEN 'dolphin.yearly' THEN 2 + WHEN 'guppy.yearly' THEN 3 + WHEN 'orca' THEN 4 + WHEN 'dolphin' THEN 5 + WHEN 'guppy' THEN 6 + WHEN 'whale' THEN 7 + WHEN 'goldfish' THEN 8 + WHEN 'plankton' THEN 9 + ELSE 10 -- For any other product_id values END, users_app_subscriptions.created_at DESC LIMIT 1 ) SELECT users.id, email, password, email_confirmed, COALESCE(product_id, '') as product_id, COALESCE(active, false) as active, COALESCE(user_group, '') AS user_group diff --git a/handlers/stripe.go b/handlers/stripe.go index 4f7b9d9e22..f1f3b07a88 100644 --- a/handlers/stripe.go +++ b/handlers/stripe.go @@ -51,16 +51,18 @@ func StripeCreateCheckoutSession(w http.ResponseWriter, r *http.Request) { return } - // don't let the user checkout another subscription in the same group - if subscription.Active != nil && *subscription.Active { - logger.Errorf("error there is an active subscription cannot create another one %v", err) - w.WriteHeader(http.StatusBadRequest) - writeJSON(w, struct { - ErrorData string `json:"error"` - }{ - ErrorData: "could not create a new stripe session", - }) - return + if purchaseGroup != utils.GROUP_ADDON { + // don't let the user checkout another subscription in the same group + if subscription.Active != nil && *subscription.Active { + logger.Errorf("error there is an active subscription cannot create another one %v", err) + w.WriteHeader(http.StatusBadRequest) + writeJSON(w, struct { + ErrorData string `json:"error"` + }{ + ErrorData: "could not create a new stripe session", + }) + return + } } // taxRates := utils.StripeDynamicRatesLive diff --git a/utils/products.go b/utils/products.go index 366ecfcad2..f08ded750b 100644 --- a/utils/products.go +++ b/utils/products.go @@ -2,6 +2,26 @@ package utils import "time" +const GROUP_API = "api" +const GROUP_MOBILE = "mobile" +const GROUP_ADDON = "addon" + +var ProductsGroups = map[string]string{ + "plankton": GROUP_MOBILE, + "goldfish": GROUP_MOBILE, + "whale": GROUP_MOBILE, + "guppy": GROUP_MOBILE, + "dolphin": GROUP_MOBILE, + "orca": GROUP_MOBILE, + "guppy.yearly": GROUP_MOBILE, + "dolphin.yearly": GROUP_MOBILE, + "orca.yearly": GROUP_MOBILE, + "vdb_addon_1k": GROUP_ADDON, + "vdb_addon_1k.yearly": GROUP_ADDON, + "vdb_addon_10k": GROUP_ADDON, + "vdb_addon_10k.yearly": GROUP_ADDON, +} + var ProductsMapV1ToV2 = map[string]string{ "plankton": "guppy", "goldfish": "guppy", @@ -9,29 +29,24 @@ var ProductsMapV1ToV2 = map[string]string{ } var ProductsMapV2ToV1 = map[string]string{ - "guppy": "goldfish", - "dolphin": "whale", - "orca": "whale", - "guppy.yearly": "goldfish", - "dolphin.yearly": "whale", - "orca.yearly": "whale", + "guppy": "goldfish", + "dolphin": "whale", + "orca": "whale", + "guppy.yearly": "goldfish", + "dolphin.yearly": "whale", + "orca.yearly": "whale", + "vdb_addon_1k": "", + "vdb_addon_1k.yearly": "", + "vdb_addon_10k": "", + "vdb_addon_10k.yearly": "", } -const GROUP_API = "api" -const GROUP_MOBILE = "mobile" -const GROUP_ADDON = "addon" - func GetPurchaseGroup(priceId string) string { - switch priceId { - case Config.Frontend.Stripe.Sapphire, Config.Frontend.Stripe.Emerald, Config.Frontend.Stripe.Diamond, Config.Frontend.Stripe.Iron, Config.Frontend.Stripe.Silver, Config.Frontend.Stripe.Gold, Config.Frontend.Stripe.IronYearly, Config.Frontend.Stripe.SilverYearly, Config.Frontend.Stripe.GoldYearly: - return GROUP_API - case Config.Frontend.Stripe.Whale, Config.Frontend.Stripe.Goldfish, Config.Frontend.Stripe.Plankton, Config.Frontend.Stripe.Orca, Config.Frontend.Stripe.Dolphin, Config.Frontend.Stripe.Guppy, Config.Frontend.Stripe.OrcaYearly, Config.Frontend.Stripe.DolphinYearly, Config.Frontend.Stripe.GuppyYearly: - return GROUP_MOBILE - case Config.Frontend.Stripe.VdbAddon1k, Config.Frontend.Stripe.VdbAddon1kYearly, Config.Frontend.Stripe.VdbAddon10k, Config.Frontend.Stripe.VdbAddon10kYearly: - return GROUP_ADDON - default: - return "" + productId := PriceIdToProductId(priceId) + if group, exists := ProductsGroups[productId]; exists { + return group } + return "" } func EffectiveProductId(productId string) string { @@ -87,6 +102,14 @@ func PriceIdToProductId(priceId string) string { return "dolphin.yearly" case Config.Frontend.Stripe.OrcaYearly: return "orca.yearly" + case Config.Frontend.Stripe.VdbAddon1k: + return "vdb_addon_1k" + case Config.Frontend.Stripe.VdbAddon1kYearly: + return "vdb_addon_1k.yearly" + case Config.Frontend.Stripe.VdbAddon10k: + return "vdb_addon_10k" + case Config.Frontend.Stripe.VdbAddon10kYearly: + return "vdb_addon_10k.yearly" default: return "" }