Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: improve export for document visits
Marc Seitz8ad40ce70ae0
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsxindex bbfe114b..8bc09f18 100644--- a/components/documents/document-header.tsx+++ b/components/documents/document-header.tsx@@ -294,40 +294,8 @@ export default function DocumentHeader({}const data = await response.json();- // Converting the json Array into CSV without using parser.- const csvString = [- [- "Viewed at",- "Name",- "Email",- "Link Name",- "Total Visit Duration (s)",- "Total Document Completion (%)",- "Document version",- "Downloaded at",- "Verified",- "Agreement accepted",- "Viewed from dataroom",- ],- ...data.visits.map((item: any) => [- item.viewedAt,- item.viewerName,- item.viewerEmail,- item.linkName,- item.totalVisitDuration / 1000.0,- item.visitCompletion,- item.documentVersion,- item.downloadedAt,- item.verified,- item.agreement,- item.dataroom,- ]),- ]- .map((row) => row.join(","))- .join("\n");-- // Creating csv as per the time stamp.- const blob = new Blob([csvString], { type: "text/csv;charset=utf-8;" });+ // Create and download the CSV file+ const blob = new Blob([data.visits], { type: "text/csv;charset=utf-8;" });const url = URL.createObjectURL(blob);const link = window.document.createElement("a");link.href = url;@@ -339,6 +307,7 @@ export default function DocumentHeader({link.click();window.document.body.removeChild(link);URL.revokeObjectURL(url);+toast.success("CSV file downloaded successfully");} catch (error) {console.error("Error:", error);diff --git a/pages/api/teams/[teamId]/documents/[id]/export-visits.ts b/pages/api/teams/[teamId]/documents/[id]/export-visits.tsindex 6e5c905b..12bcbba0 100644--- a/pages/api/teams/[teamId]/documents/[id]/export-visits.ts+++ b/pages/api/teams/[teamId]/documents/[id]/export-visits.ts@@ -78,7 +78,24 @@ export default async function handler(where: { documentId: docId },include: {link: { select: { name: true } },- agreementResponse: true,+ agreementResponse: {+ include: {+ agreement: {+ select: {+ name: true,+ links: {+ select: {+ id: true,+ },+ take: 1,+ },+ },+ },+ },+ },+ },+ orderBy: {+ viewedAt: "desc",},});@@ -86,57 +103,82 @@ export default async function handler(return res.status(404).end("Document has no views");}- // Fetching durations from tinyBird function- const durationsPromises = views?.map((view) =>- getViewPageDuration({- documentId: docId,- viewId: view.id,- since: 0,- }),+ // Create CSV rows array starting with headers+ const csvRows: string[] = [];+ csvRows.push(+ [+ "Viewed at",+ "Name",+ "Email",+ "Link Name",+ "Total Visit Duration (s)",+ "Total Document Completion (%)",+ "Document version",+ "Downloaded at",+ "Verified",+ "Agreement Accepted",+ "Agreement Name",+ "Agreement Content",+ "Agreement Accepted At",+ "Viewed from dataroom",+ ].join(","),);- const durations = await Promise.all(durationsPromises);+ // Fetch all durations in parallel+ const durations = await Promise.all(+ views.map((view) =>+ getViewPageDuration({+ documentId: docId,+ viewId: view.id,+ since: 0,+ }),+ ),+ );- const exportData = views?.map((view: any, index: number) => {- // Identifying the document version as per the time we Viewed it.+ // Process each view and add to CSV rows+ views.forEach((view, index) => {const relevantDocumentVersion = document.versions.find((version) => version.createdAt <= view.viewedAt,);const numPages =relevantDocumentVersion?.numPages || document.numPages || 0;-- // Calculating the completion rate in percentage.const completionRate = numPages? (durations[index].data.length / numPages) * 100: 0;- return {- viewedAt: view.viewedAt.toISOString(),- viewerName: view.viewerName || "NaN", // If the value is not available we are showing NaN as per csv- viewerEmail: view.viewerEmail || "NaN",- linkName: view.link?.name || "NaN",- totalVisitDuration: durations[index].data.reduce(- (total, data) => total + data.sum_duration,- 0,- ),- visitCompletion: completionRate.toFixed(2) + "%",- documentVersion:+ const totalDuration = durations[index].data.reduce(+ (total, data) => total + data.sum_duration,+ 0,+ );++ csvRows.push(+ [+ view.viewedAt.toISOString(),+ view.viewerName || "NaN",+ view.viewerEmail || "NaN",+ view.link?.name || "NaN",+ (totalDuration / 1000.0).toFixed(1),+ completionRate.toFixed(2) + "%",relevantDocumentVersion?.versionNumber ||- document.versions[0]?.versionNumber ||- "NaN",- downloadedAt: view.downloadedAt- ? view.downloadedAt.toISOString()- : "NaN",- verified: view.verified ? "Yes" : "No",- agreement: view.agreementResponse ? "Yes" : "NaN",- dataroom: view.dataroomId ? "Yes" : "No",- };+ document.versions[0]?.versionNumber ||+ "NaN",+ view.downloadedAt ? view.downloadedAt.toISOString() : "NaN",+ view.verified ? "Yes" : "No",+ view.agreementResponse ? "Yes" : "NaN",+ view.agreementResponse?.agreement.name || "NaN",+ view.agreementResponse?.agreement.links[0]?.id+ ? `${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${view.agreementResponse?.agreement.links[0]?.id}`+ : "NaN",+ view.agreementResponse?.createdAt.toISOString() || "NaN",+ view.dataroomId ? "Yes" : "No",+ ].join(","),+ );});return res.status(200).json({documentName: document.name,- visits: exportData,+ visits: csvRows.join("\n"),});} catch (error) {console.error(error);