Skip to content

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

feat: add sorting for numbered dataroom document

Marc Seitz10605ecff324
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 2a5e6f8a..41bc8d23 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -1,6 +1,11 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { Brand, DataroomBrand } from "@prisma/client";
+import {
+ Brand,
+ DataroomBrand,
+ DataroomDocument,
+ Document,
+} from "@prisma/client";
import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
@@ -157,6 +162,29 @@ export default async function handle(
},
});
+ // Sort documents by name considering the numerical part
+ linkData.dataroom.documents.sort(
+ (
+ a: DataroomDocument & { document: Document },
+ b: DataroomDocument & { document: Document },
+ ) => {
+ const getNumber = (str: string): number => {
+ const match = str.match(/^\d+/);
+ return match ? parseInt(match[0], 10) : 0;
+ };
+
+ const numA = getNumber(a.document.name);
+ const numB = getNumber(b.document.name);
+
+ if (numA !== numB) {
+ return numA - numB;
+ }
+
+ // If numerical parts are the same, fall back to lexicographical order
+ return a.document.name.localeCompare(b.document.name);
+ },
+ );
+
brand = await prisma.dataroomBrand.findFirst({
where: {
dataroomId: linkData.dataroom.id,
diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].ts
index 14c5f105..d8d103f6 100644
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@ -1,6 +1,11 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { Brand, DataroomBrand } from "@prisma/client";
+import {
+ Brand,
+ DataroomBrand,
+ DataroomDocument,
+ Document,
+} from "@prisma/client";
import prisma from "@/lib/prisma";
import { log } from "@/lib/utils";
@@ -206,6 +211,29 @@ export default async function handle(
},
});
+ // Sort documents by name considering the numerical part
+ linkData.dataroom.documents.sort(
+ (
+ a: DataroomDocument & { document: Document },
+ b: DataroomDocument & { document: Document },
+ ) => {
+ const getNumber = (str: string): number => {
+ const match = str.match(/^\d+/);
+ return match ? parseInt(match[0], 10) : 0;
+ };
+
+ const numA = getNumber(a.document.name);
+ const numB = getNumber(b.document.name);
+
+ if (numA !== numB) {
+ return numA - numB;
+ }
+
+ // If numerical parts are the same, fall back to lexicographical order
+ return a.document.name.localeCompare(b.document.name);
+ },
+ );
+
brand = await prisma.dataroomBrand.findFirst({
where: {
dataroomId: linkData.dataroom.id,
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts
index 1bd32367..c5cf1e73 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts
@@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { client } from "@/trigger";
+import { DataroomDocument, Document } from "@prisma/client";
import slugify from "@sindresorhus/slugify";
import { getServerSession } from "next-auth/next";
@@ -77,6 +78,24 @@ export default async function handle(
document: { ...document.document },
}));
+ // Sort documents by name considering the numerical part
+ documentsWithCount.sort((a, b) => {
+ const getNumber = (str: string): number => {
+ const match = str.match(/^\d+/);
+ return match ? parseInt(match[0], 10) : 0;
+ };
+
+ const numA = getNumber(a.document.name);
+ const numB = getNumber(b.document.name);
+
+ if (numA !== numB) {
+ return numA - numB;
+ }
+
+ // If numerical parts are the same, fall back to lexicographical order
+ return a.document.name.localeCompare(b.document.name);
+ });
+
return res.status(200).json(documentsWithCount);
} catch (error) {
console.error("Request error", error);