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.

send click event

Kiran K9c7766773297
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 6a3f85e717..27a508114d 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -1,7 +1,7 @@
import z from "@/lib/zod";
import { metaTagsSchema } from "@/lib/zod/schemas/metatags";
import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
-import { Link, Project } from "@prisma/client";
+import { Link, Project, Webhook } from "@prisma/client";
import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";
import { integrationSchema } from "./zod/schemas/integration";
import { createLinkBodySchema } from "./zod/schemas/links";
@@ -264,3 +264,8 @@ export type WebhookProps = z.infer<typeof webhookSchema>;
export type NewWebhook = z.infer<typeof createWebhookSchema>;
export type WebhookEventProps = z.infer<typeof webhookEventSchemaTB>;
+
+export type WebhookCacheProps = Pick<
+ Webhook,
+ "id" | "url" | "secret" | "triggers"
+>;
diff --git a/apps/web/lib/webhook/cache.ts b/apps/web/lib/webhook/cache.ts
index 6621b69ee9..410e789997 100644
--- a/apps/web/lib/webhook/cache.ts
+++ b/apps/web/lib/webhook/cache.ts
@@ -1,9 +1,7 @@
import { redis } from "@/lib/upstash";
-import type { Webhook } from "@prisma/client";
+import { WebhookCacheProps } from "../types";
import { WEBHOOK_REDIS_KEY } from "./constants";
-type WebhookCacheProps = Pick<Webhook, "id" | "url" | "secret" | "triggers">;
-
class WebhookCache {
async set(webhook: WebhookCacheProps) {
return await redis.hset(WEBHOOK_REDIS_KEY, {
@@ -15,12 +13,26 @@ class WebhookCache {
return await redis.hget<WebhookCacheProps>(WEBHOOK_REDIS_KEY, webhookId);
}
+ async mget(webhookIds: string[]) {
+ const webhooks = await redis.hmget<Record<string, WebhookCacheProps>>(
+ WEBHOOK_REDIS_KEY,
+ ...webhookIds,
+ );
+
+ if (!webhooks) {
+ return [];
+ }
+
+ return Object.values(webhooks);
+ }
+
async delete(webhookId: string) {
return await redis.hdel(WEBHOOK_REDIS_KEY, webhookId);
}
format(webhook: WebhookCacheProps) {
return {
+ id: webhook.id,
url: webhook.url,
secret: webhook.secret,
triggers: webhook.triggers,
diff --git a/apps/web/lib/webhook/publish-edge.ts b/apps/web/lib/webhook/publish-edge.ts
index 8bd6b8cf8d..e417e6c1e3 100644
--- a/apps/web/lib/webhook/publish-edge.ts
+++ b/apps/web/lib/webhook/publish-edge.ts
@@ -1,46 +1,22 @@
-import { prismaEdge } from "@/lib/prisma/edge";
-import { WebhookTrigger, WorkspaceProps } from "../types";
+import { WebhookCacheProps, WebhookTrigger } from "../types";
import { prepareWebhookPayload } from "./prepare-payload";
import { sendWebhookEventToQStash } from "./qstash";
interface SendWebhookProps {
- workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
+ webhooks: WebhookCacheProps[];
data: any;
- linkId?: string;
}
export const sendWebhook = async (
trigger: WebhookTrigger,
- props: SendWebhookProps,
+ { webhooks, data }: SendWebhookProps,
) => {
- const { workspace, linkId, data } = props;
-
- if (!workspace.webhookEnabled) {
- return;
- }
-
- const webhooks = await prismaEdge.webhook.findMany({
- where: {
- projectId: workspace.id,
- triggers: {
- array_contains: [trigger],
- },
- ...(linkId && {
- links: {
- some: {
- linkId,
- },
- },
- }),
- },
- select: {
- id: true,
- url: true,
- secret: true,
- },
+ // Filter webhooks that match the trigger
+ const matchedWebhooks = webhooks.filter((webhook) => {
+ return webhook.triggers && (webhook.triggers as string[]).includes(trigger);
});
- if (webhooks.length === 0) {
+ if (matchedWebhooks.length === 0) {
return;
}
@@ -48,11 +24,11 @@ export const sendWebhook = async (
const payload = prepareWebhookPayload(trigger, data);
await Promise.all(
- webhooks.map((webhook) =>
+ matchedWebhooks.map((webhook) =>
sendWebhookEventToQStash({
webhook,
payload,
}),
),
);
-};
+};
\ No newline at end of file