Refactor API code and simplify billing display

The code in the webhook API has been refactored to move the DatabaseWebhookHandlerService instance out of the POST function scope. Furthermore, the display of renewal plan details on the billing page has been simplified and certain parts deemed superfluous have been removed. Numerous types and interfaces in the database.types.ts file have also been corrected and formatted for consistency and improved readability.
This commit is contained in:
giancarlo
2024-03-31 15:13:44 +08:00
parent 2c0c616a2d
commit aa12ecd5a2
10 changed files with 1133 additions and 1026 deletions

View File

@@ -34,8 +34,7 @@ export function CurrentPlanCard({
subscription: Database['public']['Tables']['subscriptions']['Row'];
config: BillingConfig;
}>) {
const { plan, product } = getProductPlanPair(config, subscription.variant_id);
const baseLineItem = getBaseLineItem(config, plan.id);
const { plan, product } = getProductPlanPair(config, subscription);
return (
<Card>
@@ -61,16 +60,6 @@ export function CurrentPlanCard({
<span>{product.name}</span>
<CurrentPlanBadge status={subscription.status} />
</div>
<div className={'text-muted-foreground'}>
<Trans
i18nKey="billing:planRenewal"
values={{
interval: subscription.interval,
price: formatCurrency(product.currency, baseLineItem.cost),
}}
/>
</div>
</div>
<div>

View File

@@ -53,19 +53,21 @@ export class BillingEventHandlerService {
const ctx = {
namespace: 'billing',
subscriptionId: subscription.id,
subscriptionId: subscription.subscription_id,
provider: subscription.billing_provider,
accountId: subscription.account_id,
customerId: subscription.customer_id,
};
Logger.info(ctx, 'Processing subscription updated event');
// Handle the subscription updated event
// here we update the subscription in the database
const { error } = await client
.from('subscriptions')
.update(subscription)
.match({ id: subscription.id });
const { error } = await client.rpc('upsert_subscription', {
...subscription,
customer_id: subscription.customer_id,
account_id: subscription.account_id,
});
if (error) {
Logger.error(
@@ -88,24 +90,16 @@ export class BillingEventHandlerService {
const ctx = {
namespace: 'billing',
subscriptionId: subscription.id,
subscriptionId: subscription.subscription_id,
provider: subscription.billing_provider,
accountId: subscription.account_id,
};
Logger.info(ctx, 'Processing checkout session completed event...');
const { id: _, ...data } = subscription;
const { error } = await client.rpc('add_subscription', {
...data,
subscription_id: subscription.id,
const { error } = await client.rpc('upsert_subscription', {
...subscription,
customer_id: customerId,
price_amount: subscription.price_amount ?? 0,
period_starts_at: subscription.period_starts_at!,
period_ends_at: subscription.period_ends_at!,
trial_starts_at: subscription.trial_starts_at!,
trial_ends_at: subscription.trial_ends_at!,
});
if (error) {