Skip to content

Commit 6e8f44a

Browse files
committed
Address PR feedback: error handling and test coverage
- Handle Stripe::InvalidRequestError in sync_customer_email_to_stripe (mirrors cancel method behavior for deleted customers) - Add test for deactivated owner (owner with nil identity) - Add test for deleted Stripe customer scenario
1 parent 6ccb0d1 commit 6e8f44a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

saas/app/models/account/subscription.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def sync_customer_email_to_stripe
5252
if stripe_customer_id && (email = owner_email)
5353
Stripe::Customer.update(stripe_customer_id, email: email)
5454
end
55+
rescue Stripe::InvalidRequestError => e
56+
# Customer already deleted in Stripe - treat as success
57+
Rails.logger.warn "Stripe customer #{stripe_customer_id} not found during email sync: #{e.message}"
5558
end
5659

5760
private

saas/test/models/account/subscription_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,37 @@ class Account::SubscriptionTest < ActiveSupport::TestCase
158158

159159
subscription.sync_customer_email_to_stripe
160160
end
161+
162+
test "sync_customer_email_to_stripe does nothing when owner has no identity" do
163+
account = accounts(:"37s")
164+
owner = account.users.find_by(role: :owner) || account.users.first.tap { |u| u.update!(role: :owner) }
165+
owner.update_column(:identity_id, nil)
166+
subscription = account.create_subscription!(
167+
stripe_customer_id: "cus_test",
168+
plan_key: "monthly_v1",
169+
status: "active"
170+
)
171+
172+
Stripe::Customer.expects(:update).never
173+
174+
subscription.sync_customer_email_to_stripe
175+
end
176+
177+
test "sync_customer_email_to_stripe treats deleted customer as success" do
178+
account = accounts(:"37s")
179+
account.users.find_by(role: :owner) || account.users.first.tap { |u| u.update!(role: :owner) }
180+
subscription = account.create_subscription!(
181+
stripe_customer_id: "cus_deleted",
182+
plan_key: "monthly_v1",
183+
status: "active"
184+
)
185+
186+
Stripe::Customer.stubs(:update).raises(
187+
Stripe::InvalidRequestError.new("No such customer", {})
188+
)
189+
190+
assert_nothing_raised do
191+
subscription.sync_customer_email_to_stripe
192+
end
193+
end
161194
end

0 commit comments

Comments
 (0)