@machynx/data-db 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1567 @@
1
+ // ----------------------------------------------------------------------
2
+ // HOURLY (HOT DATA)
3
+ // ----------------------------------------------------------------------
4
+ model MachineHourlyAnalytics {
5
+ id BigInt @id @default(autoincrement()) @db.BigInt
6
+ machineId Int
7
+ organizationId Int
8
+
9
+ totalProductionCount Int?
10
+ efficiency Float?
11
+ utilization Float?
12
+ totalPowerConsumed Float?
13
+ idleTime Float?
14
+ nonWorkingTime Float?
15
+ upTime Float?
16
+ productiveTime Float?
17
+
18
+ startDateTime DateTime?
19
+ endDateTime DateTime?
20
+
21
+ machine Machine @relation("MachineToHourlyAnalytics", fields: [machineId], references: [id])
22
+ deviceHourlyAnalytics DeviceHourlyAnalytics[] @relation("MachineHourlyAnalytics_DeviceHourlyAnalytics")
23
+
24
+ createdAt DateTime @default(now())
25
+ updatedAt DateTime @updatedAt
26
+
27
+ @@unique([organizationId, machineId, startDateTime], map: "unique_machineHourlyCompound")
28
+ @@index([machineId, startDateTime])
29
+ @@schema("analytics")
30
+ }
31
+
32
+ // ----------------------------------------------------------------------
33
+ // DAILY (WARM DATA - 2 to 5 Years)
34
+ // ----------------------------------------------------------------------
35
+ model MachineDailyAnalytics {
36
+ id BigInt @id @default(autoincrement()) @db.BigInt
37
+ machineId Int
38
+ organizationId Int
39
+
40
+ totalProductionCount Int?
41
+ avgEfficiency Float?
42
+ avgUtilization Float?
43
+ totalPowerConsumed Float?
44
+ totalUpTime Float?
45
+
46
+ date DateTime @db.Date // YYYY-MM-DD
47
+
48
+ machine Machine @relation("MachineToDailyAnalytics", fields: [machineId], references: [id])
49
+
50
+ createdAt DateTime @default(now())
51
+ updatedAt DateTime @updatedAt
52
+
53
+ @@unique([organizationId, machineId, date], map: "unique_machineDailyCompound")
54
+ @@index([machineId, date])
55
+ @@schema("analytics")
56
+ }
57
+
58
+ // ----------------------------------------------------------------------
59
+ // MONTHLY (COLD DATA - Forever)
60
+ // ----------------------------------------------------------------------
61
+ model MachineMonthlyAnalytics {
62
+ id BigInt @id @default(autoincrement()) @db.BigInt
63
+ machineId Int
64
+ organizationId Int
65
+
66
+ totalProductionCount BigInt?
67
+ avgEfficiency Float?
68
+ totalPowerConsumed Float?
69
+
70
+ month Int
71
+ year Int
72
+
73
+ machine Machine @relation("MachineToMonthlyAnalytics", fields: [machineId], references: [id])
74
+
75
+ createdAt DateTime @default(now())
76
+ updatedAt DateTime @updatedAt
77
+
78
+ @@unique([organizationId, machineId, month, year], map: "unique_machineMonthlyCompound")
79
+ @@schema("analytics")
80
+ }
81
+
82
+ // ----------------------------------------------------------------------
83
+ // OTHER ENTITIES (Hourly is usually sufficient for these)
84
+ // ----------------------------------------------------------------------
85
+
86
+ model LineHourlyAnalytics {
87
+ id BigInt @id @default(autoincrement()) @db.BigInt
88
+ assemblyLineId Int
89
+ organizationId Int
90
+
91
+ averageEfficiency Float?
92
+ averageUtilization Float?
93
+ totalProductionCount Int?
94
+
95
+ startDateTime DateTime?
96
+ endDateTime DateTime?
97
+
98
+ assemblyLine AssemblyLine @relation("LineToHourlyAnalytics", fields: [assemblyLineId], references: [id])
99
+ createdAt DateTime @default(now())
100
+ updatedAt DateTime @updatedAt
101
+
102
+ @@unique([organizationId, assemblyLineId, startDateTime], map: "unique_lineHourlyCompound")
103
+ @@schema("analytics")
104
+ }
105
+
106
+ model FloorHourlyAnalytics {
107
+ id BigInt @id @default(autoincrement()) @db.BigInt
108
+ floorId Int
109
+ organizationId Int
110
+
111
+ averageEfficiency Float?
112
+ totalProductionCount Int?
113
+
114
+ startDateTime DateTime?
115
+ endDateTime DateTime?
116
+
117
+ floor Floor @relation("FloorToHourlyAnalytics", fields: [floorId], references: [id])
118
+ createdAt DateTime @default(now())
119
+ updatedAt DateTime @updatedAt
120
+
121
+ @@unique([organizationId, floorId, startDateTime], map: "unique_floorHourlyCompound")
122
+ @@schema("analytics")
123
+ }
124
+
125
+ model FactoryHourlyAnalytics {
126
+ id BigInt @id @default(autoincrement()) @db.BigInt
127
+ factoryId Int
128
+ organizationId Int
129
+
130
+ averageEfficiency Float?
131
+ totalProductionCount Int?
132
+
133
+ startDateTime DateTime?
134
+ endDateTime DateTime?
135
+
136
+ factory Factory @relation("FactoryToHourlyAnalytics", fields: [factoryId], references: [id])
137
+ createdAt DateTime @default(now())
138
+ updatedAt DateTime @updatedAt
139
+
140
+ @@unique([organizationId, factoryId, startDateTime], map: "unique_factoryHourlyCompound")
141
+ @@schema("analytics")
142
+ }
143
+
144
+ model DeviceHourlyAnalytics {
145
+ id BigInt @id @default(autoincrement()) @db.BigInt
146
+ deviceId Int
147
+ machineHourlyAnalyticsId BigInt? // Optional if device is not attached to machine
148
+ organizationId Int
149
+
150
+ totalOnTime Float?
151
+ totalActiveTime Float?
152
+ totalWorkingTime Float?
153
+
154
+ startDateTime DateTime?
155
+ endDateTime DateTime?
156
+
157
+ device Device @relation("DeviceHourlyAnalytics_Device", fields: [deviceId], references: [id])
158
+ machineHourlyAnalytics MachineHourlyAnalytics? @relation("MachineHourlyAnalytics_DeviceHourlyAnalytics", fields: [machineHourlyAnalyticsId], references: [id])
159
+ sensorHourlyAnalytics SensorHourlyAnalytics[] @relation("DeviceHourlyAnalytics_SensorHourlyAnalytics")
160
+ devicePowerSensorHourlyAnalytics DevicePowerSensorHourlyAnalytics? @relation("DevicePowerRelation")
161
+
162
+ createdAt DateTime @default(now())
163
+ updatedAt DateTime @updatedAt
164
+
165
+ @@unique([organizationId, deviceId, startDateTime], map: "unique_deviceHourlyCompound")
166
+ @@schema("analytics")
167
+ }
168
+
169
+ model SensorHourlyAnalytics {
170
+ id BigInt @id @default(autoincrement()) @db.BigInt
171
+ sensorId Int
172
+ deviceHourlyAnalyticsId BigInt
173
+
174
+ initialSense DateTime?
175
+ finalSense DateTime?
176
+ startDateTime DateTime?
177
+ endDateTime DateTime?
178
+
179
+ sensor Sensor @relation("SensorHourlyAnalytics_Sensor", fields: [sensorId], references: [id])
180
+ deviceHourlyAnalytics DeviceHourlyAnalytics @relation("DeviceHourlyAnalytics_SensorHourlyAnalytics", fields: [deviceHourlyAnalyticsId], references: [id])
181
+
182
+ proximitySensorHourlyAnalytics ProximitySensorHourlyAnalytics?
183
+ infraredSensorHourlyAnalytics InfraredSensorHourlyAnalytics?
184
+
185
+ createdAt DateTime @default(now())
186
+ updatedAt DateTime @updatedAt
187
+
188
+ @@unique([sensorId, startDateTime], map: "unique_sensorHourlyCompound")
189
+ @@schema("analytics")
190
+ }
191
+
192
+ model ProximitySensorHourlyAnalytics {
193
+ id BigInt @id @default(autoincrement()) @db.BigInt
194
+ sensorHourlyAnalyticsId BigInt @unique(map: "unique_proximityCompound")
195
+ totalDetections Int
196
+ productiveDetections Int?
197
+
198
+ sensorHourlyAnalytics SensorHourlyAnalytics @relation(fields: [sensorHourlyAnalyticsId], references: [id])
199
+ startDateTime DateTime?
200
+ endDateTime DateTime?
201
+ createdAt DateTime @default(now())
202
+ updatedAt DateTime @updatedAt
203
+
204
+ @@schema("analytics")
205
+ }
206
+
207
+ model InfraredSensorHourlyAnalytics {
208
+ id BigInt @id @default(autoincrement()) @db.BigInt
209
+ sensorHourlyAnalyticsId BigInt @unique(map: "unique_infraredCompound")
210
+ totalDetections Int
211
+ productiveDetections Int?
212
+
213
+ sensorHourlyAnalytics SensorHourlyAnalytics @relation(fields: [sensorHourlyAnalyticsId], references: [id])
214
+ startDateTime DateTime?
215
+ endDateTime DateTime?
216
+ createdAt DateTime @default(now())
217
+ updatedAt DateTime @updatedAt
218
+
219
+ @@schema("analytics")
220
+ }
221
+
222
+ model DevicePowerSensorHourlyAnalytics {
223
+ id BigInt @id @default(autoincrement()) @db.BigInt
224
+ deviceHourlyAnalyticsId BigInt @unique
225
+ powerEvents Json?
226
+ totalOnTime Float?
227
+
228
+ startDateTime DateTime?
229
+ endDateTime DateTime?
230
+
231
+ deviceHourlyAnalytics DeviceHourlyAnalytics @relation("DevicePowerRelation", fields: [deviceHourlyAnalyticsId], references: [id])
232
+ createdAt DateTime @default(now())
233
+ updatedAt DateTime @updatedAt
234
+
235
+ @@schema("analytics")
236
+ }
237
+
238
+ // ----------------------------------------------------------------------
239
+ // SCHEMA: audit
240
+ // ----------------------------------------------------------------------
241
+
242
+ model AuditLog {
243
+ id BigInt @id @default(autoincrement()) @db.BigInt
244
+ entityName String
245
+ entityId Int?
246
+ action String
247
+ userId Int? // ID for CoreUser
248
+ orgId Int? // ID for CoreOrganization
249
+ timestamp DateTime @default(now())
250
+ httpMethod String
251
+ route String
252
+ statusCode Int
253
+ ipAddress String?
254
+ userAgent String?
255
+ oldValues Json?
256
+ newValues Json?
257
+
258
+ @@index([entityName, entityId])
259
+ @@index([userId])
260
+ @@index([orgId])
261
+ @@schema("audit")
262
+ }
263
+
264
+ // ----------------------------------------------------------------------
265
+ // SCHEMA: chatbot
266
+ // ----------------------------------------------------------------------
267
+
268
+ enum ChatbotMessageSender {
269
+ USER
270
+ BOT
271
+
272
+ @@schema("chatbot")
273
+ }
274
+
275
+ model ChatbotHistory {
276
+ id Int @id @default(autoincrement())
277
+ title String
278
+ userId Int // ID for CoreUser
279
+ organizationId Int // ID for CoreOrganization
280
+ messages ChatbotMessage[]
281
+ createdAt DateTime @default(now())
282
+ updatedAt DateTime @updatedAt
283
+
284
+ @@index([userId])
285
+ @@index([organizationId])
286
+ @@index([createdAt])
287
+ @@index([title])
288
+ @@schema("chatbot")
289
+ }
290
+
291
+ model ChatbotMessage {
292
+ id Int @id @default(autoincrement())
293
+ chatHistoryId Int
294
+ sender ChatbotMessageSender
295
+ message String
296
+ tokensUsed Int
297
+ jsonContent Json?
298
+ chatHistory ChatbotHistory @relation(fields: [chatHistoryId], references: [id])
299
+ createdAt DateTime @default(now())
300
+ updatedAt DateTime @updatedAt
301
+
302
+ @@index([chatHistoryId])
303
+ @@index([tokensUsed])
304
+ @@schema("chatbot")
305
+ }
306
+
307
+ enum SensorType {
308
+ PROXIMITY
309
+ INFRARED
310
+ RFID
311
+ CURRENT
312
+
313
+ @@schema("device")
314
+ }
315
+
316
+ enum RFIDTapType {
317
+ CHECKIN
318
+ CHECKOUT
319
+
320
+ @@schema("device")
321
+ }
322
+
323
+ enum DevicePowerState {
324
+ POWER_ON
325
+ POWER_OFF
326
+
327
+ @@schema("device")
328
+ }
329
+
330
+ model Gateway {
331
+ id Int @id @default(autoincrement())
332
+ gatewayUid String @unique @default(cuid()) // Matches Core DB
333
+ gatewayName String?
334
+ isActive Boolean @default(true)
335
+ floorId Int
336
+ secret String
337
+
338
+ // Network Config
339
+ currentWifiSSID String?
340
+ currentWifiPassword String?
341
+ currentMQTTBrokerUrl String?
342
+ fallBackWifiSSID String?
343
+ fallBackWifiPassword String?
344
+
345
+ devices Device[] @relation("GatewayToDevices")
346
+ floor Floor @relation("FloorToGateways", fields: [floorId], references: [id])
347
+ apiToken GatewayApiToken?
348
+
349
+ createdAt DateTime @default(now())
350
+ updatedAt DateTime @updatedAt
351
+
352
+ @@index([floorId])
353
+ @@schema("device")
354
+ }
355
+
356
+ model GatewayApiToken {
357
+ id Int @id @default(autoincrement())
358
+ gatewayId Int @unique
359
+ apiToken String
360
+ expiresAt DateTime
361
+ gateway Gateway @relation(fields: [gatewayId], references: [id])
362
+ createdAt DateTime @default(now())
363
+ updatedAt DateTime @updatedAt
364
+
365
+ @@schema("device")
366
+ }
367
+
368
+ model Device {
369
+ id Int @id @default(autoincrement())
370
+
371
+ // PRODUCTION CRITICAL: The Link to your Global Inventory
372
+ globalDeviceUid String @unique // Immutable ID printed on the sticker (Core DB)
373
+
374
+ deviceUid String @unique @default(cuid()) // Local Internal ID
375
+ deviceName String?
376
+ isActive Boolean @default(true)
377
+ orgId Int
378
+ gatewayId Int?
379
+
380
+ // Hardware
381
+ sensors Sensor[] @relation("DeviceSensors")
382
+ gateway Gateway? @relation("GatewayToDevices", fields: [gatewayId], references: [id])
383
+
384
+ // Usage
385
+ tasks Task[] @relation("Device_UsedInTasks")
386
+ machineAssignments MachineDeviceAssignment[] @relation("DeviceToMachineAssignments")
387
+ operatorTimeLogs OperatorTimeLog[] @relation("Device_RFIDOperatorTimeLogs")
388
+ hourlyAnalytics DeviceHourlyAnalytics[] @relation("DeviceHourlyAnalytics_Device")
389
+
390
+ // Direct Assignments (Location)
391
+ machineId Int?
392
+ machine Machine? @relation("MachineToDevicesDirect", fields: [machineId], references: [id])
393
+ assemblyLineId Int?
394
+ assemblyLine AssemblyLine? @relation("AssemblyLineToDevicesDirect", fields: [assemblyLineId], references: [id])
395
+ floorId Int?
396
+ floor Floor? @relation("FloorToDevicesDirect", fields: [floorId], references: [id])
397
+ factoryId Int?
398
+ factory Factory? @relation("FactoryToDevicesDirect", fields: [factoryId], references: [id])
399
+
400
+ createdAt DateTime @default(now())
401
+ updatedAt DateTime @updatedAt
402
+
403
+ @@index([orgId])
404
+ @@index([globalDeviceUid])
405
+ @@schema("device")
406
+ }
407
+
408
+ model Sensor {
409
+ id Int @id @default(autoincrement())
410
+ sensorUid String @unique @default(cuid())
411
+ sensorName String?
412
+ sensorType SensorType @default(PROXIMITY)
413
+ deviceId Int
414
+ isActive Boolean @default(true)
415
+
416
+ device Device @relation("DeviceSensors", fields: [deviceId], references: [id])
417
+ sensorLogs SensorLog[] @relation("Sensor_SensorLogs")
418
+
419
+ // Configs
420
+ proximityConfig ProximitySensorConfig?
421
+ infraredConfig InfraredSensorConfig?
422
+ currentConfig CurrentSensorConfig?
423
+
424
+ // Locations
425
+ machineId Int?
426
+ machine Machine? @relation("MachineToSensors", fields: [machineId], references: [id])
427
+ assemblyLineId Int?
428
+ assemblyLine AssemblyLine? @relation("AssemblyLineToSensors", fields: [assemblyLineId], references: [id])
429
+ floorId Int?
430
+ floor Floor? @relation("FloorToSensors", fields: [floorId], references: [id])
431
+ factoryId Int?
432
+ factory Factory? @relation("FactoryToSensors", fields: [factoryId], references: [id])
433
+
434
+ hourlyAnalytics SensorHourlyAnalytics[] @relation("SensorHourlyAnalytics_Sensor")
435
+
436
+ createdAt DateTime @default(now())
437
+ updatedAt DateTime @updatedAt
438
+
439
+ @@index([deviceId])
440
+ @@schema("device")
441
+ }
442
+
443
+ model ProximitySensorConfig {
444
+ id Int @id @default(autoincrement())
445
+ sensorId Int @unique
446
+ range Float?
447
+ sensitivity Float?
448
+ aggregationFactor Int? @default(1)
449
+ sensor Sensor @relation(fields: [sensorId], references: [id], onDelete: Cascade)
450
+ createdAt DateTime @default(now())
451
+ updatedAt DateTime @updatedAt
452
+
453
+ @@schema("device")
454
+ }
455
+
456
+ model CurrentSensorConfig {
457
+ id Int @id @default(autoincrement())
458
+ sensorId Int @unique
459
+ currentThreshold Float?
460
+ sensor Sensor @relation(fields: [sensorId], references: [id], onDelete: Cascade)
461
+ createdAt DateTime @default(now())
462
+ updatedAt DateTime @updatedAt
463
+
464
+ @@schema("device")
465
+ }
466
+
467
+ model InfraredSensorConfig {
468
+ id Int @id @default(autoincrement())
469
+ sensorId Int @unique
470
+ wavelength Float?
471
+ resolution Int?
472
+ aggregationFactor Int? @default(1)
473
+ sensor Sensor @relation(fields: [sensorId], references: [id], onDelete: Cascade)
474
+ createdAt DateTime @default(now())
475
+ updatedAt DateTime @updatedAt
476
+
477
+ @@schema("device")
478
+ }
479
+
480
+ model MachineDeviceAssignment {
481
+ id Int @id @default(autoincrement())
482
+ deviceId Int
483
+ machineId Int
484
+ assignedAt DateTime @default(now())
485
+ unassignedAt DateTime?
486
+ device Device @relation("DeviceToMachineAssignments", fields: [deviceId], references: [id])
487
+ machine Machine @relation("MachineToDeviceAssignments", fields: [machineId], references: [id])
488
+
489
+ @@index([deviceId])
490
+ @@schema("device")
491
+ }
492
+
493
+ model SensorLog {
494
+ id BigInt @id @default(autoincrement()) @db.BigInt
495
+ sensorId Int
496
+ senseTime DateTime
497
+ isProcessed Boolean @default(false)
498
+ sensor Sensor @relation("Sensor_SensorLogs", fields: [sensorId], references: [id])
499
+
500
+ proximityLog ProximitySensorLog? @relation("SensorLogProximityLog")
501
+ infraredLog InfraredSensorLog? @relation("SensorLogInfraredLog")
502
+ rfidLog RFIDSensorLog? @relation("SensorLogRFIDSensorLog")
503
+ devicePowerLog DevicePowerSensorLog? @relation("SensorLogDevicePowerSensorLog")
504
+ currentLog CurrentSensorLog? @relation("SensorLogCurrentLog")
505
+
506
+ createdAt DateTime @default(now())
507
+ updatedAt DateTime @default(now())
508
+
509
+ @@unique([sensorId, senseTime])
510
+ @@schema("device")
511
+ }
512
+
513
+ model RFIDSensorLog {
514
+ id BigInt @id @default(autoincrement()) @db.BigInt
515
+ sensorLogId BigInt @unique
516
+ rfid String?
517
+ tapType RFIDTapType
518
+ operatorId Int?
519
+
520
+ sensorLog SensorLog @relation("SensorLogRFIDSensorLog", fields: [sensorLogId], references: [id], onDelete: Cascade)
521
+ operator Operator? @relation("OperatorToSensorLogs", fields: [operatorId], references: [id])
522
+
523
+ createdAt DateTime @default(now())
524
+ updatedAt DateTime @updatedAt
525
+
526
+ @@schema("device")
527
+ }
528
+
529
+ model ProximitySensorLog {
530
+ id BigInt @id @default(autoincrement()) @db.BigInt
531
+ sensorLogId BigInt @unique
532
+ sensorLog SensorLog @relation("SensorLogProximityLog", fields: [sensorLogId], references: [id], onDelete: Cascade)
533
+ createdAt DateTime @default(now())
534
+ updatedAt DateTime @updatedAt
535
+
536
+ @@schema("device")
537
+ }
538
+
539
+ model InfraredSensorLog {
540
+ id BigInt @id @default(autoincrement()) @db.BigInt
541
+ sensorLogId BigInt @unique
542
+ sensorLog SensorLog @relation("SensorLogInfraredLog", fields: [sensorLogId], references: [id], onDelete: Cascade)
543
+ createdAt DateTime @default(now())
544
+ updatedAt DateTime @updatedAt
545
+
546
+ @@schema("device")
547
+ }
548
+
549
+ model CurrentSensorLog {
550
+ id BigInt @id @default(autoincrement()) @db.BigInt
551
+ sensorLogId BigInt @unique
552
+ currentValue Float?
553
+ sensorLog SensorLog @relation("SensorLogCurrentLog", fields: [sensorLogId], references: [id], onDelete: Cascade)
554
+ createdAt DateTime @default(now())
555
+ updatedAt DateTime @updatedAt
556
+
557
+ @@schema("device")
558
+ }
559
+
560
+ model DevicePowerSensorLog {
561
+ id BigInt @id @default(autoincrement()) @db.BigInt
562
+ sensorLogId BigInt @unique
563
+ powerState DevicePowerState
564
+ sensorLog SensorLog @relation("SensorLogDevicePowerSensorLog", fields: [sensorLogId], references: [id], onDelete: Cascade)
565
+ createdAt DateTime @default(now())
566
+ updatedAt DateTime @updatedAt
567
+
568
+ @@schema("device")
569
+ }
570
+
571
+ enum WeekDay {
572
+ SUNDAY
573
+ MONDAY
574
+ TUESDAY
575
+ WEDNESDAY
576
+ THURSDAY
577
+ FRIDAY
578
+ SATURDAY
579
+
580
+ @@schema("factory")
581
+ }
582
+
583
+ enum AccessLevel {
584
+ PUBLIC
585
+ PRIVATE
586
+
587
+ @@schema("factory")
588
+ }
589
+
590
+ model Factory {
591
+ id Int @id @default(autoincrement())
592
+ name String
593
+ description String?
594
+ organizationId Int
595
+ timeZone String @default("UTC+5:30")
596
+ timeZoneString String @default("Asia/Kolkata")
597
+
598
+ // Hierarchy
599
+ floors Floor[] @relation("FactoryToFloors")
600
+ shifts FactoryShift[] @relation("FactoryToShifts")
601
+
602
+ // Assets & IoT
603
+ sensors Sensor[] @relation("FactoryToSensors")
604
+ devicesDirect Device[] @relation("FactoryToDevicesDirect")
605
+
606
+ // People
607
+ operators Operator[] @relation("FactoryToOperators")
608
+ technicians MaintenanceTechnician[] @relation("FactoryToTechnicians")
609
+
610
+ // Mini-ERP & Production
611
+ finalProducts FinalProduct[] @relation("FactoryToFinalProducts")
612
+ orders Order[] @relation("FactoryToOrders")
613
+ lots FactoryLot[] @relation("Factory_Lots")
614
+
615
+ // Task Management
616
+ tasksHistory Task[] @relation("Factory_Tasks")
617
+ taskGroups TaskGroup[] @relation("Factory_TaskGroups")
618
+ opCategories OperatorOperationCategory[] @relation("Factory_OperatorOperationCategories")
619
+
620
+ // Maintenance & Analytics
621
+ requestCategories MaintenanceRequestCategory[] @relation("FactoryMaintenanceRequestCategories")
622
+ hourlyAnalytics FactoryHourlyAnalytics[] @relation("FactoryToHourlyAnalytics")
623
+
624
+ createdAt DateTime @default(now())
625
+ updatedAt DateTime @updatedAt
626
+
627
+ @@index([organizationId])
628
+ @@schema("factory")
629
+ }
630
+
631
+ model Floor {
632
+ id Int @id @default(autoincrement())
633
+ code String?
634
+ name String
635
+ description String?
636
+ factoryId Int
637
+ isActive Boolean @default(true)
638
+ searchTags String[] @default([])
639
+ accessLevel AccessLevel @default(PUBLIC)
640
+
641
+ // Relations
642
+ lines AssemblyLine[] @relation("FloorToLines")
643
+ gateways Gateway[] @relation("FloorToGateways")
644
+ sensors Sensor[] @relation("FloorToSensors")
645
+ devicesDirect Device[] @relation("FloorToDevicesDirect")
646
+ hourlyAnalytics FloorHourlyAnalytics[] @relation("FloorToHourlyAnalytics")
647
+
648
+ // Access Control (FIXED: Added this relation)
649
+ userAccess OrganizationUserFactoryEntityAccess[] @relation("UserToFloors")
650
+
651
+ floorTypeId Int?
652
+ floorType FloorType? @relation("FloorToFloorType", fields: [floorTypeId], references: [id])
653
+ settings FloorSetting? @relation("FloorsToFloorSettings")
654
+ factory Factory @relation("FactoryToFloors", fields: [factoryId], references: [id])
655
+
656
+ createdAt DateTime @default(now())
657
+ updatedAt DateTime @updatedAt
658
+
659
+ @@index([factoryId])
660
+ @@schema("factory")
661
+ }
662
+
663
+ model FloorType {
664
+ id Int @id @default(autoincrement())
665
+ name String
666
+ organizationId Int
667
+ floors Floor[] @relation("FloorToFloorType")
668
+ createdAt DateTime @default(now())
669
+ updatedAt DateTime @updatedAt
670
+
671
+ @@unique([organizationId, name])
672
+ @@schema("factory")
673
+ }
674
+
675
+ model FloorSetting {
676
+ id Int @id @default(autoincrement())
677
+ floorId Int @unique
678
+ idleThresholdMinutes Int
679
+ oeeExcellentPct Int
680
+ oeeAveragePct Int
681
+ oeePoorPct Int
682
+ downtimeWarnMinutes Int
683
+ downtimeAlertMinutes Int
684
+ peakEnergykWh Int
685
+ offPeakEnergykWh Int
686
+ energyAlertPct Int
687
+ floor Floor @relation("FloorsToFloorSettings", fields: [floorId], references: [id])
688
+ createdAt DateTime @default(now())
689
+ updatedAt DateTime @updatedAt
690
+
691
+ @@schema("factory")
692
+ }
693
+
694
+ model AssemblyLine {
695
+ id Int @id @default(autoincrement())
696
+ code String?
697
+ name String
698
+ description String?
699
+ floorId Int
700
+
701
+ machines Machine[] @relation("LineToMachines")
702
+ sensors Sensor[] @relation("AssemblyLineToSensors")
703
+ devicesDirect Device[] @relation("AssemblyLineToDevicesDirect")
704
+ hourlyAnalytics LineHourlyAnalytics[] @relation("LineToHourlyAnalytics")
705
+
706
+ // Access Control (FIXED: Added this relation)
707
+ userAccess OrganizationUserFactoryEntityAccess[] @relation("UserToAssemblyLines")
708
+
709
+ floor Floor @relation("FloorToLines", fields: [floorId], references: [id])
710
+ machineOrder Int[] @default([])
711
+ assemblyLineTypeId Int?
712
+
713
+ createdAt DateTime @default(now())
714
+ updatedAt DateTime @updatedAt
715
+
716
+ @@index([floorId])
717
+ @@schema("factory")
718
+ }
719
+
720
+ model FactoryShift {
721
+ id Int @id @default(autoincrement())
722
+ factoryId Int
723
+ name String
724
+ startTime DateTime @db.Time(0)
725
+ endTime DateTime @db.Time(0)
726
+
727
+ machines FactoryShiftMachine[] @relation("ShiftToMachineAssignments")
728
+ tasks Task[] @relation("Task_Shift")
729
+ factory Factory @relation("FactoryToShifts", fields: [factoryId], references: [id])
730
+
731
+ createdAt DateTime @default(now())
732
+ updatedAt DateTime @updatedAt
733
+
734
+ @@index([factoryId])
735
+ @@schema("factory")
736
+ }
737
+
738
+ model FactoryShiftMachine {
739
+ id Int @id @default(autoincrement())
740
+ shiftId Int
741
+ machineId Int?
742
+ shift FactoryShift @relation("ShiftToMachineAssignments", fields: [shiftId], references: [id])
743
+ machine Machine? @relation("MachineToShiftAssignments", fields: [machineId], references: [id])
744
+
745
+ @@schema("factory")
746
+ }
747
+
748
+ model FactoryLot {
749
+ id Int @id @default(autoincrement())
750
+ lotNumber String
751
+ receivedAt DateTime
752
+ expiryAt DateTime?
753
+ factoryId Int
754
+
755
+ tasks TaskLot[] @relation("TaskToLots")
756
+ taskGroups TaskGroupLot[] @relation("TaskGroupToLots")
757
+ factory Factory @relation("Factory_Lots", fields: [factoryId], references: [id])
758
+
759
+ createdAt DateTime @default(now())
760
+ updatedAt DateTime @updatedAt
761
+
762
+ @@index([factoryId])
763
+ @@schema("factory")
764
+ }
765
+
766
+ model FinalProduct {
767
+ id Int @id @default(autoincrement())
768
+
769
+ // ERP OVERLAY FIELDS
770
+ externalId String? // REMOVED @index here
771
+
772
+ name String
773
+ code String?
774
+ factoryId Int
775
+ description String?
776
+
777
+ factory Factory @relation("FactoryToFinalProducts", fields: [factoryId], references: [id], onDelete: Cascade)
778
+ routes FinalProductRoute[] @relation("FinalProductToRoutes")
779
+ orderProducts OrderProduct[] @relation("OrderProductToProduct")
780
+
781
+ createdAt DateTime @default(now())
782
+ updatedAt DateTime @updatedAt
783
+
784
+ @@index([externalId]) // ADDED here
785
+ @@schema("inventory")
786
+ }
787
+
788
+ model FinalProductRoute {
789
+ id Int @id @default(autoincrement())
790
+ finalProductId Int
791
+ name String?
792
+ sequence Int
793
+
794
+ finalProduct FinalProduct @relation("FinalProductToRoutes", fields: [finalProductId], references: [id], onDelete: Cascade)
795
+ machineSteps FinalProductMachineOperationRoute[] @relation("FinalProductRouteToSteps")
796
+
797
+ dependencies FinalProductRouteDependency[] @relation("FinalProductRoute_Dependent")
798
+ dependents FinalProductRouteDependency[] @relation("FinalProductRoute_Prerequisite")
799
+ taskGroups TaskGroup[] @relation("FinalProductRouteToTaskGroups")
800
+
801
+ createdAt DateTime @default(now())
802
+ updatedAt DateTime @updatedAt
803
+
804
+ @@schema("inventory")
805
+ }
806
+
807
+ model FinalProductMachineOperationRoute {
808
+ id Int @id @default(autoincrement())
809
+ routeId Int
810
+ operationId Int
811
+ productAggregationFactor Int? @default(1)
812
+ sequence Int
813
+ expectedDuration Int?
814
+
815
+ route FinalProductRoute @relation("FinalProductRouteToSteps", fields: [routeId], references: [id], onDelete: Cascade)
816
+ operation MachineOperation @relation("RouteToOperations", fields: [operationId], references: [id], onDelete: Cascade)
817
+
818
+ createdAt DateTime @default(now())
819
+ updatedAt DateTime @updatedAt
820
+
821
+ @@schema("inventory")
822
+ }
823
+
824
+ model FinalProductRouteDependency {
825
+ routeId Int
826
+ dependsOnRouteId Int
827
+ route FinalProductRoute @relation("FinalProductRoute_Dependent", fields: [routeId], references: [id], onDelete: Cascade)
828
+ dependsOnRoute FinalProductRoute @relation("FinalProductRoute_Prerequisite", fields: [dependsOnRouteId], references: [id], onDelete: Cascade)
829
+
830
+ @@id([routeId, dependsOnRouteId])
831
+ @@schema("inventory")
832
+ }
833
+
834
+ enum ProductionSIUnit {
835
+ COUNT
836
+ WEIGHT
837
+ LITERS
838
+
839
+ @@schema("machine")
840
+ }
841
+
842
+ enum MachineAlertSeverity {
843
+ LOW
844
+ MEDIUM
845
+ HIGH
846
+ CRITICAL
847
+
848
+ @@schema("machine")
849
+ }
850
+
851
+ enum EventCategory {
852
+ GENERAL
853
+ MEDIUM
854
+ OPERATION
855
+ ALERT
856
+
857
+ @@schema("machine")
858
+ }
859
+
860
+ enum MachineTrackingType {
861
+ MANUAL
862
+ AUTOMATED
863
+
864
+ @@schema("machine")
865
+ }
866
+
867
+ model Machine {
868
+ id Int @id @default(autoincrement())
869
+ name String
870
+ details String?
871
+ idealCycleTimeInSeconds Int?
872
+ assemblyLineId Int?
873
+ machineOperationId Int?
874
+ trackingType MachineTrackingType @default(MANUAL)
875
+
876
+ // Production & Tasks
877
+ tasks Task[] @relation("Machine_UsedInTasks")
878
+
879
+ // Hierarchy
880
+ assemblyLine AssemblyLine? @relation("LineToMachines", fields: [assemblyLineId], references: [id])
881
+ operation MachineOperation? @relation("OperationToMachine", fields: [machineOperationId], references: [id])
882
+ shifts FactoryShiftMachine[] @relation("MachineToShiftAssignments")
883
+
884
+ // Hardware
885
+ sensors Sensor[] @relation("MachineToSensors")
886
+ devicesDirect Device[] @relation("MachineToDevicesDirect")
887
+ deviceAssignments MachineDeviceAssignment[] @relation("MachineToDeviceAssignments")
888
+
889
+ // Maintenance
890
+ maintenanceLogs MaintenanceLog[] @relation("MachineMaintenance")
891
+ maintenanceRequests MaintenanceRequest[] @relation("MaintenanceMachineToRequests")
892
+
893
+ // ANALYTICS - WATERFALL STRATEGY
894
+ hourlyAnalytics MachineHourlyAnalytics[] @relation("MachineToHourlyAnalytics")
895
+ dailyAnalytics MachineDailyAnalytics[] @relation("MachineToDailyAnalytics")
896
+ monthlyAnalytics MachineMonthlyAnalytics[] @relation("MachineToMonthlyAnalytics")
897
+
898
+ operatorTimeLogs OperatorTimeLog[] @relation("Machine_RFIDOperatorTimeLogs")
899
+ events MachineEvent[] @relation("MachineEvents")
900
+ alerts MachineAlert[] @relation("MachineToAlerts")
901
+ alertSubscriptions MachineAlertSubscription[]
902
+
903
+ // Access Control
904
+ userAccess OrganizationUserFactoryEntityAccess[] @relation("UserToMachines")
905
+
906
+ createdAt DateTime @default(now())
907
+ updatedAt DateTime @default(now())
908
+
909
+ @@index([assemblyLineId])
910
+ @@schema("machine")
911
+ }
912
+
913
+ model MachineOperation {
914
+ id Int @id @default(autoincrement())
915
+ operationName String
916
+ description String?
917
+ organizationId Int
918
+ productionSIUnit ProductionSIUnit @default(COUNT)
919
+
920
+ machines Machine[] @relation("OperationToMachine")
921
+ routeOperations FinalProductMachineOperationRoute[] @relation("RouteToOperations")
922
+ tasks Task[] @relation("MachineOperationTasks")
923
+
924
+ createdAt DateTime @default(now())
925
+ updatedAt DateTime @updatedAt
926
+
927
+ @@unique([organizationId, operationName])
928
+ @@schema("machine")
929
+ }
930
+
931
+ model MachineEvent {
932
+ id Int @id @default(autoincrement())
933
+ machineId Int
934
+ eventTime DateTime
935
+ category EventCategory
936
+ title String
937
+ subtitle String?
938
+ description String?
939
+ machine Machine @relation("MachineEvents", fields: [machineId], references: [id])
940
+ createdAt DateTime @default(now())
941
+ updatedAt DateTime @updatedAt
942
+
943
+ @@index([machineId])
944
+ @@schema("machine")
945
+ }
946
+
947
+ model MachineAlert {
948
+ id Int @id @default(autoincrement())
949
+ machineId Int
950
+ title String
951
+ message String
952
+ severity MachineAlertSeverity @default(LOW)
953
+ createdAt DateTime @default(now())
954
+ resolvedAt DateTime?
955
+ machine Machine @relation("MachineToAlerts", fields: [machineId], references: [id])
956
+ // Note: Subscriptions are not linked here. They are settings, not history.
957
+
958
+ @@schema("machine")
959
+ }
960
+
961
+ model MachineAlertSubscription {
962
+ id Int @id @default(autoincrement())
963
+ userId Int
964
+ machineId Int
965
+ machine Machine @relation(fields: [machineId], references: [id])
966
+ createdAt DateTime @default(now())
967
+
968
+ @@unique([userId, machineId])
969
+ @@schema("machine")
970
+ }
971
+
972
+ enum MaintenanceLogStatus {
973
+ OPEN
974
+ IN_PROGRESS
975
+ COMPLETED
976
+ CANCELLED
977
+
978
+ @@schema("maintenance")
979
+ }
980
+
981
+ model MaintenanceLog {
982
+ id Int @id @default(autoincrement())
983
+ machineId Int
984
+ orgId Int
985
+ reportedById Int?
986
+ description String?
987
+ status MaintenanceLogStatus @default(OPEN)
988
+ startTime DateTime?
989
+ endTime DateTime?
990
+
991
+ machine Machine @relation("MachineMaintenance", fields: [machineId], references: [id])
992
+
993
+ createdAt DateTime @default(now())
994
+ updatedAt DateTime @updatedAt
995
+
996
+ @@schema("maintenance")
997
+ }
998
+
999
+ model MaintenanceManagerConfig {
1000
+ id Int @id @default(autoincrement())
1001
+ managerId Int
1002
+ key String
1003
+ value Json
1004
+ updatedAt DateTime @updatedAt
1005
+
1006
+ @@unique([managerId, key])
1007
+ @@schema("maintenance")
1008
+ }
1009
+
1010
+ enum MaintenanceRequestStatus {
1011
+ NEW
1012
+ APPROVED
1013
+ ASSIGNED
1014
+ IN_PROGRESS
1015
+ COMPLETED
1016
+ CANCELLED
1017
+
1018
+ @@schema("maintenance")
1019
+ }
1020
+
1021
+ enum MaintenanceRequestPriority {
1022
+ LOW
1023
+ MEDIUM
1024
+ HIGH
1025
+ CRITICAL
1026
+
1027
+ @@schema("maintenance")
1028
+ }
1029
+
1030
+ model MaintenanceRequest {
1031
+ id Int @id @default(autoincrement())
1032
+ machineId Int
1033
+ requestedById Int
1034
+ title String
1035
+ description String
1036
+ status MaintenanceRequestStatus @default(NEW)
1037
+ priority MaintenanceRequestPriority @default(MEDIUM)
1038
+ categoryId Int
1039
+
1040
+ machine Machine @relation("MaintenanceMachineToRequests", fields: [machineId], references: [id])
1041
+ category MaintenanceRequestCategory @relation("MaintenanceRequestCategory", fields: [categoryId], references: [id])
1042
+ assignments MaintenanceRequestAssignment[]
1043
+ updates MaintenanceRequestUpdate[] @relation("MaintenanceReqUpdates")
1044
+
1045
+ createdAt DateTime @default(now())
1046
+ updatedAt DateTime @updatedAt
1047
+
1048
+ @@schema("maintenance")
1049
+ }
1050
+
1051
+ model MaintenanceRequestCategory {
1052
+ id Int @id @default(autoincrement())
1053
+ factoryId Int
1054
+ name String
1055
+ factory Factory @relation("FactoryMaintenanceRequestCategories", fields: [factoryId], references: [id])
1056
+ requests MaintenanceRequest[] @relation("MaintenanceRequestCategory")
1057
+ createdAt DateTime @default(now())
1058
+ updatedAt DateTime @updatedAt
1059
+
1060
+ @@unique([factoryId, name])
1061
+ @@schema("maintenance")
1062
+ }
1063
+
1064
+ model MaintenanceRequestAssignment {
1065
+ id Int @id @default(autoincrement())
1066
+ requestId Int
1067
+ technicianId Int
1068
+ assignedById Int
1069
+ assignedAt DateTime @default(now())
1070
+
1071
+ request MaintenanceRequest @relation(fields: [requestId], references: [id])
1072
+ technician MaintenanceTechnician @relation(fields: [technicianId], references: [id])
1073
+
1074
+ @@schema("maintenance")
1075
+ }
1076
+
1077
+ model MaintenanceRequestUpdate {
1078
+ id Int @id @default(autoincrement())
1079
+ requestId Int
1080
+ authorId Int
1081
+ message String
1082
+ createdAt DateTime @default(now())
1083
+ request MaintenanceRequest @relation("MaintenanceReqUpdates", fields: [requestId], references: [id])
1084
+ attachments MaintenanceRequestAttachment[]
1085
+
1086
+ @@schema("maintenance")
1087
+ }
1088
+
1089
+ model MaintenanceRequestAttachment {
1090
+ id Int @id @default(autoincrement())
1091
+ updateId Int
1092
+ fileUrl String
1093
+ fileName String
1094
+ uploadedAt DateTime @default(now())
1095
+ update MaintenanceRequestUpdate @relation(fields: [updateId], references: [id])
1096
+
1097
+ @@schema("maintenance")
1098
+ }
1099
+
1100
+ model MaintenanceTechnician {
1101
+ id Int @id @default(autoincrement())
1102
+ factoryId Int
1103
+ name String
1104
+ email String @unique
1105
+ phone String?
1106
+ hiredAt DateTime @default(now())
1107
+
1108
+ factory Factory @relation("FactoryToTechnicians", fields: [factoryId], references: [id])
1109
+ specializations TechnicianSpecialization[]
1110
+ assignments MaintenanceRequestAssignment[]
1111
+
1112
+ createdAt DateTime @default(now())
1113
+ updatedAt DateTime @updatedAt
1114
+
1115
+ @@schema("maintenance")
1116
+ }
1117
+
1118
+ model TechnicianSpecialization {
1119
+ id Int @id @default(autoincrement())
1120
+ technicianId Int
1121
+ specializationId Int // Links to Core DB Specialization
1122
+ technician MaintenanceTechnician @relation(fields: [technicianId], references: [id])
1123
+
1124
+ @@unique([technicianId, specializationId])
1125
+ @@schema("maintenance")
1126
+ }
1127
+
1128
+ model Operator {
1129
+ id Int @id @default(autoincrement())
1130
+ rfid String?
1131
+
1132
+ // ERP/HR Link
1133
+ externalId String? // REMOVED @index here
1134
+
1135
+ name String?
1136
+ description String?
1137
+ organizationId Int?
1138
+ factoryId Int?
1139
+
1140
+ tasks Task[] @relation("Task_Operator")
1141
+ timeLogs OperatorTimeLog[]
1142
+ factory Factory? @relation("FactoryToOperators", fields: [factoryId], references: [id])
1143
+ sensorLogs RFIDSensorLog[] @relation("OperatorToSensorLogs")
1144
+
1145
+ createdAt DateTime @default(now())
1146
+ updatedAt DateTime @updatedAt
1147
+
1148
+ @@unique([organizationId, rfid])
1149
+ @@index([externalId]) // This creates the index
1150
+ @@schema("operator")
1151
+ }
1152
+
1153
+ model OperatorTimeLog {
1154
+ id BigInt @id @default(autoincrement()) @db.BigInt
1155
+ operatorId Int
1156
+ checkInAt DateTime
1157
+ checkOutAt DateTime?
1158
+ deviceId Int?
1159
+ machineId Int?
1160
+
1161
+ // Efficiency metrics
1162
+ totalWorkingTime Int?
1163
+ totalIdleTime Int?
1164
+ totalNonWorkingTime Int?
1165
+ efficiency Float?
1166
+
1167
+ operator Operator @relation(fields: [operatorId], references: [id])
1168
+ device Device? @relation("Device_RFIDOperatorTimeLogs", fields: [deviceId], references: [id])
1169
+ machine Machine? @relation("Machine_RFIDOperatorTimeLogs", fields: [machineId], references: [id])
1170
+
1171
+ createdAt DateTime @default(now())
1172
+ updatedAt DateTime @updatedAt
1173
+
1174
+ @@schema("operator")
1175
+ }
1176
+
1177
+ enum OrderStatus {
1178
+ NEW
1179
+ CONFIRMED
1180
+ IN_PROGRESS
1181
+ ON_TRACK
1182
+ DELAYED
1183
+ AT_RISK
1184
+ COMPLETED
1185
+ CANCELED
1186
+
1187
+ @@schema("order")
1188
+ }
1189
+
1190
+ enum OrderPriority {
1191
+ LOW
1192
+ MEDIUM
1193
+ HIGH
1194
+ CRITICAL
1195
+
1196
+ @@schema("order")
1197
+ }
1198
+
1199
+ enum OrderProductStatus {
1200
+ NEW
1201
+ IN_PROGRESS
1202
+ ON_TRACK
1203
+ DELAYED
1204
+ AT_RISK
1205
+ COMPLETED
1206
+ CANCELED
1207
+
1208
+ @@schema("order")
1209
+ }
1210
+
1211
+ model Order {
1212
+ id Int @id @default(autoincrement())
1213
+ organizationId Int
1214
+
1215
+ // ERP OVERLAY FIELDS
1216
+ externalId String? @unique // e.g., "SAP-ORDER-1002"
1217
+ erpSource String? // e.g., "SAP_S4HANA", "MS_DYNAMICS"
1218
+
1219
+ customerId Int?
1220
+ orderNumber String?
1221
+ title String?
1222
+ description String?
1223
+ factoryId Int
1224
+ status OrderStatus @default(NEW)
1225
+ progress Float @default(0)
1226
+ priority OrderPriority? @default(LOW)
1227
+ deadline DateTime?
1228
+ startDate DateTime?
1229
+
1230
+ createdById Int?
1231
+ assignedToId Int?
1232
+ assignedById Int?
1233
+ approvalAssignedToId Int?
1234
+ approvedById Int?
1235
+
1236
+ orderProducts OrderProduct[] @relation("OrderToOrderProducts")
1237
+ factory Factory @relation("FactoryToOrders", fields: [factoryId], references: [id])
1238
+ orderComments OrderComment[] @relation("OrderToOrderComments")
1239
+
1240
+ createdAt DateTime @default(now())
1241
+ updatedAt DateTime @updatedAt
1242
+
1243
+ @@index([organizationId])
1244
+ @@index([customerId])
1245
+ @@index([factoryId])
1246
+ @@schema("order")
1247
+ }
1248
+
1249
+ model OrderProduct {
1250
+ id Int @id @default(autoincrement())
1251
+ orderId Int
1252
+ productId Int
1253
+ quantity Int @default(0)
1254
+ title String?
1255
+ description String?
1256
+ status OrderProductStatus @default(NEW)
1257
+ progress Float @default(0)
1258
+
1259
+ order Order @relation("OrderToOrderProducts", fields: [orderId], references: [id])
1260
+ product FinalProduct @relation("OrderProductToProduct", fields: [productId], references: [id])
1261
+ taskGroups TaskGroup[] @relation("OrderProductToTaskGroups")
1262
+ productionUnits ProductionUnit[] @relation("OrderProductToProductionUnits")
1263
+
1264
+ createdAt DateTime @default(now())
1265
+ updatedAt DateTime @updatedAt
1266
+
1267
+ @@index([orderId])
1268
+ @@index([productId])
1269
+ @@schema("order")
1270
+ }
1271
+
1272
+ model OrderComment {
1273
+ id Int @id @default(autoincrement())
1274
+ orderId Int
1275
+ order Order @relation("OrderToOrderComments", fields: [orderId], references: [id])
1276
+ organizationId Int
1277
+ authorId Int?
1278
+ text String
1279
+ createdAt DateTime @default(now())
1280
+ updatedAt DateTime @updatedAt
1281
+
1282
+ @@index([orderId])
1283
+ @@index([organizationId])
1284
+ @@index([authorId])
1285
+ @@schema("order")
1286
+ }
1287
+
1288
+ enum ProductionUnitStatus {
1289
+ IN_PRODUCTION
1290
+ IN_QA
1291
+ REWORK
1292
+ COMPLETE
1293
+ REJECTED
1294
+ SHIPPED
1295
+
1296
+ @@schema("production_tracking")
1297
+ }
1298
+
1299
+ model ProductionUnit {
1300
+ id String @id @default(cuid())
1301
+ tagId String @unique
1302
+ serialNumber String? @unique
1303
+ organizationId Int
1304
+ orderProductId Int
1305
+ currentStepId Int?
1306
+ status ProductionUnitStatus @default(IN_PRODUCTION)
1307
+
1308
+ logs ProductionUnitLog[] @relation("UnitLogs")
1309
+ qcRecords ProductionUnitQC[] @relation("UnitQCRecords")
1310
+ currentStep ProductionRouteStep? @relation("UnitToRouteStep")
1311
+ orderProduct OrderProduct @relation("OrderProductToProductionUnits", fields: [orderProductId], references: [id])
1312
+
1313
+ createdAt DateTime @default(now())
1314
+ updatedAt DateTime @updatedAt
1315
+
1316
+ @@index([orderProductId])
1317
+ @@schema("production_tracking")
1318
+ }
1319
+
1320
+ model ProductionUnitLog {
1321
+ id String @id @default(cuid())
1322
+ productionUnitId String
1323
+ eventId String
1324
+ details String?
1325
+ stepId Int?
1326
+ operatorId Int?
1327
+
1328
+ productionUnit ProductionUnit @relation("UnitLogs", fields: [productionUnitId], references: [id])
1329
+ step ProductionRouteStep? @relation("StepToLogs", fields: [stepId], references: [id])
1330
+
1331
+ createdAt DateTime @default(now())
1332
+
1333
+ @@index([productionUnitId])
1334
+ @@schema("production_tracking")
1335
+ }
1336
+
1337
+ model ProductionRouteStep {
1338
+ id Int @id @default(autoincrement())
1339
+ productionUnitId String @unique
1340
+ stepId Int?
1341
+ machineId Int?
1342
+ isCompleted Boolean @default(false)
1343
+
1344
+ unit ProductionUnit @relation("UnitToRouteStep", fields: [productionUnitId], references: [id])
1345
+ logs ProductionUnitLog[] @relation("StepToLogs")
1346
+
1347
+ createdAt DateTime @default(now())
1348
+ updatedAt DateTime @updatedAt
1349
+
1350
+ @@schema("production_tracking")
1351
+ }
1352
+
1353
+ // Add ProductionUnitQC as needed, linking to ProductionUnit
1354
+ model ProductionUnitQC {
1355
+ id String @id @default(cuid())
1356
+ productionUnitId String
1357
+ status String
1358
+ inspectorId Int?
1359
+ notes String?
1360
+ productionUnit ProductionUnit @relation("UnitQCRecords", fields: [productionUnitId], references: [id])
1361
+ createdAt DateTime @default(now())
1362
+ updatedAt DateTime @updatedAt
1363
+
1364
+ @@schema("production_tracking")
1365
+ }
1366
+
1367
+ model OrganizationUserFactoryEntityAccess {
1368
+ id Int @id @default(autoincrement())
1369
+ userId Int @unique // Links to CoreUser in Core DB
1370
+ createdAt DateTime @default(now())
1371
+ updatedAt DateTime @updatedAt
1372
+
1373
+ // Local Access Grants
1374
+ floors Floor[] @relation("UserToFloors")
1375
+ assemblyLines AssemblyLine[] @relation("UserToAssemblyLines")
1376
+ machines Machine[] @relation("UserToMachines")
1377
+
1378
+ @@index([userId])
1379
+ @@schema("organization")
1380
+ }
1381
+
1382
+ // ----------------------------------------------------------------------
1383
+ // FILE: prisma/data/schema.prisma
1384
+ // ----------------------------------------------------------------------
1385
+ // This is the "DATA DB" (Data Plane) template.
1386
+ // One instance of this schema will be deployed for EACH tenant.
1387
+ // It contains ALL tenant-specific data.
1388
+ // ----------------------------------------------------------------------
1389
+
1390
+ generator client {
1391
+ provider = "prisma-client-js"
1392
+ output = "../../client"
1393
+ binaryTargets = ["native", "debian-openssl-3.0.x", "rhel-openssl-3.0.x", "darwin-arm64"]
1394
+ }
1395
+
1396
+ datasource db {
1397
+ provider = "postgresql"
1398
+ schemas = ["factory", "machine", "device", "tasks", "maintenance", "analytics", "audit", "operator", "chatbot", "inventory", "order", "production_tracking", "organization"]
1399
+ }
1400
+
1401
+ enum TaskStatus {
1402
+ NEW
1403
+ IN_PROGRESS
1404
+ COMPLETED
1405
+ CANCELED
1406
+ OVERDUE
1407
+ INCOMPLETE
1408
+
1409
+ @@schema("tasks")
1410
+ }
1411
+
1412
+ enum TaskGroupStatus {
1413
+ NEW
1414
+ IN_PROGRESS
1415
+ COMPLETED
1416
+ CANCELED
1417
+ OVERDUE
1418
+ INCOMPLETE
1419
+
1420
+ @@schema("tasks")
1421
+ }
1422
+
1423
+ model OperatorOperationCategory {
1424
+ id Int @id @default(autoincrement())
1425
+ factoryId Int
1426
+ name String
1427
+ description String?
1428
+ tasks Task[] @relation("OperatorOperationCategory_Tasks")
1429
+ factory Factory @relation("Factory_OperatorOperationCategories", fields: [factoryId], references: [id])
1430
+ createdAt DateTime @default(now())
1431
+ updatedAt DateTime @updatedAt
1432
+
1433
+ @@index([factoryId])
1434
+ @@schema("tasks")
1435
+ }
1436
+
1437
+ model Task {
1438
+ id Int @id @default(autoincrement())
1439
+ operatorId Int?
1440
+ assignedById Int
1441
+ assignedToId Int?
1442
+ deviceId Int?
1443
+ machineId Int?
1444
+ taskGroupId Int?
1445
+ factoryId Int?
1446
+ organizationId Int
1447
+ operatorOpCategoryId Int?
1448
+ shiftId Int?
1449
+
1450
+ title String?
1451
+ description String?
1452
+ status TaskStatus @default(NEW)
1453
+
1454
+ startTime DateTime
1455
+ endTime DateTime
1456
+ targetCount Int?
1457
+ taskCount Int? @default(0)
1458
+ sequence Int @default(0)
1459
+ reworkOfTaskId Int?
1460
+ reworkCount Int @default(0)
1461
+ machineOperationId Int?
1462
+ startedAt DateTime?
1463
+ completedAt DateTime?
1464
+
1465
+ // Relations
1466
+ operator Operator? @relation("Task_Operator", fields: [operatorId], references: [id])
1467
+ device Device? @relation("Device_UsedInTasks", fields: [deviceId], references: [id])
1468
+ machine Machine? @relation("Machine_UsedInTasks", fields: [machineId], references: [id])
1469
+ taskGroup TaskGroup? @relation("TaskGroupTasks", fields: [taskGroupId], references: [id])
1470
+ shift FactoryShift? @relation("Task_Shift", fields: [shiftId], references: [id])
1471
+ factory Factory? @relation("Factory_Tasks", fields: [factoryId], references: [id])
1472
+ operatorOpCategory OperatorOperationCategory? @relation("OperatorOperationCategory_Tasks", fields: [operatorOpCategoryId], references: [id])
1473
+
1474
+ reworkOf Task? @relation("Task_Rework", fields: [reworkOfTaskId], references: [id])
1475
+ reworks Task[] @relation("Task_Rework")
1476
+
1477
+ lots TaskLot[] @relation("TaskToLots")
1478
+ inspections TaskInspection[]
1479
+ taskComments TaskComment[] @relation("OrderToTaskComments")
1480
+ machineOperation MachineOperation? @relation("MachineOperationTasks", fields: [machineOperationId], references: [id])
1481
+
1482
+ createdAt DateTime @default(now())
1483
+ updatedAt DateTime @updatedAt
1484
+
1485
+ @@index([shiftId])
1486
+ @@index([taskGroupId])
1487
+ @@schema("tasks")
1488
+ }
1489
+
1490
+ model TaskGroup {
1491
+ id Int @id @default(autoincrement())
1492
+ description String?
1493
+ status TaskGroupStatus @default(NEW)
1494
+ targetCount Int
1495
+ factoryId Int?
1496
+ organizationId Int?
1497
+ productRouteId Int?
1498
+ orderProductId Int?
1499
+ deadline DateTime
1500
+ reworkOfTaskGroupId Int?
1501
+ reworkCount Int @default(0)
1502
+ startedAt DateTime?
1503
+ completedAt DateTime?
1504
+
1505
+ reworkOf TaskGroup? @relation("TaskGroupRework", fields: [reworkOfTaskGroupId], references: [id])
1506
+ reworks TaskGroup[] @relation("TaskGroupRework")
1507
+ productMachineRoute FinalProductRoute? @relation("FinalProductRouteToTaskGroups", fields: [productRouteId], references: [id])
1508
+ orderProduct OrderProduct? @relation("OrderProductToTaskGroups", fields: [orderProductId], references: [id])
1509
+ tasks Task[] @relation("TaskGroupTasks")
1510
+ lots TaskGroupLot[] @relation("TaskGroupToLots")
1511
+ factory Factory? @relation("Factory_TaskGroups", fields: [factoryId], references: [id])
1512
+
1513
+ createdAt DateTime @default(now())
1514
+ updatedAt DateTime @updatedAt
1515
+
1516
+ @@schema("tasks")
1517
+ }
1518
+
1519
+ model TaskLot {
1520
+ id Int @id @default(autoincrement())
1521
+ taskId Int
1522
+ lotId Int
1523
+ task Task @relation("TaskToLots", fields: [taskId], references: [id])
1524
+ lot FactoryLot @relation("TaskToLots", fields: [lotId], references: [id])
1525
+
1526
+ @@unique([taskId, lotId])
1527
+ @@schema("tasks")
1528
+ }
1529
+
1530
+ model TaskGroupLot {
1531
+ id Int @id @default(autoincrement())
1532
+ taskGroupId Int
1533
+ lotId Int
1534
+ taskGroup TaskGroup @relation("TaskGroupToLots", fields: [taskGroupId], references: [id])
1535
+ lot FactoryLot @relation("TaskGroupToLots", fields: [lotId], references: [id])
1536
+
1537
+ @@unique([taskGroupId, lotId])
1538
+ @@schema("tasks")
1539
+ }
1540
+
1541
+ model TaskInspection {
1542
+ id Int @id @default(autoincrement())
1543
+ taskId Int
1544
+ inspectorId Int
1545
+ passed Boolean @default(false)
1546
+ defectCount Int @default(0)
1547
+ wasteCount Int @default(0)
1548
+ notes String?
1549
+ task Task @relation(fields: [taskId], references: [id])
1550
+ createdAt DateTime @default(now())
1551
+ updatedAt DateTime @updatedAt
1552
+
1553
+ @@schema("tasks")
1554
+ }
1555
+
1556
+ model TaskComment {
1557
+ id Int @id @default(autoincrement())
1558
+ taskId Int
1559
+ task Task @relation("OrderToTaskComments", fields: [taskId], references: [id])
1560
+ organizationId Int
1561
+ authorId Int?
1562
+ text String
1563
+ createdAt DateTime @default(now())
1564
+ updatedAt DateTime @updatedAt
1565
+
1566
+ @@schema("tasks")
1567
+ }