Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: rename slug on link soft-delete to allow slug reuse

Cursor Agent651de45e3c93
diff --git a/app/(ee)/api/workflows/[workflowId]/route.ts b/app/(ee)/api/workflows/[workflowId]/route.ts
index 1cb03c2c..4f8803f1 100644
--- a/app/(ee)/api/workflows/[workflowId]/route.ts
+++ b/app/(ee)/api/workflows/[workflowId]/route.ts
@@ -5,6 +5,7 @@ import {
formatZodError,
} from "@/ee/features/workflows/lib/validation";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { customAlphabet } from "nanoid";
import { getServerSession } from "next-auth";
import { z } from "zod";
@@ -282,6 +283,11 @@ export async function DELETE(
select: {
id: true,
entryLinkId: true,
+ entryLink: {
+ select: {
+ slug: true,
+ },
+ },
},
});
@@ -292,6 +298,12 @@ export async function DELETE(
);
}
+ // Generate a random suffix for the deleted slug to free up the original slug
+ const generateDeletedSuffix = customAlphabet(
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
+ 6,
+ );
+
// Delete workflow and entry link in transaction
// Note: Steps and executions will cascade delete via Prisma relations
await prisma.$transaction([
@@ -299,12 +311,15 @@ export async function DELETE(
prisma.workflow.delete({
where: { id: workflowId },
}),
- // Delete entry link
+ // Soft delete entry link and rename slug so it can be reused
prisma.link.update({
where: { id: workflow.entryLinkId },
data: {
deletedAt: new Date(),
isArchived: true,
+ ...(workflow.entryLink?.slug && {
+ slug: `${workflow.entryLink.slug}-DELETED-${generateDeletedSuffix()}`,
+ }),
},
}),
]);
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 45ad6abd..89100b1b 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import { Brand, DataroomBrand, LinkAudienceType } from "@prisma/client";
+import { customAlphabet } from "nanoid";
import { getServerSession } from "next-auth/next";
import {
@@ -546,7 +547,14 @@ export default async function handle(
return res.status(401).end("Unauthorized to delete this link");
}
- // Soft delete the link by setting deletedAt and isArchived
+ // Generate a random suffix for the deleted slug to free up the original slug
+ const generateDeletedSuffix = customAlphabet(
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
+ 6,
+ );
+
+ // Soft delete the link by setting deletedAt and isArchived,
+ // and rename the slug so the original can be reused
await prisma.link.update({
where: {
id: id,
@@ -554,6 +562,9 @@ export default async function handle(
data: {
deletedAt: new Date(),
isArchived: true,
+ ...(linkToBeDeleted.slug && {
+ slug: `${linkToBeDeleted.slug}-DELETED-${generateDeletedSuffix()}`,
+ }),
},
});
diff --git a/pages/api/teams/[teamId]/links/[id]/index.ts b/pages/api/teams/[teamId]/links/[id]/index.ts
index ee048807..b34fe0f8 100644
--- a/pages/api/teams/[teamId]/links/[id]/index.ts
+++ b/pages/api/teams/[teamId]/links/[id]/index.ts
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { customAlphabet } from "nanoid";
import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
@@ -67,6 +68,7 @@ export default async function handle(
},
select: {
id: true,
+ slug: true,
},
});
@@ -74,7 +76,14 @@ export default async function handle(
return res.status(404).json({ error: "Link not found" });
}
- // Soft delete the link by setting deletedAt and isArchived
+ // Generate a random suffix for the deleted slug to free up the original slug
+ const generateDeletedSuffix = customAlphabet(
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
+ 6,
+ );
+
+ // Soft delete the link by setting deletedAt and isArchived,
+ // and rename the slug so the original can be reused
await prisma.link.update({
where: {
id: linkId,
@@ -82,6 +91,9 @@ export default async function handle(
data: {
deletedAt: new Date(),
isArchived: true,
+ ...(linkToDelete.slug && {
+ slug: `${linkToDelete.slug}-DELETED-${generateDeletedSuffix()}`,
+ }),
},
});