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.

address CR comments

Kiran K8e65d9c0e29c
diff --git a/apps/web/app/api/webhooks/[webhookId]/folders/route.ts b/apps/web/app/api/webhooks/[webhookId]/folders/route.ts
index 99d82d4946..d67df56eeb 100644
--- a/apps/web/app/api/webhooks/[webhookId]/folders/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/folders/route.ts
@@ -4,28 +4,41 @@ import { MAX_WEBHOOK_FOLDERS } from "@/lib/webhook/constants";
import { NextResponse } from "next/server";
// GET /api/webhooks/[webhookId]/folders
-export const GET = withWorkspace(async ({ workspace, params }) => {
- const { webhookId } = params;
+export const GET = withWorkspace(
+ async ({ workspace, params }) => {
+ const { webhookId } = params;
- const webhook = await prisma.webhook.findUniqueOrThrow({
- where: {
- id: webhookId,
- projectId: workspace.id,
- },
- select: {
- id: true,
- },
- });
+ const webhook = await prisma.webhook.findUniqueOrThrow({
+ where: {
+ id: webhookId,
+ projectId: workspace.id,
+ },
+ select: {
+ id: true,
+ },
+ });
- const folders = await prisma.folderWebhook.findMany({
- where: {
- webhookId: webhook.id,
- },
- select: {
- folderId: true,
- },
- take: MAX_WEBHOOK_FOLDERS,
- });
+ const folders = await prisma.folderWebhook.findMany({
+ where: {
+ webhookId: webhook.id,
+ },
+ select: {
+ folderId: true,
+ },
+ take: MAX_WEBHOOK_FOLDERS,
+ });
- return NextResponse.json(folders.map(({ folderId }) => folderId));
-});
+ return NextResponse.json(folders.map(({ folderId }) => folderId));
+ },
+ {
+ requiredPermissions: ["webhooks.read"],
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/app/api/webhooks/[webhookId]/links/route.ts b/apps/web/app/api/webhooks/[webhookId]/links/route.ts
index 132755409d..4ebbff5f7c 100644
--- a/apps/web/app/api/webhooks/[webhookId]/links/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/links/route.ts
@@ -3,25 +3,38 @@ import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";
// TODO: remove once we move link.clicked webhook to workspace
-export const GET = withWorkspace(async ({ workspace, params }) => {
- const { webhookId } = params;
+export const GET = withWorkspace(
+ async ({ workspace, params }) => {
+ const { webhookId } = params;
- const webhook = await prisma.webhook.findUniqueOrThrow({
- where: {
- id: webhookId,
- projectId: workspace.id,
- },
- });
+ const webhook = await prisma.webhook.findUniqueOrThrow({
+ where: {
+ id: webhookId,
+ projectId: workspace.id,
+ },
+ });
- const links = await prisma.linkWebhook.findMany({
- where: {
- webhookId: webhook.id,
- },
- select: {
- linkId: true,
- },
- take: 1000,
- });
+ const links = await prisma.linkWebhook.findMany({
+ where: {
+ webhookId: webhook.id,
+ },
+ select: {
+ linkId: true,
+ },
+ take: 1000,
+ });
- return NextResponse.json(links.map((link) => link.linkId));
-});
+ return NextResponse.json(links.map((link) => link.linkId));
+ },
+ {
+ requiredPermissions: ["webhooks.read"],
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index b086283425..df70834bbc 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -90,11 +90,24 @@ export const PATCH = withWorkspace(
: existingWebhook.linkTarget
: null;
+ const existingLinkIds = existingWebhook.links.map(({ linkId }) => linkId);
+ const existingFolderIds = existingWebhook.folders.map(
+ ({ folderId }) => folderId,
+ );
+
+ const nextLinkIds = linkIds !== undefined ? linkIds : existingLinkIds;
+ const nextFolderIds =
+ folderIds !== undefined ? folderIds : existingFolderIds;
+
const nextWebhook: Partial<NewWebhook> = {
...existingWebhook,
...input,
triggers: finalTriggers,
linkTarget: nextLinkTarget,
+ ...(hasLinkClickedTrigger &&
+ nextLinkTarget === "links" && { linkIds: nextLinkIds }),
+ ...(hasLinkClickedTrigger &&
+ nextLinkTarget === "folders" && { folderIds: nextFolderIds }),
};
await validateWebhook({
@@ -104,11 +117,6 @@ export const PATCH = withWorkspace(
user: session.user,
});
- const existingLinkIds = existingWebhook.links.map(({ linkId }) => linkId);
- const existingFolderIds = existingWebhook.folders.map(
- ({ folderId }) => folderId,
- );
-
const linkTargetChanged = nextLinkTarget !== existingWebhook.linkTarget;
const shouldSyncLinks =
diff --git a/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts b/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts
index 38d917f27c..02fc01f862 100644
--- a/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts
+++ b/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts
@@ -1,6 +1,7 @@
-import { syncClickWebhookWorkspaceSet } from "@/lib/webhook/click-webhook-workspaces";
import "dotenv-flow/config";
+import { syncClickWebhookWorkspaceSet } from "@/lib/webhook/click-webhook-workspaces";
+
async function main() {
const count = await syncClickWebhookWorkspaceSet();
console.log(`Rebuilt webhookClickWorkspaces with ${count} workspace(s).`);