[Bug] Reassigning an expired subscription returns 200 but does not reactivate it
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
-
Create/assign a subscription-type group to a user for 30 days.
-
Let the subscription expire (or prepare an existing row with
status = expiredandexpires_at < now()). -
In the admin UI, use Assign Subscription again for the same user and group with the same validity and notes.
-
Observe the request:
POST /api/v1/admin/subscriptions/assign -> HTTP 200 -
Query the user's active subscriptions or inspect the database row.
Actual behavior
The old expired row is returned without modification:
statusremainsexpiredstarts_atandexpires_atremain unchangedupdated_atremains unchangedGET /api/v1/subscriptions/activedoes 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:
- 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 - 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.gobackend/internal/service/subscription_service.gobackend/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