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.

Support externalIds in bulk update endpoint

Steven Tey195498fce438
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 14a60f4b79..22c9e892b4 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -143,18 +143,20 @@ 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(
+ const { linkIds, externalIds, data } = bulkUpdateLinksBodySchema.parse(
await parseRequestBody(req),
);
- if (linkIds.length === 0) {
+ if (linkIds.length === 0 && externalIds.length === 0) {
return NextResponse.json("No links to update", { headers });
}
const links = await prisma.link.findMany({
where: {
- id: { in: linkIds },
projectId: workspace.id,
+ ...(linkIds.length > 0
+ ? { id: { in: linkIds } }
+ : { externalId: { in: externalIds } }),
},
});
@@ -165,7 +167,18 @@ export const PATCH = withWorkspace(
error: "Link not found",
code: "not_found",
link: { id },
- }));
+ }))
+ .concat(
+ externalIds
+ .filter(
+ (id) => links.find((link) => link.externalId === id) === undefined,
+ )
+ .map((id) => ({
+ error: "Link not found",
+ code: "not_found",
+ link: { id },
+ })),
+ );
let { tagNames, expiresAt } = data;
const tagIds = combineTagIds(data);
diff --git a/apps/web/lib/api/links/bulk-update-links.ts b/apps/web/lib/api/links/bulk-update-links.ts
index aa8fcc0acd..1503f0c6fb 100644
--- a/apps/web/lib/api/links/bulk-update-links.ts
+++ b/apps/web/lib/api/links/bulk-update-links.ts
@@ -9,7 +9,10 @@ import { propagateBulkLinkChanges } from "./propagate-bulk-link-changes";
import { combineTagIds, transformLink } from "./utils";
export async function bulkUpdateLinks(
- params: z.infer<typeof bulkUpdateLinksBodySchema> & { workspaceId: string },
+ // omit externalIds from params
+ params: Omit<z.infer<typeof bulkUpdateLinksBodySchema>, "externalIds"> & {
+ workspaceId: string;
+ },
) {
const { linkIds, data, workspaceId } = params;
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 463b38de8d..39f5c926cd 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -321,8 +321,19 @@ export const bulkCreateLinksBodySchema = z
export const bulkUpdateLinksBodySchema = z.object({
linkIds: z
.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."),
+ .describe(
+ "The IDs of the links to update. Takes precedence over `externalIds`.",
+ )
+ .max(100, "You can only update up to 100 links at a time.")
+ .default([]),
+ externalIds: z
+ .array(z.string())
+ .describe(
+ "The external IDs of the links to update as stored in your database.",
+ )
+ .max(100, "You can only update up to 100 links at a time.")
+ .refine((v) => v.map((id) => id.replace("ext_", "")))
+ .default([]),
data: createLinkBodySchema
.omit({
id: true,
@@ -542,10 +553,8 @@ export const getLinkInfoQuerySchema = domainKeySchema.partial().merge(
externalId: z
.string()
.optional()
- .describe(
- "This is the ID of the link in the your database. Must be prefixed with `ext_` when passed as a query parameter.",
- )
- .openapi({ example: "ext_123456" }),
+ .describe("This is the ID of the link in the your database.")
+ .openapi({ example: "123456" }),
}),
);