Block subscription plan delete when subscriptions exist.
Return 409 with a clear message instead of a foreign-key 500 when a plan is still referenced by user subscriptions. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
992cb3f5f1
commit
4cfc090d06
|
|
@ -57,6 +57,9 @@ WHERE plan_id = $1
|
|||
-- name: DeleteSubscriptionPlan :exec
|
||||
DELETE FROM subscription_plans WHERE id = $1;
|
||||
|
||||
-- name: CountUserSubscriptionsByPlanID :one
|
||||
SELECT COUNT(*)::bigint FROM user_subscriptions WHERE plan_id = $1;
|
||||
|
||||
-- =====================
|
||||
-- User Subscriptions
|
||||
-- =====================
|
||||
|
|
|
|||
|
|
@ -109,6 +109,17 @@ func (q *Queries) CountUserSubscriptions(ctx context.Context, userID int64) (int
|
|||
return count, err
|
||||
}
|
||||
|
||||
const CountUserSubscriptionsByPlanID = `-- name: CountUserSubscriptionsByPlanID :one
|
||||
SELECT COUNT(*)::bigint FROM user_subscriptions WHERE plan_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CountUserSubscriptionsByPlanID(ctx context.Context, planID int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountUserSubscriptionsByPlanID, planID)
|
||||
var column_1 int64
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
}
|
||||
|
||||
const CreateSubscriptionPlan = `-- name: CreateSubscriptionPlan :one
|
||||
|
||||
INSERT INTO subscription_plans (
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ type SubscriptionStore interface {
|
|||
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
|
||||
CountUserSubscriptionsByPlanID(ctx context.Context, planID int64) (int64, error)
|
||||
SetLifetimeExpiresForActiveSubscriptionsByPlanID(ctx context.Context, planID int64) error
|
||||
|
||||
// User Subscriptions
|
||||
|
|
|
|||
|
|
@ -106,6 +106,10 @@ func (s *Store) DeleteSubscriptionPlan(ctx context.Context, id int64) error {
|
|||
return s.queries.DeleteSubscriptionPlan(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Store) CountUserSubscriptionsByPlanID(ctx context.Context, planID int64) (int64, error) {
|
||||
return s.queries.CountUserSubscriptionsByPlanID(ctx, planID)
|
||||
}
|
||||
|
||||
// =====================
|
||||
// User Subscriptions
|
||||
// =====================
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ var (
|
|||
ErrSubscriptionNotOwned = errors.New("subscription does not belong to this user")
|
||||
ErrAlreadySubscribed = errors.New("user already has an active subscription for this category")
|
||||
ErrInvalidPlan = errors.New("invalid subscription plan")
|
||||
ErrPlanInUse = errors.New("subscription plan is linked to existing user subscriptions")
|
||||
ErrInvalidExtendInput = errors.New("provide exactly one of new_expires_at, duration_value+duration_unit, or renew_one_period")
|
||||
ErrExtendExpiresInPast = errors.New("new expiry must be in the future")
|
||||
ErrNotALearner = errors.New("target user is not a learner account")
|
||||
|
|
@ -90,6 +91,21 @@ func (s *Service) UpdatePlan(ctx context.Context, id int64, input domain.UpdateS
|
|||
}
|
||||
|
||||
func (s *Service) DeletePlan(ctx context.Context, id int64) error {
|
||||
if _, err := s.store.GetSubscriptionPlanByID(ctx, id); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrPlanNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
count, err := s.store.CountUserSubscriptionsByPlanID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return ErrPlanInUse
|
||||
}
|
||||
|
||||
return s.store.DeleteSubscriptionPlan(ctx, id)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -353,6 +353,18 @@ func (h *Handler) DeleteSubscriptionPlan(c *fiber.Ctx) error {
|
|||
|
||||
err = h.subscriptionsSvc.DeletePlan(c.Context(), id)
|
||||
if err != nil {
|
||||
if errors.Is(err, subscriptionsvc.ErrPlanNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Subscription plan not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
if errors.Is(err, subscriptionsvc.ErrPlanInUse) {
|
||||
return c.Status(fiber.StatusConflict).JSON(domain.ErrorResponse{
|
||||
Message: "This plan cannot be deleted because learners already have subscriptions on it. Deactivate the plan instead so it is hidden from new purchases.",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to delete subscription plan",
|
||||
Error: err.Error(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user