Fix Stripe error on demo family deletion

Merged
SE
2mo ago
+69-13

This PR addresses an Stripe::InvalidRequestError that occurred when deleting demo families, specifically Stripe::InvalidRequestError: No such subscription: 'sub_demo_123'.

Problem:
Demo families are created with a synthetic stripe_id of 'sub_demo_123' by Demo::Generator. When a demo family was destroyed (e.g., by UserPurgeJob), the Family::Subscribeable#cancel_or_reject_active_subscription callback would attempt to cancel this non-existent subscription via the Stripe API, leading to an error.

Solution:

  1. Centralized Sentinel ID: The 'sub_demo_123' string has been extracted into a constant Subscription::DEMO_STRIPE_ID to serve as the single source of truth for the demo sentinel.
  2. synthetic? Predicate: A synthetic? method has been added to the Subscription model. This predicate returns true if the subscription's stripe_id matches Subscription::DEMO_STRIPE_ID, providing a clear way to identify demo subscriptions.
  3. Guarded Stripe API Call: The Family::Subscribeable#cancel_or_reject_active_subscription callback now uses subscription.synthetic? as an early return guard. This prevents any attempt to call the Stripe API for demo subscriptions, resolving the InvalidRequestError.
  4. Dependency Direction: This refactoring ensures that core billing logic (Family::Subscribeable) does not have a direct dependency on Demo::Generator, improving architectural clarity.
  5. Code Cleanup:
    • Removed redundant subscription.update!(status: :canceled) logic from DemoFamilyRefreshJob as the primary guard is now in Family::Subscribeable.
    • Updated comments and test files to consistently use Subscription::DEMO_STRIPE_ID.
    • Fixed a Layout/EmptyLinesAroundMethodBody RuboCop violation in app/jobs/demo_family_refresh_job.rb.

This change ensures that demo family deletion proceeds smoothly without unnecessary external API calls or errors.

Fixes SURE-APP-1BW

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry. You can adjust this setting at any time.