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.

fix links count filter logic

Steven Tey36d593340437
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 0a1fa8f535..2768899205 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -1,3 +1,4 @@
+import { getFolderIdsToFilter } from "@/lib/analytics/get-folder-ids-to-filter";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinksCount } from "@/lib/api/links";
import { withWorkspace } from "@/lib/auth";
@@ -9,7 +10,8 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(
async ({ headers, searchParams, workspace, session }) => {
const params = getLinksCountQuerySchema.parse(searchParams);
- const { domain, folderId } = params;
+ const { groupBy, domain, folderId, search, tagId, tagIds, tagNames } =
+ params;
if (domain) {
await getDomainOrThrow({ domain, workspace: workspace });
@@ -24,9 +26,23 @@ export const GET = withWorkspace(
});
}
+ /* we only need to get the folder ids if we are:
+ - not filtering by folder
+ - there's a groupBy
+ - filtering by search, domain, or tags
+ */
+ const folderIds =
+ !folderId && (groupBy || search || domain || tagId || tagIds || tagNames)
+ ? await getFolderIdsToFilter({
+ workspace,
+ userId: session.user.id,
+ })
+ : undefined;
+
const count = await getLinksCount({
searchParams: params,
workspaceId: workspace.id,
+ folderIds,
});
return NextResponse.json(count, {
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 373ccc3e45..08fca39550 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -36,7 +36,10 @@ export const GET = withWorkspace(
});
}
- // we only need to get the folder ids if we are filtering by domain, tag, or search (and not )
+ /* we only need to get the folder ids if we are:
+ - not filtering by folder
+ - filtering by search, domain, or tags
+ */
const folderIds =
!folderId && (search || domain || tagId || tagIds || tagNames)
? await getFolderIdsToFilter({
diff --git a/apps/web/lib/api/links/get-links-count.ts b/apps/web/lib/api/links/get-links-count.ts
index 20521c5df0..8648a69803 100644
--- a/apps/web/lib/api/links/get-links-count.ts
+++ b/apps/web/lib/api/links/get-links-count.ts
@@ -6,9 +6,11 @@ import { prisma } from "@dub/prisma";
export async function getLinksCount({
searchParams,
workspaceId,
+ folderIds,
}: {
searchParams: z.infer<typeof getLinksCountQuerySchema>;
workspaceId: string;
+ folderIds?: string[];
}) {
const {
groupBy,
@@ -48,8 +50,24 @@ export async function getLinksCount({
groupBy !== "userId" && {
userId,
}),
- // when filtering by folder, only filter by folder if the filter group is not "Folders"
- folderId: folderId && groupBy !== "folderId" ? folderId : null,
+ // if there are folderIds, we filter by folderIds
+ // else we filter by folderId
+ ...(folderIds
+ ? {
+ OR: [
+ {
+ folderId: {
+ in: folderIds.filter((id) => id !== ""),
+ },
+ },
+ {
+ folderId: null,
+ },
+ ],
+ }
+ : {
+ folderId: folderId || null,
+ }),
};
if (groupBy === "tagId") {