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.

simplify

Steven Tey2d7219024ae5
diff --git a/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts b/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts
index 58bb7abe0c..143ab44de1 100644
--- a/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts
+++ b/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts
@@ -9,7 +9,7 @@ import {
} from "@/lib/api/customers/reattribute-customer";
import { logger } from "@/lib/axiom/server";
import { prisma } from "@/lib/prisma";
-import { deleteTinybirdCustomerEvents } from "@/lib/tinybird/delete-events";
+import { deleteTinybirdCustomerEvents } from "@/lib/tinybird/delete-customer-events";
import { reattributeCustomerWorkflowSchema } from "@/lib/zod/schemas/customers";
import { WorkflowRetryAfterError } from "@upstash/workflow";
import { serve } from "@upstash/workflow/nextjs";
@@ -149,7 +149,6 @@ export const { POST } = serve<Input>(
try {
await deleteTinybirdCustomerEvents({
customerId: input.oldCustomerId,
- clickId: plan.hasClick ? input.oldClickId : null,
});
return logAndReturn({ deleted: true });
diff --git a/apps/web/lib/tinybird/delete-customer-events.ts b/apps/web/lib/tinybird/delete-customer-events.ts
new file mode 100644
index 0000000000..1a53727ae0
--- /dev/null
+++ b/apps/web/lib/tinybird/delete-customer-events.ts
@@ -0,0 +1,66 @@
+async function deleteDatasourceRows({
+ dataSource,
+ deleteCondition,
+}: {
+ dataSource: string;
+ deleteCondition: string;
+}) {
+ const baseUrl = process.env.TINYBIRD_API_URL;
+ const token = process.env.TINYBIRD_API_KEY;
+
+ if (!baseUrl || !token) {
+ throw new Error("Tinybird is not configured.");
+ }
+
+ const response = await fetch(
+ `${baseUrl}/v0/datasources/${dataSource}/delete`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${token}`,
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ body: `delete_condition=${deleteCondition}`,
+ signal: AbortSignal.timeout(30_000),
+ },
+ );
+
+ const payload = await response.json().catch(() => null);
+
+ if (!response.ok) {
+ throw new Error(
+ `Tinybird delete failed for ${dataSource}: ${JSON.stringify(payload)}`,
+ );
+ }
+
+ console.log(`Deleted rows matching ${deleteCondition} from ${dataSource}`);
+
+ return payload;
+}
+
+export async function deleteTinybirdCustomerEvents({
+ customerId,
+}: {
+ customerId: string;
+}) {
+ if (!customerId.startsWith("cus_")) {
+ throw new Error(`Invalid customer ID: ${customerId}`);
+ }
+
+ const customerCondition = `customer_id='${customerId}'`;
+
+ const dataSources = [
+ "dub_lead_events",
+ "dub_lead_events_mv",
+ "dub_sale_events",
+ "dub_sale_events_mv",
+ ];
+
+ // Delete sequentially to avoid Tinybird rate limits
+ for (const dataSource of dataSources) {
+ await deleteDatasourceRows({
+ dataSource,
+ deleteCondition: customerCondition,
+ });
+ }
+}
diff --git a/apps/web/lib/tinybird/delete-events.ts b/apps/web/lib/tinybird/delete-events.ts
deleted file mode 100644
index af71e846ea..0000000000
--- a/apps/web/lib/tinybird/delete-events.ts
+++ /dev/null
@@ -1,107 +0,0 @@
-const TINYBIRD_ID_PATTERN = /^[a-zA-Z0-9_]+$/;
-
-function assertSafeTinybirdId(value: string, label: string) {
- if (!TINYBIRD_ID_PATTERN.test(value)) {
- throw new Error(`Invalid ${label} for Tinybird delete.`);
- }
-}
-
-async function deleteDatasourceRows({
- dataSource,
- deleteCondition,
-}: {
- dataSource: string;
- deleteCondition: string;
-}) {
- const baseUrl = process.env.TINYBIRD_API_URL;
- const token = process.env.TINYBIRD_API_KEY;
-
- if (!baseUrl || !token) {
- throw new Error("Tinybird is not configured.");
- }
-
- const response = await fetch(
- `${baseUrl}/v0/datasources/${dataSource}/delete`,
- {
- method: "POST",
- headers: {
- Authorization: `Bearer ${token}`,
- "Content-Type": "application/x-www-form-urlencoded",
- },
- body: `delete_condition=${deleteCondition}`,
- signal: AbortSignal.timeout(30_000),
- },
- );
-
- const payload = await response.json().catch(() => null);
-
- if (!response.ok) {
- throw new Error(
- `Tinybird delete failed for ${dataSource}: ${JSON.stringify(payload)}`,
- );
- }
-
- return payload;
-}
-
-export async function deleteTinybirdCustomerEvents({
- customerId,
- clickId,
-}: {
- customerId: string;
- clickId?: string | null;
-}) {
- assertSafeTinybirdId(customerId, "customerId");
-
- const customerCondition = `customer_id='${customerId}'`;
-
- const deletions: Promise<unknown>[] = [
- deleteDatasourceRows({
- dataSource: "dub_lead_events",
- deleteCondition: customerCondition,
- }),
- deleteDatasourceRows({
- dataSource: "dub_lead_events_mv",
- deleteCondition: customerCondition,
- }),
- deleteDatasourceRows({
- dataSource: "dub_sale_events",
- deleteCondition: customerCondition,
- }),
- deleteDatasourceRows({
- dataSource: "dub_sale_events_mv",
- deleteCondition: customerCondition,
- }),
- ];
-
- if (clickId) {
- assertSafeTinybirdId(clickId, "clickId");
- const clickCondition = `click_id='${clickId}'`;
-
- deletions.push(
- deleteDatasourceRows({
- dataSource: "dub_click_events",
- deleteCondition: clickCondition,
- }),
- deleteDatasourceRows({
- dataSource: "dub_click_events_mv",
- deleteCondition: clickCondition,
- }),
- deleteDatasourceRows({
- dataSource: "dub_click_events_id",
- deleteCondition: clickCondition,
- }),
- );
- }
-
- const results = await Promise.allSettled(deletions);
- const failed = results.filter((result) => result.status === "rejected");
-
- if (failed.length > 0) {
- throw new Error(
- `Failed to delete ${failed.length} Tinybird datasource(s) for customer ${customerId}.`,
- );
- }
-
- return results;
-}
diff --git a/apps/web/lib/tinybird/index.ts b/apps/web/lib/tinybird/index.ts
index a13734aa29..a295801a9c 100644
--- a/apps/web/lib/tinybird/index.ts
+++ b/apps/web/lib/tinybird/index.ts
@@ -1,5 +1,5 @@
export * from "./client";
-export * from "./delete-events";
+export * from "./delete-customer-events";
export * from "./get-click-event";
export * from "./get-lead-event";
export * from "./record-click";