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.

remove the slack webhook when uninstall

Kiran Kd65e9eb12ba8
diff --git a/apps/web/app/api/slack/callback/route.ts b/apps/web/app/api/slack/callback/route.ts
index 618de67728..6f89c89dc3 100644
--- a/apps/web/app/api/slack/callback/route.ts
+++ b/apps/web/app/api/slack/callback/route.ts
@@ -75,6 +75,13 @@ export const GET = async (req: Request) => {
const data = await response.json();
+ const webhook = await addWebhook({
+ name: "Slack",
+ url: data.incoming_webhook.url,
+ triggers: ["link.created"],
+ workspace,
+ });
+
const credentials: SlackCredential = {
appId: data.app_id,
botUserId: data.bot_user_id,
@@ -86,7 +93,7 @@ export const GET = async (req: Request) => {
incomingWebhook: {
channel: data.incoming_webhook.channel,
channelId: data.incoming_webhook.channel_id,
- url: data.incoming_webhook.url,
+ webhookId: webhook.id,
},
};
@@ -99,12 +106,6 @@ export const GET = async (req: Request) => {
// Add the webhook if the installation was successful
if (installation) {
- await addWebhook({
- name: "Slack",
- url: credentials.incomingWebhook.url,
- triggers: ["link.created", "link.updated", "link.deleted"],
- workspace,
- });
}
} catch (e: any) {
return handleAndReturnErrorResponse(e);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx
index 0d99339c4f..dc86b2073a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx
@@ -290,29 +290,37 @@ export default function IntegrationPageClient({
</div>
{integration.installed && integration.id === SLACK_INTEGRATION_ID && (
- <div className="w-full rounded-lg border border-gray-200 bg-white">
- <div className="flex items-center gap-x-2 border-b border-gray-200 px-6 py-4">
- <Webhook className="size-4" />
- <p className="text-sm font-medium text-gray-700">
- Configure webhook
- </p>
- </div>
-
- <div className="flex items-center justify-between p-6">
- <p className="text-sm leading-normal text-gray-600">
- Customize your Slack notifications by configuring webhook
- settings. You can choose which events to send to Slack and disable
- the notifications.
- </p>
-
- <Link
- href={`/${slug}/settings/webhooks`}
- >
- <Button className="w-fit" text="Update" variant="secondary" />
- </Link>
- </div>
- </div>
+ <SlackWebhookSettings webhookId={integration.credentials?.webhookId!} />
)}
</MaxWidthWrapper>
);
}
+
+const SlackWebhookSettings = ({ webhookId }: { webhookId: string }) => {
+ const { slug } = useWorkspace();
+
+ if (!webhookId) {
+ return null;
+ }
+
+ return (
+ <div className="w-full rounded-lg border border-gray-200 bg-white">
+ <div className="flex items-center gap-x-2 border-b border-gray-200 px-6 py-4">
+ <Webhook className="size-4" />
+ <p className="text-sm font-medium text-gray-700">Configure webhook</p>
+ </div>
+
+ <div className="flex items-center justify-between p-6">
+ <p className="text-sm leading-normal text-gray-600">
+ Customize your Slack notifications by configuring webhook settings.
+ You can choose which events to send to Slack and disable the
+ notifications.
+ </p>
+
+ <Link href={`/${slug}/settings/webhooks/${webhookId}/edit`}>
+ <Button className="w-fit" text="Update" variant="secondary" />
+ </Link>
+ </div>
+ </div>
+ );
+};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx
index f96c6a515a..a7ec2f33b7 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx
@@ -1,4 +1,6 @@
+import { SlackCredential } from "@/lib/integrations/slack/type";
import { prisma } from "@/lib/prisma";
+import { SLACK_INTEGRATION_ID } from "@dub/utils";
import { notFound } from "next/navigation";
import IntegrationPageClient from "./page-client";
@@ -42,24 +44,36 @@ export default async function IntegrationPage({
notFound();
}
+ const installed = integration.installations.length > 0;
+ let credentials: Record<string, string> = {};
+
+ if (integration.id === SLACK_INTEGRATION_ID && installed) {
+ const slackCredentials = integration.installations[0]
+ .credentials as SlackCredential;
+
+ credentials = {
+ webhookId: slackCredentials?.incomingWebhook?.webhookId,
+ };
+ }
+
return (
<IntegrationPageClient
integration={{
...integration,
screenshots: integration.screenshots as string[],
installations: integration._count.installations,
- installed:
- integration.installations.length > 0
- ? {
- id: integration.installations[0].id,
- by: {
- id: integration.installations[0].userId,
- name: integration.installations[0].user.name,
- image: integration.installations[0].user.image,
- },
- createdAt: integration.installations[0].createdAt,
- }
- : null,
+ installed: installed
+ ? {
+ id: integration.installations[0].id,
+ by: {
+ id: integration.installations[0].userId,
+ name: integration.installations[0].user.name,
+ image: integration.installations[0].user.image,
+ },
+ createdAt: integration.installations[0].createdAt,
+ }
+ : null,
+ credentials,
}}
/>
);
diff --git a/apps/web/lib/integrations/slack/type.ts b/apps/web/lib/integrations/slack/type.ts
index 44ee28976b..9653c1a157 100644
--- a/apps/web/lib/integrations/slack/type.ts
+++ b/apps/web/lib/integrations/slack/type.ts
@@ -9,6 +9,6 @@ export type SlackCredential = {
incomingWebhook: {
channel: string;
channelId: string;
- url: string;
+ webhookId: string;
};
};
diff --git a/apps/web/lib/integrations/slack/uninstall.ts b/apps/web/lib/integrations/slack/uninstall.ts
index fd270b084a..62e788667f 100644
--- a/apps/web/lib/integrations/slack/uninstall.ts
+++ b/apps/web/lib/integrations/slack/uninstall.ts
@@ -1,4 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
+import { prisma } from "@/lib/prisma";
import { InstalledIntegration } from "@prisma/client";
import { getSlackEnv } from "./env";
import { SlackCredential } from "./type";
@@ -12,6 +13,15 @@ export const uninstallSlackIntegration = async ({
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/types.ts b/apps/web/lib/types.ts
index b6e2765c7b..f68693bdd2 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -240,6 +240,7 @@ export type InstalledIntegrationInfoProps = Pick<
image: string | null;
};
} | null;
+ credentials?: Record<string, string>;
};
export type WebhookTrigger = keyof typeof WEBHOOK_TRIGGER_DESCRIPTIONS;