Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
refactor: simplify document.update handler by reusing createNewDocumentVersion
Cursor Agentef68f670b915
diff --git a/lib/documents/create-document.ts b/lib/documents/create-document.tsindex 00c8907b..aad7a2b4 100644--- a/lib/documents/create-document.ts+++ b/lib/documents/create-document.ts@@ -100,21 +100,28 @@ export const createNewDocumentVersion = async ({documentId,teamId,numPages,+ token,}: {documentData: DocumentData;documentId: string;teamId: string;numPages?: number;+ token?: string;}) => {try {const documentIdParsed = z.string().cuid().parse(documentId);+ // Use absolute URL when a token is provided (server-side / webhook context),+ // otherwise use a relative URL (client-side context).+ const baseUrl = token ? process.env.NEXT_PUBLIC_BASE_URL : "";+const response = await fetch(- `/api/teams/${teamId}/documents/${documentIdParsed}/versions`,+ `${baseUrl}/api/teams/${teamId}/documents/${documentIdParsed}/versions`,{method: "POST",headers: {"Content-Type": "application/json",+ ...(token ? { Authorization: `Bearer ${token}` } : {}),},body: JSON.stringify({url: documentData.key,diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.tsindex bd4826dc..05c1371b 100644--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts@@ -4,6 +4,7 @@ import { isTeamPausedById } from "@/ee/features/billing/cancellation/lib/is-teamimport { authOptions } from "@/pages/api/auth/[...nextauth]";import { getServerSession } from "next-auth/next";+import { hashToken } from "@/lib/api/auth/token";import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";import prisma from "@/lib/prisma";import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";@@ -20,16 +21,41 @@ export default async function handle() {if (req.method === "POST") {// POST /api/teams/:teamId/documents/:id/versions- const session = await getServerSession(req, res, authOptions);- if (!session) {- return res.status(401).end("Unauthorized");- }-- // get document id from query paramsconst { teamId, id: documentId } = req.query as {teamId: string;id: string;};++ // Check for API token first, then fall back to session auth+ const authHeader = req.headers.authorization;+ let userId: string;++ if (authHeader?.startsWith("Bearer ")) {+ const token = authHeader.replace("Bearer ", "");+ const hashedToken = hashToken(token);++ const restrictedToken = await prisma.restrictedToken.findUnique({+ where: { hashedKey: hashedToken },+ select: { userId: true, teamId: true },+ });++ if (!restrictedToken) {+ return res.status(401).json({ error: "Unauthorized" });+ }++ if (restrictedToken.teamId !== teamId) {+ return res.status(401).json({ error: "Unauthorized" });+ }++ userId = restrictedToken.userId;+ } else {+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }+ userId = (session.user as CustomUser).id;+ }+// Validate request body using Zod schema for securityconst validationResult = await documentUploadSchema.safeParseAsync({...req.body,@@ -50,8 +76,6 @@ export default async function handle(const { url, type, numPages, storageType, contentType, fileSize } =validationResult.data;- const userId = (session.user as CustomUser).id;-try {const team = await prisma.team.findUnique({where: {diff --git a/pages/api/webhooks/services/[...path]/index.ts b/pages/api/webhooks/services/[...path]/index.tsindex ee8dd1a1..f2a70235 100644--- a/pages/api/webhooks/services/[...path]/index.ts+++ b/pages/api/webhooks/services/[...path]/index.ts@@ -8,20 +8,15 @@ import { waitUntil } from "@vercel/functions";import { z } from "zod";import { hashToken } from "@/lib/api/auth/token";-import { createDocument } from "@/lib/documents/create-document";-import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";+import {+ createDocument,+ createNewDocumentVersion,+} from "@/lib/documents/create-document";import { putFileServer } from "@/lib/files/put-file-server";import { newId } from "@/lib/id-helper";import { extractTeamId, isValidWebhookId } from "@/lib/incoming-webhooks";import prisma from "@/lib/prisma";import { ratelimit } from "@/lib/redis";-import {- convertCadToPdfTask,- convertFilesToPdfTask,- convertKeynoteToPdfTask,-} from "@/lib/trigger/convert-files";-import { processVideo } from "@/lib/trigger/optimize-video-files";-import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";import {convertDataUrlToBuffer,generateEncrpytedPassword,@@ -32,7 +27,6 @@ import {getExtensionFromContentType,getSupportedContentType,} from "@/lib/utils/get-content-type";-import { conversionQueue } from "@/lib/utils/trigger-utils";import { sendLinkCreatedWebhook } from "@/lib/webhook/triggers/link-created";import { webhookFileUrlSchema } from "@/lib/zod/url-validation";@@ -250,6 +244,7 @@ export default async function incomingWebhookHandler(return await handleDocumentUpdate(validatedData,incomingWebhook.teamId,+ token,res,);} else if (validatedData.resourceType === "link.create") {@@ -698,24 +693,18 @@ async function handleDocumentCreate(}/**- * Handle document.update resource type – creates a new version for an existing document+ * Handle document.update resource type – creates a new version for an existing document.+ * Delegates version creation and document processing to the versions API endpoint+ * via createNewDocumentVersion.*/async function handleDocumentUpdate(data: z.infer<typeof DocumentUpdateSchema>,teamId: string,+ token: string,res: NextApiResponse,) {const { documentId, fileUrl, contentType } = data;- // Check if team is paused- const teamIsPaused = await isTeamPausedById(teamId);- if (teamIsPaused) {- return res.status(403).json({- error:- "Team is currently paused. Document updates are not available.",- });- }-// Check if the content type is supportedconst supportedContentType = getSupportedContentType(contentType);if (!supportedContentType) {@@ -728,16 +717,7 @@ async function handleDocumentUpdate(id: documentId,teamId: teamId,},- select: {- id: true,- name: true,- advancedExcelEnabled: true,- versions: {- orderBy: { createdAt: "desc" },- take: 1,- select: { versionNumber: true },- },- },+ select: { id: true, name: true },});if (!document) {@@ -746,16 +726,6 @@ async function handleDocumentUpdate(.json({ error: "Document not found or not associated with this team" });}- // Fetch the team plan for queue configuration- const team = await prisma.team.findUnique({- where: { id: teamId },- select: { plan: true },- });-- if (!team) {- return res.status(404).json({ error: "Team not found" });- }-// Fetch file from URLconst response = await fetch(fileUrl);if (!response.ok) {@@ -817,174 +787,43 @@ async function handleDocumentUpdate(return res.status(500).json({ error: "Failed to save file to storage" });}- const type = supportedContentType;-- // Determine new version number- const currentVersionNumber = document.versions.length > 0- ? document.versions[0].versionNumber- : 1;-- // Create a new document version- const version = await prisma.documentVersion.create({- data: {- documentId: documentId,- file: fileData,- originalFile: fileData,- type: type,- storageType,- numPages: document.advancedExcelEnabled ? 1 : 1,- isPrimary: true,- versionNumber: currentVersionNumber + 1,- contentType,- fileSize: fileBuffer.byteLength,- },- });-- // Turn off isPrimary flag for all other versions- await prisma.documentVersion.updateMany({- where: {- documentId: documentId,- id: { not: version.id },- },- data: {- isPrimary: false,- },- });-- // Update the document's file and contentType to reflect the new version- await prisma.document.update({- where: { id: documentId },- data: {- file: fileData,- originalFile: fileData,- contentType: contentType,- type: type,- },- });-- // Trigger appropriate conversion tasks based on document type- if (- type === "slides" &&- (contentType === "application/vnd.apple.keynote" ||- contentType === "application/x-iwork-keynote-sffkey")- ) {- await convertKeynoteToPdfTask.trigger(- {- documentId: document.id,- documentVersionId: version.id,- teamId,- },- {- idempotencyKey: `${teamId}-${version.id}-keynote`,- tags: [- `team_${teamId}`,- `document_${document.id}`,- `version:${version.id}`,- ],- queue: conversionQueue(team.plan),- concurrencyKey: teamId,- },- );- } else if (type === "docs" || type === "slides") {- await convertFilesToPdfTask.trigger(- {- documentId: document.id,- documentVersionId: version.id,- teamId,- },- {- idempotencyKey: `${teamId}-${version.id}-docs`,- tags: [- `team_${teamId}`,- `document_${document.id}`,- `version:${version.id}`,- ],- queue: conversionQueue(team.plan),- concurrencyKey: teamId,- },- );- }-- if (type === "cad") {- await convertCadToPdfTask.trigger(- {- documentId: document.id,- documentVersionId: version.id,- teamId,- },- {- idempotencyKey: `${teamId}-${version.id}-cad`,- tags: [- `team_${teamId}`,- `document_${document.id}`,- `version:${version.id}`,- ],- queue: conversionQueue(team.plan),- concurrencyKey: teamId,- },- );- }-- if (type === "pdf") {- await convertPdfToImageRoute.trigger(- {- documentId: document.id,- documentVersionId: version.id,- teamId,- versionNumber: version.versionNumber,- },- {- idempotencyKey: `${teamId}-${version.id}`,- tags: [- `team_${teamId}`,- `document_${document.id}`,- `version:${version.id}`,- ],- queue: conversionQueue(team.plan),- concurrencyKey: teamId,- },- );- }-- if (- type === "video" &&- contentType !== "video/mp4" &&- contentType?.startsWith("video/")- ) {- await processVideo.trigger(- {- videoUrl: fileData,- teamId,- docId: fileData.split("/")[1],- documentVersionId: version.id,+ // Create a new document version via the shared helper.+ // This handles version creation, primary flag management, and triggers+ // all document processing (pdf-to-image, docs/slides conversion, video, etc.)+ try {+ const versionResponse = await createNewDocumentVersion({+ documentData: {+ name: fileName,+ key: fileData,+ storageType: storageType,+ contentType: contentType,+ supportedFileType: supportedContentType,fileSize: fileBuffer.byteLength,},- {- idempotencyKey: `${teamId}-${version.id}`,- tags: [- `team_${teamId}`,- `document_${document.id}`,- `version:${version.id}`,- ],- queue: conversionQueue(team.plan),- concurrencyKey: teamId,- },- );- }+ documentId: documentId,+ teamId: teamId,+ numPages: 1,+ token: token,+ });- if (type === "sheet" && document.advancedExcelEnabled) {- await copyFileToBucketServer({- filePath: version.file,- storageType: version.storageType,- teamId,+ if (!versionResponse.ok) {+ const errorBody = await versionResponse.json().catch(() => ({}));+ return res.status(versionResponse.status).json({+ error: "Failed to create document version",+ details: errorBody,+ });+ }++ return res.status(200).json({+ message: "Document version created successfully",+ documentId: document.id,});+ } catch (error) {+ console.error("Document update error:", error);+ return res+ .status(500)+ .json({ error: "Failed to create document version" });}-- return res.status(200).json({- message: "Document version created successfully",- documentId: document.id,- versionNumber: version.versionNumber,- });}/**