fix(subscriptions): lock negative redemption updates

This commit is contained in:
Rain committed 2026-09-22 15:03:05 +08:00
1 parent 1c0a69c0ce
commit 85ed17b313
3 files changed
+169

No files matched your search

@@ -702,6 +702,14 @@ func (s *RedeemService) reduceOrCancelSubscription(ctx context.Context, userID,
return ErrSubscriptionNotFound
}
// Redemption already owns a transaction. Lock and reread the subscription
// before computing changes: the redeem-code lock cannot serialize different
// codes (or an administrator renewal) targeting the same subscription.
sub, err = s.subscriptionService.userSubRepo.GetByIDForUpdate(ctx, sub.ID)
if err != nil {
return fmt.Errorf("lock subscription for reduction: %w", err)
}
now := time.Now()
remaining := int(sub.ExpiresAt.Sub(now).Hours() / 24)
if remaining < 0 {