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.

allow updating urls

Steven Teye1bfa265815a
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 1c9b6835be..b4c8a811a5 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -5,12 +5,11 @@ import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
-import { ProcessedLinkProps } from "@/lib/types";
+import { NewLinkProps, ProcessedLinkProps } from "@/lib/types";
import {
bulkCreateLinksBodySchema,
bulkUpdateLinksBodySchema,
} from "@/lib/zod/schemas/links";
-import { parseDateTime } from "@dub/utils";
import { NextResponse } from "next/server";
// POST /api/links/bulk – bulk create up to 100 links
@@ -140,29 +139,29 @@ export const PATCH = withWorkspace(
const { linkIds, data } = bulkUpdateLinksBodySchema.parse(
await parseRequestBody(req),
);
- const validLinkIds = await prisma.link
- .findMany({
- where: {
- id: { in: linkIds },
- projectId: workspace.id,
- },
- })
- .then((links) => links.map(({ id }) => id));
- if (validLinkIds.length !== linkIds.length) {
- throw new DubApiError({
- code: "bad_request",
- message: `Invalid link IDs: ${linkIds.filter((id) => !validLinkIds.includes(id)).join(", ")}`,
- });
- }
-
- if (validLinkIds.length === 0) {
+ 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) => ({
+ link: { id },
+ error: "Link not found",
+ code: "not_found",
+ }));
+
let { tagNames, expiresAt } = data;
const tagIds = combineTagIds(data);
-
// tag checks
if (tagIds && tagIds.length > 0) {
const tags = await prisma.tag.findMany({
@@ -197,17 +196,39 @@ export const PATCH = withWorkspace(
}
}
- // expiration date checks
- if (expiresAt) {
- const datetime = parseDateTime(expiresAt);
- if (!datetime) {
- throw new DubApiError({
- code: "unprocessable_entity",
- message: "Invalid expiration date.",
- });
- }
- expiresAt = datetime.toISOString();
- }
+ 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 }) => ({
+ link,
+ error: error as string,
+ code: code as string,
+ })),
+ );
const response = await bulkUpdateLinks({
linkIds: validLinkIds,
@@ -219,7 +240,7 @@ export const PATCH = withWorkspace(
workspaceId: workspace.id,
});
- return NextResponse.json(response, { headers });
+ return NextResponse.json([...response, ...errorLinks], { headers });
},
{
requiredPlan: [
diff --git a/apps/web/lib/api/links/bulk-update-links.ts b/apps/web/lib/api/links/bulk-update-links.ts
index 8c5a27a60c..01a53fd488 100644
--- a/apps/web/lib/api/links/bulk-update-links.ts
+++ b/apps/web/lib/api/links/bulk-update-links.ts
@@ -9,14 +9,12 @@ import { propagateBulkLinkChanges } from "./propagate-bulk-link-changes";
import { combineTagIds, transformLink } from "./utils";
export async function bulkUpdateLinks(
- params: z.infer<typeof bulkUpdateLinksBodySchema> & {
- workspaceId: string;
- url?: string;
- },
+ params: z.infer<typeof bulkUpdateLinksBodySchema> & { workspaceId: string },
) {
- const { linkIds, data, workspaceId, url } = params;
+ const { linkIds, data, workspaceId } = params;
const {
+ url,
title,
description,
image,
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index d101883dfb..9d4454dcc8 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -6,6 +6,7 @@ import {
import { getPangeaDomainIntel } from "@/lib/pangea";
import { checkIfUserExists, getRandomKey } from "@/lib/planetscale";
import { prisma } from "@/lib/prisma";
+import { isStored } from "@/lib/storage";
import { NewLinkProps, ProcessedLinkProps, WorkspaceProps } from "@/lib/types";
import {
DUB_DOMAINS,
@@ -229,7 +230,7 @@ export async function processLink<T extends Record<string, any>>({
}
if (bulk) {
- if (image) {
+ if (proxy && image && !isStored(image)) {
return {
link: payload,
error: "You cannot set custom social cards with bulk link creation.",
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index cd33834bc1..673b13f42b 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -257,14 +257,24 @@ export const bulkUpdateLinksBodySchema = z.object({
.array(z.string())
.min(1, "No links updated – you must provide at least one link.")
.max(100, "You can only update up to 100 links at a time."),
- data: createLinkBodySchema.omit({
- id: true,
- domain: true,
- key: true,
- url: true,
- externalId: true,
- prefix: true,
- }),
+ data: createLinkBodySchema
+ .omit({
+ id: true,
+ domain: true,
+ key: true,
+ externalId: true,
+ prefix: true,
+ })
+ .merge(
+ z.object({
+ url: parseUrlSchema
+ .describe("The destination URL of the short link.")
+ .openapi({
+ example: "https://google/com",
+ })
+ .optional(),
+ }),
+ ),
});
export const LinkSchema = z