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.

fix ts error

Steven Teyc9aad4a701bf
diff --git a/apps/web/app/api/campaigns/webhooks/stripe/route.ts b/apps/web/app/api/campaigns/webhooks/stripe/route.ts
index 6bbb4dbad5..5714189c00 100644
--- a/apps/web/app/api/campaigns/webhooks/stripe/route.ts
+++ b/apps/web/app/api/campaigns/webhooks/stripe/route.ts
@@ -1,5 +1,5 @@
import { stripe } from "@/lib/stripe";
-import { getClickEvent, recordConversion } from "@/lib/tinybird";
+import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { nanoid } from "@dub/utils";
import type Stripe from "stripe";
@@ -72,18 +72,25 @@ async function handleChargeSucceeded(event: Stripe.Event) {
// Check customerId exists in TB
// TODO: Update clickId
- const clickEvent = await getClickEvent({ clickId: "jlNZlZKVa2X6ZCcZ" });
+ const leadEvent = await getLeadEvent({ customerId });
- if (!clickEvent || clickEvent.data.length === 0) {
+ if (!leadEvent || leadEvent.data.length === 0) {
return;
}
- await recordConversion({
- ...clickEvent.data[0],
+ await recordSale({
+ ...leadEvent.data[0],
event_id: nanoid(16),
- event_name: "Subscribed to plan",
- event_type: "sale",
- customer_id: customerId,
- metadata: "",
+ payment_processor: "stripe",
+ product_id: "",
+ amount: charge.amount,
+ currency: charge.currency,
+ recurring: true, // TODO: Update this
+ recurring_interval: "month", // TODO: Update this
+ recurring_interval_count: 1, // TODO: Update this
+ refunded: false,
+ metadata: {
+ charge,
+ },
});
}
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 2651a6125b..62b9c3f4dd 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -2,6 +2,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withSessionEdge } from "@/lib/auth/session-edge";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { trackSaleEventSchema } from "@/lib/zod/schemas";
+import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -32,6 +33,7 @@ export const POST = withSessionEdge(async ({ req }) => {
await recordSale({
...leadEvent.data[0],
+ event_id: nanoid(16),
payment_processor: paymentProcessor,
product_id: productId,
amount,
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index dd0c067896..0476174112 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -3,7 +3,7 @@ import { clickEventSchemaTB } from "./clicks";
export const trackSaleEventSchema = z.object({
customerId: z.string(),
- paymentProcessor: z.enum(["stripe", "shopify", "paddle"]).default("stripe"),
+ paymentProcessor: z.enum(["stripe", "shopify", "paddle"]),
productId: z.string(),
amount: z.number().int().positive().default(0),
currency: z.string().default("usd"),
@@ -11,17 +11,14 @@ export const trackSaleEventSchema = z.object({
recurringInterval: z.enum(["month", "year"]).default("month"),
recurringIntervalCount: z.number().int().positive().default(1),
refunded: z.boolean().default(false),
- metadata: z
- .record(z.unknown())
- .nullish()
- .transform((val) => (val ? JSON.stringify(val) : "")),
+ metadata: z.record(z.unknown()).nullish(),
});
export const saleEventSchemaTB = clickEventSchemaTB.omit({ url: true }).and(
z.object({
event_id: z.string(),
customer_id: z.string(),
- payment_processor: z.string().default("stripe"),
+ payment_processor: z.enum(["stripe", "shopify", "paddle"]),
product_id: z.string(),
amount: z.number().int().positive().default(0),
currency: z.string().default("usd"),
@@ -29,6 +26,9 @@ export const saleEventSchemaTB = clickEventSchemaTB.omit({ url: true }).and(
recurring_interval: z.enum(["month", "year"]).default("month"),
recurring_interval_count: z.number().int().positive().default(1),
refunded: z.boolean().default(false),
- metadata: z.string(), // TODO: Fix the type
+ metadata: z
+ .record(z.unknown())
+ .nullish()
+ .transform((val) => (val ? JSON.stringify(val) : "")),
}),
);