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 types

Kiran K15cee78f5c86
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index caae0481bf..ea6711afad 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -9,15 +9,13 @@ import {
recordLead,
recordSale,
} from "@/lib/tinybird";
+import { ClickEventTB, LeadEventTB } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
transformLeadEventData,
transformSaleEventData,
} from "@/lib/webhook/transform";
-import z from "@/lib/zod";
-import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
-import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
import { prisma } from "@dub/prisma";
import { Customer } from "@dub/prisma/client";
import { nanoid } from "@dub/utils";
@@ -41,8 +39,8 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
let customer: Customer | null = null;
let existingCustomer: Customer | null = null;
- let clickEvent: z.infer<typeof clickEventSchemaTB> | null = null;
- let leadEvent: z.infer<typeof leadEventSchemaTB>;
+ let clickEvent: ClickEventTB | null = null;
+ let leadEvent: LeadEventTB;
let linkId: string;
/*
diff --git a/apps/web/app/(ee)/api/track/lead/route.ts b/apps/web/app/(ee)/api/track/lead/route.ts
index 016e90283e..bf2a9882f7 100644
--- a/apps/web/app/(ee)/api/track/lead/route.ts
+++ b/apps/web/app/(ee)/api/track/lead/route.ts
@@ -8,10 +8,10 @@ import { createPartnerCommission } from "@/lib/partners/create-partner-commissio
import { isStored, storage } from "@/lib/storage";
import { getClickEvent, recordLead, recordLeadSync } from "@/lib/tinybird";
import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
+import { ClickEventTB } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformLeadEventData } from "@/lib/webhook/transform";
-import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
import {
trackLeadRequestSchema,
trackLeadResponseSchema,
@@ -23,8 +23,6 @@ import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import { z } from "zod";
-type ClickData = z.infer<typeof clickEventSchemaTB>;
-
// POST /api/track/lead – Track a lead conversion event
export const POST = withWorkspace(
async ({ req, workspace }) => {
@@ -88,7 +86,7 @@ export const POST = withWorkspace(
if (ok) {
// Find click event
- let clickData: ClickData | null = null;
+ let clickData: ClickEventTB | null = null;
const clickEvent = await getClickEvent({ clickId });
if (clickEvent && clickEvent.data && clickEvent.data.length > 0) {
@@ -96,7 +94,7 @@ export const POST = withWorkspace(
}
if (!clickData) {
- const cachedClickData = await redis.get<ClickData>(
+ const cachedClickData = await redis.get<ClickEventTB>(
`clickIdCache:${clickId}`,
);
diff --git a/apps/web/app/(ee)/api/track/sale/route.ts b/apps/web/app/(ee)/api/track/sale/route.ts
index f261160679..02602deb05 100644
--- a/apps/web/app/(ee)/api/track/sale/route.ts
+++ b/apps/web/app/(ee)/api/track/sale/route.ts
@@ -7,11 +7,11 @@ import { withWorkspace } from "@/lib/auth";
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
+import { LeadEventTB } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformSaleEventData } from "@/lib/webhook/transform";
import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
-import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
import {
trackSaleRequestSchema,
trackSaleResponseSchema,
@@ -22,8 +22,6 @@ import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import { z } from "zod";
-type LeadEvent = z.infer<typeof leadEventSchemaTB>;
-
// POST /api/track/sale – Track a sale conversion event
export const POST = withWorkspace(
async ({ req, workspace }) => {
@@ -107,14 +105,14 @@ export const POST = withWorkspace(
eventName: leadEventName,
});
- let leadEventData: LeadEvent | null = null;
+ let leadEventData: LeadEventTB | null = null;
if (!leadEvent || leadEvent.data.length === 0) {
// Check cache to see if the lead event exists
// if leadEventName is provided, we only check for that specific event
// otherwise, we check for all cached lead events for that customer
- const cachedLeadEvent = await redis.get<LeadEvent>(
+ const cachedLeadEvent = await redis.get<LeadEventTB>(
`leadCache:${customer.id}${leadEventName ? `:${leadEventName.toLowerCase().replace(" ", "-")}` : ""}`,
);
diff --git a/apps/web/lib/actions/partners/create-commission.ts b/apps/web/lib/actions/partners/create-commission.ts
index 7df2cec514..0b012baa46 100644
--- a/apps/web/lib/actions/partners/create-commission.ts
+++ b/apps/web/lib/actions/partners/create-commission.ts
@@ -7,7 +7,7 @@ import { getLeadEvent } from "@/lib/tinybird";
import { recordClick } from "@/lib/tinybird/record-click";
import { recordLeadWithTimestamp } from "@/lib/tinybird/record-lead";
import { recordSaleWithTimestamp } from "@/lib/tinybird/record-sale";
-import z from "@/lib/zod";
+import { ClickEventTB, LeadEventTB } from "@/lib/types";
import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
import { createCommissionSchema } from "@/lib/zod/schemas/commissions";
import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
@@ -104,8 +104,8 @@ export const createCommissionAction = authActionClient
}
}
- let clickEvent: z.infer<typeof clickEventSchemaTB> | null = null;
- let leadEvent: z.infer<typeof leadEventSchemaTB> | null = null;
+ let clickEvent: ClickEventTB | null = null;
+ let leadEvent: LeadEventTB | null = null;
let shouldUpdateCustomer = false;
const existingLeadEvent = await getLeadEvent({
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index 674dfaf175..27a8fc7b49 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -2,11 +2,10 @@ import { includeTags } from "@/lib/api/links/include-tags";
import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import { recordSale } from "@/lib/tinybird";
+import { LeadEventTB } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformSaleEventData } from "@/lib/webhook/transform";
-import z from "@/lib/zod";
-import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
@@ -21,7 +20,7 @@ export async function createShopifySale({
event: any;
customerId: string;
workspaceId: string;
- leadData: z.infer<typeof leadEventSchemaTB>;
+ leadData: LeadEventTB;
}) {
const order = orderSchema.parse(event);
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 3379bc91bf..8321dac549 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -22,7 +22,10 @@ import {
FOLDER_WORKSPACE_ACCESS,
} from "./folder/constants";
import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";
-import { clickEventResponseSchema } from "./zod/schemas/clicks";
+import {
+ clickEventResponseSchema,
+ clickEventSchemaTB,
+} from "./zod/schemas/clicks";
import { CommissionResponseSchema } from "./zod/schemas/commissions";
import { customerActivityResponseSchema } from "./zod/schemas/customer-activity";
import {
@@ -36,6 +39,7 @@ import { integrationSchema } from "./zod/schemas/integration";
import { InvoiceSchema } from "./zod/schemas/invoices";
import {
leadEventResponseSchema,
+ leadEventSchemaTB,
trackLeadResponseSchema,
} from "./zod/schemas/leads";
import {
@@ -491,3 +495,7 @@ export interface FolderLinkCount {
folderId: string;
_count: number;
}
+
+export type ClickEventTB = z.infer<typeof clickEventSchemaTB>;
+
+export type LeadEventTB = z.infer<typeof leadEventSchemaTB>;
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index b602902c8e..716d61d320 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -6,9 +6,8 @@ import { Webhook } from "@dub/prisma/client";
import { OG_AVATAR_URL, nanoid, toCamelCase } from "@dub/utils";
import { ExpandedLink, transformLink } from "../api/links/utils/transform-link";
import { generateRandomName } from "../names";
-import { WebhookTrigger } from "../types";
-import z from "../zod";
-import { clickEventSchema, clickEventSchemaTB } from "../zod/schemas/clicks";
+import { ClickEventTB, WebhookTrigger } from "../types";
+import { clickEventSchema } from "../zod/schemas/clicks";
import { WebhookSchema } from "../zod/schemas/webhooks";
import { WEBHOOK_EVENT_ID_PREFIX } from "./constants";
import { leadWebhookEventSchema, saleWebhookEventSchema } from "./schemas";
@@ -30,7 +29,7 @@ export const transformWebhook = (webhook: TransformWebhookProps) => {
};
export const transformClickEventData = (
- data: z.infer<typeof clickEventSchemaTB> & {
+ data: ClickEventTB & {
link: any;
},
) => {