Skip to content

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

Merge branch 'main' into padination/searchDoc

Marc Seitz1cf680691fb7
diff --cc pages/api/teams/[teamId]/documents/index.ts
index 047442e0,8f2f104b..343753fa
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@@ -42,10 -42,6 +42,10 @@@ export default async function handle
const { query, sort } = req.query as { query?: string; sort?: string };
const userId = (session.user as CustomUser).id;
+ const usePagination = !!(query || sort);
- const page = usePagination ? (Number(req.query.page) || 1) : undefined;
- const limit = usePagination ? (Number(req.query.limit) || 10) : undefined;
++ const page = usePagination ? Number(req.query.page) || 1 : undefined;
++ const limit = usePagination ? Number(req.query.limit) || 10 : undefined;
+
try {
const team = await prisma.team.findUnique({
where: {
@@@ -63,43 -59,23 +63,45 @@@
}
let orderBy: Prisma.DocumentOrderByWithRelationInput;
-
- switch (sort) {
- case "createdAt":
- orderBy = { createdAt: "desc" };
- break;
- case "views":
- orderBy = { views: { _count: "desc" } };
- break;
- case "name":
- orderBy = { name: "asc" };
- break;
- case "links":
- orderBy = { links: { _count: "desc" } };
- break;
- default:
- orderBy = { createdAt: "desc" };
++
+ if (query || sort) {
+ switch (sort) {
+ case "createdAt":
+ orderBy = { createdAt: "desc" };
+ break;
+ case "views":
+ orderBy = { views: { _count: "desc" } };
+ break;
+ case "name":
+ orderBy = { name: "asc" };
+ break;
+ case "links":
+ orderBy = { links: { _count: "desc" } };
+ break;
+ default:
+ orderBy = { createdAt: "desc" };
+ }
+ } else {
+ orderBy = { createdAt: "desc" };
}
- const totalDocuments = usePagination ? await prisma.document.count({
- where: {
- teamId: teamId,
- ...(query && {
- name: {
- contains: query,
- mode: "insensitive",
++ const totalDocuments = usePagination
++ ? await prisma.document.count({
++ where: {
++ teamId: teamId,
++ ...(query && {
++ name: {
++ contains: query,
++ mode: "insensitive",
++ },
++ }),
++ ...(!(query || sort) && {
++ folderId: null,
++ }),
+ },
- }),
- ...(!(query || sort) && {
- folderId: null,
- }),
- },
- }) : undefined;
++ })
++ : undefined;
+
const documents = await prisma.document.findMany({
where: {
teamId: teamId,
@@@ -114,28 -90,61 +116,66 @@@
}),
},
orderBy,
+ ...(usePagination && {
+ skip: ((page as number) - 1) * (limit as number),
+ take: limit,
+ }),
include: {
- ...(query && sort === "lastViewed" && {
- views: {
- select: { viewedAt: true },
- orderBy: { viewedAt: "desc" },
- take: 1,
+ folder: {
+ select: {
+ name: true,
+ path: true,
},
- }),
+ },
- ...(sort &&
++ ...(query &&
+ sort === "lastViewed" && {
+ views: {
+ select: { viewedAt: true },
+ orderBy: { viewedAt: "desc" },
+ take: 1,
+ },
+ }),
_count: {
select: { links: true, views: true, versions: true },
},
},
});
- let sortedDocuments = documents;
+ let documentsWithFolderList = documents;
-
++
+ if (query || sort) {
- documentsWithFolderList = await Promise.all(documents.map(async (doc) => {
- const folderNames = [];
- const pathSegments = doc.folder?.path?.split("/") || [];
-
- if (pathSegments.length > 0) {
- const folders = await prisma.folder.findMany({
- where: {
- teamId,
- path: {
- in: pathSegments.map((_, index) =>
- pathSegments.slice(0, index + 1).join("/")
- )
- }
- },
- select: {
- path: true,
- name: true,
- },
- orderBy: {
- path: 'asc'
- }
- });
- folderNames.push(...folders.map(f => f.name));
- }
- return { ...doc, folderList: folderNames };
- }));
++ documentsWithFolderList = await Promise.all(
++ documents.map(async (doc) => {
++ const folderNames = [];
++ const pathSegments = doc.folder?.path?.split("/") || [];
++
++ if (pathSegments.length > 0) {
++ const folders = await prisma.folder.findMany({
++ where: {
++ teamId,
++ path: {
++ in: pathSegments.map((_, index) =>
++ pathSegments.slice(0, index + 1).join("/"),
++ ),
++ },
++ },
++ select: {
++ path: true,
++ name: true,
++ },
++ orderBy: {
++ path: "asc",
++ },
++ });
++ folderNames.push(...folders.map((f) => f.name));
++ }
++ return { ...doc, folderList: folderNames };
++ }),
++ );
+ }
- // Sort after we have the complete list
- if (sort === "lastViewed") {
+ if ((query || sort) && sort === "lastViewed") {
- sortedDocuments = documents.sort((a, b) => {
+ documentsWithFolderList = documentsWithFolderList.sort((a, b) => {
const aLastView = a.views[0]?.viewedAt;
const bLastView = b.views[0]?.viewedAt;
@@@ -146,23 -155,13 +186,23 @@@
});
}
- if (sort === "name") {
+ if ((query || sort) && sort === "name") {
- sortedDocuments = documents.sort((a, b) =>
+ documentsWithFolderList = documentsWithFolderList.sort((a, b) =>
a.name.toLowerCase().localeCompare(b.name.toLowerCase()),
);
}
- return res.status(200).json(documentsWithFolderList);
+ return res.status(200).json({
- documents: sortedDocuments,
++ documents: documentsWithFolderList,
+ ...(usePagination && {
+ pagination: {
+ total: totalDocuments,
+ pages: Math.ceil(totalDocuments! / limit!),
+ currentPage: page,
+ pageSize: limit,
+ },
+ }),
+ });
} catch (error) {
errorhandler(error, res);
}