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.

assign a folder to a link

Kiran K980388c6c4f2
diff --git a/apps/web/app/api/folders/route.ts b/apps/web/app/api/folders/route.ts
index d8b8a63109..a823c4d324 100644
--- a/apps/web/app/api/folders/route.ts
+++ b/apps/web/app/api/folders/route.ts
@@ -18,8 +18,6 @@ export const GET = withWorkspace(
select: {
id: true,
name: true,
- createdAt: true,
- updatedAt: true,
},
orderBy: {
name: "asc",
@@ -52,7 +50,7 @@ export const POST = withWorkspace(
if (existingFolder) {
throw new DubApiError({
code: "conflict",
- message: `A folder with the name "${name}" already exists.`,
+ message: `A folder with the name ${name} already exists.`,
});
}
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index fe5a8e38a2..44d092a3bd 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -64,6 +64,7 @@ export async function processLink<T extends Record<string, any>>({
geo,
doIndex,
tagNames,
+ folderId,
} = payload;
let expiresAt: string | Date | null | undefined = payload.expiresAt;
@@ -321,6 +322,24 @@ export async function processLink<T extends Record<string, any>>({
}
}
+ // Folder checks
+ if (folderId && workspace) {
+ const folder = await prisma.folder.findUnique({
+ where: {
+ id: folderId,
+ projectId: workspace.id,
+ },
+ });
+
+ if (!folder) {
+ return {
+ link: payload,
+ error: "Folder does not exist.",
+ code: "unprocessable_entity",
+ };
+ }
+ }
+
// custom social media image checks (see if R2 is configured)
if (proxy && !process.env.STORAGE_SECRET_ACCESS_KEY) {
return {
diff --git a/apps/web/lib/api/rbac/resources.ts b/apps/web/lib/api/rbac/resources.ts
index 749a2fba07..3023746a96 100644
--- a/apps/web/lib/api/rbac/resources.ts
+++ b/apps/web/lib/api/rbac/resources.ts
@@ -4,6 +4,7 @@ export const RESOURCE_KEYS = [
"analytics",
"domains",
"tags",
+ "folders",
"tokens",
"conversions",
] as const;
@@ -40,6 +41,11 @@ export const RESOURCES: {
key: "tags",
description: "Create, read, update, and delete tags",
},
+ {
+ name: "Folders",
+ key: "folders",
+ description: "Create, read, update, and delete folders",
+ },
{
name: "Conversions",
key: "conversions",
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 07c2336993..84dce00064 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -9,6 +9,8 @@ export const SCOPES = [
"links.write",
"tags.read",
"tags.write",
+ "folders.read",
+ "folders.write",
"analytics.read",
"domains.read",
"domains.write",
@@ -55,6 +57,20 @@ export const RESOURCE_SCOPES: {
type: "write",
resource: "tags",
},
+ {
+ scope: "folders.read",
+ roles: ["owner", "member"],
+ permissions: ["folders.read"],
+ type: "read",
+ resource: "folders",
+ },
+ {
+ scope: "folders.write",
+ roles: ["owner", "member"],
+ permissions: ["folders.write", "folders.read"],
+ type: "write",
+ resource: "folders",
+ },
{
scope: "domains.read",
roles: ["owner", "member"],
@@ -103,6 +119,7 @@ export const RESOURCE_SCOPES: {
permissions: [
"links.read",
"tags.read",
+ "folders.read",
"domains.read",
"workspaces.read",
"analytics.read",
@@ -116,6 +133,8 @@ export const RESOURCE_SCOPES: {
"links.write",
"tags.read",
"tags.write",
+ "folders.read",
+ "folders.write",
"domains.read",
"domains.write",
"workspaces.read",
diff --git a/apps/web/lib/zod/schemas/folders.ts b/apps/web/lib/zod/schemas/folders.ts
index 1b6df33380..c163a1e812 100644
--- a/apps/web/lib/zod/schemas/folders.ts
+++ b/apps/web/lib/zod/schemas/folders.ts
@@ -3,8 +3,6 @@ import z from "@/lib/zod";
export const folderSchema = z.object({
id: z.string().describe("The unique ID of the folder."),
name: z.string().describe("The name of the folder."),
- createdAt: z.string().describe("The date the folder was created."),
- updatedAt: z.string().describe("The date the folder was updated."),
});
export const createFolderBodySchema = z.object({
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 145807f6ea..a929d1cd82 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -201,6 +201,10 @@ export const createLinkBodySchema = z.object({
.describe(
"The unique name of the tags assigned to the short link (case insensitive).",
),
+ folderId: z
+ .string()
+ .nullish()
+ .describe("The unique ID existing folder to assign the short link to."),
comments: z.string().nullish().describe("The comments for the short link."),
expiresAt: z
.string()
diff --git a/apps/web/prisma/schema/folder.prisma b/apps/web/prisma/schema/folder.prisma
index a96d48e3fe..e790bb9abb 100644
--- a/apps/web/prisma/schema/folder.prisma
+++ b/apps/web/prisma/schema/folder.prisma
@@ -5,22 +5,9 @@ model Folder {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- links LinkFolder[]
+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ links Link[]
@@unique([name, projectId])
@@index(projectId)
}
-
-model LinkFolder {
- id String @id @default(cuid())
- folderId String
- linkId String
- createdAt DateTime @default(now())
-
- link Link @relation(fields: [linkId], references: [id], onUpdate: Cascade, onDelete: Cascade)
- folder Folder @relation(fields: [folderId], references: [id], onUpdate: Cascade, onDelete: Cascade)
-
- @@unique(linkId)
- @@index(folderId)
-}
diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prisma
index 40e6b5d10f..a4cb74051b 100644
--- a/apps/web/prisma/schema/link.prisma
+++ b/apps/web/prisma/schema/link.prisma
@@ -44,6 +44,9 @@ model Link {
// Relational reference to the project domain
projectDomain Domain? @relation(fields: [domain], references: [slug], onUpdate: Cascade, onDelete: Cascade)
+ folderId String?
+ folder Folder? @relation(fields: [folderId], references: [id], onUpdate: Cascade, onDelete: SetNull)
+
publicStats Boolean @default(false) // whether to show public stats or not
clicks Int @default(0) // number of clicks
lastClicked DateTime? // when the link was last clicked
@@ -56,9 +59,6 @@ model Link {
// Link tags
tags LinkTag[]
- // Link folder
- folder LinkFolder?
-
// Link webhooks
webhooks LinkWebhook[]
@@ -77,5 +77,6 @@ model Link {
@@index(clicks(sort: Desc))
@@index(lastClicked)
@@index(userId)
+ @@index([folderId])
@@fulltext([key, url])
}