The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
deprecate webhookIds field, rename to send-link-clicked-webhooks, improve efficiency
Steven Teye5e9f8683335
diff --git a/apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.ts b/apps/web/app/(ee)/api/cron/streams/send-link-clicked-webhooks/route.tssimilarity index 90%rename from apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.tsrename to apps/web/app/(ee)/api/cron/streams/send-link-clicked-webhooks/route.tsindex b69f44c3f0..52d317fe60 100644--- a/apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.ts+++ b/apps/web/app/(ee)/api/cron/streams/send-link-clicked-webhooks/route.ts@@ -21,12 +21,12 @@ const BATCH_SIZE = 500;const SEND_CONCURRENCY = 100;// Lock for the cron job-const LOCK_KEY = "lock:send-link-click-webhooks";+const LOCK_KEY = "lock:send-link-clicked-webhooks";const LOCK_TTL_SECONDS = 300;// Drains the workspace:click:events stream and delivers link.clicked webhooks.// Runs every minute (see vercel.json).-// GET /api/cron/streams/send-link-click-webhooks+// GET /api/cron/streams/send-link-clicked-webhooksexport const GET = withCron(async () => {const acquired = await redis.set(LOCK_KEY, "1", {nx: true,@@ -35,7 +35,7 @@ export const GET = withCron(async () => {if (!acquired) {return logAndRespond(- "[send-link-click-webhooks] Another run is in progress. Skipping...",+ "[send-link-clicked-webhooks] Another run is in progress. Skipping...",);}@@ -86,26 +86,7 @@ const processStreamBatch = (): Promise<{...new Set(entries.map((e) => e.data.link_id).filter(Boolean)),];- const [webhooks, projects, links] = await Promise.all([- prisma.webhook.findMany({- where: {- projectId: {- in: workspaceIds,- },- disabledAt: null,- triggers: {- array_contains: [LINK_CLICK_WEBHOOK_TRIGGER],- },- },- select: {- id: true,- url: true,- secret: true,- linkScope: true,- projectId: true,- },- }),-+ const [workspaces, links] = await Promise.all([prisma.project.findMany({where: {id: {@@ -116,6 +97,21 @@ const processStreamBatch = (): Promise<{id: true,usage: true,usageLimit: true,+ webhooks: {+ where: {+ disabledAt: null,+ triggers: {+ array_contains: [LINK_CLICK_WEBHOOK_TRIGGER],+ },+ },+ select: {+ id: true,+ url: true,+ secret: true,+ linkScope: true,+ projectId: true,+ },+ },},}),@@ -141,6 +137,8 @@ const processStreamBatch = (): Promise<{}),]);+ const webhooks = workspaces.flatMap((w) => w.webhooks);+const linkTargetWebhookIds = webhooks.filter((w) => w.linkScope === "links").map((w) => w.id);@@ -197,7 +195,7 @@ const processStreamBatch = (): Promise<{(r) => r.folderId,);const webhooksByWorkspace = groupBy(webhooks, (w) => w.projectId);- const workspaceById = new Map(projects.map((p) => [p.id, p]));+ const workspaceById = new Map(workspaces.map((p) => [p.id, p]));const linkById = new Map(links.map((l) => [l.id, l]));const sendTasks: {diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex fe108ba4b1..467394de13 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -486,12 +486,6 @@ export const createLinkBodySchema = z.object({ref: utmTagInputSchema.describe("The referral tag of the short link. If set, this will populate or override the `ref` query parameter in the destination URL.",),- webhookIds: z- .array(z.string())- .nullish()- .describe(- "An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data.",- ),testVariants: ABTestVariantsSchema.nullish(),testStartedAt: z.string()@@ -517,6 +511,13 @@ export const createLinkBodySchema = z.object({"Deprecated: Use `tagIds` instead. The unique ID of the tag assigned to the short link.",).meta({ deprecated: true }),+ webhookIds: z+ .array(z.string())+ .nullish()+ .describe(+ "Deprecated: You can now enable link.clicked webhooks for all links in a workspace or folder without passing this field manually. An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data.",+ )+ .meta({ deprecated: true }),});export const createLinkBodySchemaAsync = createLinkBodySchema.extend({@@ -702,11 +703,6 @@ export const LinkSchema = z.string().nullable().describe("The unique ID of the folder assigned to the short link."),- webhookIds: z- .array(z.string())- .describe(- "The IDs of the webhooks that the short link is associated with.",- ),comments: z.string().nullable()@@ -798,6 +794,12 @@ export const LinkSchema = z"Deprecated: Use `workspaceId` instead. The project ID of the short link.",).meta({ deprecated: true }),+ webhookIds: z+ .array(z.string())+ .describe(+ "Deprecated: You can now enable link.clicked webhooks for all links in a workspace or folder without passing this field manually. An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data.",+ )+ .meta({ deprecated: true }),}).meta({ title: "Link" });diff --git a/apps/web/scripts/find-link.ts b/apps/web/scripts/find-link.tsindex ef8d4ea117..ceee5dc0a3 100644--- a/apps/web/scripts/find-link.ts+++ b/apps/web/scripts/find-link.ts@@ -2,19 +2,40 @@ import { prisma } from "@/lib/prisma";import "dotenv-flow/config";async function main() {- const links = await prisma.link.findMany({+ // const linkWebhook = await prisma.linkWebhook.groupBy({+ // by: ["webhookId"],+ // _count: {+ // linkId: true,+ // },+ // orderBy: {+ // _count: {+ // linkId: "desc",+ // },+ // },+ // });++ // console.table(linkWebhook);++ const linkFolders = await prisma.link.groupBy({+ by: ["folderId"],where: {- projectId: "xxx",- url: "https://example.com",+ webhooks: {+ some: {+ webhookId: "wh_1KVT5VFNB0NRA4YN3NWV3F6ZN",+ },+ },},- select: {+ _count: {id: true,- shortLink: true,- url: true,+ },+ orderBy: {+ _count: {+ id: "desc",+ },},});- console.table(links);+ console.table(linkFolders);}main();diff --git a/apps/web/vercel.json b/apps/web/vercel.jsonindex fb6e395434..a8a818477e 100644--- a/apps/web/vercel.json+++ b/apps/web/vercel.json@@ -21,7 +21,7 @@"schedule": "* * * * *"},{- "path": "/api/cron/streams/send-link-click-webhooks",+ "path": "/api/cron/streams/send-link-clicked-webhooks","schedule": "* * * * *"},{