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.

Promote link-assigned webhooks to link.clicked in sync cron

Kiran K72e11d341173
diff --git a/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts b/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts
index 051a75ce86..32e6e0e807 100644
--- a/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts
+++ b/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts
@@ -1,5 +1,8 @@
import { withCron } from "@/lib/cron/with-cron";
-import { syncClickWebhookWorkspaceSet } from "@/lib/webhook/click-webhook-workspaces";
+import {
+ promoteLinkWebhooksForClick,
+ syncClickWebhookWorkspaceSet,
+} from "@/lib/webhook/click-webhook-workspaces";
import { logAndRespond } from "../../utils";
export const dynamic = "force-dynamic";
@@ -8,11 +11,12 @@ export const dynamic = "force-dynamic";
// Rebuild the Redis set of workspaces with active link.clicked webhooks.
// Runs every minute (* * * * *)
export const GET = withCron(async () => {
- const count = await syncClickWebhookWorkspaceSet();
+ const [synced, promoted] = await Promise.all([
+ syncClickWebhookWorkspaceSet(),
+ promoteLinkWebhooksForClick(),
+ ]);
return logAndRespond(
- count === 0
- ? "No workspaces with link.clicked webhooks found. Cleared cache."
- : `Synced ${count} workspace(s) with link.clicked webhooks.`,
+ `Synced ${synced} workspace(s) with link.clicked webhooks. Promoted ${promoted} webhooks for click.`,
);
});
diff --git a/apps/web/lib/webhook/click-webhook-workspaces.ts b/apps/web/lib/webhook/click-webhook-workspaces.ts
index c471ef8dde..4949649bc9 100644
--- a/apps/web/lib/webhook/click-webhook-workspaces.ts
+++ b/apps/web/lib/webhook/click-webhook-workspaces.ts
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
import { LINK_CLICK_WEBHOOK_TRIGGER } from "./constants";
import type { WebhookTrigger } from "./types";
+import { hasLinkClickTrigger } from "./utils";
export const REDIS_KEY = "webhookClickWorkspaces";
const TMP_REDIS_KEY = `${REDIS_KEY}:tmp`;
@@ -94,3 +95,56 @@ export const syncWorkspaceWebhookStatus = async (workspaceId: string) => {
await clickWebhookWorkspaces.remove(workspaceId);
}
};
+
+// Promotes webhooks assigned via the links API to the link.clicked trigger so
+// they are picked up by the workspace click event stream delivery path.
+export const promoteLinkWebhooksForClick = async () => {
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ disabledAt: null,
+ links: {
+ some: {},
+ },
+ NOT: {
+ triggers: {
+ array_contains: [LINK_CLICK_WEBHOOK_TRIGGER],
+ },
+ },
+ },
+ select: {
+ id: true,
+ triggers: true,
+ linkTarget: true,
+ },
+ take: 100,
+ });
+
+ const webhooksToPromote = webhooks.filter(
+ (webhook) => !hasLinkClickTrigger(webhook),
+ );
+
+ if (webhooksToPromote.length === 0) {
+ return 0;
+ }
+
+ console.log(webhooksToPromote);
+
+ await Promise.all(
+ webhooksToPromote.map((webhook) =>
+ prisma.webhook.update({
+ where: {
+ id: webhook.id,
+ },
+ data: {
+ triggers: [
+ ...(webhook.triggers as WebhookTrigger[]),
+ LINK_CLICK_WEBHOOK_TRIGGER,
+ ],
+ linkTarget: webhook.linkTarget ?? "links",
+ },
+ }),
+ ),
+ );
+
+ return webhooksToPromote.length;
+};