Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update workspace sales usage

Steven Tey9b8ecbc07bb2
diff --git a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
index 5e817c8f3c..3c262fbab3 100644
--- a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/prisma";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import { nanoid } from "@dub/utils";
+import { Customer } from "@prisma/client";
import type Stripe from "stripe";
// Handle event "checkout.session.completed"
@@ -16,7 +17,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
return "Customer ID not found in Stripe checkout session metadata, skipping...";
}
- let customer;
+ let customer: Customer;
try {
// Update customer with stripe customerId if exists
customer = await prisma.customer.update({
@@ -80,6 +81,23 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
sales: {
increment: 1,
},
+ saleAmount: {
+ increment: charge.amount_total!,
+ },
+ },
+ }),
+ // update workspace sales usage
+ prisma.project.update({
+ where: {
+ id: customer.projectId,
+ },
+ data: {
+ usage: {
+ increment: 1,
+ },
+ salesUsage: {
+ increment: charge.amount_total!,
+ },
},
}),
]);
diff --git a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
index 6e3cf0afa7..938cf5a740 100644
--- a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
@@ -65,6 +65,23 @@ export async function invoicePaid(event: Stripe.Event) {
sales: {
increment: 1,
},
+ saleAmount: {
+ increment: invoice.amount_paid,
+ },
+ },
+ }),
+ // update workspace sales usage
+ prisma.project.update({
+ where: {
+ id: customer.projectId,
+ },
+ data: {
+ usage: {
+ increment: 1,
+ },
+ salesUsage: {
+ increment: invoice.amount_paid,
+ },
},
}),
]);
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 06cb8ee6df..b434eb31c5 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -83,6 +83,7 @@ export const POST = withWorkspaceEdge(
},
},
}),
+ // update workspace sales usage
prismaEdge.project.update({
where: {
id: workspace.id,