Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
refactor: function for sort items by index name
Marc Seitze638260577f8
diff --git a/lib/api/links/link-data.ts b/lib/api/links/link-data.tsindex 146e726e..a9150234 100644--- a/lib/api/links/link-data.ts+++ b/lib/api/links/link-data.ts@@ -1,4 +1,5 @@import prisma from "@/lib/prisma";+import { sortItemsByIndexAndName } from "@/lib/utils/sort-items-by-index-name";export async function fetchDataroomLinkData({ linkId }: { linkId: string }) {const linkData = await prisma.link.findUnique({@@ -60,32 +61,10 @@ export async function fetchDataroomLinkData({ linkId }: { linkId: string }) {throw new Error("Dataroom not found");}- // Helper function to extract the numerical part of a string- const getNumber = (str: string): number => {- const match = str.match(/^\d+/);- return match ? parseInt(match[0], 10) : 0;- };-- // Sort documents by orderIndex and/or name considering the numerical part- linkData.dataroom.documents.sort((a, b) => {- // First, compare by orderIndex if both items have it- if (a.orderIndex && b.orderIndex) {- if (a.orderIndex !== b.orderIndex) {- return a.orderIndex - b.orderIndex;- }- }-- // If orderIndex is not available or equal, use name-based sorting- 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);- });+ // Sort documents by index or name+ linkData.dataroom.documents = sortItemsByIndexAndName(+ linkData.dataroom.documents,+ );const brand = await prisma.dataroomBrand.findFirst({where: {diff --git a/lib/utils/sort-items-by-index-name.ts b/lib/utils/sort-items-by-index-name.tsnew file mode 100644index 00000000..7f116f87--- /dev/null+++ b/lib/utils/sort-items-by-index-name.ts@@ -0,0 +1,33 @@+export const sortItemsByIndexAndName = <+ T extends {+ orderIndex: number | null;+ document: { name: string };+ },+>(+ items: T[],+): T[] => {+ // Sort documents by orderIndex or name considering the numerical part+ return items.sort((a, b) => {+ // First, compare by orderIndex if both items have it+ if (a.orderIndex && b.orderIndex) {+ if (a.orderIndex !== b.orderIndex) {+ return a.orderIndex - b.orderIndex;+ }+ }++ // If orderIndex is not available or equal, use name-based sorting+ 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);+ });+};++// Helper function to extract the numerical part of a string+const getNumber = (str: string): number => {+ const match = str.match(/^\d+/);+ return match ? parseInt(match[0], 10) : 0;+};diff --git a/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/documents/index.tsindex 420adf35..627c82fa 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts@@ -7,6 +7,7 @@ import { errorhandler } from "@/lib/errorHandler";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";+import { sortItemsByIndexAndName } from "@/lib/utils/sort-items-by-index-name";export default async function handle(req: NextApiRequest,@@ -72,30 +73,9 @@ export default async function handle(},});- const documentsWithCount = documents.map((document) => ({- ...document,- document: { ...document.document },- }));+ const sortedDocuments = sortItemsByIndexAndName(documents);- // // 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);+ return res.status(200).json(sortedDocuments);} catch (error) {console.error("Request error", error);return resdiff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].tsindex 64f84010..85333504 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].ts@@ -5,6 +5,7 @@ import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";+import { sortItemsByIndexAndName } from "@/lib/utils/sort-items-by-index-name";export default async function handle(req: NextApiRequest,@@ -96,25 +97,9 @@ export default async function handle(},});- // Sort documents by name considering the numerical part- documents.sort((a, b) => {- const getNumber = (str: string): number => {- const match = str.match(/^\d+/);- return match ? parseInt(match[0], 10) : 0;- };+ const sortedDocuments = sortItemsByIndexAndName(documents);- 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(documents);+ return res.status(200).json(sortedDocuments);} catch (error) {console.error("Request error", error);return res