@juliobrim/prisma-shared 1.0.51 → 1.0.53

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,6 @@
1
+ -- DropIndex
2
+ DROP INDEX "Role_name_key";
3
+
4
+ -- CreateIndex
5
+ CREATE UNIQUE INDEX "Role_name_tenantId_key" ON "Role"("name", "tenantId");
6
+
@@ -0,0 +1,25 @@
1
+ /*
2
+ Warnings:
3
+
4
+ - You are about to drop the `ai_conversation` table. If the table is not empty, all the data it contains will be lost.
5
+ - You are about to drop the `ai_feedback` table. If the table is not empty, all the data it contains will be lost.
6
+ - You are about to drop the `ai_message` table. If the table is not empty, all the data it contains will be lost.
7
+
8
+ */
9
+ -- DropForeignKey
10
+ ALTER TABLE "ai_conversation" DROP CONSTRAINT "ai_conversation_tenantId_fkey";
11
+
12
+ -- DropForeignKey
13
+ ALTER TABLE "ai_conversation" DROP CONSTRAINT "ai_conversation_userId_fkey";
14
+
15
+ -- DropForeignKey
16
+ ALTER TABLE "ai_message" DROP CONSTRAINT "ai_message_conversationId_fkey";
17
+
18
+ -- DropTable
19
+ DROP TABLE "ai_conversation";
20
+
21
+ -- DropTable
22
+ DROP TABLE "ai_feedback";
23
+
24
+ -- DropTable
25
+ DROP TABLE "ai_message";
@@ -0,0 +1,201 @@
1
+ /*
2
+ Warnings:
3
+
4
+ - You are about to drop the `machine_formula` table. If the table is not empty, all the data it contains will be lost.
5
+ - You are about to drop the `machine_formula_result` table. If the table is not empty, all the data it contains will be lost.
6
+ - Made the column `tenantId` on table `Role` required. This step will fail if there are existing NULL values in that column.
7
+ - Made the column `tenantId` on table `break` required. This step will fail if there are existing NULL values in that column.
8
+ - Made the column `tenantId` on table `downtime_event` required. This step will fail if there are existing NULL values in that column.
9
+ - Made the column `tenantId` on table `downtime_reason` required. This step will fail if there are existing NULL values in that column.
10
+ - Made the column `tenantId` on table `edge` required. This step will fail if there are existing NULL values in that column.
11
+ - Made the column `tenantId` on table `flow` required. This step will fail if there are existing NULL values in that column.
12
+ - Made the column `tenantId` on table `follower` required. This step will fail if there are existing NULL values in that column.
13
+ - Made the column `tenantId` on table `input_register` required. This step will fail if there are existing NULL values in that column.
14
+ - Made the column `tenantId` on table `iot_message_register` required. This step will fail if there are existing NULL values in that column.
15
+ - Made the column `tenantId` on table `items_handling_register` required. This step will fail if there are existing NULL values in that column.
16
+ - Made the column `tenantId` on table `machine` required. This step will fail if there are existing NULL values in that column.
17
+ - Made the column `tenantId` on table `machine_operation_stitch` required. This step will fail if there are existing NULL values in that column.
18
+ - Made the column `tenantId` on table `machine_sensors` required. This step will fail if there are existing NULL values in that column.
19
+ - Made the column `tenantId` on table `node` required. This step will fail if there are existing NULL values in that column.
20
+ - Made the column `tenantId` on table `node_operation_queue` required. This step will fail if there are existing NULL values in that column.
21
+ - Made the column `tenantId` on table `notification` required. This step will fail if there are existing NULL values in that column.
22
+ - Made the column `tenantId` on table `notification_to_user` required. This step will fail if there are existing NULL values in that column.
23
+ - Made the column `tenantId` on table `operation` required. This step will fail if there are existing NULL values in that column.
24
+ - Made the column `tenantId` on table `operation_group` required. This step will fail if there are existing NULL values in that column.
25
+ - Made the column `tenantId` on table `operation_register` required. This step will fail if there are existing NULL values in that column.
26
+ - Made the column `tenantId` on table `operation_to_operation` required. This step will fail if there are existing NULL values in that column.
27
+ - Made the column `tenantId` on table `operator` required. This step will fail if there are existing NULL values in that column.
28
+ - Made the column `tenantId` on table `operator_availability_cache` required. This step will fail if there are existing NULL values in that column.
29
+ - Made the column `tenantId` on table `operator_role` required. This step will fail if there are existing NULL values in that column.
30
+ - Made the column `tenantId` on table `panel` required. This step will fail if there are existing NULL values in that column.
31
+ - Made the column `tenantId` on table `product` required. This step will fail if there are existing NULL values in that column.
32
+ - Made the column `tenantId` on table `product_batch` required. This step will fail if there are existing NULL values in that column.
33
+ - Made the column `tenantId` on table `product_components` required. This step will fail if there are existing NULL values in that column.
34
+ - Made the column `tenantId` on table `product_to_operation` required. This step will fail if there are existing NULL values in that column.
35
+ - Made the column `tenantId` on table `production_node` required. This step will fail if there are existing NULL values in that column.
36
+ - Made the column `tenantId` on table `production_order` required. This step will fail if there are existing NULL values in that column.
37
+ - Made the column `tenantId` on table `productivity_interval_cache` required. This step will fail if there are existing NULL values in that column.
38
+ - Made the column `tenantId` on table `quality_issue` required. This step will fail if there are existing NULL values in that column.
39
+ - Made the column `tenantId` on table `quality_register` required. This step will fail if there are existing NULL values in that column.
40
+ - Made the column `tenantId` on table `report` required. This step will fail if there are existing NULL values in that column.
41
+ - Made the column `tenantId` on table `resource` required. This step will fail if there are existing NULL values in that column.
42
+ - Made the column `tenantId` on table `sector` required. This step will fail if there are existing NULL values in that column.
43
+ - Made the column `tenantId` on table `sew_machine_raw_data` required. This step will fail if there are existing NULL values in that column.
44
+ - Made the column `tenantId` on table `shift` required. This step will fail if there are existing NULL values in that column.
45
+ - Made the column `tenantId` on table `shift_day` required. This step will fail if there are existing NULL values in that column.
46
+ - Made the column `tenantId` on table `system_preferences` required. This step will fail if there are existing NULL values in that column.
47
+ - Made the column `tenantId` on table `user` required. This step will fail if there are existing NULL values in that column.
48
+
49
+ */
50
+ -- DropForeignKey
51
+ ALTER TABLE "Role" DROP CONSTRAINT "Role_tenantId_fkey";
52
+
53
+ -- DropForeignKey
54
+ ALTER TABLE "machine_formula" DROP CONSTRAINT "machine_formula_machineId_fkey";
55
+
56
+ -- DropForeignKey
57
+ ALTER TABLE "machine_formula" DROP CONSTRAINT "machine_formula_tenantId_fkey";
58
+
59
+ -- DropForeignKey
60
+ ALTER TABLE "machine_formula_result" DROP CONSTRAINT "machine_formula_result_formulaId_fkey";
61
+
62
+ -- DropForeignKey
63
+ ALTER TABLE "machine_formula_result" DROP CONSTRAINT "machine_formula_result_machineId_fkey";
64
+
65
+ -- DropForeignKey
66
+ ALTER TABLE "machine_formula_result" DROP CONSTRAINT "machine_formula_result_tenantId_fkey";
67
+
68
+ -- AlterTable
69
+ ALTER TABLE "Role" ALTER COLUMN "tenantId" SET NOT NULL;
70
+
71
+ -- AlterTable
72
+ ALTER TABLE "break" ALTER COLUMN "tenantId" SET NOT NULL;
73
+
74
+ -- AlterTable
75
+ ALTER TABLE "downtime_event" ALTER COLUMN "tenantId" SET NOT NULL;
76
+
77
+ -- AlterTable
78
+ ALTER TABLE "downtime_reason" ALTER COLUMN "tenantId" SET NOT NULL;
79
+
80
+ -- AlterTable
81
+ ALTER TABLE "edge" ALTER COLUMN "tenantId" SET NOT NULL;
82
+
83
+ -- AlterTable
84
+ ALTER TABLE "flow" ALTER COLUMN "tenantId" SET NOT NULL;
85
+
86
+ -- AlterTable
87
+ ALTER TABLE "follower" ALTER COLUMN "tenantId" SET NOT NULL;
88
+
89
+ -- AlterTable
90
+ ALTER TABLE "input_register" ALTER COLUMN "tenantId" SET NOT NULL;
91
+
92
+ -- AlterTable
93
+ ALTER TABLE "iot_message_register" ALTER COLUMN "tenantId" SET NOT NULL;
94
+
95
+ -- AlterTable
96
+ ALTER TABLE "items_handling_register" ALTER COLUMN "tenantId" SET NOT NULL;
97
+
98
+ -- AlterTable
99
+ ALTER TABLE "machine" ALTER COLUMN "tenantId" SET NOT NULL;
100
+
101
+ -- AlterTable
102
+ ALTER TABLE "machine_operation_stitch" ALTER COLUMN "tenantId" SET NOT NULL;
103
+
104
+ -- AlterTable
105
+ ALTER TABLE "machine_sensors" ALTER COLUMN "tenantId" SET NOT NULL;
106
+
107
+ -- AlterTable
108
+ ALTER TABLE "node" ALTER COLUMN "tenantId" SET NOT NULL;
109
+
110
+ -- AlterTable
111
+ ALTER TABLE "node_operation_queue" ALTER COLUMN "tenantId" SET NOT NULL;
112
+
113
+ -- AlterTable
114
+ ALTER TABLE "notification" ALTER COLUMN "tenantId" SET NOT NULL;
115
+
116
+ -- AlterTable
117
+ ALTER TABLE "notification_to_user" ALTER COLUMN "tenantId" SET NOT NULL;
118
+
119
+ -- AlterTable
120
+ ALTER TABLE "operation" ALTER COLUMN "tenantId" SET NOT NULL;
121
+
122
+ -- AlterTable
123
+ ALTER TABLE "operation_group" ALTER COLUMN "tenantId" SET NOT NULL;
124
+
125
+ -- AlterTable
126
+ ALTER TABLE "operation_register" ALTER COLUMN "tenantId" SET NOT NULL;
127
+
128
+ -- AlterTable
129
+ ALTER TABLE "operation_to_operation" ALTER COLUMN "tenantId" SET NOT NULL;
130
+
131
+ -- AlterTable
132
+ ALTER TABLE "operator" ALTER COLUMN "tenantId" SET NOT NULL;
133
+
134
+ -- AlterTable
135
+ ALTER TABLE "operator_availability_cache" ALTER COLUMN "tenantId" SET NOT NULL;
136
+
137
+ -- AlterTable
138
+ ALTER TABLE "operator_role" ALTER COLUMN "tenantId" SET NOT NULL;
139
+
140
+ -- AlterTable
141
+ ALTER TABLE "panel" ALTER COLUMN "tenantId" SET NOT NULL;
142
+
143
+ -- AlterTable
144
+ ALTER TABLE "product" ALTER COLUMN "tenantId" SET NOT NULL;
145
+
146
+ -- AlterTable
147
+ ALTER TABLE "product_batch" ALTER COLUMN "tenantId" SET NOT NULL;
148
+
149
+ -- AlterTable
150
+ ALTER TABLE "product_components" ALTER COLUMN "tenantId" SET NOT NULL;
151
+
152
+ -- AlterTable
153
+ ALTER TABLE "product_to_operation" ALTER COLUMN "tenantId" SET NOT NULL;
154
+
155
+ -- AlterTable
156
+ ALTER TABLE "production_node" ALTER COLUMN "tenantId" SET NOT NULL;
157
+
158
+ -- AlterTable
159
+ ALTER TABLE "production_order" ALTER COLUMN "tenantId" SET NOT NULL;
160
+
161
+ -- AlterTable
162
+ ALTER TABLE "productivity_interval_cache" ALTER COLUMN "tenantId" SET NOT NULL;
163
+
164
+ -- AlterTable
165
+ ALTER TABLE "quality_issue" ALTER COLUMN "tenantId" SET NOT NULL;
166
+
167
+ -- AlterTable
168
+ ALTER TABLE "quality_register" ALTER COLUMN "tenantId" SET NOT NULL;
169
+
170
+ -- AlterTable
171
+ ALTER TABLE "report" ALTER COLUMN "tenantId" SET NOT NULL;
172
+
173
+ -- AlterTable
174
+ ALTER TABLE "resource" ALTER COLUMN "tenantId" SET NOT NULL;
175
+
176
+ -- AlterTable
177
+ ALTER TABLE "sector" ALTER COLUMN "tenantId" SET NOT NULL;
178
+
179
+ -- AlterTable
180
+ ALTER TABLE "sew_machine_raw_data" ALTER COLUMN "tenantId" SET NOT NULL;
181
+
182
+ -- AlterTable
183
+ ALTER TABLE "shift" ALTER COLUMN "tenantId" SET NOT NULL;
184
+
185
+ -- AlterTable
186
+ ALTER TABLE "shift_day" ALTER COLUMN "tenantId" SET NOT NULL;
187
+
188
+ -- AlterTable
189
+ ALTER TABLE "system_preferences" ALTER COLUMN "tenantId" SET NOT NULL;
190
+
191
+ -- AlterTable
192
+ ALTER TABLE "user" ALTER COLUMN "tenantId" SET NOT NULL;
193
+
194
+ -- DropTable
195
+ DROP TABLE "machine_formula";
196
+
197
+ -- DropTable
198
+ DROP TABLE "machine_formula_result";
199
+
200
+ -- AddForeignKey
201
+ ALTER TABLE "Role" ADD CONSTRAINT "Role_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "tenant"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,67 @@
1
+ /*
2
+ Warnings:
3
+
4
+ - A unique constraint covering the columns `[tenantId,name]` on the table `downtime_reason` will be added. If there are existing duplicate values, this will fail.
5
+ - A unique constraint covering the columns `[tenantId,name]` on the table `machine` will be added. If there are existing duplicate values, this will fail.
6
+ - A unique constraint covering the columns `[tenantId,name]` on the table `operator` will be added. If there are existing duplicate values, this will fail.
7
+ - A unique constraint covering the columns `[tenantId,productId,batchNumber,internalBatchNumber]` on the table `product_batch` will be added. If there are existing duplicate values, this will fail.
8
+ - A unique constraint covering the columns `[tenantId,productionOrderIndex]` on the table `production_order` will be added. If there are existing duplicate values, this will fail.
9
+ - A unique constraint covering the columns `[tenantId,name]` on the table `resource` will be added. If there are existing duplicate values, this will fail.
10
+ - A unique constraint covering the columns `[tenantId,name]` on the table `sector` will be added. If there are existing duplicate values, this will fail.
11
+ - A unique constraint covering the columns `[tenantId,name]` on the table `shift` will be added. If there are existing duplicate values, this will fail.
12
+ - A unique constraint covering the columns `[tenantId,email]` on the table `user` will be added. If there are existing duplicate values, this will fail.
13
+
14
+ */
15
+ -- DropIndex
16
+ DROP INDEX "downtime_reason_name_key";
17
+
18
+ -- DropIndex
19
+ DROP INDEX "machine_name_key";
20
+
21
+ -- DropIndex
22
+ DROP INDEX "operator_name_key";
23
+
24
+ -- DropIndex
25
+ DROP INDEX "product_batch_productId_batchNumber_internalBatchNumber_key";
26
+
27
+ -- DropIndex
28
+ DROP INDEX "production_order_productionOrderIndex_key";
29
+
30
+ -- DropIndex
31
+ DROP INDEX "resource_name_key";
32
+
33
+ -- DropIndex
34
+ DROP INDEX "sector_name_key";
35
+
36
+ -- DropIndex
37
+ DROP INDEX "shift_name_key";
38
+
39
+ -- DropIndex
40
+ DROP INDEX "user_email_key";
41
+
42
+ -- CreateIndex
43
+ CREATE UNIQUE INDEX "downtime_reason_tenantId_name_key" ON "downtime_reason"("tenantId", "name");
44
+
45
+ -- CreateIndex
46
+ CREATE UNIQUE INDEX "machine_tenantId_name_key" ON "machine"("tenantId", "name");
47
+
48
+ -- CreateIndex
49
+ CREATE UNIQUE INDEX "operator_tenantId_name_key" ON "operator"("tenantId", "name");
50
+
51
+ -- CreateIndex
52
+ CREATE UNIQUE INDEX "product_batch_tenantId_productId_batchNumber_internalBatchN_key" ON "product_batch"("tenantId", "productId", "batchNumber", "internalBatchNumber");
53
+
54
+ -- CreateIndex
55
+ CREATE UNIQUE INDEX "production_order_tenantId_productionOrderIndex_key" ON "production_order"("tenantId", "productionOrderIndex");
56
+
57
+ -- CreateIndex
58
+ CREATE UNIQUE INDEX "resource_tenantId_name_key" ON "resource"("tenantId", "name");
59
+
60
+ -- CreateIndex
61
+ CREATE UNIQUE INDEX "sector_tenantId_name_key" ON "sector"("tenantId", "name");
62
+
63
+ -- CreateIndex
64
+ CREATE UNIQUE INDEX "shift_tenantId_name_key" ON "shift"("tenantId", "name");
65
+
66
+ -- CreateIndex
67
+ CREATE UNIQUE INDEX "user_tenantId_email_key" ON "user"("tenantId", "email");
@@ -0,0 +1,85 @@
1
+ -- RLS multi-tenant - onda 1
2
+ --
3
+ -- Este arquivo prepara as tabelas tenant-scoped mais criticas para isolamento
4
+ -- definitivo no PostgreSQL. Ele deve ser portado para a origem do pacote
5
+ -- shared Prisma e executado como migration SQL, nao aplicado manualmente em
6
+ -- node_modules.
7
+ --
8
+ -- Pre-requisitos:
9
+ -- 1. As tabelas abaixo devem ter "tenantId" NOT NULL.
10
+ -- 2. A aplicacao deve abrir transacoes com:
11
+ -- SELECT set_config('app.tenant_id', '<tenant-id>', true);
12
+ -- 3. O usuario tecnico do banco nao deve ter BYPASSRLS.
13
+ --
14
+ -- Rollout sugerido:
15
+ -- 1. Subir a aplicacao ja usando withTenantRlsTransaction().
16
+ -- 2. Aplicar esta migration em staging.
17
+ -- 3. Validar fluxos de leitura/escrita e acessos cross-tenant.
18
+ -- 4. Aplicar em producao por janela controlada.
19
+
20
+ BEGIN;
21
+
22
+ CREATE OR REPLACE FUNCTION app_current_tenant_id()
23
+ RETURNS text
24
+ LANGUAGE sql
25
+ STABLE
26
+ AS $$
27
+ SELECT NULLIF(current_setting('app.tenant_id', true), '');
28
+ $$;
29
+
30
+ DO $$
31
+ DECLARE
32
+ tenant_tables text[] := ARRAY[
33
+ 'flow',
34
+ 'machine',
35
+ 'node',
36
+ 'operation',
37
+ 'operator',
38
+ 'panel',
39
+ 'product',
40
+ 'production_node',
41
+ 'production_order',
42
+ 'report',
43
+ 'resource',
44
+ 'Role',
45
+ 'sector',
46
+ 'shift',
47
+ 'user'
48
+ ];
49
+ table_name text;
50
+ BEGIN
51
+ FOREACH table_name IN ARRAY tenant_tables LOOP
52
+ EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', table_name);
53
+ EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY', table_name);
54
+
55
+ EXECUTE format('DROP POLICY IF EXISTS %I ON %I', table_name || '_tenant_select', table_name);
56
+ EXECUTE format(
57
+ 'CREATE POLICY %I ON %I FOR SELECT USING ("tenantId" = app_current_tenant_id())',
58
+ table_name || '_tenant_select',
59
+ table_name
60
+ );
61
+
62
+ EXECUTE format('DROP POLICY IF EXISTS %I ON %I', table_name || '_tenant_insert', table_name);
63
+ EXECUTE format(
64
+ 'CREATE POLICY %I ON %I FOR INSERT WITH CHECK ("tenantId" = app_current_tenant_id())',
65
+ table_name || '_tenant_insert',
66
+ table_name
67
+ );
68
+
69
+ EXECUTE format('DROP POLICY IF EXISTS %I ON %I', table_name || '_tenant_update', table_name);
70
+ EXECUTE format(
71
+ 'CREATE POLICY %I ON %I FOR UPDATE USING ("tenantId" = app_current_tenant_id()) WITH CHECK ("tenantId" = app_current_tenant_id())',
72
+ table_name || '_tenant_update',
73
+ table_name
74
+ );
75
+
76
+ EXECUTE format('DROP POLICY IF EXISTS %I ON %I', table_name || '_tenant_delete', table_name);
77
+ EXECUTE format(
78
+ 'CREATE POLICY %I ON %I FOR DELETE USING ("tenantId" = app_current_tenant_id())',
79
+ table_name || '_tenant_delete',
80
+ table_name
81
+ );
82
+ END LOOP;
83
+ END $$;
84
+
85
+ COMMIT;
@@ -0,0 +1,5 @@
1
+ -- CreateIndex
2
+ CREATE INDEX "pic_operator_end_idx" ON "productivity_interval_cache"("operatorId", "group_end_timestamp");
3
+
4
+ -- CreateIndex
5
+ CREATE INDEX "pic_tenant_operator_end_idx" ON "productivity_interval_cache"("tenantId", "operatorId", "group_end_timestamp");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juliobrim/prisma-shared",
3
- "version": "1.0.51",
3
+ "version": "1.0.53",
4
4
  "description": "Schema Prisma compartilhado entre projetos Sabcon",
