-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathschema.diff
More file actions
1270 lines (983 loc) · 59.7 KB
/
schema.diff
File metadata and controls
1270 lines (983 loc) · 59.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/local_schema.sql b/remote_schema.sql
index a51caeb..4979dfa 100644
--- a/local_schema.sql
+++ b/remote_schema.sql
@@ -123,8 +123,7 @@ CREATE OR REPLACE FUNCTION "public"."can_select_chat"("check_chat_id" "uuid") RE
SELECT EXISTS (
SELECT 1
FROM public.chats c
- WHERE c.id = check_chat_id
- AND (
+ WHERE c.id = check_chat_id AND (
-- Case 1: Personal chat, user is the owner
(c.organization_id IS NULL AND c.user_id = auth.uid()) OR
-- Case 2: Organization chat, user is a member of that organization
@@ -146,19 +145,13 @@ DECLARE
BEGIN
-- 1. Find user_id from email
SELECT id INTO target_user_id FROM auth.users WHERE email = target_email LIMIT 1;
-
-- 2. If user_id found, check membership status in the target org
IF target_user_id IS NOT NULL THEN
RETURN QUERY
- SELECT om.status
- FROM public.organization_members om
- WHERE om.organization_id = target_org_id
- AND om.user_id = target_user_id
- AND om.status IN ('active', 'pending'); -- Check for active or pending join request
+ SELECT om.status::text FROM public.organization_members om
+ WHERE om.organization_id = target_org_id AND om.user_id = target_user_id;
END IF;
-
- -- If user_id not found or no matching membership, return empty set
- RETURN;
+ -- 3. If no user or membership found, return nothing
END;
$$;
@@ -186,29 +179,25 @@ BEGIN
RETURN NEW;
END IF;
END IF;
-
IF TG_OP = 'DELETE' THEN
v_organization_id := OLD.organization_id;
ELSE
v_organization_id := NEW.organization_id;
END IF;
-
IF EXISTS (SELECT 1 FROM public.organizations WHERE id = v_organization_id AND deleted_at IS NOT NULL) THEN
IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF;
END IF;
-
v_is_admin_being_removed := (
TG_OP = 'DELETE' AND OLD.role = 'admin' AND OLD.status = 'active'
) OR (
TG_OP = 'UPDATE' AND
- OLD.role = 'admin' AND OLD.status = 'active' AND
+ OLD.role = 'admin' AND
+ OLD.status = 'active' AND
(NEW.role <> 'admin' OR NEW.status <> 'active')
);
-
IF NOT v_is_admin_being_removed THEN
IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF;
END IF;
-
SELECT count(*)
INTO v_other_admin_count
FROM public.organization_members om
@@ -218,11 +207,9 @@ BEGIN
AND om.status = 'active'
AND o.deleted_at IS NULL
AND om.id <> OLD.id;
-
IF v_other_admin_count = 0 THEN
RAISE EXCEPTION 'Cannot remove or demote the last admin of organization %', v_organization_id;
END IF;
-
IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF;
END;
$$;
@@ -284,22 +271,14 @@ CREATE OR REPLACE FUNCTION "public"."create_org_and_admin_member"("p_user_id" "u
DECLARE
new_org_id uuid;
BEGIN
- -- Insert the new organization
- INSERT INTO public.organizations (name, visibility)
- VALUES (p_org_name, p_org_visibility)
+ -- Create the organization
+ INSERT INTO public.organizations (name, created_by, visibility)
+ VALUES (p_org_name, p_user_id, p_org_visibility)
RETURNING id INTO new_org_id;
-
- -- Insert the creating user as the initial admin member
- INSERT INTO public.organization_members (user_id, organization_id, role, status)
- VALUES (p_user_id, new_org_id, 'admin', 'active');
-
- -- Return the new organization's ID
+ -- Create the organization member
+ INSERT INTO public.organization_members (organization_id, user_id, role, status)
+ VALUES (new_org_id, p_user_id, 'admin', 'active');
RETURN new_org_id;
-EXCEPTION
- WHEN OTHERS THEN
- -- Log the error and re-raise it to ensure the transaction is rolled back
- RAISE WARNING 'Error in create_org_and_admin_member: SQLSTATE: %, MESSAGE: %', SQLSTATE, SQLERRM;
- RAISE;
END;
$$;
@@ -324,18 +303,15 @@ BEGIN
SELECT user_id, organization_id INTO v_chat_owner_id, v_chat_org_id
FROM public.chats
WHERE id = p_chat_id;
-
IF NOT FOUND THEN
RETURN 'NOT FOUND';
END IF;
-
-- 2. Permission Check
IF v_chat_org_id IS NOT NULL THEN
-- Organization chat
SELECT role INTO v_user_role
FROM public.organization_members
WHERE organization_id = v_chat_org_id AND user_id = p_user_id AND status = 'active';
-
IF v_user_role IS NULL OR NOT (v_user_role = 'admin' OR v_chat_owner_id = p_user_id) THEN
RETURN 'ORG PERMISSION DENIED';
END IF;
@@ -345,13 +321,10 @@ BEGIN
RETURN 'PERSONAL PERMISSION DENIED';
END IF;
END IF;
-
-- 3. Perform Deletions
DELETE FROM public.chat_messages WHERE chat_id = p_chat_id;
DELETE FROM public.chats WHERE id = p_chat_id;
-
RETURN 'DELETED';
-
END;
$$;
@@ -372,18 +345,15 @@ BEGIN
SELECT user_id, organization_id INTO v_chat_owner_id, v_chat_org_id
FROM public.chats
WHERE id = p_chat_id;
-
IF NOT FOUND THEN
RETURN 'NOT FOUND';
END IF;
-
-- 2. Permission Check
IF v_chat_org_id IS NOT NULL THEN
-- Organization chat
SELECT role INTO v_user_role
FROM public.organization_members
WHERE organization_id = v_chat_org_id AND user_id = p_user_id AND status = 'active';
-
IF NOT (v_user_role = 'admin' OR v_chat_owner_id = p_user_id) THEN
RETURN 'ORG PERMISSION DENIED';
END IF;
@@ -393,13 +363,10 @@ BEGIN
RETURN 'PERSONAL PERMISSION DENIED';
END IF;
END IF;
-
-- 3. Perform Deletions
DELETE FROM public.chat_messages WHERE chat_id = p_chat_id;
DELETE FROM public.chats WHERE id = p_chat_id;
-
RETURN 'DELETED';
-
END;
$$;
@@ -415,11 +382,9 @@ BEGIN
IF NEW.user_id <> OLD.user_id THEN
RAISE EXCEPTION 'Changing the user_id of a chat is not allowed.';
END IF;
-
IF NEW.organization_id IS DISTINCT FROM OLD.organization_id THEN
RAISE EXCEPTION 'Changing the organization_id of a chat is not allowed.';
END IF;
-
RETURN NEW;
END;
$$;
@@ -445,6 +410,23 @@ $$;
ALTER FUNCTION "public"."execute_sql"("query" "text") OWNER TO "postgres";
+CREATE OR REPLACE FUNCTION "public"."get_user_email"("p_user_id" "uuid") RETURNS "text"
+ LANGUAGE "plpgsql" SECURITY DEFINER
+ SET "search_path" TO ''
+ AS $$
+BEGIN
+ RETURN (SELECT email FROM auth.users WHERE id = p_user_id);
+END;
+$$;
+
+
+ALTER FUNCTION "public"."get_user_email"("p_user_id" "uuid") OWNER TO "postgres";
+
+
+COMMENT ON FUNCTION "public"."get_user_email"("p_user_id" "uuid") IS 'Retrieves a user''s email by their UUID. SECURITY DEFINER is used to bypass RLS.';
+
+
+
CREATE OR REPLACE FUNCTION "public"."grant_initial_free_tokens_to_user"("p_user_id" "uuid", "p_free_plan_id" "uuid") RETURNS "void"
LANGUAGE "plpgsql" SECURITY DEFINER
SET "search_path" TO ''
@@ -516,31 +498,31 @@ CREATE OR REPLACE FUNCTION "public"."handle_member_removed"() RETURNS "trigger"
AS $$
DECLARE
org_name TEXT;
- is_org_deleted BOOLEAN;
BEGIN
- -- Check if the organization is soft-deleted (though removal might still happen)
- SELECT deleted_at IS NOT NULL, name
- INTO is_org_deleted, org_name
+ -- Only trigger when status changes from 'active' to 'removed'
+ IF OLD.status <> 'active' OR NEW.status <> 'removed' THEN
+ RETURN NULL;
+ END IF;
+ -- Get organization name
+ SELECT name INTO org_name
FROM public.organizations
- WHERE id = NEW.organization_id; -- Use NEW or OLD, should be same org
-
+ WHERE id = NEW.organization_id;
IF NOT FOUND THEN
-- Org might be hard deleted? Or FK constraint failed?
-- For now, just exit gracefully if org not found.
RETURN NULL;
END IF;
-
- -- Create notification for the removed user
- PERFORM public.create_notification_for_user(
+ -- Insert notification for the removed user
+ INSERT INTO public.notifications (user_id, type, data)
+ VALUES (
NEW.user_id,
- 'org_membership_removed',
+ 'org_membership_terminated',
jsonb_build_object(
+ 'reason', 'removed_by_admin',
'organization_id', NEW.organization_id,
- 'organization_name', org_name,
- 'target_path', '/dashboard/organizations' -- General path after removal
+ 'organization_name', org_name
)
);
-
RETURN NULL;
END;
$$;
@@ -555,21 +537,18 @@ CREATE OR REPLACE FUNCTION "public"."handle_member_role_change"() RETURNS "trigg
AS $$
DECLARE
org_name TEXT;
- is_org_deleted BOOLEAN;
BEGIN
- -- Check if the organization is soft-deleted
- SELECT deleted_at IS NOT NULL, name
- INTO is_org_deleted, org_name
- FROM public.organizations
- WHERE id = NEW.organization_id;
-
- -- Only proceed if the organization exists and is not deleted
- IF NOT FOUND OR is_org_deleted THEN
+ -- Only trigger if the role has actually changed
+ IF OLD.role = NEW.role THEN
RETURN NULL;
END IF;
-
- -- Create notification for the affected user
- PERFORM public.create_notification_for_user(
+ -- Get organization name
+ SELECT name INTO org_name
+ FROM public.organizations
+ WHERE id = NEW.organization_id;
+ -- Insert a notification for the affected user
+ INSERT INTO public.notifications (user_id, type, data)
+ VALUES (
NEW.user_id,
'org_role_changed',
jsonb_build_object(
@@ -580,7 +559,6 @@ BEGIN
'target_path', '/dashboard/organizations/' || NEW.organization_id::text || '/settings'
)
);
-
RETURN NULL;
END;
$$;
@@ -599,14 +577,12 @@ DECLARE
inviter_name text;
full_name text;
BEGIN
- -- Find the user_id associated with the invited email
+ -- Find the user_id for the invited email
SELECT id INTO invited_user_id FROM auth.users WHERE email = NEW.invited_email;
-
- -- Only proceed if the user exists in auth.users
+ -- If the user exists, create a notification for them
IF invited_user_id IS NOT NULL THEN
-- Get organization name
SELECT name INTO organization_name FROM public.organizations WHERE id = NEW.organization_id;
-
-- Get inviter name (optional, use email if profile/name not found)
SELECT
TRIM(p.first_name || ' ' || p.last_name),
@@ -617,11 +593,9 @@ BEGIN
FROM auth.users u
LEFT JOIN public.user_profiles p ON u.id = p.id
WHERE u.id = NEW.invited_by_user_id;
-
IF full_name IS NOT NULL AND full_name <> '' THEN
inviter_name := full_name;
END IF;
-
INSERT INTO public.notifications (user_id, type, data)
VALUES (
invited_user_id,
@@ -642,7 +616,6 @@ BEGIN
ELSE
RAISE LOG 'Invited user with email % not found in auth.users, no notification created.', NEW.invited_email;
END IF;
-
RETURN NEW;
END;
$$;
@@ -664,17 +637,15 @@ DECLARE
org_name TEXT;
is_org_deleted BOOLEAN;
BEGIN
- -- Check if the organization is soft-deleted
- SELECT deleted_at IS NOT NULL, name
- INTO is_org_deleted, org_name
+ -- Get organization name and check if it's deleted
+ SELECT name, (deleted_at IS NOT NULL)
+ INTO org_name, is_org_deleted
FROM public.organizations
WHERE id = NEW.organization_id;
-
-- Only proceed if the organization exists and is not deleted
IF NOT FOUND OR is_org_deleted THEN
RETURN NULL;
END IF;
-
-- Find all active admins of this organization
FOR admin_record IN
SELECT user_id
@@ -683,20 +654,17 @@ BEGIN
AND role = 'admin'
AND status = 'active'
LOOP
- -- Create notification for each admin
+ -- Create a notification for each admin
PERFORM public.create_notification_for_user(
admin_record.user_id,
- 'org_join_request',
+ 'join_request',
jsonb_build_object(
'requesting_user_id', NEW.user_id,
'organization_id', NEW.organization_id,
- 'organization_name', org_name,
- 'membership_id', NEW.id,
- 'target_path', '/dashboard/organizations/' || NEW.organization_id::text || '/members?action=review&memberId=' || NEW.id::text
+ 'organization_name', org_name
)
);
END LOOP;
-
RETURN NULL;
END;
$$;
@@ -724,7 +692,7 @@ ALTER FUNCTION "public"."handle_new_organization"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."handle_new_user"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
- SET "search_path" TO ''
+ SET "search_path" TO 'public'
AS $$
DECLARE
v_user_id UUID := NEW.id;
@@ -741,58 +709,48 @@ DECLARE
v_idempotency_key_grant TEXT;
BEGIN
RAISE LOG '[handle_new_user] Processing new user ID: %, Email: %', v_user_id, v_user_email;
-
v_profile_first_name := v_raw_user_meta_data ->> 'first_name';
INSERT INTO public.user_profiles (id, role, first_name)
VALUES (v_user_id, 'user', v_profile_first_name)
ON CONFLICT (id) DO NOTHING;
RAISE LOG '[handle_new_user] Ensured profile for user ID: %.', v_user_id;
-
INSERT INTO public.token_wallets (user_id, currency)
VALUES (v_user_id, 'AI_TOKEN')
ON CONFLICT (user_id) WHERE organization_id IS NULL
DO NOTHING
RETURNING wallet_id INTO v_target_wallet_id;
-
IF v_target_wallet_id IS NULL THEN
SELECT wallet_id INTO v_target_wallet_id
FROM public.token_wallets
WHERE user_id = v_user_id AND organization_id IS NULL;
END IF;
-
IF v_target_wallet_id IS NULL THEN
RAISE WARNING '[handle_new_user] Failed to create or find personal wallet for user ID: %. Aborting token grant.', v_user_id;
RETURN NEW;
END IF;
RAISE LOG '[handle_new_user] Ensured wallet ID: % for user ID: %.', v_target_wallet_id, v_user_id;
-
SELECT id, tokens_to_award INTO v_free_plan_id, v_tokens_to_award
FROM public.subscription_plans
WHERE name = 'Free'
LIMIT 1;
-
IF v_free_plan_id IS NULL THEN
RAISE LOG '[handle_new_user] "Free" plan not found. No initial tokens will be granted for user ID: %.', v_user_id;
ELSIF v_tokens_to_award IS NULL OR v_tokens_to_award <= 0 THEN
RAISE LOG '[handle_new_user] "Free" plan (ID: %) found, but tokens_to_award is not positive (Value: %). No initial tokens for user ID: %.', v_free_plan_id, v_tokens_to_award, v_user_id;
ELSE
RAISE LOG '[handle_new_user] "Free" plan ID: % found with % tokens to award for user ID: %.', v_free_plan_id, v_tokens_to_award, v_user_id;
-
INSERT INTO public.user_subscriptions (user_id, plan_id, status, current_period_start, current_period_end)
VALUES (v_user_id, v_free_plan_id, 'free', NOW(), NOW() + interval '1 month')
ON CONFLICT (user_id)
DO UPDATE SET plan_id = EXCLUDED.plan_id, status = EXCLUDED.status, updated_at = NOW(), current_period_start = EXCLUDED.current_period_start, current_period_end = EXCLUDED.current_period_end
WHERE public.user_subscriptions.status <> 'free';
RAISE LOG '[handle_new_user] Ensured user % subscribed to Free plan %.', v_user_id, v_free_plan_id;
-
SELECT id INTO v_system_user_id FROM auth.users WHERE email LIKE v_system_user_email_pattern ORDER BY created_at DESC LIMIT 1;
-
IF v_system_user_id IS NULL THEN
RAISE WARNING '[handle_new_user] System user for token allocation (pattern: %) not found. Grant for user % will be recorded by the user themselves.', v_system_user_email_pattern, v_user_id;
END IF;
v_idempotency_key_grant := 'initial_free_grant_' || v_user_id::text || '_' || v_free_plan_id::text;
-
IF EXISTS (SELECT 1 FROM public.token_wallet_transactions WHERE wallet_id = v_target_wallet_id AND idempotency_key = v_idempotency_key_grant) THEN
RAISE LOG '[handle_new_user] Initial free tokens (Plan ID: %) already granted to user ID: % (Wallet: %) via idempotency key: %.', v_free_plan_id, v_user_id, v_target_wallet_id, v_idempotency_key_grant;
ELSE
@@ -800,9 +758,7 @@ BEGIN
BEGIN
SELECT balance INTO v_current_wallet_balance FROM public.token_wallets WHERE wallet_id = v_target_wallet_id FOR UPDATE;
v_new_wallet_balance := v_current_wallet_balance + v_tokens_to_award;
-
UPDATE public.token_wallets SET balance = v_new_wallet_balance, updated_at = now() WHERE public.token_wallets.wallet_id = v_target_wallet_id;
-
INSERT INTO public.token_wallet_transactions (
wallet_id, transaction_type, amount, balance_after_txn,
recorded_by_user_id, related_entity_id, related_entity_type, notes, idempotency_key
@@ -819,7 +775,6 @@ BEGIN
END;
END IF;
END IF;
-
RETURN NEW;
EXCEPTION
WHEN OTHERS THEN
@@ -848,7 +803,6 @@ BEGIN
example_admin_id := auth.uid();
example_requesting_user_id := '00000000-0000-0000-0000-000000000001';
example_org_id := '00000000-0000-0000-0000-000000000002';
-
PERFORM public.create_notification_for_user(
example_admin_id,
'join_request',
@@ -913,7 +867,7 @@ BEGIN
SELECT 1
FROM public.organization_members om
WHERE om.organization_id = org_id
- AND om.user_id = (SELECT auth.uid())
+ AND om.user_id = auth.uid()
AND om.role = 'admin'
AND om.status = 'active'
);
@@ -973,15 +927,12 @@ BEGIN
invited_user_id = NEW.id,
status = 'accepted'
WHERE id = invite_record.id;
-
INSERT INTO public.organization_members (user_id, organization_id, role, status)
VALUES (NEW.id, invite_record.organization_id, invite_record.role_to_assign, 'active')
ON CONFLICT (user_id, organization_id) DO UPDATE
SET role = EXCLUDED.role, status = 'active';
-
END LOOP;
END IF;
-
RETURN NEW;
END;
$$;
@@ -1246,21 +1197,17 @@ BEGIN
SELECT current_setting('role', true) INTO effective_role;
SELECT rolsuper INTO is_effective_role_superuser FROM pg_roles WHERE rolname = effective_role LIMIT 1;
is_effective_role_superuser := COALESCE(is_effective_role_superuser, FALSE);
-
current_auth_role := auth.role();
current_auth_uid := auth.uid()::text;
current_session_user := session_user;
-
RAISE LOG '[restrict_invite_update_fields V3] Current User: %, Session User: %, Effective Role: %, Is Superuser: %, Auth Role: %, Auth UID: %',
current_user, current_session_user, effective_role, is_effective_role_superuser, current_auth_role, current_auth_uid;
RAISE LOG '[restrict_invite_update_fields V3] OLD.invited_user_id: %, NEW.invited_user_id: %, OLD.status: %, NEW.status: %, OLD.invited_email: %, NEW.invited_email: %',
OLD.invited_user_id, NEW.invited_user_id, OLD.status, NEW.status, OLD.invited_email, NEW.invited_email;
-
IF effective_role IN ('service_role', 'supabase_admin', 'supabase_storage_admin') OR is_effective_role_superuser IS TRUE THEN
RAISE LOG '[restrict_invite_update_fields V3] Allowing update due to powerful service/superuser role: %', effective_role;
RETURN NEW;
END IF;
-
IF current_user = 'postgres' AND
OLD.status = 'pending' AND
OLD.invited_user_id IS NULL AND
@@ -1278,18 +1225,15 @@ BEGIN
RAISE LOG '[restrict_invite_update_fields V3] Allowing update by current_user "postgres" for automated invite linking. Invite ID: %', OLD.id;
RETURN NEW;
END IF;
-
SELECT public.is_org_admin(OLD.organization_id) INTO is_org_admin_check;
IF COALESCE(is_org_admin_check, FALSE) THEN
RAISE LOG '[restrict_invite_update_fields V3] Allowing update due to org admin status for user % and org %', current_auth_uid, OLD.organization_id;
RETURN NEW;
END IF;
-
IF current_auth_role != 'authenticated' OR (auth.jwt() ->> 'email') != OLD.invited_email THEN
RAISE WARNING '[restrict_invite_update_fields V3] Auth check failed for non-admin/non-service/non-postgres-auto. Auth Role: %, Invite Email: %, JWT Email: %', current_auth_role, OLD.invited_email, (auth.jwt() ->> 'email');
RAISE EXCEPTION 'User is not authorized to modify this invite (not invited user or not authenticated properly).';
END IF;
-
IF OLD.status = 'pending' AND NEW.status IN ('accepted', 'declined') THEN
IF NEW.status != OLD.status AND
(NEW.invited_user_id IS NOT DISTINCT FROM OLD.invited_user_id OR (OLD.invited_user_id IS NULL AND NEW.invited_user_id = auth.uid())) AND
@@ -1315,7 +1259,6 @@ BEGIN
RAISE WARNING '[restrict_invite_update_fields V3] Invalid status transition by non-admin/non-service/non-postgres-auto. OLD: %, NEW: %', row_to_json(OLD), row_to_json(NEW);
RAISE EXCEPTION 'Invite update rejected: Invalid status transition attempt by non-privileged user.';
END IF;
-
END;
$$;
@@ -2293,7 +2236,7 @@ COMMENT ON COLUMN "public"."payment_transactions"."status" IS 'Status of the pay
-COMMENT ON COLUMN "public"."payment_transactions"."amount_requested_fiat" IS 'Amount in fiat currency (e.g., USD, EUR), stored as an integer in cents, exactly as received from Stripe.';
+COMMENT ON COLUMN "public"."payment_transactions"."amount_requested_fiat" IS 'Amount of fiat currency user intended to pay.';
@@ -2312,7 +2255,7 @@ CREATE TABLE IF NOT EXISTS "public"."subscription_plans" (
"amount" integer,
"currency" "text",
"interval" "text",
- "interval_count" integer DEFAULT 1,
+ "interval_count" integer,
"metadata" "jsonb",
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"updated_at" timestamp with time zone DEFAULT "now"() NOT NULL,
@@ -2432,8 +2375,8 @@ CREATE TABLE IF NOT EXISTS "public"."token_wallet_transactions" (
"notes" "text",
"idempotency_key" character varying(255) NOT NULL,
"timestamp" timestamp with time zone DEFAULT "now"() NOT NULL,
- "recorded_by_user_id" "uuid" NOT NULL,
"payment_transaction_id" "uuid",
+ "recorded_by_user_id" "uuid" NOT NULL,
CONSTRAINT "token_wallet_transactions_amount_check" CHECK (("amount" > (0)::numeric))
);
@@ -2457,11 +2400,11 @@ COMMENT ON COLUMN "public"."token_wallet_transactions"."idempotency_key" IS 'Cli
-COMMENT ON COLUMN "public"."token_wallet_transactions"."recorded_by_user_id" IS 'ID of the user or system entity that recorded/initiated this transaction. Mandatory for auditability.';
+COMMENT ON COLUMN "public"."token_wallet_transactions"."payment_transaction_id" IS 'Link to the payment_transactions table if this ledger entry was created as a direct result of a payment.';
-COMMENT ON COLUMN "public"."token_wallet_transactions"."payment_transaction_id" IS 'Link to the payment_transactions table if this ledger entry was created as a direct result of a payment.';
+COMMENT ON COLUMN "public"."token_wallet_transactions"."recorded_by_user_id" IS 'ID of the user or system entity that recorded/initiated this transaction. Mandatory for auditability.';
@@ -2557,25 +2500,6 @@ COMMENT ON COLUMN "public"."user_subscriptions"."status" IS 'Matches Stripe subs
-CREATE OR REPLACE VIEW "public"."v_pending_membership_requests" AS
- SELECT "om"."id",
- "om"."user_id",
- "om"."organization_id",
- "om"."status",
- "om"."created_at",
- "om"."role",
- "up"."first_name",
- "up"."last_name",
- "au"."email" AS "user_email"
- FROM (("public"."organization_members" "om"
- LEFT JOIN "public"."user_profiles" "up" ON (("om"."user_id" = "up"."id")))
- LEFT JOIN "auth"."users" "au" ON (("om"."user_id" = "au"."id")))
- WHERE ("om"."status" = 'pending_approval'::"text");
-
-
-ALTER TABLE "public"."v_pending_membership_requests" OWNER TO "postgres";
-
-
ALTER TABLE ONLY "public"."ai_providers"
ADD CONSTRAINT "ai_providers_api_identifier_key" UNIQUE ("api_identifier");
@@ -2746,11 +2670,6 @@ ALTER TABLE ONLY "public"."system_prompts"
-ALTER TABLE ONLY "public"."system_prompts"
- ADD CONSTRAINT "system_prompts_name_unique" UNIQUE ("name");
-
-
-
ALTER TABLE ONLY "public"."system_prompts"
ADD CONSTRAINT "system_prompts_pkey" PRIMARY KEY ("id");
@@ -2781,11 +2700,6 @@ ALTER TABLE ONLY "public"."dialectic_feedback"
-ALTER TABLE ONLY "public"."dialectic_project_resources"
- ADD CONSTRAINT "unique_storage_path" UNIQUE ("storage_bucket", "storage_path");
-
-
-
ALTER TABLE ONLY "public"."organization_members"
ADD CONSTRAINT "unique_user_organization" UNIQUE ("user_id", "organization_id");
@@ -2815,16 +2729,15 @@ ALTER TABLE ONLY "public"."user_subscriptions"
-ALTER TABLE ONLY "public"."user_subscriptions"
- ADD CONSTRAINT "user_subscriptions_user_id_unique" UNIQUE ("user_id");
+CREATE INDEX "idx_ai_providers_provider" ON "public"."ai_providers" USING "btree" ("provider");
-CREATE INDEX "idx_ai_providers_provider" ON "public"."ai_providers" USING "btree" ("provider");
+CREATE INDEX "idx_chat_messages_active_thread" ON "public"."chat_messages" USING "btree" ("chat_id", "created_at") WHERE ("is_active_in_thread" = true);
-CREATE INDEX "idx_chat_messages_active_thread" ON "public"."chat_messages" USING "btree" ("chat_id", "created_at") WHERE ("is_active_in_thread" = true);
+CREATE INDEX "idx_chat_messages_ai_provider_id" ON "public"."chat_messages" USING "btree" ("ai_provider_id");
@@ -2852,10 +2765,22 @@ CREATE INDEX "idx_chat_messages_response_to_message_id" ON "public"."chat_messag
+CREATE INDEX "idx_chat_messages_system_prompt_id" ON "public"."chat_messages" USING "btree" ("system_prompt_id");
+
+
+
+CREATE INDEX "idx_chat_messages_user_id" ON "public"."chat_messages" USING "btree" ("user_id");
+
+
+
CREATE INDEX "idx_chats_organization_id" ON "public"."chats" USING "btree" ("organization_id") WHERE ("organization_id" IS NOT NULL);
+CREATE INDEX "idx_chats_system_prompt_id" ON "public"."chats" USING "btree" ("system_prompt_id");
+
+
+
CREATE INDEX "idx_chats_user_id" ON "public"."chats" USING "btree" ("user_id");
@@ -2876,6 +2801,10 @@ CREATE INDEX "idx_dialectic_contributions_original_model_is_latest" ON "public".
+CREATE INDEX "idx_dialectic_contributions_prompt_template_id_used" ON "public"."dialectic_contributions" USING "btree" ("prompt_template_id_used");
+
+
+
CREATE INDEX "idx_dialectic_contributions_session_id" ON "public"."dialectic_contributions" USING "btree" ("session_id");
@@ -2892,6 +2821,14 @@ CREATE INDEX "idx_dialectic_contributions_user_id" ON "public"."dialectic_contri
+CREATE INDEX "idx_dialectic_domains_parent_domain_id" ON "public"."dialectic_domains" USING "btree" ("parent_domain_id");
+
+
+
+CREATE INDEX "idx_dialectic_feedback_project_id" ON "public"."dialectic_feedback" USING "btree" ("project_id");
+
+
+
CREATE INDEX "idx_dialectic_feedback_session_id" ON "public"."dialectic_feedback" USING "btree" ("session_id");
@@ -2900,6 +2837,10 @@ CREATE INDEX "idx_dialectic_feedback_user_id" ON "public"."dialectic_feedback" U
+CREATE INDEX "idx_dialectic_process_templates_starting_stage_id" ON "public"."dialectic_process_templates" USING "btree" ("starting_stage_id");
+
+
+
CREATE INDEX "idx_dialectic_project_resources_project_id" ON "public"."dialectic_project_resources" USING "btree" ("project_id");
@@ -2908,10 +2849,66 @@ CREATE INDEX "idx_dialectic_project_resources_user_id" ON "public"."dialectic_pr
+CREATE INDEX "idx_dialectic_projects_initial_prompt_resource_id" ON "public"."dialectic_projects" USING "btree" ("initial_prompt_resource_id");
+
+
+
+CREATE INDEX "idx_dialectic_projects_process_template_id" ON "public"."dialectic_projects" USING "btree" ("process_template_id");
+
+
+
+CREATE INDEX "idx_dialectic_projects_selected_domain_id" ON "public"."dialectic_projects" USING "btree" ("selected_domain_id");
+
+
+
+CREATE INDEX "idx_dialectic_projects_selected_domain_overlay_id" ON "public"."dialectic_projects" USING "btree" ("selected_domain_overlay_id");
+
+
+
+CREATE INDEX "idx_dialectic_projects_user_id" ON "public"."dialectic_projects" USING "btree" ("user_id");
+
+
+
CREATE INDEX "idx_dialectic_sessions_associated_chat_id" ON "public"."dialectic_sessions" USING "btree" ("associated_chat_id");
+CREATE INDEX "idx_dialectic_sessions_current_stage_id" ON "public"."dialectic_sessions" USING "btree" ("current_stage_id");
+
+
+
+CREATE INDEX "idx_dialectic_sessions_project_id" ON "public"."dialectic_sessions" USING "btree" ("project_id");
+
+
+
+CREATE INDEX "idx_dialectic_stage_transitions_process_template_id" ON "public"."dialectic_stage_transitions" USING "btree" ("process_template_id");
+
+
+
+CREATE INDEX "idx_dialectic_stage_transitions_source_stage_id" ON "public"."dialectic_stage_transitions" USING "btree" ("source_stage_id");
+
+
+
+CREATE INDEX "idx_dialectic_stage_transitions_target_stage_id" ON "public"."dialectic_stage_transitions" USING "btree" ("target_stage_id");
+
+
+
+CREATE INDEX "idx_dialectic_stages_default_system_prompt_id" ON "public"."dialectic_stages" USING "btree" ("default_system_prompt_id");
+
+
+
+CREATE INDEX "idx_domain_process_associations_process_template_id" ON "public"."domain_process_associations" USING "btree" ("process_template_id");
+
+
+
+CREATE INDEX "idx_domain_specific_prompt_overlays_domain_id" ON "public"."domain_specific_prompt_overlays" USING "btree" ("domain_id");
+
+
+
+CREATE INDEX "idx_domain_specific_prompt_overlays_system_prompt_id" ON "public"."domain_specific_prompt_overlays" USING "btree" ("system_prompt_id");
+
+
+
CREATE INDEX "idx_invites_invited_by_user_id" ON "public"."invites" USING "btree" ("invited_by_user_id");
@@ -2940,6 +2937,10 @@ CREATE INDEX "idx_invites_status" ON "public"."invites" USING "btree" ("status")
+CREATE INDEX "idx_notifications_user_id" ON "public"."notifications" USING "btree" ("user_id");
+
+
+
CREATE INDEX "idx_notifications_user_id_created_at" ON "public"."notifications" USING "btree" ("user_id", "created_at" DESC);
@@ -2960,6 +2961,10 @@ CREATE INDEX "idx_payment_transactions_gateway_id" ON "public"."payment_transact
+CREATE INDEX "idx_payment_transactions_organization_id" ON "public"."payment_transactions" USING "btree" ("organization_id");
+
+
+
CREATE INDEX "idx_payment_transactions_status" ON "public"."payment_transactions" USING "btree" ("status");
@@ -2968,6 +2973,10 @@ CREATE INDEX "idx_payment_transactions_target_wallet_id" ON "public"."payment_tr
+CREATE INDEX "idx_payment_transactions_user_id" ON "public"."payment_transactions" USING "btree" ("user_id");
+
+
+
CREATE INDEX "idx_subscription_plans_stripe_product_id" ON "public"."subscription_plans" USING "btree" ("stripe_product_id");
@@ -2988,6 +2997,14 @@ CREATE INDEX "idx_subscription_transactions_user_id" ON "public"."subscription_t
+CREATE INDEX "idx_subscription_transactions_user_subscription_id" ON "public"."subscription_transactions" USING "btree" ("user_subscription_id");
+
+
+
+CREATE INDEX "idx_token_wallet_transactions_payment_transaction_id" ON "public"."token_wallet_transactions" USING "btree" ("payment_transaction_id");
+
+
+
CREATE INDEX "idx_token_wallet_transactions_recorded_by" ON "public"."token_wallet_transactions" USING "btree" ("recorded_by_user_id");
@@ -3004,10 +3021,6 @@ CREATE INDEX "idx_token_wallet_transactions_wallet_id" ON "public"."token_wallet
-CREATE INDEX "idx_token_wallet_transactions_wallet_id_timestamp" ON "public"."token_wallet_transactions" USING "btree" ("wallet_id", "timestamp" DESC);
-
-
-
CREATE INDEX "idx_token_wallets_organization_id" ON "public"."token_wallets" USING "btree" ("organization_id");
@@ -3024,6 +3037,10 @@ CREATE INDEX "idx_user_profiles_privacy_setting" ON "public"."user_profiles" USI
+CREATE INDEX "idx_user_subscriptions_plan_id" ON "public"."user_subscriptions" USING "btree" ("plan_id");
+
+
+
CREATE UNIQUE INDEX "one_default_process_per_domain_idx" ON "public"."domain_process_associations" USING "btree" ("domain_id") WHERE ("is_default_for_domain" = true);
@@ -3385,11 +3402,6 @@ ALTER TABLE ONLY "public"."token_wallet_transactions"
-ALTER TABLE ONLY "public"."token_wallet_transactions"
- ADD CONSTRAINT "token_wallet_transactions_recorded_by_user_id_fkey" FOREIGN KEY ("recorded_by_user_id") REFERENCES "auth"."users"("id") ON DELETE RESTRICT;
-
-
-
ALTER TABLE ONLY "public"."token_wallet_transactions"
ADD CONSTRAINT "token_wallet_transactions_wallet_id_fkey" FOREIGN KEY ("wallet_id") REFERENCES "public"."token_wallets"("wallet_id") ON DELETE CASCADE;
@@ -3433,35 +3445,33 @@ CREATE POLICY "Admin INSERT access for organization invites" ON "public"."invite
-CREATE POLICY "Admin SELECT access for organization invites" ON "public"."invites" FOR SELECT TO "authenticated" USING ("public"."is_org_admin"("organization_id"));
-
+CREATE POLICY "Allow active members to view memberships in their orgs" ON "public"."organization_members" FOR SELECT TO "authenticated" USING ((( SELECT "om"."role"
+ FROM "public"."organization_members" "om"
+ WHERE (("om"."organization_id" = "organization_members"."organization_id") AND ("om"."user_id" = "auth"."uid"()))) = 'admin'::"text"));
-CREATE POLICY "Admin UPDATE access for organization invites" ON "public"."invites" FOR UPDATE TO "authenticated" USING ("public"."is_org_admin"("organization_id")) WITH CHECK ("public"."is_org_admin"("organization_id"));
+CREATE POLICY "Allow active members to view memberships of their own organizat" ON "public"."organization_members" FOR SELECT TO "authenticated" USING (("organization_id" IN ( SELECT "organization_members_1"."organization_id"
+ FROM "public"."organization_members" "organization_members_1"
+ WHERE (("organization_members_1"."user_id" = "auth"."uid"()) AND ("organization_members_1"."status" = 'active'::"text")))));
-CREATE POLICY "Allow active members to view memberships in their orgs" ON "public"."organization_members" FOR SELECT TO "authenticated" USING ("public"."is_org_member"("organization_id", "auth"."uid"(), 'active'::"text"));
+CREATE POLICY "Allow active members to view their own organization" ON "public"."organizations" FOR SELECT TO "authenticated" USING (("id" IN ( SELECT "organization_members"."organization_id"
+ FROM "public"."organization_members"
+ WHERE (("organization_members"."user_id" = "auth"."uid"()) AND ("organization_members"."status" = 'active'::"text")))));
-CREATE POLICY "Allow active members to view their non-deleted organizations" ON "public"."organizations" FOR SELECT TO "authenticated" USING ("public"."is_org_member"("id", "auth"."uid"(), 'active'::"text"));
+CREATE POLICY "Allow admins or self to update memberships" ON "public"."organization_members" FOR UPDATE TO "authenticated" USING (("public"."is_org_admin"("organization_id") OR (( SELECT "auth"."uid"() AS "uid") = "user_id"))) WITH CHECK (("public"."is_org_admin"("organization_id") OR (( SELECT "auth"."uid"() AS "uid") = "user_id")));
-CREATE POLICY "Allow admins or self to update memberships" ON "public"."organization_members" FOR UPDATE TO "authenticated" USING (("public"."is_org_member"("organization_id", "auth"."uid"(), 'active'::"text", 'admin'::"text") OR (("user_id" = "auth"."uid"()) AND (EXISTS ( SELECT 1
- FROM "public"."organizations"
- WHERE (("organizations"."id" = "organization_members"."organization_id") AND ("organizations"."deleted_at" IS NULL))))))) WITH CHECK (("public"."is_org_member"("organization_id", "auth"."uid"(), 'active'::"text", 'admin'::"text") OR (("user_id" = "auth"."uid"()) AND (EXISTS ( SELECT 1
- FROM "public"."organizations"
- WHERE (("organizations"."id" = "organization_members"."organization_id") AND ("organizations"."deleted_at" IS NULL)))))));
+CREATE POLICY "Allow admins to insert new members" ON "public"."organization_members" FOR INSERT TO "authenticated" WITH CHECK ("public"."is_org_admin"("organization_id"));
-CREATE POLICY "Allow admins to insert new members" ON "public"."organization_members" FOR INSERT TO "authenticated" WITH CHECK ("public"."is_org_member"("organization_id", "auth"."uid"(), 'active'::"text", 'admin'::"text"));
-
-
-CREATE POLICY "Allow admins to update their non-deleted organizations" ON "public"."organizations" FOR UPDATE TO "authenticated" USING ("public"."is_org_member"("id", "auth"."uid"(), 'active'::"text", 'admin'::"text")) WITH CHECK ("public"."is_org_member"("id", "auth"."uid"(), 'active'::"text", 'admin'::"text"));
+CREATE POLICY "Allow admins to update their non-deleted organizations" ON "public"."organizations" FOR UPDATE TO "authenticated" USING ((("deleted_at" IS NULL) AND "public"."is_org_admin"("id"))) WITH CHECK (("deleted_at" IS NULL));
@@ -3477,15 +3487,7 @@ CREATE POLICY "Allow authenticated read access" ON "public"."subscription_plans"
-CREATE POLICY "Allow authenticated read access to active providers" ON "public"."ai_providers" FOR SELECT TO "authenticated" USING (("is_active" = true));
-
-
-
-CREATE POLICY "Allow authenticated users to create organizations" ON "public"."organizations" FOR INSERT TO "authenticated" WITH CHECK (("auth"."role"() = 'authenticated'::"text"));
-
-
-
-CREATE POLICY "Allow authenticated users to read active system_prompts" ON "public"."system_prompts" FOR SELECT TO "authenticated" USING (("is_active" = true));
+CREATE POLICY "Allow authenticated users to create organizations" ON "public"."organizations" FOR INSERT TO "authenticated" WITH CHECK ((( SELECT "auth"."role"() AS "role") = 'authenticated'::"text"));
@@ -3509,76 +3511,45 @@ CREATE POLICY "Allow authenticated users to read stages" ON "public"."dialectic_
-CREATE POLICY "Allow authenticated users to select their own payment transacti" ON "public"."payment_transactions" FOR SELECT TO "authenticated" USING (("user_id" = "auth"."uid"()));
+CREATE POLICY "Allow authenticated users to select their own payment transacti" ON "public"."payment_transactions" FOR SELECT TO "authenticated" USING (((( SELECT "auth"."uid"() AS "uid") = "user_id") OR (("organization_id" IS NOT NULL) AND "public"."is_org_admin"("organization_id"))));
CREATE POLICY "Allow authenticated users to select their own wallet transactio" ON "public"."token_wallet_transactions" FOR SELECT TO "authenticated" USING ((EXISTS ( SELECT 1
- FROM "public"."token_wallets" "tw"
- WHERE (("tw"."wallet_id" = "token_wallet_transactions"."wallet_id") AND ("tw"."user_id" = "auth"."uid"())))));
-
+ FROM "public"."token_wallets" "w"
+ WHERE (("w"."wallet_id" = "token_wallet_transactions"."wallet_id") AND (("w"."user_id" = ( SELECT "auth"."uid"() AS "uid")) OR (("w"."organization_id" IS NOT NULL) AND "public"."is_org_admin"("w"."organization_id")))))));
-CREATE POLICY "Allow individual insert access" ON "public"."user_profiles" FOR INSERT TO "authenticated" WITH CHECK (("auth"."uid"() = "id"));
+CREATE POLICY "Allow individual insert access" ON "public"."user_subscriptions" FOR INSERT TO "authenticated" WITH CHECK ((( SELECT "auth"."uid"() AS "uid") = "user_id"));
-CREATE POLICY "Allow individual insert access" ON "public"."user_subscriptions" FOR INSERT TO "authenticated" WITH CHECK (("auth"."uid"() = "user_id"));
+CREATE POLICY "Allow individual read access" ON "public"."user_subscriptions" FOR SELECT TO "authenticated" USING ((( SELECT "auth"."uid"() AS "uid") = "user_id"));
-CREATE POLICY "Allow individual read access" ON "public"."user_profiles" FOR SELECT TO "authenticated" USING (("auth"."uid"() = "id"));
+CREATE POLICY "Allow individual update access" ON "public"."user_subscriptions" FOR UPDATE TO "authenticated" USING ((( SELECT "auth"."uid"() AS "uid") = "user_id")) WITH CHECK ((( SELECT "auth"."uid"() AS "uid") = "user_id"));
-CREATE POLICY "Allow individual read access" ON "public"."user_subscriptions" FOR SELECT TO "authenticated" USING (("auth"."uid"() = "user_id"));
+CREATE POLICY "Allow org admins and chat owners to delete chats" ON "public"."chats" FOR DELETE TO "authenticated" USING (((( SELECT "auth"."uid"() AS "uid") = "user_id") OR (("organization_id" IS NOT NULL) AND "public"."is_org_admin"("organization_id"))));
-CREATE POLICY "Allow individual update access" ON "public"."user_profiles" FOR UPDATE TO "authenticated" USING (("auth"."uid"() = "id")) WITH CHECK (("auth"."uid"() = "id"));
+CREATE POLICY "Allow org admins and chat owners to update chats" ON "public"."chats" FOR UPDATE TO "authenticated" USING (((( SELECT "auth"."uid"() AS "uid") = "user_id") OR (("organization_id" IS NOT NULL) AND "public"."is_org_admin"("organization_id"))));
-CREATE POLICY "Allow individual update access" ON "public"."user_subscriptions" FOR UPDATE TO "authenticated" USING (("auth"."uid"() = "user_id")) WITH CHECK (("auth"."uid"() = "user_id"));
+CREATE POLICY "Allow org members/admins and chat owners to select chats" ON "public"."chats" FOR SELECT TO "authenticated" USING (((( SELECT "auth"."uid"() AS "uid") = "user_id") OR (("organization_id" IS NOT NULL) AND "public"."is_org_member"("organization_id", ( SELECT "auth"."uid"() AS "uid"), 'active'::"text"))));
-CREATE POLICY "Allow org admins and chat owners to delete chats" ON "public"."chats" FOR DELETE TO "authenticated" USING (((("organization_id" IS NULL) AND ("auth"."uid"() = "user_id")) OR (("organization_id" IS NOT NULL) AND "public"."is_org_admin"("organization_id"))));
+CREATE POLICY "Allow read access to active providers" ON "public"."ai_providers" FOR SELECT TO "authenticated" USING (("is_active" = true));
-CREATE POLICY "Allow org admins and chat owners to update chats" ON "public"."chats" FOR UPDATE TO "authenticated" USING (((("organization_id" IS NULL) AND ("auth"."uid"() = "user_id")) OR (("organization_id" IS NOT NULL) AND "public"."is_org_admin"("organization_id")))) WITH CHECK (((("organization_id" IS NULL) AND ("user_id" = "auth"."uid"())) OR (("organization_id" IS NOT NULL) AND "public"."is_org_admin"("organization_id"))));
-
-
-CREATE POLICY "Allow org members/admins and chat owners to select chats" ON "public"."chats" FOR SELECT TO "authenticated" USING (((("organization_id" IS NULL) AND ("auth"."uid"() = "user_id")) OR (("organization_id" IS NOT NULL) AND "public"."is_org_member"("organization_id", "auth"."uid"(), 'active'::"text"))));
-
-
-
-CREATE POLICY "Allow organization admins to select their organization wallets" ON "public"."token_wallets" FOR SELECT TO "authenticated" USING ((("user_id" IS NULL) AND ("organization_id" IS NOT NULL) AND "public"."is_admin_of_org_for_wallet"("organization_id")));
-
-
-
-CREATE POLICY "Allow organization admins to select their organization's paymen" ON "public"."payment_transactions" FOR SELECT TO "authenticated" USING (((EXISTS ( SELECT 1
- FROM "public"."token_wallets" "tw"
- WHERE (("tw"."wallet_id" = "payment_transactions"."target_wallet_id") AND ("tw"."user_id" IS NULL) AND ("tw"."organization_id" IS NOT NULL) AND "public"."is_admin_of_org_for_wallet"("tw"."organization_id")))) OR (("organization_id" IS NOT NULL) AND "public"."is_admin_of_org_for_wallet"("organization_id"))));
-