Skip to content

Commit

Permalink
(BIDS-3049) wip
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Jun 3, 2024
1 parent 46aaac9 commit 524a00e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 69 deletions.
60 changes: 30 additions & 30 deletions db/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions handlers/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 42 additions & 19 deletions utils/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@ 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",
"whale": "dolphin",
}

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 {
Expand Down Expand Up @@ -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 ""
}
Expand Down

0 comments on commit 524a00e

Please sign in to comment.