The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update API endpoints to support multiple tags (`tagIds`)
Tim Wilson74593b9c8c0e
diff --git a/apps/web/app/api/admin/links/count/route.ts b/apps/web/app/api/admin/links/count/route.tsindex 89edc7c0de..0a175b0d16 100644--- a/apps/web/app/api/admin/links/count/route.ts+++ b/apps/web/app/api/admin/links/count/route.ts@@ -6,7 +6,7 @@ import { DUB_DOMAINS, LEGAL_USER_ID } from "@dub/utils";// GET /api/links – get all user linksexport const GET = withAdmin(async ({ searchParams }) => {let { groupBy, search, domain, tagId } = searchParams as {- groupBy?: "domain" | "tagId";+ groupBy?: "domain";search?: string;domain?: string;tagId?: string;@@ -69,7 +69,13 @@ export const GET = withAdmin(async ({ searchParams }) => {in: DUB_DOMAINS.map((domain) => domain.slug),},}),- ...(tagId && { tagId }),+ ...(tagId && {+ tags: {+ some: {+ id: tagId,+ },+ },+ }),},});}diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.tsindex 073bc887f3..e65165e6c8 100644--- a/apps/web/app/api/links/route.ts+++ b/apps/web/app/api/links/route.ts@@ -7,20 +7,22 @@ import { NextResponse } from "next/server";// GET /api/links – get all user linksexport const GET = withAuth(async ({ headers, searchParams, project }) => {- const { domain, tagId, search, sort, page, userId, showArchived } =+ const { domain, tagId, tagIds, search, sort, page, userId, showArchived } =searchParams as {domain?: string;tagId?: string;+ tagIds?: string[];search?: string;sort?: "createdAt" | "clicks" | "lastClicked";page?: string;userId?: string;showArchived?: string;};+const response = await getLinksForProject({projectId: project.id,domain,- tagId,+ tagIds: (tagIds ?? []).concat(tagId ?? []),search,sort,page,diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.tsindex 1b554ba603..30e0b7ff00 100644--- a/apps/web/app/api/route.ts+++ b/apps/web/app/api/route.ts@@ -65,11 +65,24 @@ export function GET(): NextResponse<OpenAPIV3.Document> {description: "The tag ID to filter the links by.",in: "query",required: false,+ deprecated: true,schema: {description: "The tag ID to filter the links by.",type: "string",},},+ {+ name: "tagIds",+ description: "The tag IDs to filter the links by.",+ in: "query",+ required: false,+ schema: {+ type: "array",+ items: {+ type: "string",+ },+ },+ },{name: "search",description:@@ -811,6 +824,17 @@ export function GET(): NextResponse<OpenAPIV3.Document> {"The unique id of the tag assigned to the short link.",default: null,nullable: true,+ deprecated: true,+ },+ tagIds: {+ type: "array",+ items: {+ type: "string",+ format: "cuid",+ },+ description:+ "The unique IDs of the tags assigned to the short link.",+ default: [],},comments: {type: "string",diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.tsindex 1102c36291..d7fe0943ff 100644--- a/apps/web/lib/api/links.ts+++ b/apps/web/lib/api/links.ts@@ -27,7 +27,7 @@ import { getLinkViaEdge } from "../planetscale";export async function getLinksForProject({projectId,domain,- tagId,+ tagIds,search,sort = "createdAt",page,@@ -36,7 +36,7 @@ export async function getLinksForProject({}: {projectId: string;domain?: string;- tagId?: string;+ tagIds?: string[];search?: string;sort?: "createdAt" | "clicks" | "lastClicked"; // descending for allpage?: string;@@ -58,7 +58,7 @@ export async function getLinksForProject({},],}),- ...(tagId && { tagId }),+ ...(tagIds?.length && { tags: { some: { id: { in: tagIds } } } }),...(userId && { userId }),},include: {@@ -95,75 +95,93 @@ export async function getLinksCount({projectId: string;userId?: string | null;}) {- let { groupBy, search, domain, tagId, showArchived } = searchParams as {- groupBy?: "domain" | "tagId";- search?: string;- domain?: string;- tagId?: string;- showArchived?: boolean;+ let { groupBy, search, domain, tagId, tagIds, showArchived } =+ searchParams as {+ groupBy?: "domain" | "tagId";+ search?: string;+ domain?: string;+ tagId?: string;+ tagIds?: string[];+ showArchived?: boolean;+ };++ const combinedTagIds = (tagIds ?? []).concat(tagId ?? []);++ const linksWhere = {+ archived: showArchived ? undefined : false,+ ...(userId && { userId }),+ ...(search && {+ OR: [+ {+ key: { contains: search },+ },+ {+ url: { contains: search },+ },+ ],+ }),+ // when filtering by domain, only filter by domain if the filter group is not "Domains"+ ...(domain &&+ groupBy !== "domain" && {+ domain,+ }),};- if (groupBy) {- return await prisma.link.groupBy({- by: [groupBy],+ if (groupBy === "tagId") {+ const tags = await prisma.tag.findMany({+ select: {+ id: true,+ _count: {+ select: {+ linksNew: true,+ },+ },+ },where: {projectId,- archived: showArchived ? undefined : false,- ...(userId && { userId }),- ...(search && {- OR: [- {- key: { contains: search },- },- {- url: { contains: search },- },- ],- }),- // when filtering by domain, only filter by domain if the filter group is not "Domains"- ...(domain &&- groupBy !== "domain" && {- domain,- }),- // when filtering by tagId, only filter by tagId if the filter group is not "Tags"- ...(tagId &&- groupBy !== "tagId" && {- tagId,- }),- // for the "Tags" filter group, only count links that have a tagId- ...(groupBy === "tagId" && {- NOT: {- tagId: null,+ linksNew: {+ some: {+ link: linksWhere,},- }),- },- _count: true,- orderBy: {- _count: {- [groupBy]: "desc",},},});++ return tags.map((tag) => ({+ tagId: tag.id,+ _count: tag._count.linksNew,+ }));} else {- return await prisma.link.count({- where: {- projectId,- archived: showArchived ? undefined : false,- ...(userId && { userId }),- ...(search && {- OR: [- {- key: { contains: search },- },- {- url: { contains: search },+ const where = {+ projectId,+ ...linksWhere,+ ...(combinedTagIds?.length && {+ tags: {+ some: {+ id: {+ in: combinedTagIds,},- ],- }),- ...(domain && { domain }),- ...(tagId && { tagId }),- },- });+ },+ },+ }),+ };++ if (groupBy) {+ return await prisma.link.groupBy({+ by: [groupBy],+ where,+ _count: true,+ orderBy: {+ _count: {+ [groupBy]: "desc",+ },+ },+ });+ } else {+ return await prisma.link.count({+ where,+ });+ }}}