@greatapps/greatagents 0.1.10 → 0.1.11
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.
- package/package.json +1 -1
- package/src/modules/objectives/index.js +1 -1
- package/src/modules/objectives/properties.js +8 -7
- package/src/product.js +1 -25
- package/docs/ERD_GAGents_Database.mmd +0 -331
- package/scripts/MIGRATION_COMPLETE_DOCUMENTATION.md +0 -672
- package/scripts/README.md +0 -165
- package/scripts/constraints.sql +0 -585
- package/scripts/indexes.sql +0 -661
- package/scripts/migrate_v1.sql +0 -1225
- package/scripts/tables/001_accounts.sql +0 -51
- package/scripts/tables/002_users.sql +0 -69
- package/scripts/tables/003_people.sql +0 -70
- package/scripts/tables/004_tags.sql +0 -62
- package/scripts/tables/005_credentials.sql +0 -79
- package/scripts/tables/006_agents.sql +0 -70
- package/scripts/tables/007_tools.sql +0 -68
- package/scripts/tables/008_channels.sql +0 -75
- package/scripts/tables/009_leads.sql +0 -85
- package/scripts/tables/010_missions.sql +0 -62
- package/scripts/tables/011_objectives.sql +0 -68
- package/scripts/tables/012_conversations.sql +0 -80
- package/scripts/tables/013_objectives_tools.sql +0 -66
- package/scripts/tables/014_messages.sql +0 -78
- package/scripts/tables/015_companies.sql +0 -77
- package/scripts/tables/016_conversations_tags.sql +0 -64
- package/scripts/tables/017_leads_tags.sql +0 -64
- package/scripts/triggers.sql +0 -497
- package/src/modules/channels/index.js +0 -10
- package/src/modules/channels/properties.js +0 -150
- package/src/modules/companies/index.js +0 -10
- package/src/modules/companies/properties.js +0 -168
- package/src/modules/conversations/index.js +0 -10
- package/src/modules/conversations/properties.js +0 -163
- package/src/modules/conversations_tags/index.js +0 -10
- package/src/modules/conversations_tags/properties.js +0 -84
- package/src/modules/credentials/index.js +0 -10
- package/src/modules/credentials/properties.js +0 -271
- package/src/modules/leads/index.js +0 -10
- package/src/modules/leads/properties.js +0 -193
- package/src/modules/leads_tags/index.js +0 -10
- package/src/modules/leads_tags/properties.js +0 -84
- package/src/modules/messages/index.js +0 -10
- package/src/modules/messages/properties.js +0 -152
- package/src/modules/missions/index.js +0 -10
- package/src/modules/missions/properties.js +0 -101
- package/src/modules/objectives_tools/index.js +0 -10
- package/src/modules/objectives_tools/properties.js +0 -109
- package/src/modules/people/index.js +0 -10
- package/src/modules/people/properties.js +0 -161
- package/src/modules/tags/index.js +0 -10
- package/src/modules/tags/properties.js +0 -80
- package/src/modules/tools/index.js +0 -10
- package/src/modules/tools/properties.js +0 -121
package/scripts/constraints.sql
DELETED
|
@@ -1,585 +0,0 @@
|
|
|
1
|
-
-- ============================================================================
|
|
2
|
-
-- Script: constraints.sql
|
|
3
|
-
-- Descrição: Constraints adicionais e foreign keys para banco GAgents
|
|
4
|
-
-- Versão: 1.0
|
|
5
|
-
-- Dependências: Todas as tabelas (001-017) e indexes.sql
|
|
6
|
-
-- Autor: Sistema de Migração GAgents
|
|
7
|
-
-- Data: 2025-09-20
|
|
8
|
-
-- ============================================================================
|
|
9
|
-
|
|
10
|
-
-- Verificação de pré-requisitos
|
|
11
|
-
DO $$
|
|
12
|
-
BEGIN
|
|
13
|
-
-- Verificar PostgreSQL 14+
|
|
14
|
-
IF current_setting('server_version_num')::integer < 140000 THEN
|
|
15
|
-
RAISE EXCEPTION 'PostgreSQL versão 14+ é obrigatória. Versão atual: %', version();
|
|
16
|
-
END IF;
|
|
17
|
-
|
|
18
|
-
-- Verificar se tabelas críticas existem
|
|
19
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'conversations') THEN
|
|
20
|
-
RAISE EXCEPTION 'Tabela conversations não encontrada. Execute primeiro os scripts de criação de tabelas.';
|
|
21
|
-
END IF;
|
|
22
|
-
|
|
23
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'messages') THEN
|
|
24
|
-
RAISE EXCEPTION 'Tabela messages não encontrada. Execute primeiro os scripts de criação de tabelas.';
|
|
25
|
-
END IF;
|
|
26
|
-
|
|
27
|
-
RAISE NOTICE '[%] Iniciando adição de constraints adicionais do sistema GAgents', now();
|
|
28
|
-
END $$;
|
|
29
|
-
|
|
30
|
-
-- ========================================
|
|
31
|
-
-- REFERÊNCIA CÍCLICA - CRÍTICA
|
|
32
|
-
-- ========================================
|
|
33
|
-
DO $$
|
|
34
|
-
BEGIN
|
|
35
|
-
-- Verificar se a constraint já existe
|
|
36
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
37
|
-
WHERE constraint_name = 'fk_conversations_last_message'
|
|
38
|
-
AND table_name = 'conversations') THEN
|
|
39
|
-
|
|
40
|
-
-- Adicionar FK cíclica conversations → messages
|
|
41
|
-
ALTER TABLE conversations
|
|
42
|
-
ADD CONSTRAINT fk_conversations_last_message
|
|
43
|
-
FOREIGN KEY (id_last_message) REFERENCES messages(id)
|
|
44
|
-
ON DELETE SET NULL;
|
|
45
|
-
|
|
46
|
-
RAISE NOTICE '[%] ✓ Constraint cíclica conversations → messages adicionada', now();
|
|
47
|
-
ELSE
|
|
48
|
-
RAISE NOTICE '[%] ⚠ Constraint cíclica conversations → messages já existe', now();
|
|
49
|
-
END IF;
|
|
50
|
-
EXCEPTION
|
|
51
|
-
WHEN OTHERS THEN
|
|
52
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint cíclica: %', now(), SQLERRM;
|
|
53
|
-
END $$;
|
|
54
|
-
|
|
55
|
-
-- ========================================
|
|
56
|
-
-- VALIDAÇÕES APRIMORADAS DE EMAIL
|
|
57
|
-
-- ========================================
|
|
58
|
-
DO $$
|
|
59
|
-
BEGIN
|
|
60
|
-
-- Melhorar validação de email em users (substituir constraint existente)
|
|
61
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
62
|
-
WHERE constraint_name = 'users_email_valid'
|
|
63
|
-
AND table_name = 'users') THEN
|
|
64
|
-
ALTER TABLE users DROP CONSTRAINT users_email_valid;
|
|
65
|
-
END IF;
|
|
66
|
-
|
|
67
|
-
ALTER TABLE users
|
|
68
|
-
ADD CONSTRAINT chk_users_email_format
|
|
69
|
-
CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$');
|
|
70
|
-
|
|
71
|
-
RAISE NOTICE '[%] ✓ Validação aprimorada de email em users', now();
|
|
72
|
-
EXCEPTION
|
|
73
|
-
WHEN OTHERS THEN
|
|
74
|
-
RAISE WARNING '[%] ✗ Erro ao aprimorar validação de email: %', now(), SQLERRM;
|
|
75
|
-
END $$;
|
|
76
|
-
|
|
77
|
-
-- ========================================
|
|
78
|
-
-- CHECK CONSTRAINTS PARA ENUMS EXPANDIDOS
|
|
79
|
-
-- ========================================
|
|
80
|
-
|
|
81
|
-
-- USERS (profile) - Expandir valores permitidos
|
|
82
|
-
DO $$
|
|
83
|
-
BEGIN
|
|
84
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
85
|
-
WHERE constraint_name = 'users_profile_valid'
|
|
86
|
-
AND table_name = 'users') THEN
|
|
87
|
-
ALTER TABLE users DROP CONSTRAINT users_profile_valid;
|
|
88
|
-
END IF;
|
|
89
|
-
|
|
90
|
-
ALTER TABLE users
|
|
91
|
-
ADD CONSTRAINT chk_users_profile_expanded
|
|
92
|
-
CHECK (profile IN ('viewer', 'collaborator', 'admin', 'owner', 'attendant', 'user', 'agent'));
|
|
93
|
-
|
|
94
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para users.profile', now();
|
|
95
|
-
EXCEPTION
|
|
96
|
-
WHEN OTHERS THEN
|
|
97
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint users.profile: %', now(), SQLERRM;
|
|
98
|
-
END $$;
|
|
99
|
-
|
|
100
|
-
-- CHANNELS (provider) - Expandir valores permitidos
|
|
101
|
-
DO $$
|
|
102
|
-
BEGIN
|
|
103
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
104
|
-
WHERE constraint_name = 'channels_provider_valid'
|
|
105
|
-
AND table_name = 'channels') THEN
|
|
106
|
-
ALTER TABLE channels DROP CONSTRAINT channels_provider_valid;
|
|
107
|
-
END IF;
|
|
108
|
-
|
|
109
|
-
ALTER TABLE channels
|
|
110
|
-
ADD CONSTRAINT chk_channels_provider_expanded
|
|
111
|
-
CHECK (provider IN ('web-chat', 'whatsap-oficial', 'z-api', 'evolution-api', 'telegram', 'facebook', 'instagram', 'whatsapp', 'discord', 'slack', 'email'));
|
|
112
|
-
|
|
113
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para channels.provider', now();
|
|
114
|
-
EXCEPTION
|
|
115
|
-
WHEN OTHERS THEN
|
|
116
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint channels.provider: %', now(), SQLERRM;
|
|
117
|
-
END $$;
|
|
118
|
-
|
|
119
|
-
-- CREDENTIALS (provider) - Expandir valores permitidos
|
|
120
|
-
DO $$
|
|
121
|
-
BEGIN
|
|
122
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
123
|
-
WHERE constraint_name = 'credentials_provider_valid'
|
|
124
|
-
AND table_name = 'credentials') THEN
|
|
125
|
-
ALTER TABLE credentials DROP CONSTRAINT credentials_provider_valid;
|
|
126
|
-
END IF;
|
|
127
|
-
|
|
128
|
-
ALTER TABLE credentials
|
|
129
|
-
ADD CONSTRAINT chk_credentials_provider_expanded
|
|
130
|
-
CHECK (provider IN ('web-chat', 'whatsapp-oficial', 'z-api', 'evolution-api', 'telegram', 'facebook', 'instagram', 'custom', 'openai', 'anthropic', 'google'));
|
|
131
|
-
|
|
132
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para credentials.provider', now();
|
|
133
|
-
EXCEPTION
|
|
134
|
-
WHEN OTHERS THEN
|
|
135
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint credentials.provider: %', now(), SQLERRM;
|
|
136
|
-
END $$;
|
|
137
|
-
|
|
138
|
-
-- CREDENTIALS (authType) - Expandir valores permitidos
|
|
139
|
-
DO $$
|
|
140
|
-
BEGIN
|
|
141
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
142
|
-
WHERE constraint_name = 'credentials_authtype_valid'
|
|
143
|
-
AND table_name = 'credentials') THEN
|
|
144
|
-
ALTER TABLE credentials DROP CONSTRAINT credentials_authtype_valid;
|
|
145
|
-
END IF;
|
|
146
|
-
|
|
147
|
-
ALTER TABLE credentials
|
|
148
|
-
ADD CONSTRAINT chk_credentials_auth_type_expanded
|
|
149
|
-
CHECK (authType IN ('none', 'basic', 'oauth2', 'apikey', 'api_key', 'oauth', 'jwt', 'basic_auth', 'webhook'));
|
|
150
|
-
|
|
151
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para credentials.authType', now();
|
|
152
|
-
EXCEPTION
|
|
153
|
-
WHEN OTHERS THEN
|
|
154
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint credentials.authType: %', now(), SQLERRM;
|
|
155
|
-
END $$;
|
|
156
|
-
|
|
157
|
-
-- CONVERSATIONS (status) - Expandir valores permitidos
|
|
158
|
-
DO $$
|
|
159
|
-
BEGIN
|
|
160
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
161
|
-
WHERE constraint_name = 'conversations_status_valid'
|
|
162
|
-
AND table_name = 'conversations') THEN
|
|
163
|
-
ALTER TABLE conversations DROP CONSTRAINT conversations_status_valid;
|
|
164
|
-
END IF;
|
|
165
|
-
|
|
166
|
-
ALTER TABLE conversations
|
|
167
|
-
ADD CONSTRAINT chk_conversations_status_expanded
|
|
168
|
-
CHECK (status IN ('open', 'pending', 'closed', 'assigned', 'resolved'));
|
|
169
|
-
|
|
170
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para conversations.status', now();
|
|
171
|
-
EXCEPTION
|
|
172
|
-
WHEN OTHERS THEN
|
|
173
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint conversations.status: %', now(), SQLERRM;
|
|
174
|
-
END $$;
|
|
175
|
-
|
|
176
|
-
-- MESSAGES (direction) - Expandir valores permitidos
|
|
177
|
-
DO $$
|
|
178
|
-
BEGIN
|
|
179
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
180
|
-
WHERE constraint_name = 'messages_direction_valid'
|
|
181
|
-
AND table_name = 'messages') THEN
|
|
182
|
-
ALTER TABLE messages DROP CONSTRAINT messages_direction_valid;
|
|
183
|
-
END IF;
|
|
184
|
-
|
|
185
|
-
ALTER TABLE messages
|
|
186
|
-
ADD CONSTRAINT chk_messages_direction_expanded
|
|
187
|
-
CHECK (direction IN ('in', 'out', 'incoming', 'outgoing'));
|
|
188
|
-
|
|
189
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para messages.direction', now();
|
|
190
|
-
EXCEPTION
|
|
191
|
-
WHEN OTHERS THEN
|
|
192
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint messages.direction: %', now(), SQLERRM;
|
|
193
|
-
END $$;
|
|
194
|
-
|
|
195
|
-
-- MESSAGES (role) - Expandir valores permitidos
|
|
196
|
-
DO $$
|
|
197
|
-
BEGIN
|
|
198
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
199
|
-
WHERE constraint_name = 'messages_role_valid'
|
|
200
|
-
AND table_name = 'messages') THEN
|
|
201
|
-
ALTER TABLE messages DROP CONSTRAINT messages_role_valid;
|
|
202
|
-
END IF;
|
|
203
|
-
|
|
204
|
-
ALTER TABLE messages
|
|
205
|
-
ADD CONSTRAINT chk_messages_role_expanded
|
|
206
|
-
CHECK (role IN ('user', 'agent', 'system', 'assistant', 'note', 'function'));
|
|
207
|
-
|
|
208
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para messages.role', now();
|
|
209
|
-
EXCEPTION
|
|
210
|
-
WHEN OTHERS THEN
|
|
211
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint messages.role: %', now(), SQLERRM;
|
|
212
|
-
END $$;
|
|
213
|
-
|
|
214
|
-
-- TOOLS (type) - Expandir valores permitidos
|
|
215
|
-
DO $$
|
|
216
|
-
BEGIN
|
|
217
|
-
IF EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
218
|
-
WHERE constraint_name = 'tools_type_valid'
|
|
219
|
-
AND table_name = 'tools') THEN
|
|
220
|
-
ALTER TABLE tools DROP CONSTRAINT tools_type_valid;
|
|
221
|
-
END IF;
|
|
222
|
-
|
|
223
|
-
ALTER TABLE tools
|
|
224
|
-
ADD CONSTRAINT chk_tools_type_expanded
|
|
225
|
-
CHECK (type IN ('mcp', 'a2a', 'api', 'internal-agent', 'internal-function', 'native-integration', 'knowledge-base', 'function', 'plugin', 'webhook', 'database'));
|
|
226
|
-
|
|
227
|
-
RAISE NOTICE '[%] ✓ Check constraint expandida para tools.type', now();
|
|
228
|
-
EXCEPTION
|
|
229
|
-
WHEN OTHERS THEN
|
|
230
|
-
RAISE WARNING '[%] ✗ Erro ao expandir constraint tools.type: %', now(), SQLERRM;
|
|
231
|
-
END $$;
|
|
232
|
-
|
|
233
|
-
-- ========================================
|
|
234
|
-
-- CONSTRAINTS DE INTEGRIDADE TEMPORAL
|
|
235
|
-
-- ========================================
|
|
236
|
-
|
|
237
|
-
-- CONVERSATIONS - datetime_alt deve ser >= datetime_add
|
|
238
|
-
DO $$
|
|
239
|
-
BEGIN
|
|
240
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
241
|
-
WHERE constraint_name = 'chk_conversations_datetime_consistency'
|
|
242
|
-
AND table_name = 'conversations') THEN
|
|
243
|
-
|
|
244
|
-
ALTER TABLE conversations
|
|
245
|
-
ADD CONSTRAINT chk_conversations_datetime_consistency
|
|
246
|
-
CHECK (datetime_alt >= datetime_add);
|
|
247
|
-
|
|
248
|
-
RAISE NOTICE '[%] ✓ Constraint temporal conversations.datetime_alt >= datetime_add', now();
|
|
249
|
-
END IF;
|
|
250
|
-
EXCEPTION
|
|
251
|
-
WHEN OTHERS THEN
|
|
252
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint temporal conversations: %', now(), SQLERRM;
|
|
253
|
-
END $$;
|
|
254
|
-
|
|
255
|
-
-- CONVERSATIONS - datetime_del deve ser > datetime_add quando não for NULL
|
|
256
|
-
DO $$
|
|
257
|
-
BEGIN
|
|
258
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
259
|
-
WHERE constraint_name = 'chk_conversations_delete_after_add'
|
|
260
|
-
AND table_name = 'conversations') THEN
|
|
261
|
-
|
|
262
|
-
ALTER TABLE conversations
|
|
263
|
-
ADD CONSTRAINT chk_conversations_delete_after_add
|
|
264
|
-
CHECK (datetime_del IS NULL OR datetime_del > datetime_add);
|
|
265
|
-
|
|
266
|
-
RAISE NOTICE '[%] ✓ Constraint temporal conversations.datetime_del > datetime_add', now();
|
|
267
|
-
END IF;
|
|
268
|
-
EXCEPTION
|
|
269
|
-
WHEN OTHERS THEN
|
|
270
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint temporal delete conversations: %', now(), SQLERRM;
|
|
271
|
-
END $$;
|
|
272
|
-
|
|
273
|
-
-- MESSAGES - datetime_alt deve ser >= datetime_add
|
|
274
|
-
DO $$
|
|
275
|
-
BEGIN
|
|
276
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
277
|
-
WHERE constraint_name = 'chk_messages_datetime_consistency'
|
|
278
|
-
AND table_name = 'messages') THEN
|
|
279
|
-
|
|
280
|
-
ALTER TABLE messages
|
|
281
|
-
ADD CONSTRAINT chk_messages_datetime_consistency
|
|
282
|
-
CHECK (datetime_alt >= datetime_add);
|
|
283
|
-
|
|
284
|
-
RAISE NOTICE '[%] ✓ Constraint temporal messages.datetime_alt >= datetime_add', now();
|
|
285
|
-
END IF;
|
|
286
|
-
EXCEPTION
|
|
287
|
-
WHEN OTHERS THEN
|
|
288
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint temporal messages: %', now(), SQLERRM;
|
|
289
|
-
END $$;
|
|
290
|
-
|
|
291
|
-
-- MESSAGES - datetime_del deve ser > datetime_add quando não for NULL
|
|
292
|
-
DO $$
|
|
293
|
-
BEGIN
|
|
294
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
295
|
-
WHERE constraint_name = 'chk_messages_delete_after_add'
|
|
296
|
-
AND table_name = 'messages') THEN
|
|
297
|
-
|
|
298
|
-
ALTER TABLE messages
|
|
299
|
-
ADD CONSTRAINT chk_messages_delete_after_add
|
|
300
|
-
CHECK (datetime_del IS NULL OR datetime_del > datetime_add);
|
|
301
|
-
|
|
302
|
-
RAISE NOTICE '[%] ✓ Constraint temporal messages.datetime_del > datetime_add', now();
|
|
303
|
-
END IF;
|
|
304
|
-
EXCEPTION
|
|
305
|
-
WHEN OTHERS THEN
|
|
306
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint temporal delete messages: %', now(), SQLERRM;
|
|
307
|
-
END $$;
|
|
308
|
-
|
|
309
|
-
-- ========================================
|
|
310
|
-
-- CONSTRAINTS DE INTEGRIDADE LÓGICA
|
|
311
|
-
-- ========================================
|
|
312
|
-
|
|
313
|
-
-- MESSAGES - não pode ter tanto id_agent quanto id_user simultaneamente
|
|
314
|
-
DO $$
|
|
315
|
-
BEGIN
|
|
316
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
317
|
-
WHERE constraint_name = 'chk_messages_single_author'
|
|
318
|
-
AND table_name = 'messages') THEN
|
|
319
|
-
|
|
320
|
-
ALTER TABLE messages
|
|
321
|
-
ADD CONSTRAINT chk_messages_single_author
|
|
322
|
-
CHECK (
|
|
323
|
-
(id_agent IS NOT NULL AND id_user IS NULL) OR
|
|
324
|
-
(id_agent IS NULL AND id_user IS NOT NULL) OR
|
|
325
|
-
(id_agent IS NULL AND id_user IS NULL)
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
RAISE NOTICE '[%] ✓ Constraint lógica messages autor único', now();
|
|
329
|
-
END IF;
|
|
330
|
-
EXCEPTION
|
|
331
|
-
WHEN OTHERS THEN
|
|
332
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint lógica messages autor: %', now(), SQLERRM;
|
|
333
|
-
END $$;
|
|
334
|
-
|
|
335
|
-
-- AGENTS - delay_typing deve ser >= 0 (se não existir)
|
|
336
|
-
DO $$
|
|
337
|
-
BEGIN
|
|
338
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
339
|
-
WHERE constraint_name = 'agents_delay_typing_valid'
|
|
340
|
-
AND table_name = 'agents')
|
|
341
|
-
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
342
|
-
WHERE constraint_name = 'chk_agents_delay_positive'
|
|
343
|
-
AND table_name = 'agents') THEN
|
|
344
|
-
|
|
345
|
-
ALTER TABLE agents
|
|
346
|
-
ADD CONSTRAINT chk_agents_delay_positive
|
|
347
|
-
CHECK (delay_typing IS NULL OR delay_typing >= 0);
|
|
348
|
-
|
|
349
|
-
RAISE NOTICE '[%] ✓ Constraint lógica agents.delay_typing >= 0', now();
|
|
350
|
-
END IF;
|
|
351
|
-
EXCEPTION
|
|
352
|
-
WHEN OTHERS THEN
|
|
353
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint lógica agents.delay_typing: %', now(), SQLERRM;
|
|
354
|
-
END $$;
|
|
355
|
-
|
|
356
|
-
-- AGENTS - waiting_time deve ser >= 0 (se não existir)
|
|
357
|
-
DO $$
|
|
358
|
-
BEGIN
|
|
359
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
360
|
-
WHERE constraint_name = 'agents_waiting_time_valid'
|
|
361
|
-
AND table_name = 'agents')
|
|
362
|
-
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
363
|
-
WHERE constraint_name = 'chk_agents_waiting_positive'
|
|
364
|
-
AND table_name = 'agents') THEN
|
|
365
|
-
|
|
366
|
-
ALTER TABLE agents
|
|
367
|
-
ADD CONSTRAINT chk_agents_waiting_positive
|
|
368
|
-
CHECK (waiting_time IS NULL OR waiting_time >= 0);
|
|
369
|
-
|
|
370
|
-
RAISE NOTICE '[%] ✓ Constraint lógica agents.waiting_time >= 0', now();
|
|
371
|
-
END IF;
|
|
372
|
-
EXCEPTION
|
|
373
|
-
WHEN OTHERS THEN
|
|
374
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint lógica agents.waiting_time: %', now(), SQLERRM;
|
|
375
|
-
END $$;
|
|
376
|
-
|
|
377
|
-
-- OBJECTIVES - order_number deve ser > 0 (se não existir)
|
|
378
|
-
DO $$
|
|
379
|
-
BEGIN
|
|
380
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
381
|
-
WHERE constraint_name = 'objectives_order_valid'
|
|
382
|
-
AND table_name = 'objectives')
|
|
383
|
-
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
384
|
-
WHERE constraint_name = 'chk_objectives_order_positive'
|
|
385
|
-
AND table_name = 'objectives') THEN
|
|
386
|
-
|
|
387
|
-
ALTER TABLE objectives
|
|
388
|
-
ADD CONSTRAINT chk_objectives_order_positive
|
|
389
|
-
CHECK (order_number IS NULL OR order_number > 0);
|
|
390
|
-
|
|
391
|
-
RAISE NOTICE '[%] ✓ Constraint lógica objectives.order_number > 0', now();
|
|
392
|
-
END IF;
|
|
393
|
-
EXCEPTION
|
|
394
|
-
WHEN OTHERS THEN
|
|
395
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint lógica objectives.order_number: %', now(), SQLERRM;
|
|
396
|
-
END $$;
|
|
397
|
-
|
|
398
|
-
-- ========================================
|
|
399
|
-
-- CONSTRAINTS DE SEGURANÇA
|
|
400
|
-
-- ========================================
|
|
401
|
-
|
|
402
|
-
-- CREDENTIALS - campos críticos não podem estar vazios
|
|
403
|
-
DO $$
|
|
404
|
-
BEGIN
|
|
405
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
406
|
-
WHERE constraint_name = 'chk_credentials_required_fields'
|
|
407
|
-
AND table_name = 'credentials') THEN
|
|
408
|
-
|
|
409
|
-
ALTER TABLE credentials
|
|
410
|
-
ADD CONSTRAINT chk_credentials_required_fields
|
|
411
|
-
CHECK (
|
|
412
|
-
title IS NOT NULL AND trim(title) != '' AND
|
|
413
|
-
provider IS NOT NULL AND trim(provider) != '' AND
|
|
414
|
-
authType IS NOT NULL AND trim(authType) != ''
|
|
415
|
-
);
|
|
416
|
-
|
|
417
|
-
RAISE NOTICE '[%] ✓ Constraint segurança credentials campos obrigatórios', now();
|
|
418
|
-
END IF;
|
|
419
|
-
EXCEPTION
|
|
420
|
-
WHEN OTHERS THEN
|
|
421
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint segurança credentials: %', now(), SQLERRM;
|
|
422
|
-
END $$;
|
|
423
|
-
|
|
424
|
-
-- ACCOUNTS - nome deve existir e não estar vazio
|
|
425
|
-
DO $$
|
|
426
|
-
BEGIN
|
|
427
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
428
|
-
WHERE constraint_name = 'chk_accounts_name_required'
|
|
429
|
-
AND table_name = 'accounts') THEN
|
|
430
|
-
|
|
431
|
-
ALTER TABLE accounts
|
|
432
|
-
ADD CONSTRAINT chk_accounts_name_required
|
|
433
|
-
CHECK (name IS NOT NULL AND trim(name) != '');
|
|
434
|
-
|
|
435
|
-
RAISE NOTICE '[%] ✓ Constraint segurança accounts.name obrigatório', now();
|
|
436
|
-
END IF;
|
|
437
|
-
EXCEPTION
|
|
438
|
-
WHEN OTHERS THEN
|
|
439
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint segurança accounts.name: %', now(), SQLERRM;
|
|
440
|
-
END $$;
|
|
441
|
-
|
|
442
|
-
-- USERS - email deve existir e não estar vazio
|
|
443
|
-
DO $$
|
|
444
|
-
BEGIN
|
|
445
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
446
|
-
WHERE constraint_name = 'chk_users_email_required'
|
|
447
|
-
AND table_name = 'users') THEN
|
|
448
|
-
|
|
449
|
-
ALTER TABLE users
|
|
450
|
-
ADD CONSTRAINT chk_users_email_required
|
|
451
|
-
CHECK (email IS NOT NULL AND trim(email) != '');
|
|
452
|
-
|
|
453
|
-
RAISE NOTICE '[%] ✓ Constraint segurança users.email obrigatório', now();
|
|
454
|
-
END IF;
|
|
455
|
-
EXCEPTION
|
|
456
|
-
WHEN OTHERS THEN
|
|
457
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint segurança users.email: %', now(), SQLERRM;
|
|
458
|
-
END $$;
|
|
459
|
-
|
|
460
|
-
-- MESSAGES - content deve existir e não estar vazio
|
|
461
|
-
DO $$
|
|
462
|
-
BEGIN
|
|
463
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
464
|
-
WHERE constraint_name = 'chk_messages_content_required'
|
|
465
|
-
AND table_name = 'messages') THEN
|
|
466
|
-
|
|
467
|
-
ALTER TABLE messages
|
|
468
|
-
ADD CONSTRAINT chk_messages_content_required
|
|
469
|
-
CHECK (content IS NOT NULL AND trim(content) != '');
|
|
470
|
-
|
|
471
|
-
RAISE NOTICE '[%] ✓ Constraint segurança messages.content obrigatório', now();
|
|
472
|
-
END IF;
|
|
473
|
-
EXCEPTION
|
|
474
|
-
WHEN OTHERS THEN
|
|
475
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar constraint segurança messages.content: %', now(), SQLERRM;
|
|
476
|
-
END $$;
|
|
477
|
-
|
|
478
|
-
-- ========================================
|
|
479
|
-
-- VALIDAÇÕES AVANÇADAS ADICIONAIS
|
|
480
|
-
-- ========================================
|
|
481
|
-
|
|
482
|
-
-- COMPANIES - validação URL mais robusta (se não existir)
|
|
483
|
-
DO $$
|
|
484
|
-
BEGIN
|
|
485
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
486
|
-
WHERE constraint_name = 'companies_website_valid'
|
|
487
|
-
AND table_name = 'companies')
|
|
488
|
-
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
489
|
-
WHERE constraint_name = 'chk_companies_website_format'
|
|
490
|
-
AND table_name = 'companies') THEN
|
|
491
|
-
|
|
492
|
-
ALTER TABLE companies
|
|
493
|
-
ADD CONSTRAINT chk_companies_website_format
|
|
494
|
-
CHECK (website IS NULL OR website ~* '^https?://[A-Za-z0-9.-]+\.[A-Za-z]{2,}(/.*)?$');
|
|
495
|
-
|
|
496
|
-
RAISE NOTICE '[%] ✓ Validação avançada companies.website', now();
|
|
497
|
-
END IF;
|
|
498
|
-
EXCEPTION
|
|
499
|
-
WHEN OTHERS THEN
|
|
500
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar validação avançada companies.website: %', now(), SQLERRM;
|
|
501
|
-
END $$;
|
|
502
|
-
|
|
503
|
-
-- LEADS - validação telefone mais robusta (se não existir)
|
|
504
|
-
DO $$
|
|
505
|
-
BEGIN
|
|
506
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
507
|
-
WHERE constraint_name = 'leads_phone_valid'
|
|
508
|
-
AND table_name = 'leads')
|
|
509
|
-
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
510
|
-
WHERE constraint_name = 'chk_leads_phone_format'
|
|
511
|
-
AND table_name = 'leads') THEN
|
|
512
|
-
|
|
513
|
-
ALTER TABLE leads
|
|
514
|
-
ADD CONSTRAINT chk_leads_phone_format
|
|
515
|
-
CHECK (phone IS NULL OR phone ~ '^\+?[1-9]\d{1,14}$');
|
|
516
|
-
|
|
517
|
-
RAISE NOTICE '[%] ✓ Validação avançada leads.phone', now();
|
|
518
|
-
END IF;
|
|
519
|
-
EXCEPTION
|
|
520
|
-
WHEN OTHERS THEN
|
|
521
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar validação avançada leads.phone: %', now(), SQLERRM;
|
|
522
|
-
END $$;
|
|
523
|
-
|
|
524
|
-
-- TAGS - validação cor hexadecimal mais robusta (se não existir)
|
|
525
|
-
DO $$
|
|
526
|
-
BEGIN
|
|
527
|
-
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
528
|
-
WHERE constraint_name = 'tags_color_valid'
|
|
529
|
-
AND table_name = 'tags')
|
|
530
|
-
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints
|
|
531
|
-
WHERE constraint_name = 'chk_tags_color_format'
|
|
532
|
-
AND table_name = 'tags') THEN
|
|
533
|
-
|
|
534
|
-
ALTER TABLE tags
|
|
535
|
-
ADD CONSTRAINT chk_tags_color_format
|
|
536
|
-
CHECK (color IS NULL OR color ~ '^#[A-Fa-f0-9]{6}$');
|
|
537
|
-
|
|
538
|
-
RAISE NOTICE '[%] ✓ Validação avançada tags.color', now();
|
|
539
|
-
END IF;
|
|
540
|
-
EXCEPTION
|
|
541
|
-
WHEN OTHERS THEN
|
|
542
|
-
RAISE WARNING '[%] ✗ Erro ao adicionar validação avançada tags.color: %', now(), SQLERRM;
|
|
543
|
-
END $$;
|
|
544
|
-
|
|
545
|
-
-- ========================================
|
|
546
|
-
-- VERIFICAÇÃO E RELATÓRIO FINAL
|
|
547
|
-
-- ========================================
|
|
548
|
-
DO $$
|
|
549
|
-
DECLARE
|
|
550
|
-
constraint_count INTEGER;
|
|
551
|
-
fk_count INTEGER;
|
|
552
|
-
check_count INTEGER;
|
|
553
|
-
BEGIN
|
|
554
|
-
-- Contar constraints totais
|
|
555
|
-
SELECT COUNT(*) INTO constraint_count
|
|
556
|
-
FROM information_schema.table_constraints
|
|
557
|
-
WHERE table_schema = current_schema();
|
|
558
|
-
|
|
559
|
-
-- Contar foreign keys
|
|
560
|
-
SELECT COUNT(*) INTO fk_count
|
|
561
|
-
FROM information_schema.table_constraints
|
|
562
|
-
WHERE table_schema = current_schema()
|
|
563
|
-
AND constraint_type = 'FOREIGN KEY';
|
|
564
|
-
|
|
565
|
-
-- Contar check constraints
|
|
566
|
-
SELECT COUNT(*) INTO check_count
|
|
567
|
-
FROM information_schema.table_constraints
|
|
568
|
-
WHERE table_schema = current_schema()
|
|
569
|
-
AND constraint_type = 'CHECK';
|
|
570
|
-
|
|
571
|
-
RAISE NOTICE '';
|
|
572
|
-
RAISE NOTICE '========================================';
|
|
573
|
-
RAISE NOTICE 'RELATÓRIO FINAL - CONSTRAINTS ADICIONADAS';
|
|
574
|
-
RAISE NOTICE '========================================';
|
|
575
|
-
RAISE NOTICE '[%] ✓ Constraint cíclica conversations ↔ messages: RESOLVIDA', now();
|
|
576
|
-
RAISE NOTICE '[%] ✓ Total de constraints no schema: %', now(), constraint_count;
|
|
577
|
-
RAISE NOTICE '[%] ✓ Total de foreign keys: %', now(), fk_count;
|
|
578
|
-
RAISE NOTICE '[%] ✓ Total de check constraints: %', now(), check_count;
|
|
579
|
-
RAISE NOTICE '[%] ✓ Validações de integridade: IMPLEMENTADAS', now();
|
|
580
|
-
RAISE NOTICE '[%] ✓ Validações de segurança: IMPLEMENTADAS', now();
|
|
581
|
-
RAISE NOTICE '[%] ✓ Validações temporais: IMPLEMENTADAS', now();
|
|
582
|
-
RAISE NOTICE '[%] ✓ Constraints adicionais criadas com sucesso!', now();
|
|
583
|
-
RAISE NOTICE '========================================';
|
|
584
|
-
RAISE NOTICE '';
|
|
585
|
-
END $$;
|