customers.prisma 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. model Customer {
  2. id String @id @default(cuid())
  3. /// Human-facing customer number: auto-assigned sequentially on create,
  4. /// editable afterwards. Unique per organization (nulls allowed for
  5. /// legacy customers).
  6. customerNumber String?
  7. name String
  8. email String?
  9. phone String?
  10. address String?
  11. company String?
  12. taxId String?
  13. taxExempt Boolean @default(false)
  14. /// Asked not to receive reminders and campaigns. Honoured by every outreach flow.
  15. reminderOptOut Boolean @default(false)
  16. /// This customer's invoices need their reference or purchase order number:
  17. /// a job for them cannot be invoiced while ServiceRecord.customerReference
  18. /// is empty. Only enforced while the workshop has the field switched on.
  19. poRequired Boolean @default(false)
  20. notes String?
  21. telegramChatId String?
  22. createdAt DateTime @default(now())
  23. updatedAt DateTime @updatedAt
  24. userId String
  25. user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  26. organizationId String?
  27. organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  28. vehicles Vehicle[]
  29. quotes Quote[]
  30. smsMessages SmsMessage[]
  31. telegramMessages TelegramMessage[]
  32. whatsappMessages WhatsappMessage[]
  33. portalMessages PortalMessage[]
  34. emailMessages EmailMessage[]
  35. customerSessions CustomerSession[]
  36. scheduledMessages ScheduledMessage[]
  37. serviceRequests ServiceRequest[]
  38. inspectionReminders InspectionReminderSend[]
  39. serviceRecords ServiceRecord[]
  40. reminders Reminder[]
  41. appointments Appointment[]
  42. tireSets TireSet[]
  43. aiChats AiChat[]
  44. /// The invoice design this customer's invoices use in place of the
  45. /// workshop's default: a fleet account that wants its own paper. An
  46. /// invoice's own choice wins over it. SetNull: a deleted design falls
  47. /// back to the default.
  48. invoiceDesignId String?
  49. invoiceDesign DocumentDesign? @relation(fields: [invoiceDesignId], references: [id], onDelete: SetNull)
  50. importBatchId String?
  51. importBatch ImportBatch? @relation(fields: [importBatchId], references: [id], onDelete: SetNull)
  52. @@unique([organizationId, customerNumber])
  53. @@index([organizationId])
  54. @@index([importBatchId])
  55. @@index([invoiceDesignId])
  56. @@map("customers")
  57. }
  58. model CustomerSession {
  59. id String @id @default(cuid())
  60. token String @unique
  61. expiresAt DateTime
  62. createdAt DateTime @default(now())
  63. customerId String
  64. customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
  65. organizationId String
  66. @@index([token])
  67. @@index([customerId, organizationId])
  68. @@map("customer_sessions")
  69. }
  70. model CustomerMagicLink {
  71. id String @id @default(cuid())
  72. token String @unique
  73. email String
  74. organizationId String
  75. expiresAt DateTime
  76. usedAt DateTime?
  77. createdAt DateTime @default(now())
  78. @@index([token])
  79. @@map("customer_magic_links")
  80. }
  81. model CustomerSmsCode {
  82. id String @id @default(cuid())
  83. /// SHA-256 of the digits, not the digits.
  84. ///
  85. /// The code reaches the customer in a text message and lives in their head
  86. /// for a minute. It has no business also sitting in a table where reading a
  87. /// row hands over somebody's portal session.
  88. code String
  89. /// Wrong guesses so far. Six digits is a million, which sounds like plenty
  90. /// and is not: limiting by address does nothing about a hundred machines
  91. /// sharing the work. Five attempts against one code makes the space
  92. /// irrelevant, because the code is gone long before the guessing arrives.
  93. attempts Int @default(0)
  94. phone String
  95. organizationId String
  96. expiresAt DateTime
  97. usedAt DateTime?
  98. createdAt DateTime @default(now())
  99. @@index([phone, organizationId])
  100. @@index([organizationId])
  101. @@map("customer_sms_codes")
  102. }
  103. model ServiceRequest {
  104. id String @id @default(cuid())
  105. description String
  106. preferredDate DateTime?
  107. /// The part of the day the customer would rather come: "morning" or
  108. /// "afternoon". Null when any time suits, and on every request without a day.
  109. preferredTime String?
  110. /// Photos and video the customer sent with the request from the portal, in
  111. /// order, as stored file urls
  112. /// (`/api/protected/files/<org>/portal-uploads/<uuid>.jpg`). A text array,
  113. /// like `PortalMessage.attachments`, so the file manager
  114. /// (lib/files/references.ts) keeps each file for as long as the request
  115. /// mentions it. What kind of file each is follows from its extension.
  116. attachments String[] @default([])
  117. status String @default("pending")
  118. adminNotes String?
  119. createdAt DateTime @default(now())
  120. updatedAt DateTime @updatedAt
  121. customerId String
  122. customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
  123. vehicleId String
  124. vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  125. organizationId String
  126. @@index([organizationId, status])
  127. @@index([customerId])
  128. @@map("service_requests")
  129. }