Skip to content

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

11.6 KB
Raw Blame
model Conversation {
  id        String  @id @default(cuid())
  title     String? // Optional title for the conversation
  isEnabled Boolean @default(true)

  // Visibility control
  visibilityMode ConversationVisibility @default(PRIVATE)

  // Core relationships
  dataroomId String
  dataroom   Dataroom @relation(fields: [dataroomId], references: [id], onDelete: Cascade)

  // Optional attachments
  dataroomDocumentId    String?
  dataroomDocument      DataroomDocument? @relation(fields: [dataroomDocumentId], references: [id], onDelete: SetNull)
  documentVersionNumber Int? // Optional document version number reference
  documentPageNumber    Int? // Optional document page number reference

  // Optional link relationship
  linkId String?
  link   Link?   @relation(fields: [linkId], references: [id], onDelete: SetNull)

  // Optional viewer group relationship
  viewerGroupId String?
  viewerGroup   ViewerGroup? @relation(fields: [viewerGroupId], references: [id], onDelete: SetNull)

  // Buy-side Q&A: an admin-originated question that this thread answers.
  // When set, `origin` is "admin" and `recipientViewerId` identifies the
  // single seller who answers in this private thread. Visitor-originated
  // threads keep questionId/recipientViewerId null and origin "visitor".
  questionId String?
  question   DataroomQuestion? @relation(fields: [questionId], references: [id], onDelete: Cascade)

  // Origin of the conversation: "visitor" (default) or "admin".
  // Plain string validated by zod; intentionally not a DB enum.
  origin String @default("visitor")

  // Admin-only triage metadata, never returned to visitors.
  category String?
  priority String? // "urgent" | "high" | "medium" | "low" | "none"

  // The single viewer expected to answer an admin-origin question. NULL for
  // visitor-origin threads. Combined with questionId this enforces one thread
  // per (question, seller) via @@unique below.
  recipientViewerId String?
  recipientViewer   Viewer? @relation(name: "ConversationRecipient", fields: [recipientViewerId], references: [id], onDelete: SetNull)

  // Original view that initiated the conversation (for reference)
  initialViewId String?
  initialView   View?   @relation(name: "initialView", fields: [initialViewId], references: [id], onDelete: SetNull)

  teamId String
  team   Team   @relation(fields: [teamId], references: [id], onDelete: Cascade)

  // Track all views that accessed this conversation
  views ConversationView[]

  // Track participants including the owner
  participants ConversationParticipant[]

  // Track conversations
  messages      Message[]
  lastMessageAt DateTime? // Last message timestamp

  // Published FAQs created from this conversation
  faqItems DataroomFaqItem[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  // One private thread per admin question + recipient seller. Visitor-origin
  // rows keep both columns NULL; Postgres treats NULLs as distinct so they
  // never collide.
  @@unique([questionId, recipientViewerId])
  @@index([dataroomId])
  @@index([dataroomDocumentId])
  @@index([linkId])
  @@index([teamId])
  @@index([viewerGroupId])
  @@index([initialViewId])
  @@index([questionId])
  @@index([recipientViewerId])
}

// An admin-authored due-diligence question. It fans out into one private
// Conversation per answering viewer (see Conversation.questionId). Stages
// (pre-LOI vs DD) are expressed through link/group assignments plus the
// optional `category` field.
model DataroomQuestion {
  id      String @id @default(cuid())
  content String @db.Text

  // Optional categorization / triage metadata. Plain strings validated by zod.
  category   String?
  priority   String? // "high" | "medium" | "low" | "none"
  status     String  @default("unanswered") // see question zod schema for allowed values
  orderIndex Int?

  // Groups questions created together via a single import.
  importBatchId String?

  dataroomId String
  dataroom   Dataroom @relation(fields: [dataroomId], references: [id], onDelete: Cascade)

  teamId String
  team   Team   @relation(fields: [teamId], references: [id], onDelete: Cascade)

  // Author of the question; used as the sender of the seeded team message.
  createdByUserId String?
  createdByUser   User?   @relation(fields: [createdByUserId], references: [id], onDelete: SetNull)

  // Optional document context the question relates to.
  dataroomDocumentId String?
  dataroomDocument   DataroomDocument? @relation(fields: [dataroomDocumentId], references: [id], onDelete: SetNull)
  documentPageNumber Int?

  assignments   DataroomQuestionAssignment[]
  conversations Conversation[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([dataroomId])
  @@index([teamId])
  @@index([status])
  @@index([importBatchId])
  @@index([dataroomDocumentId])
  @@index([createdByUserId])
}

// Assigns a question to exactly one target: link, viewer group, viewer, or
// email. Link/group targets materialize threads lazily; viewer/email targets
// seed the private answer thread immediately.
model DataroomQuestionAssignment {
  id String @id @default(cuid())

  questionId String
  question   DataroomQuestion @relation(fields: [questionId], references: [id], onDelete: Cascade)

  linkId String?
  link   Link?   @relation(fields: [linkId], references: [id], onDelete: Cascade)

  groupId String?
  group   ViewerGroup? @relation(fields: [groupId], references: [id], onDelete: Cascade)

  viewerId String?
  viewer   Viewer? @relation(fields: [viewerId], references: [id], onDelete: Cascade)

  email String?

  createdAt DateTime @default(now())

  @@unique([questionId, linkId])
  @@unique([questionId, groupId])
  @@unique([questionId, viewerId])
  @@unique([questionId, email])
  @@index([questionId])
  @@index([linkId])
  @@index([groupId])
  @@index([viewerId])
}

// Define conversation visibility options
enum ConversationVisibility {
  PRIVATE // Only visible to participants and team members
  PUBLIC_LINK // Visible to all viewers with access to the specific link
  PUBLIC_GROUP // Visible to all viewers in the specific group
  PUBLIC_DOCUMENT // Visible to all viewers with access to the document, across any link
  PUBLIC_DATAROOM // Visible to all viewers with access to the dataroom
}

// Define participant roles
enum ParticipantRole {
  OWNER // Created the conversation
  PARTICIPANT // Joined the conversation later
}

// Track participants in a conversation (including the owner)
model ConversationParticipant {
  id             String       @id @default(cuid())
  conversationId String
  conversation   Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)

  // Role in the conversation
  role ParticipantRole @default(PARTICIPANT)

  // Participant can be either a viewer or a user
  viewerId String?
  viewer   Viewer? @relation(fields: [viewerId], references: [id], onDelete: SetNull)
  userId   String?
  user     User?   @relation(fields: [userId], references: [id], onDelete: SetNull)

  // Notification preferences
  receiveNotifications Boolean @default(false)

  createdAt DateTime @default(now())

  @@unique([conversationId, viewerId])
  @@unique([conversationId, userId])
  @@index([conversationId])
  @@index([viewerId])
  @@index([userId])
}

model Message {
  id      String @id @default(cuid())
  content String // The actual message content

  // Conversation relationship
  conversationId String
  conversation   Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)

  // Sender information
  userId String? // Optional - for team members
  user   User?   @relation(fields: [userId], references: [id], onDelete: SetNull)

  viewerId String? // Optional - for viewers
  viewer   Viewer? @relation(fields: [viewerId], references: [id], onDelete: SetNull)

  // The specific view when this message was sent (for tracking)
  viewId String?
  view   View?   @relation(fields: [viewId], references: [id], onDelete: SetNull)

  // Tracking
  isRead Boolean @default(false)

  // Team-only note, never returned to visitors. Requires a userId.
  isInternalNote Boolean @default(false)

  // FAQ relationships (reverse relations)
  faqAsQuestion DataroomFaqItem[] @relation("FAQQuestionMessage")
  faqAsAnswer   DataroomFaqItem[] @relation("FAQAnswerMessage")

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([conversationId])
  @@index([userId])
  @@index([viewerId])
  @@index([viewId])
}

