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.

folder checks for bulk link creation

Steven Teyfc258042182d
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index ce89d395af..d3585cb913 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -8,6 +8,7 @@ import {
import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";
import { bulkUpdateLinks } from "@/lib/api/links/bulk-update-links";
import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
+import { checkIfLinksHaveFolders } from "@/lib/api/links/utils/check-if-links-have-folders";
import { combineTagIds } from "@/lib/api/tags/combine-tag-ids";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -151,6 +152,53 @@ export const POST = withWorkspace(
});
}
+ if (checkIfLinksHaveFolders(validLinks)) {
+ const folderIds = validLinks
+ .map((link) => link.folderId)
+ .filter(Boolean) as string[];
+ const folders = await prisma.folder.findMany({
+ where: { id: { in: folderIds }, projectId: workspace.id },
+ select: {
+ id: true,
+ users: {
+ select: {
+ userId: true,
+ role: true,
+ },
+ },
+ },
+ });
+ validLinks.forEach((link, index) => {
+ if (link.folderId) {
+ const validFolder = folders.find(
+ (folder) => folder.id === link.folderId,
+ );
+ if (!validFolder) {
+ validLinks = validLinks.filter((_, i) => i !== index);
+ errorLinks.push({
+ error: `Invalid folderId detected: ${link.folderId}`,
+ code: "unprocessable_entity",
+ link,
+ });
+ // if user doesn't have write access to the folder, remove the link from validLinks and add error to errorLinks
+ } else if (
+ !validFolder.users.some(
+ (user) =>
+ user.userId === session.user.id &&
+ (user.role === "owner" || user.role === "editor"),
+ )
+ ) {
+ validLinks = validLinks.filter((_, i) => i !== index);
+ errorLinks.push({
+ error: `You don't have write access to this folder`,
+ code: "forbidden",
+ link,
+ });
+ }
+ }
+ });
+ }
+
if (checkIfLinksHaveWebhooks(validLinks)) {
if (workspace.plan === "free" || workspace.plan === "pro") {
throw new DubApiError({
diff --git a/apps/web/lib/api/links/utils/check-if-links-have-folders.ts b/apps/web/lib/api/links/utils/check-if-links-have-folders.ts
new file mode 100644
index 0000000000..e56c319071
--- /dev/null
+++ b/apps/web/lib/api/links/utils/check-if-links-have-folders.ts
@@ -0,0 +1,4 @@
+import { ProcessedLinkProps } from "../../../types";
+
+export const checkIfLinksHaveFolders = (links: ProcessedLinkProps[]) =>
+ links.some((link) => link.folderId);