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.

Merge branch 'main' into webhooks-ga

Steven Teyd1cb1582763b
diff --cc apps/web/app/api/webhooks/[webhookId]/route.ts
index 1895aab24a,90e16ac055..1595969352
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@@ -306,20 -275,8 +290,11 @@@ export const DELETE = withWorkspace
},
});
- const formatedLinks = links.map((link) => {
- return {
- ...link,
- webhookIds: link.webhooks.map((webhook) => webhook.webhookId),
- };
- });
-
await Promise.all([
+ toggleWebhooksForWorkspace({
+ workspaceId: workspace.id,
+ }),
-
- linkCache.mset(formatedLinks),
-
+ linkCache.mset(links),
webhookCache.delete(webhookId),
]);
})(),
diff --cc apps/web/app/api/webhooks/route.ts
index 82bc39db4f,7417abbfd3..6d74283fa6
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@@ -1,14 -1,13 +1,19 @@@
import { DubApiError } from "@/lib/api/errors";
+ import { linkCache } from "@/lib/api/links/cache";
-import { createId, parseRequestBody } from "@/lib/api/utils";
+import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+ import { webhookCache } from "@/lib/webhook/cache";
-import { WEBHOOK_ID_PREFIX } from "@/lib/webhook/constants";
+import { createWebhook } from "@/lib/webhook/create-webhook";
import { transformWebhook } from "@/lib/webhook/transform";
-import { isLinkLevelWebhook } from "@/lib/webhook/utils";
+import { toggleWebhooksForWorkspace } from "@/lib/webhook/update-webhook";
- import { identifyWebhookReceiver } from "@/lib/webhook/utils";
++import {
++ identifyWebhookReceiver,
++ isLinkLevelWebhook,
++} from "@/lib/webhook/utils";
import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";
import { prisma } from "@dub/prisma";
+import { WebhookReceiver } from "@dub/prisma/client";
+import { ZAPIER_INTEGRATION_ID } from "@dub/utils/src/constants";
import { waitUntil } from "@vercel/functions";
import { sendEmail } from "emails";
import WebhookAdded from "emails/webhook-added";
@@@ -99,54 -89,82 +104,72 @@@ export const POST = withWorkspace
}
}
- const webhook = await prisma.webhook.create({
- data: {
- id: createId({ prefix: WEBHOOK_ID_PREFIX }),
- name,
- url,
- secret,
- triggers,
- projectId: workspace.id,
- receiver: "user",
- links: {
- ...(linkIds &&
- linkIds.length > 0 && {
- create: linkIds.map((linkId) => ({
- linkId,
- })),
- }),
- },
- },
- select: {
- id: true,
- name: true,
- url: true,
- secret: true,
- triggers: true,
- links: true,
- },
- });
+ // Zapier use this endpoint to create webhooks from their app
+ const isZapierWebhook =
+ identifyWebhookReceiver(url) === WebhookReceiver.zapier;
- if (webhook) {
- await prisma.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- webhookEnabled: true,
- },
- });
- }
+ const zapierInstallation = isZapierWebhook
+ ? await prisma.installedIntegration.findFirst({
+ where: {
+ projectId: workspace.id,
+ integrationId: ZAPIER_INTEGRATION_ID,
+ },
+ select: {
+ id: true,
+ },
+ })
+ : undefined;
+
+ const webhook = await createWebhook({
+ name,
+ url,
+ receiver: isZapierWebhook ? WebhookReceiver.zapier : WebhookReceiver.user,
+ triggers,
+ linkIds,
+ secret,
+ workspaceId: workspace.id,
+ installationId: zapierInstallation ? zapierInstallation.id : undefined,
+ });
waitUntil(
- Promise.allSettled([
- toggleWebhooksForWorkspace({
- workspaceId: workspace.id,
- }),
+ (async () => {
+ const links = await prisma.link.findMany({
+ where: {
+ id: { in: linkIds },
+ projectId: workspace.id,
+ },
+ include: {
+ webhooks: {
+ select: {
+ webhookId: true,
+ },
+ },
+ },
+ });
- sendEmail({
- email: session.user.email,
- subject: "New webhook added",
- react: WebhookAdded({
- Promise.all([
- ...(links && links.length > 0 ? [linkCache.mset(links), []] : []),
-
- ...(isLinkLevelWebhook(webhook) ? [webhookCache.set(webhook)] : []),
-
++ Promise.allSettled([
++ toggleWebhooksForWorkspace({
++ workspaceId: workspace.id,
++ }),
+ sendEmail({
email: session.user.email,
- workspace: {
- name: workspace.name,
- slug: workspace.slug,
- },
- webhook: {
- name,
- },
+ subject: "New webhook added",
+ react: WebhookAdded({
+ email: session.user.email,
+ workspace: {
+ name: workspace.name,
+ slug: workspace.slug,
+ },
+ webhook: {
+ name,
+ },
+ }),
}),
- }),
- ]),
++ ...(links && links.length > 0 ? [linkCache.mset(links), []] : []),
++
++ ...(isLinkLevelWebhook(webhook) ? [webhookCache.set(webhook)] : []),
+ ]);
+ })(),
);
return NextResponse.json(transformWebhook(webhook), { status: 201 });