-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
2424 lines (2107 loc) · 70.6 KB
/
Copy pathschema.sql
File metadata and controls
2424 lines (2107 loc) · 70.6 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
-- ─────────────────────────────────────────────────────────────────────────────
-- SentinelAI — database schema
--
-- How to run: open your Supabase project -> SQL Editor -> New query, paste this
-- whole file, and click "Run". Safe to re-run: policies/functions use drop-if-exists
-- guards before create.
--
-- What this does:
-- 1. Creates a `public.profiles` table, one row per authenticated user (with username).
-- 2. Turns on Row Level Security so each user can only see/edit their own row
-- (this is what makes the publishable/anon key safe to ship in the browser).
-- 3. Adds a trigger that creates a profile row only after email confirmation.
-- 4. Backfills profiles for confirmed users only.
-- ─────────────────────────────────────────────────────────────────────────────
-- 1. Profiles table -----------------------------------------------------------
create table if not exists public.profiles (
id uuid primary key references auth.users (id) on delete cascade,
email text,
username text,
full_name text,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
-- Username rules: unique (case-insensitive), max 20 chars, no whitespace.
alter table public.profiles add column if not exists username text;
alter table public.profiles drop constraint if exists profiles_username_format;
alter table public.profiles add constraint profiles_username_format
check (username is null or (char_length(username) <= 20 and username !~ '\s'));
create unique index if not exists profiles_username_lower_idx
on public.profiles (lower(username))
where username is not null;
-- 2. Row Level Security -------------------------------------------------------
alter table public.profiles enable row level security;
drop policy if exists "Profiles are viewable by their owner" on public.profiles;
create policy "Profiles are viewable by their owner"
on public.profiles for select
using (auth.uid() = id);
drop policy if exists "Users can insert their own profile" on public.profiles;
create policy "Users can insert their own profile"
on public.profiles for insert
with check (auth.uid() = id);
drop policy if exists "Users can update their own profile" on public.profiles;
create policy "Users can update their own profile"
on public.profiles for update
using (auth.uid() = id);
-- 3. Create profile only after email confirmation --------------------------------
-- Supabase always stores pending signups in auth.users; we only mirror confirmed
-- users into public.profiles so the app database stays clean until verify.
create or replace function public.sync_confirmed_user_profile()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
if new.email_confirmed_at is null then
return new;
end if;
insert into public.profiles (id, email, full_name, username)
values (
new.id,
new.email,
new.raw_user_meta_data ->> 'full_name',
new.raw_user_meta_data ->> 'username'
)
on conflict (id) do update set
email = excluded.email,
username = coalesce(excluded.username, public.profiles.username),
updated_at = now();
return new;
end;
$$;
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
after insert on auth.users
for each row
when (new.email_confirmed_at is not null)
execute function public.sync_confirmed_user_profile();
drop trigger if exists on_auth_user_confirmed on auth.users;
create trigger on_auth_user_confirmed
after update of email_confirmed_at on auth.users
for each row
when (old.email_confirmed_at is null and new.email_confirmed_at is not null)
execute function public.sync_confirmed_user_profile();
-- Client-side fallback after /auth/callback (if trigger timing ever races).
create or replace function public.ensure_profile()
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
uid uuid := auth.uid();
u record;
begin
if uid is null then
raise exception 'Not authenticated';
end if;
select id, email, email_confirmed_at, raw_user_meta_data
into u
from auth.users
where id = uid;
if u.email_confirmed_at is null then
return;
end if;
insert into public.profiles (id, email, full_name, username)
values (
u.id,
u.email,
u.raw_user_meta_data ->> 'full_name',
u.raw_user_meta_data ->> 'username'
)
on conflict (id) do update set
email = excluded.email,
username = coalesce(excluded.username, public.profiles.username),
updated_at = now();
end;
$$;
-- 4. Backfill confirmed users; drop profiles for unconfirmed -------------------
delete from public.profiles p
using auth.users u
where p.id = u.id
and u.email_confirmed_at is null;
insert into public.profiles (id, email, username)
select id, email, raw_user_meta_data ->> 'username'
from auth.users
where email_confirmed_at is not null
on conflict (id) do nothing;
-- ─────────────────────────────────────────────────────────────────────────────
-- Auth helper RPCs (login by username, signup checks, settings updates)
-- ─────────────────────────────────────────────────────────────────────────────
create or replace function public.resolve_login_email(identifier text)
returns text
language plpgsql
security definer
set search_path = ''
as $$
declare
resolved_email text;
begin
if identifier is null or trim(identifier) = '' then
return null;
end if;
if position('@' in trim(identifier)) > 0 then
return lower(trim(identifier));
end if;
select u.email
into resolved_email
from public.profiles p
join auth.users u on u.id = p.id
where lower(p.username) = lower(trim(identifier));
return resolved_email;
end;
$$;
create or replace function public.is_username_available(desired_username text)
returns boolean
language sql
security definer
set search_path = ''
as $$
select not exists (
select 1
from public.profiles
where lower(username) = lower(trim(desired_username))
)
and not exists (
select 1
from auth.users u
where lower(u.raw_user_meta_data ->> 'username') = lower(trim(desired_username))
and u.email_confirmed_at is null
);
$$;
create or replace function public.update_username(old_username text, new_username text)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
uid uuid := auth.uid();
begin
if uid is null then
raise exception 'Not authenticated';
end if;
if new_username is null or trim(new_username) = '' then
raise exception 'Username is required';
end if;
if char_length(trim(new_username)) > 20 or trim(new_username) ~ '\s' then
raise exception 'Invalid username format';
end if;
if not exists (
select 1
from public.profiles
where id = uid and lower(username) = lower(trim(old_username))
) then
raise exception 'Current username does not match';
end if;
if exists (
select 1
from public.profiles
where lower(username) = lower(trim(new_username)) and id <> uid
) then
raise exception 'Username is already taken';
end if;
update public.profiles
set username = trim(new_username), updated_at = now()
where id = uid;
end;
$$;
create or replace function public.delete_own_account()
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
if auth.uid() is null then
raise exception 'Not authenticated';
end if;
delete from auth.users where id = auth.uid();
end;
$$;
create or replace function public.auth_user_email(p_user_id uuid default auth.uid())
returns text
language sql
security definer
set search_path = ''
stable
as $$
select lower(trim(coalesce(
(select u.email from auth.users u where u.id = p_user_id),
(select pr.email from public.profiles pr where pr.id = p_user_id),
''
)));
$$;
create or replace function public.invitation_is_for_me(p_email text)
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select p_email is not null
and auth.uid() is not null
and (
lower(p_email) = coalesce(
(select lower(trim(u.email)) from auth.users u where u.id = auth.uid()),
''
)
or lower(p_email) = coalesce(
(select lower(trim(pr.email)) from public.profiles pr where pr.id = auth.uid()),
''
)
);
$$;
grant execute on function public.resolve_login_email(text) to anon, authenticated;
grant execute on function public.is_username_available(text) to anon, authenticated;
grant execute on function public.ensure_profile() to authenticated;
grant execute on function public.update_username(text, text) to authenticated;
grant execute on function public.delete_own_account() to authenticated;
-- ─────────────────────────────────────────────────────────────────────────────
-- 5. Projects table -----------------------------------------------------------
-- One row per project a user wires up for Sentinel to watch. Each project holds
-- its GitHub repo, Slack webhook, and runbooks. Row Level Security ensures a
-- user can only see and manage their own projects.
-- ─────────────────────────────────────────────────────────────────────────────
create table if not exists public.projects (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users (id) on delete cascade,
name text not null,
github_repo text,
slack_webhook text,
runbooks text,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists projects_user_id_idx on public.projects (user_id);
alter table public.projects enable row level security;
drop policy if exists "Users can view their own projects" on public.projects;
create policy "Users can view their own projects"
on public.projects for select
using (auth.uid() = user_id);
drop policy if exists "Users can insert their own projects" on public.projects;
create policy "Users can insert their own projects"
on public.projects for insert
with check (auth.uid() = user_id);
drop policy if exists "Users can update their own projects" on public.projects;
create policy "Users can update their own projects"
on public.projects for update
using (auth.uid() = user_id)
with check (auth.uid() = user_id);
drop policy if exists "Users can delete their own projects" on public.projects;
create policy "Users can delete their own projects"
on public.projects for delete
using (auth.uid() = user_id);
-- ─────────────────────────────────────────────────────────────────────────────
-- 6. Runbooks file storage ----------------------------------------------------
-- Runbook .md / .pdf files are uploaded to a private Storage bucket. Files are
-- stored under a per-user folder ("<user_id>/<uuid>/<filename>"), and the
-- projects.runbooks column stores the newline-separated list of those paths.
-- The RLS policies below scope every file to its owner: the first path segment
-- must equal the requesting user's id.
-- ─────────────────────────────────────────────────────────────────────────────
insert into storage.buckets (id, name, public)
values ('runbooks', 'runbooks', false)
on conflict (id) do nothing;
drop policy if exists "Users can upload their own runbooks" on storage.objects;
create policy "Users can upload their own runbooks"
on storage.objects for insert
with check (bucket_id = 'runbooks' and (storage.foldername(name))[1] = auth.uid()::text);
drop policy if exists "Users can read their own runbooks" on storage.objects;
create policy "Users can read their own runbooks"
on storage.objects for select
using (bucket_id = 'runbooks' and (storage.foldername(name))[1] = auth.uid()::text);
drop policy if exists "Users can update their own runbooks" on storage.objects;
create policy "Users can update their own runbooks"
on storage.objects for update
using (bucket_id = 'runbooks' and (storage.foldername(name))[1] = auth.uid()::text);
drop policy if exists "Users can delete their own runbooks" on storage.objects;
create policy "Users can delete their own runbooks"
on storage.objects for delete
using (bucket_id = 'runbooks' and (storage.foldername(name))[1] = auth.uid()::text);
-- ─────────────────────────────────────────────────────────────────────────────
-- 7. Project teams, incidents, and fix approvals
-- ─────────────────────────────────────────────────────────────────────────────
create table if not exists public.project_members (
id uuid primary key default gen_random_uuid(),
project_id uuid not null references public.projects (id) on delete cascade,
user_id uuid not null references auth.users (id) on delete cascade,
role text not null check (role in ('admin', 'member')),
created_at timestamptz not null default now(),
unique (project_id, user_id)
);
create index if not exists project_members_user_id_idx on public.project_members (user_id);
create table if not exists public.project_invitations (
id uuid primary key default gen_random_uuid(),
project_id uuid not null references public.projects (id) on delete cascade,
email text not null,
role text not null check (role in ('admin', 'member')),
invited_by uuid not null references auth.users (id) on delete cascade,
status text not null default 'pending' check (status in ('pending', 'accepted', 'declined', 'revoked')),
created_at timestamptz not null default now()
);
create unique index if not exists project_invitations_pending_email_idx
on public.project_invitations (project_id, lower(email))
where status = 'pending';
create table if not exists public.incidents (
id uuid primary key default gen_random_uuid(),
project_id uuid not null references public.projects (id) on delete cascade,
incident_number int not null,
title text not null,
status text not null default 'active' check (status in ('active', 'resolved')),
alert_description text,
analysis jsonb,
slack_posted boolean not null default false,
assigned_to uuid references auth.users (id) on delete set null,
created_by uuid references auth.users (id) on delete set null,
created_at timestamptz not null default now(),
resolved_at timestamptz,
unique (project_id, incident_number)
);
create index if not exists incidents_project_id_idx on public.incidents (project_id);
create table if not exists public.incident_fixes (
id uuid primary key default gen_random_uuid(),
incident_id uuid not null references public.incidents (id) on delete cascade,
submitted_by uuid not null references auth.users (id) on delete cascade,
fix_description text not null,
status text not null default 'pending' check (status in ('pending', 'approved', 'declined')),
reviewed_by uuid references auth.users (id) on delete set null,
reviewed_at timestamptz,
review_note text,
created_at timestamptz not null default now()
);
create unique index if not exists incident_fixes_one_pending_per_incident_idx
on public.incident_fixes (incident_id)
where status = 'pending';
-- Backfill project owners as admins in project_members.
insert into public.project_members (project_id, user_id, role)
select p.id, p.user_id, 'admin'
from public.projects p
where not exists (
select 1 from public.project_members pm
where pm.project_id = p.id and pm.user_id = p.user_id
);
create or replace function public.can_access_project(p_project_id uuid, p_user_id uuid default null)
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select exists (
select 1
from public.projects p
where p.id = p_project_id
and (
p.user_id = coalesce(p_user_id, (select auth.uid()))
or exists (
select 1
from public.project_members pm
where pm.project_id = p.id
and pm.user_id = coalesce(p_user_id, (select auth.uid()))
)
)
);
$$;
create or replace function public.is_project_admin(p_project_id uuid, p_user_id uuid default null)
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select exists (
select 1
from public.projects p
where p.id = p_project_id and p.user_id = coalesce(p_user_id, (select auth.uid()))
)
or exists (
select 1
from public.project_members pm
where pm.project_id = p_project_id
and pm.user_id = coalesce(p_user_id, (select auth.uid()))
and pm.role = 'admin'
);
$$;
create or replace function public.get_my_project_role(p_project_id uuid)
returns text
language sql
security definer
set search_path = ''
stable
as $$
select case
when exists (
select 1 from public.projects p
where p.id = p_project_id and p.user_id = (select auth.uid())
) then 'owner'
else (
select pm.role
from public.project_members pm
where pm.project_id = p_project_id and pm.user_id = (select auth.uid())
)
end;
$$;
create or replace function public.add_project_owner_member()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
insert into public.project_members (project_id, user_id, role)
values (new.id, new.user_id, 'admin')
on conflict (project_id, user_id) do nothing;
return new;
end;
$$;
drop trigger if exists on_project_created_add_owner on public.projects;
create trigger on_project_created_add_owner
after insert on public.projects
for each row
execute function public.add_project_owner_member();
create or replace function public.set_incident_number()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
next_num int;
begin
select coalesce(max(i.incident_number), 100) + 1
into next_num
from public.incidents i
where i.project_id = new.project_id;
new.incident_number := next_num;
return new;
end;
$$;
drop trigger if exists on_incident_set_number on public.incidents;
create trigger on_incident_set_number
before insert on public.incidents
for each row
execute function public.set_incident_number();
-- Replace single-user project policies with team-aware access.
drop policy if exists "Users can view their own projects" on public.projects;
drop policy if exists "Users can update their own projects" on public.projects;
drop policy if exists "Users can delete their own projects" on public.projects;
drop policy if exists "Project members can view projects" on public.projects;
drop policy if exists "Project admins can update projects" on public.projects;
drop policy if exists "Project admins can delete projects" on public.projects;
drop policy if exists "Project owners can update projects" on public.projects;
drop policy if exists "Project owners can delete projects" on public.projects;
create policy "Project members can view projects"
on public.projects for select
using (
(select auth.uid()) = user_id
or exists (
select 1
from public.project_members pm
where pm.project_id = id
and pm.user_id = (select auth.uid())
)
);
create policy "Project admins can update projects"
on public.projects for update
using (public.is_project_admin(id))
with check (public.is_project_admin(id));
create policy "Project admins can delete projects"
on public.projects for delete
using (public.is_project_admin(id));
-- Teammates can read each other's profiles on shared projects.
drop policy if exists "Profiles are viewable by their owner" on public.profiles;
drop policy if exists "Profiles are viewable by owner or teammates" on public.profiles;
create policy "Profiles are viewable by owner or teammates"
on public.profiles for select
using (
auth.uid() = id
or exists (
select 1
from public.project_members pm_self
join public.project_members pm_other on pm_self.project_id = pm_other.project_id
where pm_self.user_id = auth.uid()
and pm_other.user_id = profiles.id
)
or exists (
select 1
from public.projects p
join public.project_members pm on pm.project_id = p.id
where p.user_id = profiles.id and pm.user_id = auth.uid()
)
or exists (
select 1
from public.projects p
join public.project_members pm on pm.project_id = p.id
where p.user_id = auth.uid() and pm.user_id = profiles.id
)
);
alter table public.project_members enable row level security;
alter table public.project_invitations enable row level security;
alter table public.incidents enable row level security;
alter table public.incident_fixes enable row level security;
drop policy if exists "Members can view project team" on public.project_members;
drop policy if exists "Users can view own project memberships" on public.project_members;
create policy "Users can view own project memberships"
on public.project_members for select
using (user_id = (select auth.uid()));
create policy "Members can view project team"
on public.project_members for select
using (public.can_access_project(project_id));
drop policy if exists "Admins can manage project team" on public.project_members;
create policy "Admins can manage project team"
on public.project_members for delete
using (public.is_project_admin(project_id));
drop policy if exists "Members can view project invitations" on public.project_invitations;
create policy "Members can view project invitations"
on public.project_invitations for select
using (
public.is_project_admin(project_id)
or (status = 'pending' and public.invitation_is_for_me(email))
);
drop policy if exists "Members can view incidents" on public.incidents;
create policy "Members can view incidents"
on public.incidents for select
using (public.can_access_project(project_id));
drop policy if exists "Members can create incidents" on public.incidents;
create policy "Members can create incidents"
on public.incidents for insert
with check (public.can_access_project(project_id));
drop policy if exists "Members can view incident fixes" on public.incident_fixes;
create policy "Members can view incident fixes"
on public.incident_fixes for select
using (
exists (
select 1
from public.incidents i
where i.id = incident_id and public.can_access_project(i.project_id)
)
);
-- Shared runbook reads for project teammates.
drop policy if exists "Users can read their own runbooks" on storage.objects;
drop policy if exists "Users can read own or shared runbooks" on storage.objects;
create policy "Users can read own or shared runbooks"
on storage.objects for select
using (
bucket_id = 'runbooks'
and (
(storage.foldername(name))[1] = auth.uid()::text
or exists (
select 1
from public.projects p
where public.can_access_project(p.id)
and coalesce(p.runbooks, '') like '%' || name || '%'
)
)
);
create or replace function public.invite_project_member(
p_project_id uuid,
p_email text,
p_role text default 'member'
)
returns uuid
language plpgsql
security definer
set search_path = ''
as $$
declare
invite_id uuid;
raw_identifier text := trim(p_email);
normalized_email text;
begin
if auth.uid() is null then
raise exception 'Not authenticated';
end if;
if not public.is_project_admin(p_project_id) then
raise exception 'Only project admins can invite teammates';
end if;
if raw_identifier = '' then
raise exception 'Enter a username or email address';
end if;
if position('@' in raw_identifier) > 0 then
select lower(trim(coalesce(u.email, pr.email)))
into normalized_email
from auth.users u
inner join public.profiles pr on pr.id = u.id
where lower(coalesce(u.email, pr.email)) = lower(raw_identifier)
limit 1;
else
select lower(trim(coalesce(u.email, pr.email)))
into normalized_email
from auth.users u
inner join public.profiles pr on pr.id = u.id
where lower(pr.username) = lower(raw_identifier)
limit 1;
end if;
if normalized_email is null or normalized_email = '' then
raise exception 'Invalid user/email. Please ask them to register to SentinelAI';
end if;
if p_role not in ('admin', 'member') then
raise exception 'Invalid role';
end if;
if p_role = 'admin' and not public.is_project_owner(p_project_id) then
raise exception 'Only the project owner can invite admins';
end if;
if exists (
select 1
from public.profiles pr
join public.projects p on p.user_id = pr.id and p.id = p_project_id
where lower(pr.email) = normalized_email
) then
raise exception 'That user is already on this project';
end if;
if exists (
select 1
from public.profiles pr
join public.project_members pm on pm.user_id = pr.id
where pm.project_id = p_project_id and lower(pr.email) = normalized_email
) then
raise exception 'That user is already on this project';
end if;
if exists (
select 1
from public.project_invitations pi
where pi.project_id = p_project_id
and lower(pi.email) = normalized_email
and pi.status = 'pending'
) then
select pi.id into invite_id
from public.project_invitations pi
where pi.project_id = p_project_id
and lower(pi.email) = normalized_email
and pi.status = 'pending';
return invite_id;
end if;
insert into public.project_invitations (project_id, email, role, invited_by)
values (p_project_id, normalized_email, p_role, auth.uid())
returning id into invite_id;
return invite_id;
end;
$$;
create or replace function public.accept_project_invitation(p_invitation_id uuid)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
inv record;
uid uuid := auth.uid();
begin
if uid is null then
raise exception 'Not authenticated';
end if;
select *
into inv
from public.project_invitations
where id = p_invitation_id and status = 'pending';
if inv.id is null then
raise exception 'Invitation not found';
end if;
if not public.invitation_is_for_me(inv.email) then
raise exception 'This invitation was sent to a different email address';
end if;
insert into public.project_members (project_id, user_id, role)
values (inv.project_id, uid, inv.role)
on conflict (project_id, user_id) do update set role = excluded.role;
update public.project_invitations
set status = 'accepted'
where id = inv.id;
end;
$$;
create or replace function public.decline_project_invitation(p_invitation_id uuid)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
inv record;
begin
if auth.uid() is null then
raise exception 'Not authenticated';
end if;
select *
into inv
from public.project_invitations
where id = p_invitation_id and status = 'pending';
if inv.id is null then
raise exception 'Invitation not found';
end if;
if not public.invitation_is_for_me(inv.email) then
raise exception 'This invitation was sent to a different email address';
end if;
update public.project_invitations
set status = 'declined'
where id = inv.id;
end;
$$;
create or replace function public.revoke_project_invitation(p_invitation_id uuid)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
inv record;
begin
if auth.uid() is null then
raise exception 'Not authenticated';
end if;
select *
into inv
from public.project_invitations
where id = p_invitation_id and status = 'pending';
if inv.id is null then
raise exception 'Invitation not found';
end if;
if not public.is_project_admin(inv.project_id) then
raise exception 'Only project admins can cancel invitations';
end if;
update public.project_invitations
set status = 'revoked'
where id = inv.id;
end;
$$;
create or replace function public.remove_project_member(p_member_id uuid)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
member_row record;
admin_count int;
begin
if auth.uid() is null then
raise exception 'Not authenticated';
end if;
select pm.*, p.user_id as owner_id
into member_row
from public.project_members pm
join public.projects p on p.id = pm.project_id
where pm.id = p_member_id;
if member_row.id is null then
raise exception 'Member not found';
end if;
if not public.is_project_admin(member_row.project_id) then
raise exception 'Only project admins can remove teammates';
end if;
if member_row.user_id = member_row.owner_id then
raise exception 'The project owner cannot be removed';
end if;
select count(*) into admin_count
from (
select 1
from public.projects p
where p.id = member_row.project_id and p.user_id = member_row.user_id
union all
select 1
from public.project_members pm
where pm.project_id = member_row.project_id and pm.role = 'admin'
) admins;
if member_row.role = 'admin' and admin_count <= 1 then
raise exception 'Cannot remove the last admin from a project';
end if;
delete from public.project_members where id = p_member_id;
end;
$$;
create or replace function public.submit_incident_fix(
p_incident_id uuid,
p_fix_description text
)
returns uuid
language plpgsql
security definer
set search_path = ''
as $$
declare
inc record;
fix_id uuid;
trimmed text := trim(p_fix_description);
begin
if auth.uid() is null then
raise exception 'Not authenticated';
end if;
if trimmed = '' then
raise exception 'Describe what you fixed before submitting';
end if;
select * into inc
from public.incidents
where id = p_incident_id;
if inc.id is null then
raise exception 'Incident not found';
end if;
if not public.can_access_project(inc.project_id) then
raise exception 'Not allowed';
end if;
if inc.status <> 'active' then
raise exception 'This incident is already resolved';
end if;
if exists (
select 1 from public.incident_fixes f
where f.incident_id = inc.id and f.status = 'pending'
) then
raise exception 'A fix is already waiting for admin review';
end if;
if public.is_project_admin(inc.project_id) then
insert into public.incident_fixes (
incident_id, submitted_by, fix_description, status, reviewed_by, reviewed_at
)
values (inc.id, auth.uid(), trimmed, 'approved', auth.uid(), now())
returning id into fix_id;
update public.incidents
set status = 'resolved', resolved_at = now()
where id = inc.id;
return fix_id;
end if;
insert into public.incident_fixes (incident_id, submitted_by, fix_description, status)
values (inc.id, auth.uid(), trimmed, 'pending')
returning id into fix_id;
return fix_id;
end;
$$;
create or replace function public.review_incident_fix(
p_fix_id uuid,
p_approve boolean,
p_review_note text default null
)
returns void
language plpgsql