Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: update incoming webhooks
Marc Seitz595dcf395463
diff --git a/pages/api/webhooks/services/[...path]/index.ts b/pages/api/webhooks/services/[...path]/index.tsindex 2dea78cf..33344021 100644--- a/pages/api/webhooks/services/[...path]/index.ts+++ b/pages/api/webhooks/services/[...path]/index.ts@@ -1,6 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { waitUntil } from "@vercel/functions";+import { z } from "zod";import { hashToken } from "@/lib/api/auth/token";import { createDocument } from "@/lib/documents/create-document";@@ -8,14 +9,69 @@ import { putFileServer } from "@/lib/files/put-file-server";import { extractTeamId, isValidWebhookId } from "@/lib/incoming-webhooks";import prisma from "@/lib/prisma";import { ratelimit } from "@/lib/redis";+import { generateEncrpytedPassword } from "@/lib/utils";import { getSupportedContentType } from "@/lib/utils/get-content-type";export const config = {// in order to enable `waitUntil` functionsupportsResponseStreaming: true,- maxDuration: 60,+ maxDuration: 120,};+// Define a common link schema to reuse+const LinkSchema = z.object({+ name: z.string().optional(),+ domain: z.string().optional(),+ slug: z.string().optional(),+ password: z.string().optional(),+ expiresAt: z.string().optional(), // ISO string date+ emailProtected: z.boolean().optional(),+ emailAuthenticated: z.boolean().optional(),+ allowDownload: z.boolean().optional(),+ enableNotification: z.boolean().optional(),+ enableFeedback: z.boolean().optional(),+ enableScreenshotProtection: z.boolean().optional(),+ audienceType: z.enum(["GENERAL", "GROUP", "TEAM"]).optional(),+ groupId: z.string().optional(),+ allowList: z.array(z.string()).optional(),+});++// Define validation schemas for different resource types+const BaseSchema = z.object({+ resourceType: z.enum(["document.create", "link.create", "dataroom.create"]),+});++const DocumentCreateSchema = BaseSchema.extend({+ resourceType: z.literal("document.create"),+ fileUrl: z.string().url(),+ name: z.string(),+ contentType: z.string(),+ dataroomId: z.string().optional(),+ createLink: z.boolean().optional().default(false),+ link: LinkSchema.optional(),+});++const LinkCreateSchema = BaseSchema.extend({+ resourceType: z.literal("link.create"),+ targetId: z.string(),+ linkType: z.enum(["DOCUMENT_LINK", "DATAROOM_LINK"]),+ link: LinkSchema,+});++const DataroomCreateSchema = BaseSchema.extend({+ resourceType: z.literal("dataroom.create"),+ name: z.string(),+ description: z.string().optional(),+ createLink: z.boolean().optional().default(false),+ link: LinkSchema.optional(),+});++const RequestBodySchema = z.discriminatedUnion("resourceType", [+ DocumentCreateSchema,+ LinkCreateSchema,+ DataroomCreateSchema,+]);+export default async function incomingWebhookHandler(req: NextApiRequest,res: NextApiResponse,@@ -106,105 +162,458 @@ export default async function incomingWebhookHandler(return res.status(404).json({ error: "Webhook not found" });}- // 3. Validate request body- const { fileUrl, name, contentType, dataroomId, createLink } = req.body;+ // Validate request body against the schema+ const validationResult = RequestBodySchema.safeParse(req.body);+ if (!validationResult.success) {+ return res.status(400).json({+ error: "Invalid request body",+ details: validationResult.error.format(),+ });+ }++ const validatedData = validationResult.data;++ // Handle different resource types+ if (validatedData.resourceType === "document.create") {+ return await handleDocumentCreate(+ validatedData,+ incomingWebhook.teamId,+ token,+ res,+ );+ } else if (validatedData.resourceType === "link.create") {+ return await handleLinkCreate(+ validatedData,+ incomingWebhook.teamId,+ token,+ res,+ );+ } else if (validatedData.resourceType === "dataroom.create") {+ return await handleDataroomCreate(+ validatedData,+ incomingWebhook.teamId,+ token,+ res,+ );+ }++ // This shouldn't be reached due to the validation schema, but just in case+ return res.status(400).json({ error: "Invalid resource type" });+ } catch (error) {+ console.error("Webhook error:", error);+ return res.status(500).json({ error: "Internal server error" });+ }+}++/**+ * Handle document.create resource type+ */+async function handleDocumentCreate(+ data: z.infer<typeof DocumentCreateSchema>,+ teamId: string,+ token: string,+ res: NextApiResponse,+) {+ const { fileUrl, name, contentType, dataroomId, createLink, link } = data;++ // Check if the content type is supported+ const supportedContentType = getSupportedContentType(contentType);+ if (!supportedContentType) {+ return res.status(400).json({ error: "Unsupported content type" });+ }++ if (dataroomId) {+ // Verify dataroom exists and belongs to team+ const dataroom = await prisma.dataroom.findUnique({+ where: {+ id: dataroomId,+ teamId: teamId,+ },+ });++ if (!dataroom) {+ return res.status(400).json({ error: "Invalid dataroom ID" });+ }+ }++ // If custom domain and slug are provided, validate them+ if (createLink && link?.domain && link?.slug) {+ // Check if domain exists+ const domain = await prisma.domain.findUnique({+ where: {+ slug: link.domain,+ teamId: teamId,+ },+ });- if (!fileUrl) {- return res.status(400).json({ error: "File URL is required" });+ if (!domain) {+ return res+ .status(400)+ .json({ error: "Domain not found or not associated with this team" });}- if (dataroomId) {- // Verify dataroom exists and belongs to team- const dataroom = await prisma.dataroom.findUnique({+ // Check if the slug is already in use with this domain+ const existingLink = await prisma.link.findUnique({+ where: {+ domainSlug_slug: {+ slug: link.slug,+ domainSlug: link.domain,+ },+ },+ });++ if (existingLink) {+ return res+ .status(400)+ .json({ error: "The link with this domain and slug already exists" });+ }+ }++ // 4. Fetch file from URL+ const response = await fetch(fileUrl);+ if (!response.ok) {+ return res.status(400).json({ error: "Failed to fetch file from URL" });+ }++ // 5. Convert to buffer+ const fileBuffer = Buffer.from(await response.arrayBuffer());++ console.log("Uploading file to storage", teamId, name, contentType);++ // Upload the file to storage+ const { type: storageType, data: fileData } = await putFileServer({+ file: {+ name: name,+ type: contentType,+ buffer: fileBuffer,+ },+ teamId: teamId,+ restricted: false, // allows all supported file types+ });++ if (!fileData || !storageType) {+ return res.status(500).json({ error: "Failed to save file to storage" });+ }++ // 6. Create document using our service+ // Note: The createDocument function doesn't accept linkData in its parameters+ // so we will just pass createLink flag+ const documentCreationResponse = await createDocument({+ documentData: {+ name: name,+ key: fileData,+ storageType: storageType,+ contentType: contentType,+ supportedFileType: supportedContentType,+ fileSize: fileBuffer.byteLength,+ },+ teamId: teamId,+ numPages: 1,+ token: token,+ createLink: createLink,+ });++ if (!documentCreationResponse.ok) {+ return res.status(500).json({ error: "Failed to create document" });+ }++ const document = await documentCreationResponse.json();++ // If we need to customize the link, update it after creation+ if (createLink && document.links && document.links.length > 0 && link) {+ const linkId = document.links[0].id;++ // Process fields for link update+ const hashedPassword = link.password+ ? await generateEncrpytedPassword(link.password)+ : null;++ const expiresAtDate = link.expiresAt ? new Date(link.expiresAt) : null;+ const isGroupAudience = link.audienceType === "GROUP";++ let domainId = null;+ if (link.domain) {+ const domain = await prisma.domain.findUnique({where: {- id: dataroomId,- teamId: incomingWebhook.teamId,+ slug: link.domain,+ teamId: teamId,},+ select: { id: true },});-- if (!dataroom) {- return res.status(400).json({ error: "Invalid dataroom ID" });- }+ domainId = domain?.id || null;}- // Check if the content type is supported- const supportedContentType = getSupportedContentType(contentType);- if (!supportedContentType) {- return res.status(400).json({ error: "Unsupported content type" });+ // Update the link with custom settings+ await prisma.link.update({+ where: { id: linkId, teamId: teamId },+ data: {+ name: link.name,+ password: hashedPassword,+ expiresAt: expiresAtDate,+ domainId: domainId,+ domainSlug: link.domain || null,+ slug: link.slug || null,+ emailProtected: link.emailProtected,+ emailAuthenticated: link.emailAuthenticated,+ allowDownload: link.allowDownload,+ enableNotification: link.enableNotification,+ enableFeedback: link.enableFeedback,+ enableScreenshotProtection: link.enableScreenshotProtection,+ audienceType: link.audienceType,+ groupId: isGroupAudience ? link.groupId : null,+ allowList: link.allowList,+ },+ });+ }++ // If dataroomId was provided, create the relationship+ if (dataroomId) {+ await prisma.dataroomDocument.create({+ data: {+ dataroomId,+ documentId: document.id,+ },+ });+ }++ return res.status(200).json({+ message: `Document created successfully${+ dataroomId ? ` and added to dataroom` : ""+ }`,+ documentId: document.id,+ dataroomId: dataroomId ?? undefined,+ linkId: document.links?.[0]?.id,+ });+}++/**+ * Handle link.create resource type+ */+async function handleLinkCreate(+ data: z.infer<typeof LinkCreateSchema>,+ teamId: string,+ token: string,+ res: NextApiResponse,+) {+ const { targetId, linkType, link } = data;++ // Validate target exists and belongs to the team+ if (linkType === "DOCUMENT_LINK") {+ const document = await prisma.document.findUnique({+ where: {+ id: targetId,+ teamId: teamId,+ },+ });++ if (!document) {+ return res+ .status(400)+ .json({ error: "Document not found or not associated with this team" });}+ } else if (linkType === "DATAROOM_LINK") {+ const dataroom = await prisma.dataroom.findUnique({+ where: {+ id: targetId,+ teamId: teamId,+ },+ });- // 4. Fetch file from URL- const response = await fetch(fileUrl);- if (!response.ok) {- return res.status(400).json({ error: "Failed to fetch file from URL" });+ if (!dataroom) {+ return res+ .status(400)+ .json({ error: "Dataroom not found or not associated with this team" });}+ }++ // If domain and slug are provided, validate them+ let domainId = null;+ if (link.domain && link.slug) {+ // Check if domain exists+ const domain = await prisma.domain.findUnique({+ where: {+ slug: link.domain,+ teamId: teamId,+ },+ select: { id: true },+ });- // 5. Convert to buffer- const fileBuffer = Buffer.from(await response.arrayBuffer());+ if (!domain) {+ return res+ .status(400)+ .json({ error: "Domain not found or not associated with this team" });+ }- console.log(- "Uploading file to storage",- incomingWebhook.teamId,- name,- contentType,- );+ domainId = domain.id;- // Upload the file to storage- const { type: storageType, data } = await putFileServer({- file: {- name: name,- type: contentType,- buffer: fileBuffer,+ // Check if the slug is already in use with this domain+ const existingLink = await prisma.link.findUnique({+ where: {+ domainSlug_slug: {+ slug: link.slug,+ domainSlug: link.domain,+ },},- teamId: incomingWebhook.teamId,- restricted: false, // allows all supported file types});- if (!data || !storageType) {- return res.status(500).json({ error: "Failed to save file to storage" });+ if (existingLink) {+ return res+ .status(400)+ .json({ error: "The link with this domain and slug already exists" });}+ }++ // Create the link+ try {+ // Hash password if provided+ const hashedPassword = link.password+ ? await generateEncrpytedPassword(link.password)+ : null;++ const expiresAtDate = link.expiresAt ? new Date(link.expiresAt) : null;++ const isGroupAudience = link.audienceType === "GROUP";- // 6. Create document using our service- const documentCreationResponse = await createDocument({- documentData: {- name: name,- key: data,- storageType: storageType,- contentType: contentType,- supportedFileType: supportedContentType,- fileSize: fileBuffer.byteLength,+ const newLink = await prisma.link.create({+ data: {+ documentId: linkType === "DOCUMENT_LINK" ? targetId : null,+ dataroomId: linkType === "DATAROOM_LINK" ? targetId : null,+ linkType,+ teamId,+ name: link.name,+ password: hashedPassword,+ domainId: domainId,+ domainSlug: link.domain || null,+ slug: link.slug || null,+ expiresAt: expiresAtDate,+ emailProtected: link.emailProtected,+ emailAuthenticated: link.emailAuthenticated,+ allowDownload: link.allowDownload,+ enableNotification: link.enableNotification,+ enableFeedback: link.enableFeedback,+ enableScreenshotProtection: link.enableScreenshotProtection,+ audienceType: link.audienceType,+ groupId: isGroupAudience ? link.groupId : null,+ allowList: link.allowList,+ },+ });++ return res.status(200).json({+ message: "Link created successfully",+ linkId: newLink.id,+ targetId,+ linkType,+ linkUrl:+ domainId && link.domain && link.slug+ ? `https://${newLink.domainSlug}/${newLink.slug}`+ : `${process.env.NEXT_PUBLIC_MARKETING_URL}/link/${newLink.slug}`,+ });+ } catch (error) {+ console.error("Link creation error:", error);+ return res.status(500).json({ error: "Failed to create link" });+ }+}++/**+ * Handle dataroom.create resource type+ */+async function handleDataroomCreate(+ data: z.infer<typeof DataroomCreateSchema>,+ teamId: string,+ token: string,+ res: NextApiResponse,+) {+ const { name, description, createLink, link } = data;++ // If custom domain and slug are provided for link, validate them+ let domainId = null;+ if (createLink && link?.domain && link?.slug) {+ // Check if domain exists+ const domain = await prisma.domain.findUnique({+ where: {+ slug: link.domain,+ teamId: teamId,},- teamId: incomingWebhook.teamId,- numPages: 1,- token: token,- createLink: createLink,});- if (!documentCreationResponse.ok) {- return res.status(500).json({ error: "Failed to create document" });+ if (!domain) {+ return res+ .status(400)+ .json({ error: "Domain not found or not associated with this team" });}- const document = await documentCreationResponse.json();+ domainId = domain.id;- // If dataroomId was provided, create the relationship- if (dataroomId) {- await prisma.dataroomDocument.create({- data: {- dataroomId,- documentId: document.id,+ // Check if the slug is already in use with this domain+ const existingLink = await prisma.link.findUnique({+ where: {+ domainSlug_slug: {+ slug: link.slug,+ domainSlug: link.domain,},- });+ },+ });++ if (existingLink) {+ return res+ .status(400)+ .json({ error: "The link with this domain and slug already exists" });}+ }++ // Create the dataroom+ try {+ // Create dataroom with link if requested+ let createData: any = {+ name,+ description,+ teamId,+ };++ if (createLink && link) {+ const isGroupAudience = link.audienceType === "GROUP";+ const hashedPassword = link.password+ ? await generateEncrpytedPassword(link.password)+ : null;+ const expiresAtDate = link.expiresAt ? new Date(link.expiresAt) : null;++ createData.links = {+ create: {+ name: link.name,+ teamId,+ linkType: "DATAROOM_LINK",+ domainId: domainId,+ domainSlug: link.domain || null,+ slug: link.slug || null,+ password: hashedPassword,+ expiresAt: expiresAtDate,+ emailProtected: link.emailProtected,+ emailAuthenticated: link.emailAuthenticated,+ allowDownload: link.allowDownload,+ enableNotification: link.enableNotification,+ enableFeedback: link.enableFeedback,+ enableScreenshotProtection: link.enableScreenshotProtection,+ audienceType: link.audienceType,+ groupId: isGroupAudience ? link.groupId : null,+ allowList: link.allowList,+ },+ };+ }++ const dataroom = await prisma.dataroom.create({+ data: createData,+ include: {+ links: createLink, // Only include links if we're creating one+ },+ });return res.status(200).json({- message: `Document created successfully${- dataroomId ? ` and added to dataroom` : ""- }`,- documentId: document.id,- dataroomId: dataroomId ?? undefined,+ message: "Dataroom created successfully",+ dataroomId: dataroom.id,+ linkId: dataroom.links?.[0]?.id,});} catch (error) {- console.error("Webhook error:", error);- return res.status(500).json({ error: "Internal server error" });+ console.error("Dataroom creation error:", error);+ return res.status(500).json({ error: "Failed to create dataroom" });}}