Skip to content

Commit

Permalink
Merge pull request #2639 from manuelsc/nobids/fix_app_subs_queries
Browse files Browse the repository at this point in the history
NOBIDS Fix app subs queries
  • Loading branch information
recy21 authored Oct 24, 2023
2 parents f406e9c + 468c13e commit 495071c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
41 changes: 37 additions & 4 deletions db/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@ func GetUserIdByApiKey(apiKey string) (*types.UserWithPremium, error) {
return cached.(*types.UserWithPremium), nil
}
data := &types.UserWithPremium{}
row := FrontendWriterDB.QueryRow("SELECT id, (SELECT product_id from users_app_subscriptions WHERE user_id = users.id AND active = true order by id desc limit 1) FROM users WHERE api_key = $1", apiKey)
row := FrontendWriterDB.QueryRow(`
SELECT id, (
SELECT product_id
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
ELSE 4 -- For any other product_id values
END, id desc limit 1
) FROM users
WHERE api_key = $1`, apiKey)
err := row.Scan(&data.ID, &data.Product)
if err != nil {
return nil, err
Expand Down Expand Up @@ -428,16 +440,37 @@ type PremiumResult struct {

func GetUserPremiumPackage(userID uint64) (PremiumResult, error) {
var pkg PremiumResult
err := FrontendWriterDB.Get(&pkg,
"SELECT COALESCE(product_id, '') as product_id, COALESCE(store, '') as store from users_app_subscriptions WHERE user_id = $1 AND active = true order by id desc",
err := FrontendWriterDB.Get(&pkg, `
SELECT COALESCE(product_id, '') as product_id, COALESCE(store, '') as store
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
ELSE 4 -- For any other product_id values
END, id desc`,
userID,
)
return pkg, err
}

func GetUserPremiumSubscription(id uint64) (types.UserPremiumSubscription, error) {
userSub := types.UserPremiumSubscription{}
err := FrontendWriterDB.Get(&userSub, "SELECT user_id, store, active, COALESCE(product_id, '') as product_id, COALESCE(reject_reason, '') as reject_reason FROM users_app_subscriptions WHERE user_id = $1 ORDER BY active desc, id desc LIMIT 1", id)
err := FrontendWriterDB.Get(&userSub, `
SELECT user_id, store, active, COALESCE(product_id, '') as product_id, COALESCE(reject_reason, '') as reject_reason
FROM users_app_subscriptions
WHERE user_id = $1
ORDER BY
active desc,
CASE product_id
WHEN 'whale' THEN 1
WHEN 'goldfish' THEN 2
WHEN 'plankton' THEN 3
ELSE 4 -- For any other product_id values
END,
id desc
LIMIT 1`, id)
return userSub, err
}

Expand Down
36 changes: 34 additions & 2 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,23 @@ func LoginPost(w http.ResponseWriter, r *http.Request) {
}
}

err = db.FrontendWriterDB.Get(&user, "SELECT users.id, email, password, email_confirmed, COALESCE(product_id, '') as product_id, COALESCE(active, false) as active, COALESCE(user_group, '') AS user_group FROM users left join users_app_subscriptions on users_app_subscriptions.user_id = users.id WHERE email = $1", email)
err = db.FrontendWriterDB.Get(&user, `
WITH
latest_and_greatest_sub AS (
SELECT user_id, product_id, active, created_at FROM users_app_subscriptions
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
ELSE 4 -- 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
FROM users
left join latest_and_greatest_sub on latest_and_greatest_sub.user_id = users.id
WHERE email = $1`, email)
if err != nil {
if err != sql.ErrNoRows {
logger.Errorf("error retrieving password for user %v: %v", email, err)
Expand Down Expand Up @@ -373,7 +389,23 @@ func ResetPassword(w http.ResponseWriter, r *http.Request) {
ProductID string `db:"product_id"`
Active bool `db:"active"`
}{}
err = db.FrontendWriterDB.Get(&dbUser, "SELECT users.id, email_confirmed, email, COALESCE(product_id, '') as product_id, COALESCE(active, false) as active FROM users LEFT JOIN users_app_subscriptions on users_app_subscriptions.user_id = users.id WHERE password_reset_hash = $1", hash)
err = db.FrontendWriterDB.Get(&dbUser, `
WITH
latest_and_greatest_sub AS (
SELECT user_id, product_id, active, created_at FROM users_app_subscriptions
left join users on users.id = user_id
WHERE users.password_reset_hash = $1 AND active = true
ORDER BY CASE product_id
WHEN 'whale' THEN 1
WHEN 'goldfish' THEN 2
WHEN 'plankton' THEN 3
ELSE 4 -- For any other product_id values
END, users_app_subscriptions.created_at DESC LIMIT 1
)
SELECT users.id, email_confirmed, email, COALESCE(product_id, '') as product_id, COALESCE(active, false) as active
FROM users
left join latest_and_greatest_sub on latest_and_greatest_sub.user_id = users.id
WHERE password_reset_hash = $1`, hash)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
session.AddFlash("Error: Invalid reset link, please retry.")
Expand Down

0 comments on commit 495071c

Please sign in to comment.