summaryrefslogtreecommitdiff
path: root/apps/web/app/api
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/app/api')
-rw-r--r--apps/web/app/api/billing/webhook/route.ts60
-rw-r--r--apps/web/app/api/v1/entries/route.ts3
2 files changed, 39 insertions, 24 deletions
diff --git a/apps/web/app/api/billing/webhook/route.ts b/apps/web/app/api/billing/webhook/route.ts
index 297ef8e..52b1420 100644
--- a/apps/web/app/api/billing/webhook/route.ts
+++ b/apps/web/app/api/billing/webhook/route.ts
@@ -45,6 +45,7 @@ async function updateBillingState(
if (error) {
console.error("failed to update billing state:", error)
+ throw new Error(`failed to update billing state: ${error.message}`)
}
}
@@ -62,7 +63,7 @@ async function handleCheckoutSessionCompleted(
)
const adminClient = createSupabaseAdminClient()
- await adminClient
+ const { error } = await adminClient
.from("user_profiles")
.update({
tier: determineTierFromSubscription(subscription),
@@ -72,6 +73,11 @@ async function handleCheckoutSessionCompleted(
stripe_current_period_end: extractPeriodEnd(subscription),
})
.eq("id", userIdentifier)
+
+ if (error) {
+ console.error("failed to update billing state on checkout:", error)
+ throw new Error(`failed to update billing state on checkout: ${error.message}`)
+ }
}
async function handleSubscriptionUpdated(subscription: Stripe.Subscription) {
@@ -153,28 +159,36 @@ export async function POST(request: Request) {
return NextResponse.json({ error: "invalid signature" }, { status: 400 })
}
- switch (event.type) {
- case "checkout.session.completed":
- await handleCheckoutSessionCompleted(
- event.data.object as Stripe.Checkout.Session
- )
- break
- case "customer.subscription.updated":
- await handleSubscriptionUpdated(
- event.data.object as Stripe.Subscription
- )
- break
- case "customer.subscription.deleted":
- await handleSubscriptionDeleted(
- event.data.object as Stripe.Subscription
- )
- break
- case "invoice.payment_failed":
- await handleInvoicePaymentFailed(event.data.object as Stripe.Invoice)
- break
- case "invoice.paid":
- await handleInvoicePaid(event.data.object as Stripe.Invoice)
- break
+ try {
+ switch (event.type) {
+ case "checkout.session.completed":
+ await handleCheckoutSessionCompleted(
+ event.data.object as Stripe.Checkout.Session
+ )
+ break
+ case "customer.subscription.updated":
+ await handleSubscriptionUpdated(
+ event.data.object as Stripe.Subscription
+ )
+ break
+ case "customer.subscription.deleted":
+ await handleSubscriptionDeleted(
+ event.data.object as Stripe.Subscription
+ )
+ break
+ case "invoice.payment_failed":
+ await handleInvoicePaymentFailed(event.data.object as Stripe.Invoice)
+ break
+ case "invoice.paid":
+ await handleInvoicePaid(event.data.object as Stripe.Invoice)
+ break
+ }
+ } catch (handlerError) {
+ console.error("webhook handler failed:", handlerError)
+ return NextResponse.json(
+ { error: "webhook handler failed" },
+ { status: 500 }
+ )
}
return NextResponse.json({ received: true })
diff --git a/apps/web/app/api/v1/entries/route.ts b/apps/web/app/api/v1/entries/route.ts
index e782e3b..8a2de62 100644
--- a/apps/web/app/api/v1/entries/route.ts
+++ b/apps/web/app/api/v1/entries/route.ts
@@ -18,7 +18,8 @@ export async function GET(request: Request) {
const isSaved = searchParams.get("isSaved")
const cursor = searchParams.get("cursor")
const limitParameter = searchParams.get("limit")
- const limit = Math.min(Math.max(Number(limitParameter) || 50, 1), 100)
+ const parsedLimit = Number(limitParameter)
+ const limit = Number.isFinite(parsedLimit) && parsedLimit > 0 ? Math.min(Math.floor(parsedLimit), 100) : 50
const adminClient = createSupabaseAdminClient()