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.

Persist customer sales data to MySQL

Steven Teyc056ca98f6fd
diff --git a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
index 0dc20baeb0..ffa0b74879 100644
--- a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -295,6 +295,21 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
},
}),
+
+ // update customer sales count
+ prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: charge.amount_total!,
+ },
+ },
+ }),
]);
// for program links
diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
index f5edb85f20..7847a9e3dc 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -157,6 +157,21 @@ export async function invoicePaid(event: Stripe.Event) {
},
},
}),
+
+ // update customer sales count
+ prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: invoice.amount_paid,
+ },
+ },
+ }),
]);
// for program links
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 63d5ecb627..2c9e19fc3a 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -158,7 +158,7 @@ export const POST = withWorkspace(
waitUntil(
(async () => {
- const [_sale, link, _project] = await Promise.all([
+ const [_sale, link] = await Promise.all([
recordSale(saleData),
// update link sales count
@@ -190,6 +190,20 @@ export const POST = withWorkspace(
},
},
}),
+ // update customer sales count
+ prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: amount,
+ },
+ },
+ }),
logConversionEvent({
workspace_id: workspace.id,
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index e964e0f077..e613d3a743 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -97,10 +97,18 @@ export async function createShopifySale({
},
}),
- prisma.customer.findUniqueOrThrow({
+ prisma.customer.update({
where: {
id: customerId,
},
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: amount,
+ },
+ },
}),
redis.del(`shopify:checkout:${checkoutToken}`),
diff --git a/packages/prisma/schema/customer.prisma b/packages/prisma/schema/customer.prisma
index 557abdd72f..42d9b01d76 100644
--- a/packages/prisma/schema/customer.prisma
+++ b/packages/prisma/schema/customer.prisma
@@ -11,6 +11,9 @@ model Customer {
clickedAt DateTime?
country String?
+ sales Int @default(0)
+ saleAmount Int @default(0)
+
projectId String
projectConnectId String?
createdAt DateTime @default(now())