feat: filter subscription plans by category
Add optional category query param to GET /subscription-plans (LEARN_ENGLISH, IELTS, DUOLINGO), combined with active_only. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
66b1b52d55
commit
69788a1e43
|
|
@ -14,7 +14,15 @@ SELECT * FROM subscription_plans WHERE id = $1;
|
|||
|
||||
-- name: ListSubscriptionPlans :many
|
||||
SELECT * FROM subscription_plans
|
||||
WHERE ($1::BOOLEAN IS NULL OR $1 = true AND is_active = true OR $1 = false)
|
||||
WHERE (
|
||||
sqlc.narg('active_only')::boolean IS NULL
|
||||
OR sqlc.narg('active_only')::boolean = false
|
||||
OR is_active = true
|
||||
)
|
||||
AND (
|
||||
sqlc.narg('category')::varchar IS NULL
|
||||
OR category = sqlc.narg('category')::varchar
|
||||
)
|
||||
ORDER BY price ASC;
|
||||
|
||||
-- name: ListActiveSubscriptionPlans :many
|
||||
|
|
|
|||
|
|
@ -957,12 +957,25 @@ func (q *Queries) ListSubscriptionDisplayStatusesByUserIDs(ctx context.Context,
|
|||
|
||||
const ListSubscriptionPlans = `-- name: ListSubscriptionPlans :many
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category FROM subscription_plans
|
||||
WHERE ($1::BOOLEAN IS NULL OR $1 = true AND is_active = true OR $1 = false)
|
||||
WHERE (
|
||||
$1::boolean IS NULL
|
||||
OR $1::boolean = false
|
||||
OR is_active = true
|
||||
)
|
||||
AND (
|
||||
$2::varchar IS NULL
|
||||
OR category = $2::varchar
|
||||
)
|
||||
ORDER BY price ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListSubscriptionPlans(ctx context.Context, dollar_1 bool) ([]SubscriptionPlan, error) {
|
||||
rows, err := q.db.Query(ctx, ListSubscriptionPlans, dollar_1)
|
||||
type ListSubscriptionPlansParams struct {
|
||||
ActiveOnly pgtype.Bool `json:"active_only"`
|
||||
Category pgtype.Text `json:"category"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListSubscriptionPlans(ctx context.Context, arg ListSubscriptionPlansParams) ([]SubscriptionPlan, error) {
|
||||
rows, err := q.db.Query(ctx, ListSubscriptionPlans, arg.ActiveOnly, arg.Category)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,18 @@ var CategorySubscriptionGateDisabled = false
|
|||
// ExamPrepSubscriptionGateDisabled skips IELTS/DUOLINGO subscription checks on exam-prep routes (temporary).
|
||||
var ExamPrepSubscriptionGateDisabled = true
|
||||
|
||||
// IsValidSubscriptionCategory reports whether category is a known plan category.
|
||||
func IsValidSubscriptionCategory(category string) bool {
|
||||
switch normalizeSubscriptionCategory(category) {
|
||||
case string(SubscriptionCategoryLearnEnglish),
|
||||
string(SubscriptionCategoryIELTS),
|
||||
string(SubscriptionCategoryDuolingo):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsLMSSubscriptionCategory reports plan categories that unlock Learn English (LMS) content.
|
||||
func IsLMSSubscriptionCategory(category string) bool {
|
||||
return normalizeSubscriptionCategory(category) == string(SubscriptionCategoryLearnEnglish)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ type SubscriptionStore interface {
|
|||
// Subscription Plans
|
||||
CreateSubscriptionPlan(ctx context.Context, input domain.CreateSubscriptionPlanInput) (*domain.SubscriptionPlan, error)
|
||||
GetSubscriptionPlanByID(ctx context.Context, id int64) (*domain.SubscriptionPlan, error)
|
||||
ListSubscriptionPlans(ctx context.Context, activeOnly bool) ([]domain.SubscriptionPlan, error)
|
||||
ListSubscriptionPlans(ctx context.Context, activeOnly bool, category *string) ([]domain.SubscriptionPlan, error)
|
||||
UpdateSubscriptionPlan(ctx context.Context, id int64, input domain.UpdateSubscriptionPlanInput) error
|
||||
DeleteSubscriptionPlan(ctx context.Context, id int64) error
|
||||
|
||||
|
|
|
|||
|
|
@ -66,15 +66,11 @@ func (s *Store) GetSubscriptionPlanByID(ctx context.Context, id int64) (*domain.
|
|||
return subscriptionPlanToDomain(plan), nil
|
||||
}
|
||||
|
||||
func (s *Store) ListSubscriptionPlans(ctx context.Context, activeOnly bool) ([]domain.SubscriptionPlan, error) {
|
||||
var plans []dbgen.SubscriptionPlan
|
||||
var err error
|
||||
|
||||
if activeOnly {
|
||||
plans, err = s.queries.ListActiveSubscriptionPlans(ctx)
|
||||
} else {
|
||||
plans, err = s.queries.ListSubscriptionPlans(ctx, false)
|
||||
}
|
||||
func (s *Store) ListSubscriptionPlans(ctx context.Context, activeOnly bool, category *string) ([]domain.SubscriptionPlan, error) {
|
||||
plans, err := s.queries.ListSubscriptionPlans(ctx, dbgen.ListSubscriptionPlansParams{
|
||||
ActiveOnly: pgtype.Bool{Bool: activeOnly, Valid: true},
|
||||
Category: toPgText(category),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ func (s *Service) GetPlanByID(ctx context.Context, id int64) (*domain.Subscripti
|
|||
return s.store.GetSubscriptionPlanByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) ListPlans(ctx context.Context, activeOnly bool) ([]domain.SubscriptionPlan, error) {
|
||||
return s.store.ListSubscriptionPlans(ctx, activeOnly)
|
||||
func (s *Service) ListPlans(ctx context.Context, activeOnly bool, category *string) ([]domain.SubscriptionPlan, error) {
|
||||
return s.store.ListSubscriptionPlans(ctx, activeOnly, category)
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePlan(ctx context.Context, id int64, input domain.UpdateSubscriptionPlanInput) error {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
|
@ -152,17 +153,31 @@ func (h *Handler) CreateSubscriptionPlan(c *fiber.Ctx) error {
|
|||
|
||||
// ListSubscriptionPlans godoc
|
||||
// @Summary List subscription plans
|
||||
// @Description Returns all subscription plans
|
||||
// @Description Returns subscription plans. Optional category filter: LEARN_ENGLISH, IELTS, or DUOLINGO.
|
||||
// @Tags subscriptions
|
||||
// @Produce json
|
||||
// @Param active_only query bool false "Return only active plans"
|
||||
// @Param active_only query bool false "Return only active plans (default true)"
|
||||
// @Param category query string false "Filter by plan category (LEARN_ENGLISH, IELTS, DUOLINGO)"
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Failure 400 {object} domain.ErrorResponse
|
||||
// @Failure 500 {object} domain.ErrorResponse
|
||||
// @Router /api/v1/subscription-plans [get]
|
||||
func (h *Handler) ListSubscriptionPlans(c *fiber.Ctx) error {
|
||||
activeOnly := c.Query("active_only", "true") == "true"
|
||||
|
||||
plans, err := h.subscriptionsSvc.ListPlans(c.Context(), activeOnly)
|
||||
var categoryFilter *string
|
||||
if raw := strings.TrimSpace(c.Query("category")); raw != "" {
|
||||
normalized := strings.ToUpper(raw)
|
||||
if !domain.IsValidSubscriptionCategory(normalized) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid category",
|
||||
Error: "category must be one of: LEARN_ENGLISH, IELTS, DUOLINGO",
|
||||
})
|
||||
}
|
||||
categoryFilter = &normalized
|
||||
}
|
||||
|
||||
plans, err := h.subscriptionsSvc.ListPlans(c.Context(), activeOnly, categoryFilter)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to list subscription plans",
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user