Log account delete and subscription changes in UserAuthEvent
Add account_deleted, subscription_started, and subscription_updated event types. Soft-delete, Checkout/Backer assign, and Stripe portal lifecycle webhooks write audit rows visible on the user admin.
This commit is contained in:
@@ -14,6 +14,7 @@ from finance.models import Invoice, Payment, UserSubscription
|
||||
from finance.services.plans import (
|
||||
assign_plan_from_stripe,
|
||||
get_or_create_user_subscription,
|
||||
log_subscription_auth_event,
|
||||
resolve_plan_from_stripe_price,
|
||||
)
|
||||
|
||||
@@ -421,6 +422,7 @@ def handle_customer_subscription_deleted(subscription: dict[str, Any]):
|
||||
return None
|
||||
|
||||
sub = get_or_create_user_subscription(user)
|
||||
prev_status = sub.status
|
||||
sub.status = UserSubscription.Status.CANCELED
|
||||
sub.cancel_at_period_end = False
|
||||
sub.current_period_end = _ts_to_dt(subscription.get("current_period_end"))
|
||||
@@ -430,6 +432,16 @@ def handle_customer_subscription_deleted(subscription: dict[str, Any]):
|
||||
if sub.source == UserSubscription.Source.NONE:
|
||||
sub.source = UserSubscription.Source.STRIPE
|
||||
sub.save()
|
||||
if prev_status != UserSubscription.Status.CANCELED:
|
||||
log_subscription_auth_event(
|
||||
user,
|
||||
started=False,
|
||||
detail=(
|
||||
f"plan={sub.plan.slug if sub.plan_id else 'none'} "
|
||||
f"source={sub.source} status={sub.status} "
|
||||
f"stripe_subscription_id={sub.stripe_subscription_id}"
|
||||
),
|
||||
)
|
||||
return sub
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user