The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
make bulk endpoint available on all plans
Steven Tey254efabf40e2
diff --git a/apps/docs/api-reference/endpoint/bulk-update-links.mdx b/apps/docs/api-reference/endpoint/bulk-update-links.mdxindex 2049f636d3..034aeed762 100644--- a/apps/docs/api-reference/endpoint/bulk-update-links.mdx+++ b/apps/docs/api-reference/endpoint/bulk-update-links.mdx@@ -12,5 +12,5 @@ Some potential use cases:- Updating UTM parameters for multiple links<Warning>- Caveat: You cannot update the domain or key of a link with this endpoint.+ You cannot update the domain or key of a link with this endpoint.</Warning>diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.tsindex f27f11e568..0f42e11936 100644--- a/apps/web/app/api/links/bulk/route.ts+++ b/apps/web/app/api/links/bulk/route.ts@@ -137,125 +137,113 @@ export const POST = withWorkspace();// PATCH /api/links/bulk – bulk update up to 100 links with the same data-export const PATCH = withWorkspace(- async ({ req, workspace, headers }) => {- const { linkIds, data } = bulkUpdateLinksBodySchema.parse(- await parseRequestBody(req),- );+export const PATCH = withWorkspace(async ({ req, workspace, headers }) => {+ const { linkIds, data } = bulkUpdateLinksBodySchema.parse(+ await parseRequestBody(req),+ );++ if (linkIds.length === 0) {+ return NextResponse.json("No links to update", { headers });+ }++ const links = await prisma.link.findMany({+ where: {+ id: { in: linkIds },+ projectId: workspace.id,+ },+ });++ // linkIds that don't exist+ let errorLinks = linkIds+ .filter((id) => links.find((link) => link.id === id) === undefined)+ .map((id) => ({+ error: "Link not found",+ code: "not_found",+ link: { id },+ }));++ let { tagNames, expiresAt } = data;+ const tagIds = combineTagIds(data);+ // tag checks+ if (tagIds && tagIds.length > 0) {+ const tags = await prisma.tag.findMany({+ select: {+ id: true,+ },+ where: { projectId: workspace?.id, id: { in: tagIds } },+ });- if (linkIds.length === 0) {- return NextResponse.json("No links to update", { headers });+ if (tags.length !== tagIds.length) {+ throw new DubApiError({+ code: "unprocessable_entity",+ message: `Invalid tagIds detected: ${tagIds.filter((tagId) => tags.find(({ id }) => tagId === id) === undefined).join(", ")}`,+ });}-- const links = await prisma.link.findMany({+ } else if (tagNames && tagNames.length > 0) {+ const tags = await prisma.tag.findMany({+ select: {+ name: true,+ },where: {- id: { in: linkIds },- projectId: workspace.id,+ projectId: workspace?.id,+ name: { in: tagNames },},});- // linkIds that don't exist- let errorLinks = linkIds- .filter((id) => links.find((link) => link.id === id) === undefined)- .map((id) => ({- error: "Link not found",- code: "not_found",- link: { id },- }));-- let { tagNames, expiresAt } = data;- const tagIds = combineTagIds(data);- // tag checks- if (tagIds && tagIds.length > 0) {- const tags = await prisma.tag.findMany({- select: {- id: true,- },- where: { projectId: workspace?.id, id: { in: tagIds } },- });-- if (tags.length !== tagIds.length) {- throw new DubApiError({- code: "unprocessable_entity",- message: `Invalid tagIds detected: ${tagIds.filter((tagId) => tags.find(({ id }) => tagId === id) === undefined).join(", ")}`,- });- }- } else if (tagNames && tagNames.length > 0) {- const tags = await prisma.tag.findMany({- select: {- name: true,- },- where: {- projectId: workspace?.id,- name: { in: tagNames },- },+ if (tags.length !== tagNames.length) {+ throw new DubApiError({+ code: "unprocessable_entity",+ message: `Invalid tagNames detected: ${tagNames.filter((tagName) => tags.find(({ name }) => tagName === name) === undefined).join(", ")}`,});-- if (tags.length !== tagNames.length) {- throw new DubApiError({- code: "unprocessable_entity",- message: `Invalid tagNames detected: ${tagNames.filter((tagName) => tags.find(({ name }) => tagName === name) === undefined).join(", ")}`,- });- }}-- const processedLinks = await Promise.all(- links.map(async (link) =>- processLink({- payload: {- ...link,- expiresAt:- link.expiresAt instanceof Date- ? link.expiresAt.toISOString()- : link.expiresAt,- geo: link.geo as NewLinkProps["geo"],+ }++ const processedLinks = await Promise.all(+ links.map(async (link) =>+ processLink({+ payload: {+ ...link,+ expiresAt:+ link.expiresAt instanceof Date+ ? link.expiresAt.toISOString()+ : link.expiresAt,+ geo: link.geo as NewLinkProps["geo"],+ ...data,+ },+ workspace,+ userId: link.userId ?? undefined,+ bulk: true,+ skipKeyChecks: true,+ }),+ ),+ );++ const validLinkIds = processedLinks+ .filter(({ error }) => error == null)+ .map(({ link }) => link.id) as string[];++ errorLinks = errorLinks.concat(+ processedLinks+ .filter(({ error }) => error != null)+ .map(({ link, error, code }) => ({+ error: error as string,+ code: code as string,+ link,+ })),+ );++ const response =+ validLinkIds.length > 0+ ? await bulkUpdateLinks({+ linkIds: validLinkIds,+ data: {...data,+ tagIds,+ expiresAt,},- workspace,- userId: link.userId ?? undefined,- bulk: true,- skipKeyChecks: true,- }),- ),- );-- const validLinkIds = processedLinks- .filter(({ error }) => error == null)- .map(({ link }) => link.id) as string[];-- errorLinks = errorLinks.concat(- processedLinks- .filter(({ error }) => error != null)- .map(({ link, error, code }) => ({- error: error as string,- code: code as string,- link,- })),- );-- const response =- validLinkIds.length > 0- ? await bulkUpdateLinks({- linkIds: validLinkIds,- data: {- ...data,- tagIds,- expiresAt,- },- workspaceId: workspace.id,- })- : [];+ workspaceId: workspace.id,+ })+ : [];- return NextResponse.json([...response, ...errorLinks], { headers });- },- {- requiredPlan: [- "pro",- "business",- "business plus",- "business extra",- "business max",- "enterprise",- ],- },-);+ return NextResponse.json([...response, ...errorLinks], { headers });+});