LifeMate — Personal, Family & Learning Companion
fix: separate family admin capability from member role
davoodmehraban89 committed
Oct 5, 2026 at 07:38 UTC
34808c274febf2de09bbeb4f9ceb8f990d039e80
1 file changed
+29
-5
backend/migrations/0001_identity_family.sql
+29
-5
@@ -2,7 +2,7 @@ begin;
2
3
create extension if not exists pgcrypto;
4
5
-create type member_role as enum ('owner_admin','parent_guardian','teen_minor','adult_member');
5
+create type member_role as enum ('parent_guardian','teen_minor','adult_member');
6
create type invitation_status as enum ('pending','accepted','revoked','expired');
7
create type visibility_scope as enum ('private','selected_members','parent_guardian','family','safety_controlled');
8
@@ -35,6 +35,7 @@ create table family_membership (
35
family_id uuid not null references family_workspace(id) on delete cascade,
36
user_id uuid not null references app_user(id) on delete cascade,
37
role member_role not null,
38
+ is_admin boolean not null default false,
39
joined_at timestamptz not null default now(),
40
ended_at timestamptz,
41
primary key (family_id, user_id)
@@ -79,21 +80,44 @@ create table access_audit (
80
);
81
82
create index family_membership_user_idx on family_membership(user_id) where ended_at is null;
83
+create index family_membership_admin_idx on family_membership(family_id, is_admin) where ended_at is null and is_admin;
84
create index family_invitation_pending_idx on family_invitation(family_id, invited_email_normalized) where status = 'pending';
85
create index access_audit_family_time_idx on access_audit(family_id, occurred_at desc);
86
87
create function is_active_family_member(p_family uuid, p_user uuid) returns boolean
88
language sql stable as $$
87
- select exists(select 1 from family_membership m where m.family_id=p_family and m.user_id=p_user and m.ended_at is null)
89
+ select exists(
90
+ select 1 from family_membership m
91
+ where m.family_id=p_family and m.user_id=p_user and m.ended_at is null
92
+ )
93
+$$;
94
+
95
+create function is_family_admin(p_family uuid, p_user uuid) returns boolean
96
+language sql stable as $$
97
+ select exists(
98
+ select 1 from family_membership m
99
+ where m.family_id=p_family and m.user_id=p_user and m.ended_at is null and m.is_admin
100
+ )
101
$$;
102
103
create function is_active_guardian(p_family uuid, p_guardian uuid, p_minor uuid) returns boolean
104
language sql stable as $$
105
select exists(
106
select 1 from guardian_relationship g
94
- join family_membership gm on gm.family_id=g.family_id and gm.user_id=g.guardian_user_id and gm.ended_at is null
95
- join family_membership mm on mm.family_id=g.family_id and mm.user_id=g.minor_user_id and mm.ended_at is null
96
- where g.family_id=p_family and g.guardian_user_id=p_guardian and g.minor_user_id=p_minor and g.active
107
+ join family_membership gm
108
+ on gm.family_id=g.family_id
109
+ and gm.user_id=g.guardian_user_id
110
+ and gm.ended_at is null
111
+ and gm.role='parent_guardian'
112
+ join family_membership mm
113
+ on mm.family_id=g.family_id
114
+ and mm.user_id=g.minor_user_id
115
+ and mm.ended_at is null
116
+ and mm.role='teen_minor'
117
+ where g.family_id=p_family
118
+ and g.guardian_user_id=p_guardian
119
+ and g.minor_user_id=p_minor
120
+ and g.active
121
)
122
$$;
123