billing hotfix (#1670)

# READ CAREFULLY THEN REMOVE

Remove bullet points that are not relevant.

PLEASE REFRAIN FROM USING AI TO WRITE YOUR CODE AND PR DESCRIPTION. IF YOU DO USE AI TO WRITE YOUR CODE PLEASE PROVIDE A DESCRIPTION AND REVIEW IT CAREFULLY. MAKE SURE YOU UNDERSTAND THE CODE YOU ARE SUBMITTING USING AI.

- Pull requests that do not follow these guidelines will be closed without review or comment.
- If you use AI to write your PR description your pr will be close without review or comment.
- If you are unsure about anything, feel free to ask for clarification.

## Description

Please provide a clear description of your changes.

---

## Type of Change

Please delete options that are not relevant.

- [ ] 🐛 Bug fix (non-breaking change which fixes an issue)
- [ ]  New feature (non-breaking change which adds functionality)
- [ ] 💥 Breaking change (fix or feature with breaking changes)
- [ ] 📝 Documentation update
- [ ] 🎨 UI/UX improvement
- [ ] 🔒 Security enhancement
- [ ]  Performance improvement

## Areas Affected

Please check all that apply:

- [ ] Email Integration (Gmail, IMAP, etc.)
- [ ] User Interface/Experience
- [ ] Authentication/Authorization
- [ ] Data Storage/Management
- [ ] API Endpoints
- [ ] Documentation
- [ ] Testing Infrastructure
- [ ] Development Workflow
- [ ] Deployment/Infrastructure

## Testing Done

Describe the tests you've done:

- [ ] Unit tests added/updated
- [ ] Integration tests added/updated
- [ ] Manual testing performed
- [ ] Cross-browser testing (if UI changes)
- [ ] Mobile responsiveness verified (if UI changes)

## Security Considerations

For changes involving data or authentication:

- [ ] No sensitive data is exposed
- [ ] Authentication checks are in place
- [ ] Input validation is implemented
- [ ] Rate limiting is considered (if applicable)

## Checklist

- [ ] I have read the [CONTRIBUTING](https://github.com/Mail-0/Zero/blob/staging/.github/CONTRIBUTING.md) document
- [ ] My code follows the project's style guidelines
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in complex areas
- [ ] I have updated the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix/feature works
- [ ] All tests pass locally
- [ ] Any dependent changes are merged and published

## Additional Notes

Add any other context about the pull request here.

## Screenshots/Recordings

Add screenshots or recordings here if applicable.

---

_By submitting this pull request, I confirm that my contribution is made under the terms of the project's license._


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **Bug Fixes**
  * Improved error handling in billing to automatically sign out users if a billing error occurs.

* **Chores**
  * Updated internal cache key version for improved data management.
  * Enhanced user deletion process to ensure user data is fully removed from all relevant systems.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Adam
2025-07-07 17:07:59 -07:00
committed by GitHub
parent ddb4fed6a3
commit d0113d0716
3 changed files with 25 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
import { useAutumn, useCustomer } from 'autumn-js/react';
import { useMemo } from 'react';
import { signOut } from '@/lib/auth-client';
import { useEffect, useMemo } from 'react';
type FeatureState = {
total: number;
@@ -60,18 +61,22 @@ const FEATURE_IDS = {
const PRO_PLANS = ['pro-example', 'pro_annual', 'team', 'enterprise'] as const;
export const useBilling = () => {
const { customer, refetch, isLoading } = useCustomer();
const { customer, refetch, isLoading, error } = useCustomer();
const { attach, track, openBillingPortal } = useAutumn();
const isPro = useMemo(() => {
if (!customer?.products || !Array.isArray(customer.products)) return false;
return customer.products.some((product) =>
PRO_PLANS.some((plan) => product.id?.includes(plan) || product.name?.includes(plan)),
);
}, [customer]);
useEffect(() => {
if (error) signOut();
}, [error]);
const customerFeatures = useMemo(() => {
if (!customer?.features) return DEFAULT_FEATURES;
const { isPro, ...customerFeatures } = useMemo(() => {
const isPro =
customer?.products && Array.isArray(customer.products)
? customer.products.some((product) =>
PRO_PLANS.some((plan) => product.id?.includes(plan) || product.name?.includes(plan)),
)
: false;
if (!customer?.features) return { isPro, ...DEFAULT_FEATURES };
const features = { ...DEFAULT_FEATURES };
@@ -117,7 +122,7 @@ export const useBilling = () => {
};
}
return features;
return { isPro, ...features };
}, [customer]);
return {

View File

@@ -10,7 +10,7 @@ export const SIDEBAR_WIDTH_ICON = '3rem';
export const SIDEBAR_KEYBOARD_SHORTCUT = 'b';
export const BASE_URL = import.meta.env.VITE_PUBLIC_APP_URL;
export const MAX_URL_LENGTH = 2000;
export const CACHE_BURST_KEY = 'cache-burst:v0.0.3';
export const CACHE_BURST_KEY = 'cache-burst:v0.0.4';
export const emailProviders = [
{

View File

@@ -16,9 +16,9 @@ import { getContext } from 'hono/context-storage';
import { defaultUserSettings } from './schemas';
import { getMigrations } from 'better-auth/db';
import { disableBrainFunction } from './brain';
import { type EProviders } from '../types';
import { APIError } from 'better-auth/api';
import { getZeroDB } from './server-utils';
import type { EProviders } from '../types';
import type { HonoContext } from '../ctx';
import { env } from 'cloudflare:workers';
import { createDriver } from './driver';
@@ -117,6 +117,13 @@ export const createAuth = () => {
if (!request) throw new APIError('BAD_REQUEST', { message: 'Request object is missing' });
const db = getZeroDB(user.id);
const connections = await db.findManyConnections();
const context = getContext<HonoContext>();
try {
await context.var.autumn.customers.delete(user.id);
} catch (error) {
console.error('Failed to delete Autumn customer:', error);
// Continue with deletion process despite Autumn failure
}
const revokedAccounts = (
await Promise.allSettled(