messaging.prisma 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. model Notification {
  2. id String @id @default(cuid())
  3. type String
  4. title String
  5. message String
  6. entityType String
  7. entityId String
  8. entityUrl String
  9. read Boolean @default(false)
  10. organizationId String
  11. createdAt DateTime @default(now())
  12. @@index([organizationId, createdAt(sort: Desc)])
  13. @@index([organizationId, read])
  14. @@map("notifications")
  15. }
  16. model SmsMessage {
  17. id String @id @default(cuid())
  18. direction String // "inbound" | "outbound"
  19. fromNumber String
  20. toNumber String
  21. body String
  22. status String @default("queued") // queued | sent | delivered | failed | received
  23. providerMsgId String?
  24. errorMessage String?
  25. relatedEntityType String?
  26. relatedEntityId String?
  27. createdAt DateTime @default(now())
  28. updatedAt DateTime @updatedAt
  29. organizationId String
  30. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  31. customerId String?
  32. customer Customer? @relation(fields: [customerId], references: [id], onDelete: SetNull)
  33. @@index([organizationId, customerId, createdAt(sort: Desc)])
  34. @@index([organizationId, createdAt(sort: Desc)])
  35. @@index([toNumber, organizationId])
  36. @@map("sms_messages")
  37. }
  38. model TelegramMessage {
  39. id String @id @default(cuid())
  40. direction String // "inbound" | "outbound"
  41. chatId String // Telegram chat ID
  42. body String
  43. status String @default("queued") // queued | sent | delivered | failed | received
  44. telegramMessageId String? // Telegram's message ID for tracking
  45. errorMessage String?
  46. relatedEntityType String?
  47. relatedEntityId String?
  48. createdAt DateTime @default(now())
  49. updatedAt DateTime @updatedAt
  50. organizationId String
  51. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  52. customerId String?
  53. customer Customer? @relation(fields: [customerId], references: [id], onDelete: SetNull)
  54. @@index([organizationId, customerId, createdAt(sort: Desc)])
  55. @@index([organizationId, chatId, createdAt(sort: Desc)])
  56. @@map("telegram_messages")
  57. }
  58. /// One WhatsApp message in either direction.
  59. ///
  60. /// `provider` records which adapter carried it, because a workshop can switch
  61. /// provider without its history becoming unreadable, and a status callback
  62. /// arriving later has to find the row it belongs to.
  63. model WhatsappMessage {
  64. id String @id @default(cuid())
  65. direction String // "inbound" | "outbound"
  66. provider String // "meta" | "twilio" | any adapter registered later
  67. /// E.164 without the "whatsapp:" prefix a provider may use on the wire.
  68. fromNumber String
  69. toNumber String
  70. body String?
  71. /// Media the message carried. Inbound URLs may need the provider's
  72. /// credentials to fetch, so they are not necessarily public.
  73. mediaUrl String?
  74. mediaType String? // image | document | video | audio | sticker
  75. mediaFilename String?
  76. /// Set when the message had to go out as an approved template, which is the
  77. /// only thing WhatsApp allows outside the 24 hour service window.
  78. templateName String?
  79. status String @default("queued") // queued | sent | delivered | read | failed | received
  80. providerMessageId String?
  81. errorMessage String?
  82. relatedEntityType String?
  83. relatedEntityId String?
  84. createdAt DateTime @default(now())
  85. updatedAt DateTime @updatedAt
  86. organizationId String
  87. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  88. customerId String?
  89. customer Customer? @relation(fields: [customerId], references: [id], onDelete: SetNull)
  90. @@index([organizationId, customerId, createdAt(sort: Desc)])
  91. @@index([organizationId, createdAt(sort: Desc)])
  92. @@index([providerMessageId])
  93. @@map("whatsapp_messages")
  94. }
  95. /// A message the workshop lines up to go out on a given day and time, through
  96. /// whichever channel suits the customer. One-off by default; a frequency other
  97. /// than "once" reschedules itself after each send, the way ReportSchedule does.
  98. model ScheduledMessage {
  99. id String @id @default(cuid())
  100. channel String // "email" | "sms" | "telegram" | "in_app"
  101. subject String? // email only
  102. body String
  103. /// Where it goes when no customer is attached, or when the workshop typed an
  104. /// address by hand. Falls back to the customer's email/phone/chat id.
  105. recipient String?
  106. status String @default("scheduled") // scheduled | sent | failed | cancelled
  107. sendAt DateTime
  108. frequency String @default("once") // once | daily | weekly | biweekly | monthly | yearly
  109. endDate DateTime?
  110. lastRunAt DateTime?
  111. sentAt DateTime?
  112. runCount Int @default(0)
  113. errorMessage String?
  114. createdAt DateTime @default(now())
  115. updatedAt DateTime @updatedAt
  116. organizationId String
  117. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  118. customerId String?
  119. customer Customer? @relation(fields: [customerId], references: [id], onDelete: SetNull)
  120. vehicleId String?
  121. vehicle Vehicle? @relation(fields: [vehicleId], references: [id], onDelete: SetNull)
  122. createdById String
  123. inspectionReminder InspectionReminderSend?
  124. @@index([organizationId, sendAt])
  125. @@index([status, sendAt])
  126. @@map("scheduled_messages")
  127. }