// Join table to track all views that accessed a conversation
model ConversationView {
  id             String       @id @default(cuid())
  conversationId String
  conversation   Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
  viewId         String
  view           View         @relation(fields: [viewId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())

  @@unique([conversationId, viewId])
  @@index([conversationId])
  @@index([viewId])
}

// Published FAQ items for datarooms
model DataroomFaqItem {
  id               String  @id @default(cuid())
  title            String? // Optional title for the FAQ
  editedQuestion   String // Admin-edited version of the question
  originalQuestion String? // Original question from visitor (for reference)
  answer           String // The answer content
  description      String? // Optional context or description

  // Relationships
  dataroomId String
  dataroom   Dataroom @relation(fields: [dataroomId], references: [id], onDelete: Cascade)

  linkId String? // Optional: specific link visibility
  link   Link?   @relation(fields: [linkId], references: [id], onDelete: SetNull)

  dataroomDocumentId String? // Optional: document-specific FAQ
  dataroomDocument   DataroomDocument? @relation(fields: [dataroomDocumentId], references: [id], onDelete: SetNull)

  // Source conversation and messages (for reference and editing)
  sourceConversationId String? // Original conversation
  sourceConversation   Conversation? @relation(fields: [sourceConversationId], references: [id], onDelete: SetNull)

  questionMessageId String? // Reference to original question message
  questionMessage   Message? @relation(name: "FAQQuestionMessage", fields: [questionMessageId], references: [id], onDelete: SetNull)

  answerMessageId String? // Reference to answer message
  answerMessage   Message? @relation(name: "FAQAnswerMessage", fields: [answerMessageId], references: [id], onDelete: SetNull)

  // Publishing details
  teamId String
  team   Team   @relation(fields: [teamId], references: [id], onDelete: Cascade)

  publishedByUserId String
  publishedByUser   User   @relation(fields: [publishedByUserId], references: [id], onDelete: Cascade)

  // Visibility and status
  visibilityMode FaqVisibility @default(PUBLIC_DATAROOM)
  status         FaqStatus     @default(PUBLISHED)
  isAnonymized   Boolean       @default(true)

  // Analytics
  viewCount Int @default(0)

  // Optional categorization
  tags String[] @default([])

  // Metadata
  documentPageNumber    Int? // Optional: specific page reference
  documentVersionNumber Int? // Optional: specific version reference

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([dataroomId])
  @@index([linkId])
  @@index([dataroomDocumentId])
  @@index([sourceConversationId])
  @@index([teamId])
  @@index([publishedByUserId])
  @@index([status])
  @@index([visibilityMode])
  @@index([createdAt])
}

// Define FAQ visibility options
enum FaqVisibility {
  PUBLIC_DATAROOM // Visible to all dataroom visitors
  PUBLIC_LINK // Visible only to specific link visitors
  PUBLIC_DOCUMENT // Visible only when viewing specific document
}

// Define FAQ status
enum FaqStatus {
  DRAFT // Not yet published
  PUBLISHED // Live and visible to visitors
  ARCHIVED // No longer visible but kept for reference
}