vehicles.prisma 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. model Vehicle {
  2. id String @id @default(cuid())
  3. make String
  4. model String
  5. year Int
  6. vin String?
  7. licensePlate String?
  8. color String?
  9. mileage Int @default(0)
  10. /// The job whose reading the vehicle's reading is, when a job set it
  11. /// (vehicleReading.ts). Correcting that job's reading brings the vehicle
  12. /// back down; anything that writes the reading directly clears it.
  13. readingJobId String? @unique
  14. readingJob ServiceRecord? @relation("VehicleReadingJob", fields: [readingJobId], references: [id], onDelete: SetNull)
  15. /// What the vehicle read before that job set it: where a correction of the
  16. /// job may take it back to. Means nothing without readingJobId.
  17. readingBeforeJob Int?
  18. fuelType String? @default("gasoline")
  19. transmission String? @default("automatic")
  20. engineSize String?
  21. engineCode String?
  22. /// German type approval key from the registration certificate: HSN is the
  23. /// four-digit manufacturer code in field 2.1, TSN the three-character type
  24. /// code at the start of field 2.2. Parts catalogues and insurers look a car
  25. /// up by the pair, since Germany has no public plate registry.
  26. hsn String?
  27. tsn String?
  28. /// The number the owner knows the vehicle by in their own fleet ("Unit 14",
  29. /// "T-208"). Free text, shown, printed and sent only while the workshop has
  30. /// the field switched on (src/features/vehicles/Lib/fleetNumberSetting.ts).
  31. fleetNumber String?
  32. purchaseDate DateTime?
  33. purchasePrice Float?
  34. imageUrl String?
  35. /// sedan | hatchback | estate | suv | van | pickup | motorcycle | boat: which
  36. /// line drawing the condition map is marked on. Null until somebody picks
  37. /// one, and the map then guesses from the workshop's trade.
  38. bodyType String?
  39. isArchived Boolean @default(false)
  40. archiveReason String?
  41. /// Set when the customer said in the portal that the vehicle is no longer theirs.
  42. soldReportedAt DateTime?
  43. maintenanceDismissed Boolean @default(false)
  44. maintenanceDismissedAt DateTime?
  45. aiMessages AiGeneratedMessage[]
  46. aiChats AiChat[]
  47. createdAt DateTime @default(now())
  48. updatedAt DateTime @updatedAt
  49. userId String
  50. user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  51. organizationId String?
  52. organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  53. customerId String?
  54. customer Customer? @relation(fields: [customerId], references: [id], onDelete: SetNull)
  55. serviceRecords ServiceRecord[]
  56. notes Note[]
  57. fuelLogs FuelLog[]
  58. reminders Reminder[]
  59. appointments Appointment[]
  60. quotes Quote[]
  61. recurringInvoices RecurringInvoice[]
  62. inspections Inspection[]
  63. serviceRequests ServiceRequest[]
  64. findings VehicleFinding[]
  65. scheduledMessages ScheduledMessage[]
  66. tireSets TireSet[]
  67. inspectionStatus VehicleInspectionStatus?
  68. inspectionReminders InspectionReminderSend[]
  69. conditionMarks ConditionMark[]
  70. meters VehicleMeter[]
  71. equipment EquipmentItem[]
  72. equipmentFittings EquipmentFitting[]
  73. importBatchId String?
  74. importBatch ImportBatch? @relation(fields: [importBatchId], references: [id], onDelete: SetNull)
  75. @@index([organizationId])
  76. @@index([importBatchId])
  77. @@map("vehicles")
  78. }
  79. model Note {
  80. id String @id @default(cuid())
  81. title String
  82. content String
  83. isPinned Boolean @default(false)
  84. createdAt DateTime @default(now())
  85. updatedAt DateTime @updatedAt
  86. vehicleId String
  87. vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  88. @@index([vehicleId])
  89. @@map("notes")
  90. }
  91. model FuelLog {
  92. id String @id @default(cuid())
  93. date DateTime @default(now())
  94. mileage Int
  95. gallons Float
  96. pricePerGallon Float
  97. totalCost Float
  98. isFillUp Boolean @default(true)
  99. station String?
  100. notes String?
  101. createdAt DateTime @default(now())
  102. updatedAt DateTime @updatedAt
  103. vehicleId String
  104. vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  105. @@index([vehicleId])
  106. @@map("fuel_logs")
  107. }
  108. model Reminder {
  109. id String @id @default(cuid())
  110. title String
  111. description String?
  112. dueDate DateTime?
  113. /// True when the time of day in dueDate was chosen, not the noon a
  114. /// day-only reminder is stored at
  115. hasDueTime Boolean @default(false)
  116. dueMileage Int?
  117. isCompleted Boolean @default(false)
  118. /// Per-reminder notification channels, chosen when creating the reminder
  119. notifyInApp Boolean @default(true)
  120. notifyEmail Boolean @default(false)
  121. /// Set by the due-reminder cron once notified; cleared when dueDate changes
  122. notifiedAt DateTime?
  123. createdAt DateTime @default(now())
  124. updatedAt DateTime @updatedAt
  125. /// A reminder relates to a vehicle, a customer, or just the workshop
  126. /// (both null). organizationId is the canonical scope for all three.
  127. vehicleId String?
  128. vehicle Vehicle? @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  129. customerId String?
  130. customer Customer? @relation(fields: [customerId], references: [id], onDelete: Cascade)
  131. organizationId String?
  132. organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  133. @@index([vehicleId])
  134. @@index([customerId])
  135. @@index([organizationId])
  136. @@map("reminders")
  137. }
  138. model VehicleFinding {
  139. id String @id @default(cuid())
  140. description String
  141. severity String @default("needs_work")
  142. status String @default("open")
  143. notes String?
  144. imageUrls String[] @default([])
  145. createdAt DateTime @default(now())
  146. updatedAt DateTime @updatedAt
  147. vehicleId String
  148. vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  149. serviceRecordId String?
  150. serviceRecord ServiceRecord? @relation("FindingDiscoveredIn", fields: [serviceRecordId], references: [id], onDelete: SetNull)
  151. resolvedServiceRecordId String?
  152. resolvedServiceRecord ServiceRecord? @relation("FindingResolvedIn", fields: [resolvedServiceRecordId], references: [id], onDelete: SetNull)
  153. /// The customer concern this finding answers, if it answers one.
  154. ///
  155. /// Nullable because plenty of findings are things nobody asked about: the
  156. /// perished bush somebody noticed while the car was on the lift. Those are
  157. /// worth just as much, they just are not part of the story the customer
  158. /// started. SetNull rather than Cascade because a finding is a fact about
  159. /// the vehicle and outlives the concern that prompted the look.
  160. concernId String?
  161. concern ServiceConcern? @relation(fields: [concernId], references: [id], onDelete: SetNull)
  162. /// A diagnostic trouble code, when the finding is one: normalised and
  163. /// upper case, such as P0420 (`^[PCBU][0-3][0-9A-F]{3}$`).
  164. faultCode String?
  165. /// stored, pending, permanent or history, when the scan tool said.
  166. faultStatus String?
  167. /// The control unit the code was read from ("Engine", "ABS"), when known.
  168. faultModule String?
  169. /// The meaning as it was when the code was added, in the workshop's
  170. /// language. Kept on the finding so prints, history and search never wait
  171. /// on the code service and survive a disconnect.
  172. faultMeaning String?
  173. /// Where that meaning came from: generic, manufacturer or report.
  174. faultMeaningSource String?
  175. /// The diagnostic report the code was read from. SetNull: the finding is
  176. /// a fact about the vehicle and outlives the file.
  177. sourceAttachmentId String?
  178. sourceAttachment ServiceAttachment? @relation(fields: [sourceAttachmentId], references: [id], onDelete: SetNull)
  179. @@index([vehicleId])
  180. @@index([serviceRecordId])
  181. @@index([concernId])
  182. @@index([vehicleId, faultCode])
  183. @@index([sourceAttachmentId])
  184. @@map("vehicle_findings")
  185. }
  186. /// What a vehicle registry, or the workshop itself, last said about when the
  187. /// vehicle's periodic roadworthiness inspection (EU-kontroll, MOT, APK, HU,
  188. /// contrôle technique, and so on) is due. One row per vehicle. The
  189. /// organisation is on the row so "what is due for this workshop" is one
  190. /// indexed query and the isolation is explicit rather than inherited.
  191. model VehicleInspectionStatus {
  192. id String @id @default(cuid())
  193. vehicleId String @unique
  194. vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  195. organizationId String
  196. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  197. /// Next periodic inspection deadline, when known.
  198. dueAt DateTime?
  199. /// Last approved periodic inspection, when known.
  200. lastAt DateTime?
  201. /// Connector id ("vegvesen"), or "manual" / "inspection" for the workshop's own entries.
  202. source String
  203. /// When a registry last confirmed the row. Null for manual entries.
  204. checkedAt DateTime?
  205. /// False when the registry answered but had no such vehicle.
  206. found Boolean @default(true)
  207. /// Whether the registry lists the vehicle as currently registered, when it says.
  208. registered Boolean?
  209. /// Last lookup failure, cleared on success. Never a plate or VIN.
  210. lastError String?
  211. /// Whatever else the registry returned and the app has no column for yet:
  212. /// approved tyre sizes, weights, vehicle class, first registration.
  213. extras Json?
  214. createdAt DateTime @default(now())
  215. updatedAt DateTime @updatedAt
  216. @@index([organizationId, dueAt])
  217. @@index([organizationId, checkedAt])
  218. @@map("vehicle_inspection_statuses")
  219. }
  220. /// One "remind these customers" action, created only by a person confirming
  221. /// a reviewed list. Never created by a timer.
  222. model InspectionReminderCampaign {
  223. id String @id @default(cuid())
  224. organizationId String
  225. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  226. createdById String
  227. /// Client-generated, so a double submit creates one campaign, not two.
  228. idempotencyToken String @unique
  229. /// Deadline window the list was built from, in days.
  230. windowDays Int
  231. channel String
  232. subject String?
  233. body String
  234. /// "sent" once rows exist; "dry_run" in demo mode, where nothing goes out.
  235. status String @default("sent")
  236. recipientCount Int @default(0)
  237. createdAt DateTime @default(now())
  238. sends InspectionReminderSend[]
  239. @@index([organizationId, createdAt])
  240. @@map("inspection_reminder_campaigns")
  241. }
  242. /// One customer told once about one vehicle's deadline. The unique pair of
  243. /// vehicle and deadline is the guarantee that no deadline is ever reminded
  244. /// twice, whatever the code above it does.
  245. model InspectionReminderSend {
  246. id String @id @default(cuid())
  247. campaignId String
  248. campaign InspectionReminderCampaign @relation(fields: [campaignId], references: [id], onDelete: Cascade)
  249. organizationId String
  250. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  251. vehicleId String
  252. vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade)
  253. customerId String
  254. customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
  255. /// The deadline as it stood when the reminder went out.
  256. dueAt DateTime
  257. channel String
  258. recipient String
  259. /// Booking link secret; the link is /b/<token>.
  260. token String @unique
  261. expiresAt DateTime
  262. scheduledMessageId String? @unique
  263. scheduledMessage ScheduledMessage? @relation(fields: [scheduledMessageId], references: [id], onDelete: SetNull)
  264. /// Set when the customer booked from the link.
  265. bookedAt DateTime?
  266. bookedServiceRecordId String?
  267. bookedServiceRequestId String?
  268. cancelledAt DateTime?
  269. createdAt DateTime @default(now())
  270. @@unique([vehicleId, dueAt])
  271. @@index([organizationId, createdAt])
  272. @@index([campaignId])
  273. @@map("inspection_reminder_sends")
  274. }