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.

Update route.ts

Steven Tey518dbc22b693
diff --git a/apps/web/app/api/admin/links/count/route.ts b/apps/web/app/api/admin/links/count/route.ts
index 4e67f3c622..5629507c3f 100644
--- a/apps/web/app/api/admin/links/count/route.ts
+++ b/apps/web/app/api/admin/links/count/route.ts
@@ -6,81 +6,85 @@ import { NextResponse } from "next/server";
// GET /api/admin/links/count
export const GET = withAdmin(async ({ searchParams }) => {
let { groupBy, search, domain, tagId } = searchParams as {
- groupBy?: "domain";
+ groupBy?: "domain" | "tagId";
search?: string;
domain?: string;
tagId?: string;
};
- const tagIds = tagId ? tagId.split(",") : [];
-
let response;
- if (groupBy) {
- response = await prisma.link.groupBy({
- by: [groupBy],
- where: {
- ...(search && {
- OR: [
- {
- key: { contains: search },
- },
- {
- url: { contains: search },
- },
- ],
+ const tagIds = tagId ? tagId.split(",") : [];
+
+ const linksWhere = {
+ // when filtering by domain, only filter by domain if the filter group is not "Domains"
+ ...(domain && groupBy !== "domain"
+ ? {
+ domain,
+ }
+ : {
+ domain: {
+ in: DUB_DOMAINS.map((domain) => domain.slug),
+ },
}),
- // when filtering by domain, only filter by domain if the filter group is not "Domains"
- ...(domain && groupBy !== "domain"
- ? {
- domain,
- }
- : {
- domain: {
- in: DUB_DOMAINS.map((domain) => domain.slug),
- },
- }),
- userId: {
- not: LEGAL_USER_ID,
+ userId: {
+ not: LEGAL_USER_ID,
+ },
+ ...(search && {
+ OR: [
+ {
+ key: { contains: search },
+ },
+ {
+ url: { contains: search },
},
+ ],
+ }),
+ };
+
+ if (groupBy === "tagId") {
+ response = await prisma.linkTag.groupBy({
+ by: ["tagId"],
+ where: {
+ link: linksWhere,
},
_count: true,
orderBy: {
_count: {
- [groupBy]: "desc",
+ tagId: "desc",
},
},
});
} else {
- response = await prisma.link.count({
- where: {
- ...(search && {
- OR: [
- {
- key: { contains: search },
- },
- {
- url: { contains: search },
- },
- ],
- }),
- ...(domain
- ? { domain }
- : {
- domain: {
- in: DUB_DOMAINS.map((domain) => domain.slug),
- },
- }),
- ...(tagIds.length > 0 && {
- tags: {
- some: {
- id: tagId,
+ const where = {
+ ...linksWhere,
+ ...(tagIds.length > 0 && {
+ tags: {
+ some: {
+ tagId: {
+ in: tagIds,
},
},
- }),
- },
- });
- }
+ },
+ }),
+ };
+ if (groupBy === "domain") {
+ response = await prisma.link.groupBy({
+ by: [groupBy],
+ where,
+ _count: true,
+ orderBy: {
+ _count: {
+ [groupBy]: "desc",
+ },
+ },
+ });
+ } else {
+ response = await prisma.link.count({
+ where,
+ });
+ }
+ }
return NextResponse.json(response);
});