Repository: aipotheosis-labs/aci
File: backend/aci/server/routes/billing.py (Python)

Code before:
    subscription = crud.subscriptions.update_subscription_by_stripe_id(
        db_session,
        latest_subscription_details.stripe_subscription_id,
        update_data,
    )
    if not subscription:
        logger.error(
            f"Could not find existing Subscription record to update "
            f"stripe_subscription_id={latest_subscription_details.stripe_subscription_id}"
        )
        raise BillingError(
            "Could not find existing Subscription record to update",
            error_code=status.HTTP_400_BAD_REQUEST,
        )

    logger.info(f"SUCCESS {plan}")
    # 5. Update PropelAuth organization max_users based on the new plan
    new_max_users = plan.features["developer_seats"]
    try:
        auth.update_org_metadata(org_id=str(subscription.org_id), max_users=new_max_users)
        logger.info(
            f"Updated PropelAuth org max_users, org_id={subscription.org_id}, "
            f"new_max_users={new_max_users}, plan={plan.name}"
        )
    except Exception as e:
        logger.error(
            f"Failed to update PropelAuth org max_users, org_id={subscription.org_id}, "
            f"new_max_users={new_max_users}, plan={plan.name}, error={e}"
        )
        # Don't fail the webhook if PropelAuth update fails, just log the error
        # The subscription update in our DB is still valid

    db_session.commit()

    logger.info(
        f"Successfully updated subscription record, stripe_subscription_id={latest_subscription_details.stripe_subscription_id}"
    )


async def handle_customer_subscription_deleted(
    subscription_data: dict, db_session: Session
) -> None:
    """
    Handles the customer.subscription.deleted event.
    1. Find the existing subscription record by stripe_subscription_id
    2. Delete the subscription record
    """
    logger.info(
        f"Handling customer.subscription.deleted event, subscription_data={subscription_data}"
    )
    stripe_subscription_id = subscription_data.get("id")

Review comment (bug):
if you want to catch the fulll exception stack trace you can just use `logger.exception(f"Failed to update PropelAuth org max_users, org_id={subscription.org_id}.....")`

Code after:
        raise BillingError(
            f"Could not find internal plan matching stripe price id, "
            f"stripe_price_id={latest_subscription_details.stripe_price_id}",
            error_code=status.HTTP_400_BAD_REQUEST,
        )

    # 4. Update the subscription record with the new details
    update_data = SubscriptionUpdate(
        status=StripeSubscriptionStatus(latest_subscription_details.status),
        plan_id=plan.id,
        interval=latest_subscription_details.interval,
        current_period_end=latest_subscription_details.current_period_end,
        cancel_at_period_end=latest_subscription_details.cancel_at_period_end,
        stripe_customer_id=latest_subscription_details.stripe_customer_id,
    )

    subscription = crud.subscriptions.update_subscription_by_stripe_id(
        db_session,
        latest_subscription_details.stripe_subscription_id,
        update_data,
    )
    if not subscription:
        logger.error(
            f"Could not find existing Subscription record to update "
            f"stripe_subscription_id={latest_subscription_details.stripe_subscription_id}"
        )
        raise BillingError(
            "Could not find existing Subscription record to update",
            error_code=status.HTTP_400_BAD_REQUEST,
        )

    # 5. Update PropelAuth organization max_users based on the new plan
    new_max_users = plan.features["developer_seats"]
    try:
        auth.update_org_metadata(org_id=str(subscription.org_id), max_users=new_max_users)
        logger.info(
            f"Updated PropelAuth org max_users, org_id={subscription.org_id}, "
            f"new_max_users={new_max_users}, plan={plan.name}"
        )
    except Exception:
        logger.exception(
            f"Failed to update PropelAuth org max_users, org_id={subscription.org_id}, "
            f"new_max_users={new_max_users}, plan={plan.name}",
        )
        # Don't fail the webhook if PropelAuth update fails, just log the error
        # The subscription update in our DB is still valid

    db_session.commit()

    logger.info(
        f"Successfully updated subscription record, stripe_subscription_id={latest_subscription_details.stripe_subscription_id}"