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.

improve integration webhook handling

Kiran K574b6808d465
diff --git a/apps/web/app/api/slack/callback/route.ts b/apps/web/app/api/slack/callback/route.ts
index 9a26bab872..f024d38050 100644
--- a/apps/web/app/api/slack/callback/route.ts
+++ b/apps/web/app/api/slack/callback/route.ts
@@ -75,14 +75,6 @@ export const GET = async (req: Request) => {
const data = await response.json();
- const webhook = await createWebhook({
- name: "Slack",
- url: data.incoming_webhook.url,
- receiver: WebhookReceiver.slack,
- triggers: [],
- workspace,
- });
-
const credentials: SlackCredential = {
appId: data.app_id,
botUserId: data.bot_user_id,
@@ -94,16 +86,24 @@ export const GET = async (req: Request) => {
incomingWebhook: {
channel: data.incoming_webhook.channel,
channelId: data.incoming_webhook.channel_id,
- webhookId: webhook.id,
},
};
- await installIntegration({
+ const installation = await installIntegration({
integrationId: SLACK_INTEGRATION_ID,
userId: session.user.id,
workspaceId,
credentials,
});
+
+ await createWebhook({
+ name: "Slack",
+ url: data.incoming_webhook.url,
+ receiver: WebhookReceiver.slack,
+ triggers: [],
+ workspaceId,
+ installationId: installation.id,
+ });
} catch (e: any) {
return handleAndReturnErrorResponse(e);
}
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index a1c51d2d8d..c4f5a4d07a 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -94,7 +94,7 @@ export const POST = withWorkspace(
triggers,
linkIds,
secret,
- workspace,
+ workspaceId: workspace.id,
});
waitUntil(
diff --git a/apps/web/lib/integrations/segment/install.ts b/apps/web/lib/integrations/segment/install.ts
index 7d9773eb08..3b6b134b52 100644
--- a/apps/web/lib/integrations/segment/install.ts
+++ b/apps/web/lib/integrations/segment/install.ts
@@ -19,24 +19,24 @@ export const installSegmentAction = authActionClient
const { workspace, user } = ctx;
const { writeKey } = parsedInput;
- const webhook = await createWebhook({
- name: "Segment",
- url: "https://api.segment.io/v1/track",
- receiver: WebhookReceiver.segment,
- triggers: [],
- workspace,
- secret: writeKey,
- });
-
- await installIntegration({
+ const installation = await installIntegration({
integrationId: SEGMENT_INTEGRATION_ID,
userId: user.id,
workspaceId: workspace.id,
credentials: {
writeKey,
- webhookId: webhook.id,
},
});
+ await createWebhook({
+ name: "Segment",
+ url: "https://api.segment.io/v1/track",
+ receiver: WebhookReceiver.segment,
+ triggers: [],
+ workspaceId: workspace.id,
+ secret: writeKey,
+ installationId: installation.id,
+ });
+
revalidatePath(`/${workspace.slug}/settings/integrations/segment`);
});
diff --git a/apps/web/lib/integrations/slack/type.ts b/apps/web/lib/integrations/slack/type.ts
index 9653c1a157..d8cc22502a 100644
--- a/apps/web/lib/integrations/slack/type.ts
+++ b/apps/web/lib/integrations/slack/type.ts
@@ -9,6 +9,5 @@ export type SlackCredential = {
incomingWebhook: {
channel: string;
channelId: string;
- webhookId: string;
};
};
diff --git a/apps/web/lib/integrations/slack/uninstall.ts b/apps/web/lib/integrations/slack/uninstall.ts
index 62e788667f..997aa81ae9 100644
--- a/apps/web/lib/integrations/slack/uninstall.ts
+++ b/apps/web/lib/integrations/slack/uninstall.ts
@@ -1,5 +1,4 @@
import { DubApiError } from "@/lib/api/errors";
-import { prisma } from "@/lib/prisma";
import { InstalledIntegration } from "@prisma/client";
import { getSlackEnv } from "./env";
import { SlackCredential } from "./type";
@@ -10,18 +9,8 @@ export const uninstallSlackIntegration = async ({
installation: InstalledIntegration;
}) => {
const env = getSlackEnv();
-
const credentials = installation.credentials as SlackCredential;
- // Remove the webhook added by the Slack integration
- if (credentials.incomingWebhook.webhookId) {
- await prisma.webhook.delete({
- where: {
- id: credentials.incomingWebhook.webhookId,
- },
- });
- }
-
const response = await fetch("https://slack.com/api/apps.uninstall", {
method: "POST",
headers: {
diff --git a/apps/web/lib/webhook/create-webhook.ts b/apps/web/lib/webhook/create-webhook.ts
index dd8008da0e..0c602d6d89 100644
--- a/apps/web/lib/webhook/create-webhook.ts
+++ b/apps/web/lib/webhook/create-webhook.ts
@@ -3,13 +3,11 @@ import { createId } from "@/lib/api/utils";
import { prisma } from "@/lib/prisma";
import { webhookCache } from "@/lib/webhook/cache";
import { WEBHOOK_ID_PREFIX } from "@/lib/webhook/constants";
-import {
- identifyWebhookReceiver,
- isLinkLevelWebhook,
-} from "@/lib/webhook/utils";
-import { Project, WebhookReceiver } from "@prisma/client";
+import { isLinkLevelWebhook } from "@/lib/webhook/utils";
+import { WebhookReceiver } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
-import { WebhookTrigger } from "../types";
+import { z } from "zod";
+import { createWebhookSchema } from "../zod/schemas/webhooks";
import { createWebhookSecret } from "./secret";
export async function createWebhook({
@@ -17,17 +15,14 @@ export async function createWebhook({
url,
secret,
triggers,
- workspace,
linkIds,
+ workspaceId,
receiver,
-}: {
- name: string;
- url: string;
- secret?: string;
- triggers: WebhookTrigger[];
- workspace: Pick<Project, "id">;
- linkIds?: string[];
+ installationId,
+}: z.infer<typeof createWebhookSchema> & {
+ workspaceId: string;
receiver?: WebhookReceiver;
+ installationId?: string;
}) {
const webhook = await prisma.webhook.create({
data: {
@@ -35,8 +30,9 @@ export async function createWebhook({
name,
url,
triggers,
- projectId: workspace.id,
- receiver: receiver || identifyWebhookReceiver(url),
+ receiver,
+ installationId,
+ projectId: workspaceId,
secret: secret || createWebhookSecret(),
links: {
...(linkIds &&
@@ -60,7 +56,7 @@ export async function createWebhook({
await prisma.project.update({
where: {
- id: workspace.id,
+ id: workspaceId,
},
data: {
webhookEnabled: true,
@@ -72,7 +68,7 @@ export async function createWebhook({
const links = await prisma.link.findMany({
where: {
id: { in: linkIds },
- projectId: workspace.id,
+ projectId: workspaceId,
},
include: {
webhooks: {
diff --git a/apps/web/prisma/schema/webhook.prisma b/apps/web/prisma/schema/webhook.prisma
index 17836b239d..44c400eb7f 100644
--- a/apps/web/prisma/schema/webhook.prisma
+++ b/apps/web/prisma/schema/webhook.prisma
@@ -8,9 +8,9 @@ enum WebhookReceiver {
model Webhook {
id String @id @default(cuid())
- receiver WebhookReceiver @default(user)
projectId String
- installationId String? @unique // webhook belongs to an installed integration
+ installationId String? @unique // indicates which integration installation added the webhook
+ receiver WebhookReceiver @default(user)
name String
url String
secret String