5
5
  "main": "schema.prisma",
6
6
  "files": [
package/schema.prisma CHANGED
@@ -29,8 +29,6 @@ model Tenant {
29
29
  IotMessageRegister IotMessageRegister[]
30
30
  itemsHandlingRegisters ItemsHandlingRegister[]
31
31
  machines Machine[]
32
- MachineFormula MachineFormula[]
33
- MachineFormulaResult MachineFormulaResult[]
34
32
  machineSensors MachineSensors[]
35
33
  nodes Node[]
36
34
  nodeOperationQueue NodeOperationQueue[]
@@ -63,7 +61,6 @@ model Tenant {
63
61
  machineOperationStitch MachineOperationStitch[]
64
62
  productivityIntervals ProductivityIntervalCache[]
65
63
  operatorAvailability OperatorAvailabilityCache[]
66
- aiConversations AiConversation[]
67
64
  qualityRegisters QualityRegister[]
68
65
  qualityIssues QualityIssue[]
69
66
 
@@ -98,9 +95,9 @@ model Break {
98
95
  start_time DateTime @db.Time(6)
99
96
  end_time DateTime @db.Time(6)
100
97
  shiftDayId String?
101
- tenantId String?
98
+ tenantId String
102
99
  shiftDay ShiftDay? @relation(fields: [shiftDayId], references: [id], onDelete: Cascade)
103
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
100
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
104
101
 
105
102
  @@map("break")
106
103
  }
@@ -116,8 +113,8 @@ model DowntimeEvent {
116
113
  end_time DateTime?
117
114
  elapsedTime BigInt?
118
115
  isJustified Boolean @default(false)
119
- tenantId String?
120
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
116
+ tenantId String
117
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
121
118
  downtimeReason DowntimeReason? @relation(fields: [downtimeReasonId], references: [id], onUpdate: NoAction, map: "fk_downtimeReason")
122
119
  machine Machine @relation(fields: [machine_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "fk_machine")
123
120
  operation Operation? @relation(fields: [operationId], references: [id], onDelete: Cascade, map: "fk_operation")
@@ -134,14 +131,15 @@ model DowntimeEvent {
134
131
 
135
132
  model DowntimeReason {
136
133
  id String @id @default(cuid())
137
- name String @unique @db.VarChar(100)
134
+ name String @db.VarChar(100)
138
135
  description String? @db.VarChar(255)
139
136
  isImpactful Boolean @default(false)
140
137
  isDeleted Boolean @default(false)
141
- tenantId String?
138
+ tenantId String
142
139
  downtime DowntimeEvent[]
143
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
140
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
144
141
 
142
+ @@unique([tenantId, name])
145
143
  @@map("downtime_reason")
146
144
  }
147
145
 
@@ -149,10 +147,10 @@ model Edge {
149
147
  amount Int
150
148
  inputNodeId String
151
149
  outputNodeId String
152
- tenantId String?
153
- inputNode Node @relation("inputNode", fields: [inputNodeId], references: [id], onDelete: Cascade)
154
- outputNode Node @relation("outputNode", fields: [outputNodeId], references: [id], onDelete: Cascade)
155
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
150
+ tenantId String
151
+ inputNode Node @relation("inputNode", fields: [inputNodeId], references: [id], onDelete: Cascade)
152
+ outputNode Node @relation("outputNode", fields: [outputNodeId], references: [id], onDelete: Cascade)
153
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
156
154
 
157
155
  @@id([inputNodeId, outputNodeId])
158
156
  @@map("edge")
@@ -163,9 +161,9 @@ model Flow {
163
161
  title String
164
162
  productId String?
165
163
  isDeleted Boolean @default(false)
166
- tenantId String?
164
+ tenantId String
167
165
  product Product? @relation(fields: [productId], references: [id])
168
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
166
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
169
167
  nodes Node[]
170
168
  operationGroups OperationGroup[]
171
169
  productionOrders ProductionOrder[]
@@ -176,9 +174,9 @@ model Flow {
176
174
  model Follower {
177
175
  userId String
178
176
  machineId String
179
- tenantId String?
177
+ tenantId String
180
178
  machine Machine @relation(fields: [machineId], references: [id])
181
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
179
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
182
180
  user User @relation(fields: [userId], references: [id])
183
181
 
184
182
  @@id([userId, machineId])
@@ -193,13 +191,13 @@ model InputRegister {
193
191
  in4Count BigInt @map("in4_count")
194
192
  timestamp DateTime @default(now())
195
193
  machineId String
196
- tenantId String?
194
+ tenantId String
197
195
  operationId String?
198
196
  productionOrderId String?
199
197
  machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
200
198
  operation Operation? @relation(fields: [operationId], references: [id], onDelete: Cascade)
201
199
  productionOrder ProductionOrder? @relation(fields: [productionOrderId], references: [id], onDelete: Cascade)
202
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
200
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
203
201
 
204
202
  @@index([machineId, timestamp])
205
203
  @@index([productionOrderId])
@@ -219,9 +217,9 @@ model IotMessageRegister {
219
217
  di3State Boolean @map("di3_state")
220
218
  di4State Boolean @map("di4_state")
221
219
  machineId String
222
- tenantId String?
220
+ tenantId String
223
221
  machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
224
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
222
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
225
223
 
226
224
  @@index([machineId, timestamp])
227
225
  @@map("iot_message_register")
@@ -238,7 +236,7 @@ model ItemsHandlingRegister {
238
236
  sectorFromId String?
239
237
  sectorToId String?
240
238
  timestamp DateTime @default(now())
241
- tenantId String?
239
+ tenantId String
242
240
  operation Operation? @relation(fields: [operationId], references: [id])
243
241
  product Product? @relation(fields: [productId], references: [id])
244
242
  productionOrder ProductionOrder @relation(fields: [productionOrderId], references: [id])
@@ -246,7 +244,7 @@ model ItemsHandlingRegister {
246
244
  sectorTo Sector? @relation("sectorTo", fields: [sectorToId], references: [id])
247
245
  stationFrom Machine? @relation("stationFrom", fields: [stationFromId], references: [id])
248
246
  stationTo Machine? @relation("stationTo", fields: [stationToId], references: [id])
249
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
247
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
250
248
 
251
249
  @@index([productionOrderId])
252
250
  @@index([productId])
@@ -258,14 +256,14 @@ model ItemsHandlingRegister {
258
256
  model Machine {
259
257
  id String @id @default(cuid())
260
258
  model String? @db.VarChar(100)
261
- name String @unique @db.VarChar(100)
259
+ name String @db.VarChar(100)
262
260
  capacity Float? @db.Real
263
261
  clp_index String?
264
262
  acquired_date DateTime? @db.Date
265
263
  macAddress String? @db.VarChar(17)
266
264
  isDeleted Boolean @default(false)
267
265
  sectorId String?
268
- tenantId String?
266
+ tenantId String
269
267
  type MachineType? @default(discrete)
270
268
  device Device?
271
269
  downtime DowntimeEvent[]
@@ -275,9 +273,7 @@ model Machine {
275
273
  stationFrom ItemsHandlingRegister[] @relation("stationFrom")
276
274
  stationTo ItemsHandlingRegister[] @relation("stationTo")
277
275
  sector Sector? @relation(fields: [sectorId], references: [id])
278
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
279
- MachineFormula MachineFormula[]
280
- MachineFormulaResult MachineFormulaResult[]
276
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
281
277
  machineSensors MachineSensors?
282
278
  operationRegister OperationRegister[]
283
279
  productionNodes ProductionNode?
@@ -289,6 +285,7 @@ model Machine {
289
285
  machineOperationStitch MachineOperationStitch[]
290
286
  productivityIntervals ProductivityIntervalCache[]
291
287
 
288
+ @@unique([tenantId, name])
292
289
  @@index([sectorId])
293
290
  @@index([tenantId])
294
291
  @@index([isDeleted])
@@ -313,39 +310,6 @@ model ProcessingScript {
313
310
  @@map("processing_script")
314
311
  }
315
312
 
316
- model MachineFormula {
317
- id String @id @default(cuid())
318
- machineId String
319
- name String
320
- formula Json
321
- isActive Boolean @default(true)
322
- createdAt DateTime @default(now())
323
- updatedAt DateTime @updatedAt
324
- tenantId String?
325
- machine Machine @relation(fields: [machineId], references: [id])
326
- tenant Tenant? @relation(fields: [tenantId], references: [id])
327
- results MachineFormulaResult[]
328
-
329
- @@map("machine_formula")
330
- }
331
-
332
- model MachineFormulaResult {
333
- id String @id @default(cuid())
334
- machineId String
335
- formulaId String
336
- timestamp DateTime @default(now())
337
- sensorData Json
338
- result Json
339
- tenantId String?
340
- formula MachineFormula @relation(fields: [formulaId], references: [id])
341
- machine Machine @relation(fields: [machineId], references: [id])
342
- tenant Tenant? @relation(fields: [tenantId], references: [id])
343
-
344
- @@index([machineId, timestamp])
345
- @@index([formulaId])
346
- @@map("machine_formula_result")
347
- }
348
-
349
313
  model Node {
350
314
  id String @id @default(cuid())
351
315
  positionAbsoluteX Int
@@ -353,12 +317,12 @@ model Node {
353
317
  type NodeType
354
318
  flowId String
355
319
  productId String?
356
- tenantId String?
320
+ tenantId String
357
321
  inputOf Edge[] @relation("inputNode")
358
322
  outputOf Edge[] @relation("outputNode")
359
323
  flow Flow @relation(fields: [flowId], references: [id], onDelete: Cascade)
360
324
  product Product? @relation(fields: [productId], references: [id])
361
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
325
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
362
326
  nodeOperationQueue NodeOperationQueue[]
363
327
  operation Operation?
364
328
  productionNode ProductionNode?
@@ -374,9 +338,9 @@ model Notification {
374
338
  type String
375
339
  createdAt DateTime @default(now())
376
340
  downtimeEventId String?
377
- tenantId String?
341
+ tenantId String
378
342
  downtimeEvent DowntimeEvent? @relation(fields: [downtimeEventId], references: [id])
379
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
343
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
380
344
  NotificationToUser NotificationToUser[]
381
345
 
382
346
  @@map("notification")
@@ -386,9 +350,9 @@ model NotificationToUser {
386
350
  notificationId String
387
351
  userId String
388
352
  isRead Boolean @default(false)
389
- tenantId String?
353
+ tenantId String
390
354
  notification Notification @relation(fields: [notificationId], references: [id])
391
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
355
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
392
356
  user User @relation(fields: [userId], references: [id])
393
357
 
394
358
  @@id([notificationId, userId])
@@ -410,14 +374,14 @@ model Operation {
410
374
  engineeringTime Int @default(0)
411
375
  initialTime Int @default(0)
412
376
  pcpTime Int @default(0)
413
- tenantId String?
377
+ tenantId String
414
378
  downtimeEvents DowntimeEvent[]
415
379
  inputRegisters InputRegister[]
416
380
  itemsHandlingRegisters ItemsHandlingRegister[]
417
381
  nodeOperationQueue NodeOperationQueue[]
418
382
  node Node @relation(fields: [nodeId], references: [id], onDelete: Cascade)
419
383
  resource Resource @relation(fields: [resourceId], references: [id])
420
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
384
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
421
385
  operationRegisters OperationRegister[]
422
386
  inputOf OperationToOperation[] @relation("inputOperation")
423
387
  outputOf OperationToOperation[] @relation("outputOperation")
@@ -444,10 +408,10 @@ model OperationGroup {
444
408
  numberOfCuts Int @default(0)
445
409
  flowId String
446
410
  isDeleted Boolean @default(false)
447
- tenantId String?
411
+ tenantId String
448
412
  downtimeEvents DowntimeEvent[]
449
413
  flow Flow @relation(fields: [flowId], references: [id])
450
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
414
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
451
415
  operationRegisters OperationRegister[]
452
416
  productionNodes ProductionNode[]
453
417
  operations Operation[] @relation("OperationToOperationGroup")
@@ -486,7 +450,7 @@ model OperationRegister {
486
450
  endTime DateTime? @db.Timestamp(6)
487
451
  elapsedTime BigInt?
488
452
  productionNodeId String?
489
- tenantId String?
453
+ tenantId String
490
454
  quantity Int @default(1)
491
455
  timeSinceLastOperation BigInt?
492
456
  timeToProduceSnapshot Int?
@@ -496,7 +460,7 @@ model OperationRegister {
496
460
  operation Operation? @relation(fields: [operationId], references: [id], onDelete: Cascade)
497
461
  productionNode ProductionNode? @relation(fields: [productionNodeId], references: [id])
498
462
  productionOrder ProductionOrder @relation(fields: [productionOrderId], references: [id])
499
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
463
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
500
464
 
501
465
  @@index([operatorId, endTime, elapsedTime])
502
466
  @@index([tenantId, operatorId, endTime])
@@ -511,10 +475,10 @@ model OperationToOperation {
511
475
  amount Int
512
476
  inputOperationId String
513
477
  outputOperationId String
514
- tenantId String?
478
+ tenantId String
515
479
  inputOperation Operation @relation("inputOperation", fields: [inputOperationId], references: [id], onDelete: Cascade)
516
480
  outputOperation Operation @relation("outputOperation", fields: [outputOperationId], references: [id], onDelete: Cascade)
517
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
481
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
518
482
 
519
483
  @@id([inputOperationId, outputOperationId])
520
484
  @@map("operation_to_operation")
@@ -522,7 +486,7 @@ model OperationToOperation {
522
486
 
523
487
  model Operator {
524
488
  id String @id @default(cuid())
525
- name String @unique
489
+ name String
526
490
  nickname String?
527
491
  birthDate DateTime? @db.Date
528
492
  phoneNumber String?
@@ -531,12 +495,12 @@ model Operator {
531
495
  operatorRoleId String?
532
496
  pin String?
533
497
  isDeleted Boolean @default(false)
534
- tenantId String?
498
+ tenantId String
535
499
  deviceOperators DeviceOperator[]
536
500
  downtimeEvents DowntimeEvent[]
537
501
  operationRegisters OperationRegister[]
538
502
  operatorRole OperatorRole? @relation(fields: [operatorRoleId], references: [id])
539
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
503
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
540
504
  ProductionNode ProductionNode?
541
505
  habilityResources Resource[] @relation("OperatorToResource")
542
506
  shiftId String?
@@ -545,6 +509,7 @@ model Operator {
545
509
  operatorAvailability OperatorAvailabilityCache[]
546
510
  qualityRegisters QualityRegister[]
547
511
 
512
+ @@unique([tenantId, name])
548
513
  @@index([tenantId, isDeleted])
549
514
  @@map("operator")
550
515
  }
@@ -553,9 +518,9 @@ model OperatorRole {
553
518
  id String @id @default(cuid())
554
519
  name String
555
520
  isDeleted Boolean @default(false)
556
- tenantId String?
521
+ tenantId String
557
522
  operators Operator[]
558
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
523
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
559
524
 
560
525
  @@map("operator_role")
561
526
  }
@@ -564,8 +529,8 @@ model Panel {
564
529
  id String @id @default(cuid())
565
530
  title String
566
531
  userId String @map("user_id")
567
- tenantId String?
568
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
532
+ tenantId String
533
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
569
534
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
570
535
  reports Report[] @relation("PanelToReport")
571
536
 
@@ -597,7 +562,7 @@ model Product {
597
562
  name String
598
563
  isLocal Boolean?
599
564
  isDeleted Boolean @default(false)
600
- tenantId String?
565
+ tenantId String
601
566
  customerProductId String?
602
567
  description String?
603
568
  sku String?
@@ -609,7 +574,7 @@ model Product {
609
574
  flows Flow[]
610
575
  itemsHandlingRegisters ItemsHandlingRegister[]
611
576
  node Node[]
612
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
577
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
613
578
  batches ProductBatch[]
614
579
  composedBy ProductComponents[] @relation("componentOf")
615
580
  componentOf ProductComponents[] @relation("composedBy")
@@ -625,10 +590,10 @@ model ProductComponents {
625
590
  composedById String
626
591
  componentOfId String
627
592
  quantity Float
628
- tenantId String?
593
+ tenantId String
629
594
  componentOf Product @relation("componentOf", fields: [componentOfId], references: [id], onDelete: Cascade)
630
595
  composedBy Product @relation("composedBy", fields: [composedById], references: [id], onDelete: Cascade)
631
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
596
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
632
597
 
633
598
  @@id([componentOfId, composedById])
634
599
  @@map("product_components")
@@ -642,7 +607,7 @@ model ProductionNode {
642
607
  operationId String?
643
608
  operationGroupId Int?
644
609
  productionOrderId String?
645
- tenantId String?
610
+ tenantId String
646
611
  operationRegister OperationRegister[]
647
612
  machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
648
613
  node Node @relation(fields: [nodeId], references: [id], onDelete: Cascade)
@@ -650,7 +615,7 @@ model ProductionNode {
650
615
  operation Operation? @relation(fields: [operationId], references: [id])
651
616
  operator Operator? @relation(fields: [operatorId], references: [id])
652
617
  productionOrder ProductionOrder? @relation(fields: [productionOrderId], references: [id])
653
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
618
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
654
619
 
655
620
  @@map("production_node")
656
621
  }
@@ -659,13 +624,13 @@ model ProductionOrder {
659
624
  id String @id @default(cuid())
660
625
  productId String
661
626
  flowId String?
662
- productionOrderIndex Int? @unique
627
+ productionOrderIndex Int?
663
628
  batchSize Float
664
629
  hasStarted Boolean @default(false)
665
630
  isRunning Boolean @default(false)
666
631
  isFinished Boolean @default(false)
667
632
  sectorAggregatedValue Float?
668
- tenantId String?
633
+ tenantId String
669
634
  endDate DateTime? @db.Date
670
635
  expectedEndDate DateTime? @db.Date
671
636
  startDate DateTime? @db.Date
@@ -679,8 +644,9 @@ model ProductionOrder {
679
644
  qualityRegisters QualityRegister[]
680
645
  flow Flow? @relation(fields: [flowId], references: [id])
681
646
  Product Product @relation(fields: [productId], references: [id])
682
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
647
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
683
648
 
649
+ @@unique([tenantId, productionOrderIndex])
684
650
  @@index([tenantId, isFinished])
685
651
  @@index([productId])
686
652
  @@index([flowId])
@@ -691,10 +657,10 @@ model ProductToOperation {
691
657
  amount Int
692
658
  productId String
693
659
  operationId String
694
- tenantId String?
660
+ tenantId String
695
661
  operation Operation @relation(fields: [operationId], references: [id], onDelete: Cascade)
696
662
  product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
697
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
663
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
698
664
 
699
665
  @@id([productId, operationId])
700
666
  @@map("product_to_operation")
@@ -707,8 +673,8 @@ model Report {
707
673
  data String
708
674
  createdAt DateTime @default(now())
709
675
  updatedAt DateTime? @updatedAt
710
- tenantId String?
711
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
676
+ tenantId String
677
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
712
678
  panels Panel[] @relation("PanelToReport")
713
679
 
714
680
  @@map("report")
@@ -716,27 +682,30 @@ model Report {
716
682
 
717
683
  model Resource {
718
684
  id String @id @default(cuid())
719
- name String @unique
685
+ name String
720
686
  resourceType ResourceType
721
687
  description String?
722
- tenantId String?
688
+ tenantId String
723
689
  Operation Operation[]
724
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
690
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
725
691
  machines Machine[] @relation("MachineToResource")
726
692
  operators Operator[] @relation("OperatorToResource")
727
693
 
694
+ @@unique([tenantId, name])
728
695
  @@map("resource")
729
696
  }
730
697
 
731
698
  model Role {
732
699
  id String @id @default(cuid())
733
- name String @unique // Ex: "Admin", "Operador", "Gerente"
700
+ name String // Ex: "Admin", "Operador", "Gerente"
734
701
  description String?
735
- tenantId String?
702
+ tenantId String
736
703
  permissions RolePermission[]
737
704
  users User[]
738
705
 
739
- tenant Tenant? @relation(fields: [tenantId], references: [id])
706
+ tenant Tenant @relation(fields: [tenantId], references: [id])
707
+
708
+ @@unique([name, tenantId])
740
709
  }
741
710
 
742
711
  model RolePermission {
@@ -751,16 +720,17 @@ model RolePermission {
751
720
 
752
721
  model Sector {
753
722
  id String @id @default(cuid())
754
- name String @unique @db.VarChar(100)
723
+ name String @db.VarChar(100)
755
724
  description String? @db.VarChar(255)
756
725
  isDeleted Boolean @default(false)
757
- tenantId String?
726
+ tenantId String
758
727
  stationFrom ItemsHandlingRegister[] @relation("sectorFrom")
759
728
  stationTo ItemsHandlingRegister[] @relation("sectorTo")
760
729
  machines Machine[]
761
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
730
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
762
731
  qualityRegisters QualityRegister[]
763
732
 
733
+ @@unique([tenantId, name])
764
734
  @@map("sector")
765
735
  }
766
736
 
@@ -771,8 +741,8 @@ model SewMachineRawData {
771
741
  stitches_count Int
772
742
  line_cut Boolean @default(false)
773
743
  machine_id String?
774
- tenantId String?
775
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
744
+ tenantId String
745
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
776
746
 
777
747
  @@index([machine_id, start_timestamp])
778
748
  @@map("sew_machine_raw_data")
@@ -780,13 +750,14 @@ model SewMachineRawData {
780
750
 
781
751
  model Shift {
782
752
  id String @id @default(cuid())
783
- name String @unique @db.VarChar(100)
784
- tenantId String?
753
+ name String @db.VarChar(100)
754
+ tenantId String
785
755
  days ShiftDay[]
786
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
756
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
787
757
  operators Operator[]
788
758
  operatorAvailability OperatorAvailabilityCache[]
789
759
 
760
+ @@unique([tenantId, name])
790
761
  @@map("shift")
791
762
  }
792
763
 
@@ -799,24 +770,24 @@ model ShiftDay {
799
770
  shiftId String
800
771
  shift Shift @relation(fields: [shiftId], references: [id], onDelete: Cascade)
801
772
  breaks Break[]
802
- tenantId String?
803
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
773
+ tenantId String
774
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
804
775
 
805
776
  @@unique([shiftId, dayOfWeek])
806
777
  @@map("shift_day")
807
778
  }
808
779
 
809
780
  model SystemPreferences {
810
- id Int @id @default(autoincrement())
811
- downtimeEventTime Int @default(300)
812
- notificationTime Int @default(10)
813
- lowPercentageColor Colors @default(red)
814
- averagePercentageColor Colors @default(yellow)
815
- highPercentageColor Colors @default(green)
816
- averagePercentageValue Float @default(50)
817
- highPercentageValue Float @default(80)
818
- tenantId String?
819
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
781
+ id Int @id @default(autoincrement())
782
+ downtimeEventTime Int @default(300)
783
+ notificationTime Int @default(10)
784
+ lowPercentageColor Colors @default(red)
785
+ averagePercentageColor Colors @default(yellow)
786
+ highPercentageColor Colors @default(green)
787
+ averagePercentageValue Float @default(50)
788
+ highPercentageValue Float @default(80)
789
+ tenantId String
790
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
820
791
 
821
792
  @@map("system_preferences")
822
793
  }
@@ -842,22 +813,22 @@ model TwoFactorToken {
842
813
  model User {
843
814
  id String @id @default(uuid())
844
815
  name String
845
- email String @unique
816
+ email String
846
817
  emailVerified DateTime?
847
818
  image String?
848
819
  password String
849
820
  isTwoFactorEnabled Boolean @default(false)
850
821
  roleId String?
851
822
  role Role? @relation(fields: [roleId], references: [id])
852
- tenantId String?
823
+ tenantId String
853
824
  accounts Account[]
854
825
  follows Follower[]
855
826
  NotificationToUser NotificationToUser[]
856
827
  Panel Panel[]
857
828
  twoFactorConfirmation TwoFactorConfirmation?
858
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
859
- aiConversations AiConversation[]
829
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
860
830
 
831
+ @@unique([tenantId, email])
861
832
  @@map("user")
862
833
  }
863
834
 
@@ -875,7 +846,7 @@ view ProductivityInterval {
875
846
  efficiencyPercentage Float? @map("efficiency_percentage")
876
847
  timeToProduceSnapshot Int? @map("time_to_produce_snapshot")
877
848
  downtimeSeconds Float? @map("downtime_seconds")
878
- tenantId String?
849
+ tenantId String
879
850
 
880
851
  @@unique([machineId, sequenceId])
881
852
  @@map("productivity_intervals")
@@ -899,13 +870,13 @@ model NodeOperationQueue {
899
870
  createdAt DateTime @default(now())
900
871
  updatedAt DateTime @updatedAt
901
872
  productionOrderId String
902
- tenantId String?
873
+ tenantId String
903
874
  deviceId String?
904
875
  device Device? @relation(fields: [deviceId], references: [id], onDelete: Cascade)
905
876
  node Node @relation(fields: [nodeId], references: [id], onDelete: Cascade)
906
877
  operation Operation @relation(fields: [operationId], references: [id], onDelete: Cascade)
907
878
  productionOrder ProductionOrder @relation(fields: [productionOrderId], references: [id], onDelete: Cascade)
908
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
879
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
909
880
 
910
881
  @@map("node_operation_queue")
911
882
  }
@@ -945,11 +916,11 @@ model MachineSensors {
945
916
  sensor2Name String?
946
917
  sensor1Show Boolean @default(true)
947
918
  sensor2Show Boolean @default(true)
948
- tenantId String?
919
+ tenantId String
949
920
  sensor1Multiplier Float?
950
921
  sensor2Multiplier Float?
951
922
  machine Machine @relation(fields: [machineId], references: [id])
952
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
923
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
953
924
 
954
925
  @@index([machineId], map: "idx_machine_sensors_machine_id")
955
926
  @@map("machine_sensors")
@@ -1014,11 +985,11 @@ model ProductBatch {
1014
985
  expiryDate DateTime @db.Date
1015
986
  createdAt DateTime @default(now())
1016
987
  updatedAt DateTime @updatedAt
1017
- tenantId String?
988
+ tenantId String
1018
989
  product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
1019
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
990
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1020
991
 
1021
- @@unique([productId, batchNumber, internalBatchNumber])
992
+ @@unique([tenantId, productId, batchNumber, internalBatchNumber])
1022
993
  @@map("product_batch")
1023
994
  }
1024
995
 
@@ -1060,11 +1031,11 @@ model MachineOperationStitch {
1060
1031
  machineId String
1061
1032
  operationId String
1062
1033
  stitches Int
1063
- tenantId String?
1034
+ tenantId String
1064
1035
 
1065
1036
  machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
1066
1037
  operation Operation @relation(fields: [operationId], references: [id], onDelete: Cascade)
1067
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1038
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1068
1039
 
1069
1040
  @@id([machineId, operationId])
1070
1041
  @@map("machine_operation_stitch")
@@ -1072,7 +1043,7 @@ model MachineOperationStitch {
1072
1043
 
1073
1044
  model ProductivityIntervalCache {
1074
1045
  id String @id @default(cuid())
1075
- tenantId String?
1046
+ tenantId String
1076
1047
  machineId String
1077
1048
  operatorId String
1078
1049
  operationId String?
@@ -1092,7 +1063,7 @@ model ProductivityIntervalCache {
1092
1063
 
1093
1064
  createdAt DateTime @default(now())
1094
1065
 
1095
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1066
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1096
1067
  machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
1097
1068
  operator Operator @relation(fields: [operatorId], references: [id], onDelete: Cascade)
1098
1069
  operation Operation? @relation(fields: [operationId], references: [id], onDelete: Cascade)
@@ -1102,55 +1073,11 @@ model ProductivityIntervalCache {
1102
1073
  @@index([tenantId, machineId, groupStartTimestamp])
1103
1074
  @@index([operatorId, groupStartTimestamp])
1104
1075
  @@index([operationId])
1076
+ @@index([operatorId, groupEndTimestamp], map: "pic_operator_end_idx")
1077
+ @@index([tenantId, operatorId, groupEndTimestamp], map: "pic_tenant_operator_end_idx")
1105
1078
  @@map("productivity_interval_cache")
1106
1079
  }
1107
1080
 
1108
- // --- AI & I. Intelligence ---
1109
-
1110
- model AiConversation {
1111
- id String @id @default(cuid())
1112
- userId String
1113
- tenantId String?
1114
- title String?
1115
- messages AiMessage[]
1116
- createdAt DateTime @default(now())
1117
- updatedAt DateTime @updatedAt
1118
- user User @relation(fields: [userId], references: [id])
1119
- tenant Tenant? @relation(fields: [tenantId], references: [id])
1120
-
1121
- @@index([userId])
1122
- @@map("ai_conversation")
1123
- }
1124
-
1125
- model AiMessage {
1126
- id String @id @default(cuid())
1127
- conversationId String
1128
- role String // 'user' | 'assistant' | 'system' | 'data'
1129
- content String // Markdown content
1130
- sqlQuery String? // The generated SQL (if assistant)
1131
- toolCalls Json? // If the AI called tools
1132
- isCorrect Boolean? // Feedback: true=Good, false=Bad
1133
- correction String? // User's textual correction or correct SQL
1134
- createdAt DateTime @default(now())
1135
- conversation AiConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
1136
-
1137
- @@index([conversationId])
1138
- @@map("ai_message")
1139
- }
1140
-
1141
- model AiFeedback {
1142
- id String @id @default(cuid())
1143
- tenantId String?
1144
- userId String
1145
- question String
1146
- generatedSql String
1147
- correctedSql String?
1148
- comment String?
1149
- createdAt DateTime @default(now())
1150
-
1151
- @@map("ai_feedback")
1152
- }
1153
-
1154
1081
  model QualityRegister {
1155
1082
  id String @id @default(cuid())
1156
1083
  timestamp DateTime @default(now())
@@ -1161,14 +1088,14 @@ model QualityRegister {
1161
1088
  qualityIssueId String?
1162
1089
  amount Int @default(1)
1163
1090
  comments String?
1164
- tenantId String?
1091
+ tenantId String
1165
1092
 
1166
1093
  operation Operation @relation(fields: [operationId], references: [id])
1167
1094
  productionOrder ProductionOrder? @relation(fields: [productionOrderId], references: [id])
1168
1095
  operator Operator? @relation(fields: [operatorId], references: [id])
1169
1096
  sector Sector? @relation(fields: [sectorId], references: [id])
1170
1097
  qualityIssue QualityIssue? @relation(fields: [qualityIssueId], references: [id])
1171
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1098
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1172
1099
 
1173
1100
  @@index([tenantId])
1174
1101
  @@index([operationId])
@@ -1184,10 +1111,10 @@ model QualityIssue {
1184
1111
  description String?
1185
1112
  category QualityIssueCategory @default(OTHER)
1186
1113
  isDeleted Boolean @default(false)
1187
- tenantId String?
1114
+ tenantId String
1188
1115
 
1189
1116
  qualityRegisters QualityRegister[]
1190
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1117
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1191
1118
 
1192
1119
  @@index([tenantId])
1193
1120
  @@map("quality_issue")
@@ -1207,7 +1134,7 @@ model OperatorAvailabilityCache {
1207
1134
 
1208
1135
  operatorId String
1209
1136
  shiftId String? // Pode ser nulo se o operador não estava atrelado a turno no dia, mas trabalhou
1210
- tenantId String?
1137
+ tenantId String
1211
1138
 
1212
1139
  // Tempos esperados (baseados na configuração de ShiftDay / Break)
1213
1140
  expectedGrossMinutes Int @default(0) // Tempo total de turno configurado (end_time - start_time)
@@ -1225,7 +1152,7 @@ model OperatorAvailabilityCache {
1225
1152
 
1226
1153
  operator Operator @relation(fields: [operatorId], references: [id], onDelete: Cascade)
1227
1154
  shift Shift? @relation(fields: [shiftId], references: [id], onDelete: SetNull)
1228
- tenant Tenant? @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1155
+ tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
1229
1156
 
1230
1157
  @@unique([operatorId, date]) // Garante que há apenas 1 registro de fechamento por operador por dia
1231
1158
  @@index([tenantId, date])