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.

getFolderIdsToFilter

Steven Tey8979db95e67b
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index 72d6b19073..fe014e9443 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -1,5 +1,6 @@
import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { getAnalytics } from "@/lib/analytics/get-analytics";
+import { getFolderIdsToFilter } from "@/lib/analytics/get-folder-ids-to-filter";
import { convertToCSV, validDateRangeForPlan } from "@/lib/analytics/utils";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
@@ -56,6 +57,12 @@ export const GET = withWorkspace(
throwError: true,
});
+ const folderIds = await getFolderIdsToFilter({
+ workspace,
+ userId: session.user.id,
+ folderIdToVerify,
+ });
+
const zip = new JSZip();
await Promise.all(
@@ -72,6 +79,7 @@ export const GET = withWorkspace(
...(link && { linkId: link.id }),
event: "clicks",
groupBy: endpoint,
+ folderIds,
folderId: folderId || "",
});
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.ts
index b5d2fcb9a0..cfc11f9570 100644
--- a/apps/web/app/api/analytics/route.ts
+++ b/apps/web/app/api/analytics/route.ts
@@ -1,13 +1,12 @@
import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { getAnalytics } from "@/lib/analytics/get-analytics";
+import { getFolderIdsToFilter } from "@/lib/analytics/get-folder-ids-to-filter";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { throwIfClicksUsageExceeded } from "@/lib/api/links/usage-checks";
import { withWorkspace } from "@/lib/auth";
-import { getFolders } from "@/lib/folder/get-folders";
import { verifyFolderAccess } from "@/lib/folder/permissions";
-import { getPlanCapabilities } from "@/lib/plan-capabilities";
import {
analyticsPathParamsSchema,
analyticsQuerySchema,
@@ -83,23 +82,11 @@ export const GET = withWorkspace(
throwError: true,
});
- // If the request is not for a specific folder, find folders the user has access to + unsorted folder
- let folderIds: string[] | undefined = undefined;
-
- if (!folderIdToVerify) {
- const { canManageFolderPermissions } = getPlanCapabilities(
- workspace.plan,
- );
-
- if (canManageFolderPermissions) {
- const folders = await getFolders({
- workspaceId: workspace.id,
- userId: session.user.id,
- });
-
- folderIds = folders.map((folder) => folder.id).concat("");
- }
- }
+ const folderIds = await getFolderIdsToFilter({
+ workspace,
+ userId: session.user.id,
+ folderIdToVerify,
+ });
// Identify the request is from deprecated clicks endpoint
// (/api/analytics/clicks)
@@ -125,4 +112,4 @@ export const GET = withWorkspace(
{
requiredPermissions: ["analytics.read"],
},
-);
\ No newline at end of file
+);
diff --git a/apps/web/app/api/events/export/route.ts b/apps/web/app/api/events/export/route.ts
index 3d6e941672..0c7137cf67 100644
--- a/apps/web/app/api/events/export/route.ts
+++ b/apps/web/app/api/events/export/route.ts
@@ -1,4 +1,5 @@
import { getEvents } from "@/lib/analytics/get-events";
+import { getFolderIdsToFilter } from "@/lib/analytics/get-folder-ids-to-filter";
import { convertToCSV, validDateRangeForPlan } from "@/lib/analytics/utils";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
@@ -79,11 +80,18 @@ export const GET = withWorkspace(
throwError: true,
});
+ const folderIds = await getFolderIdsToFilter({
+ workspace,
+ userId: session.user.id,
+ folderIdToVerify,
+ });
+
const response = await getEvents({
...parsedParams,
...(link && { linkId: link.id }),
workspaceId: workspace.id,
limit: 100000,
+ folderIds,
folderId: folderId || "",
});
diff --git a/apps/web/app/api/events/route.ts b/apps/web/app/api/events/route.ts
index febc3aad17..dbfa2b1d85 100644
--- a/apps/web/app/api/events/route.ts
+++ b/apps/web/app/api/events/route.ts
@@ -1,12 +1,11 @@
import { getEvents } from "@/lib/analytics/get-events";
+import { getFolderIdsToFilter } from "@/lib/analytics/get-folder-ids-to-filter";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { throwIfClicksUsageExceeded } from "@/lib/api/links/usage-checks";
import { withWorkspace } from "@/lib/auth";
-import { getFolders } from "@/lib/folder/get-folders";
import { verifyFolderAccess } from "@/lib/folder/permissions";
-import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { eventsQuerySchema } from "@/lib/zod/schemas/analytics";
import { Link } from "@dub/prisma/client";
import { NextResponse } from "next/server";
@@ -65,23 +64,11 @@ export const GET = withWorkspace(
throwError: true,
});
- // If the request is not for a specific folder, find folders the user has access to + unsorted folder
- let folderIds: string[] | undefined = undefined;
-
- if (!folderIdToVerify) {
- const { canManageFolderPermissions } = getPlanCapabilities(
- workspace.plan,
- );
-
- if (canManageFolderPermissions) {
- const folders = await getFolders({
- workspaceId: workspace.id,
- userId: session.user.id,
- });
-
- folderIds = folders.map((folder) => folder.id).concat("");
- }
- }
+ const folderIds = await getFolderIdsToFilter({
+ workspace,
+ userId: session.user.id,
+ folderIdToVerify,
+ });
const response = await getEvents({
...parsedParams,
diff --git a/apps/web/lib/analytics/get-folder-ids-to-filter.ts b/apps/web/lib/analytics/get-folder-ids-to-filter.ts
new file mode 100644
index 0000000000..018643569f
--- /dev/null
+++ b/apps/web/lib/analytics/get-folder-ids-to-filter.ts
@@ -0,0 +1,31 @@
+import { Project } from "@prisma/client";
+import { getFolders } from "../folder/get-folders";
+import { getPlanCapabilities } from "../plan-capabilities";
+
+export const getFolderIdsToFilter = async ({
+ workspace,
+ userId,
+ folderIdToVerify,
+}: {
+ workspace: Pick<Project, "id" | "plan">;
+ userId: string;
+ folderIdToVerify?: string;
+}) => {
+ // If the request is not for a specific folder, find folders the user has access to + unsorted folder
+ let folderIds: string[] | undefined = undefined;
+
+ if (!folderIdToVerify) {
+ const { canManageFolderPermissions } = getPlanCapabilities(workspace.plan);
+
+ if (canManageFolderPermissions) {
+ const folders = await getFolders({
+ workspaceId: workspace.id,
+ userId,
+ });
+
+ folderIds = folders.map((folder) => folder.id).concat("");
+ }
+ }
+
+ return folderIds;
+};