sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

[Bug] Reassigning an expired subscription returns 200 but does not reactivate it

Open fanrj3 opened this issue 4 months ago • 0 comments

Bug description

Reassigning a subscription through the admin API returns HTTP 200 when a row already exists for the same user_id + group_id, even if that subscription is already expired. The existing row is returned unchanged, so the admin UI reports success while the user still has no active subscription.

Environment

  • Sub2API: v0.1.160
  • Official Docker image: ghcr.io/wei-shaw/sub2api:0.1.160
  • Commit: 8bfbc5ca99bf2c0ac96e0f29ffd35eb6aca27e62
  • PostgreSQL deployment

Steps to reproduce

  1. Create/assign a subscription-type group to a user for 30 days.

  2. Let the subscription expire (or prepare an existing row with status = expired and expires_at < now()).

  3. In the admin UI, use Assign Subscription again for the same user and group with the same validity and notes.

  4. Observe the request:

    POST /api/v1/admin/subscriptions/assign
    -> HTTP 200
    
  5. Query the user's active subscriptions or inspect the database row.

Actual behavior

The old expired row is returned without modification:

  • status remains expired
  • starts_at and expires_at remain unchanged
  • updated_at remains unchanged
  • GET /api/v1/subscriptions/active does not include the subscription
  • the UI nevertheless reports that assignment succeeded

This was reproduced repeatedly: every assign request returned HTTP 200, while the database retained only the original expired subscription.

Expected behavior

An expired subscription must not be treated as a successful idempotent duplicate.

Either:

  1. reactivate/renew it from the current time, set status = active, calculate the new expiry, reset usage windows/counters as appropriate, and invalidate subscription/billing caches; or
  2. return a clear conflict/error telling the admin to use the renewal/extend action.

Returning HTTP 200 with an unchanged expired subscription is misleading.

Suspected root cause

The admin handler calls:

SubscriptionHandler.Assign
  -> SubscriptionService.AssignSubscription
  -> assignSubscriptionWithReuse

In assignSubscriptionWithReuse, any existing user_id + group_id row is fetched. If validity and notes do not conflict, the function returns the existing subscription as an idempotent success without checking status or expires_at.

By contrast, AssignOrExtendSubscription already contains logic to renew an expired subscription, but the admin assign endpoint does not use that path.

Relevant files:

  • backend/internal/handler/admin/subscription_handler.go
  • backend/internal/service/subscription_service.go
  • backend/internal/repository/user_subscription_repo.go

Suggested regression tests

  • assigning the same still-active subscription remains idempotent
  • assigning the same expired subscription renews it or returns an explicit conflict
  • an expired subscription is never returned as a successful active assignment
  • renewal resets the intended usage windows/counters
  • subscription and billing caches are invalidated after renewal

fanrj3 avatar Jul 18 '26 04:07 fanrj3