LifeMate — Personal, Family & Learning Companion

docs/offline-data-rescue-options
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
sql 125 lines 4.74 KB
Raw
1 begin;
2
3 create extension if not exists pgcrypto;
4
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
9 create table app_user (
10 id uuid primary key default gen_random_uuid(),
11 identity_subject text not null unique,
12 email_normalized text not null unique check (email_normalized = lower(email_normalized)),
13 email_verified_at timestamptz,
14 created_at timestamptz not null default now(),
15 disabled_at timestamptz
16 );
17
18 create table profile (
19 user_id uuid primary key references app_user(id) on delete cascade,
20 display_name text not null check (char_length(display_name) between 1 and 100),
21 birth_date date,
22 theme_preference text check (theme_preference in ('girl_pink','boy_blue','adult_blue','custom')),
23 updated_at timestamptz not null default now()
24 );
25
26 create table family_workspace (
27 id uuid primary key default gen_random_uuid(),
28 name text not null check (char_length(name) between 1 and 120),
29 created_by uuid not null references app_user(id),
30 created_at timestamptz not null default now(),
31 archived_at timestamptz
32 );
33
34 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)
42 );
43
44 create table guardian_relationship (
45 family_id uuid not null,
46 guardian_user_id uuid not null,
47 minor_user_id uuid not null,
48 active boolean not null default true,
49 created_at timestamptz not null default now(),
50 primary key (family_id, guardian_user_id, minor_user_id),
51 foreign key (family_id, guardian_user_id) references family_membership(family_id, user_id) on delete cascade,
52 foreign key (family_id, minor_user_id) references family_membership(family_id, user_id) on delete cascade,
53 check (guardian_user_id <> minor_user_id)
54 );
55
56 create table family_invitation (
57 id uuid primary key default gen_random_uuid(),
58 family_id uuid not null references family_workspace(id) on delete cascade,
59 invited_email_normalized text not null check (invited_email_normalized = lower(invited_email_normalized)),
60 intended_role member_role not null,
61 intended_theme text check (intended_theme in ('girl_pink','boy_blue','adult_blue','custom')),
62 token_digest text not null unique,
63 status invitation_status not null default 'pending',
64 invited_by uuid not null references app_user(id),
65 expires_at timestamptz not null,
66 accepted_by uuid references app_user(id),
67 created_at timestamptz not null default now(),
68 accepted_at timestamptz,
69 check (expires_at > created_at)
70 );
71
72 create table access_audit (
73 id bigint generated always as identity primary key,
74 actor_user_id uuid references app_user(id),
75 family_id uuid references family_workspace(id),
76 action text not null,
77 target_type text not null,
78 target_id text,
79 occurred_at timestamptz not null default now(),
80 metadata jsonb not null default '{}'::jsonb
81 );
82
83 create index family_membership_user_idx on family_membership(user_id) where ended_at is null;
84 create index family_membership_admin_idx on family_membership(family_id, is_admin) where ended_at is null and is_admin;
85 create index family_invitation_pending_idx on family_invitation(family_id, invited_email_normalized) where status = 'pending';
86 create index access_audit_family_time_idx on access_audit(family_id, occurred_at desc);
87
88 create function is_active_family_member(p_family uuid, p_user uuid) returns boolean
89 language sql stable as $$
90 select exists(
91 select 1 from family_membership m
92 where m.family_id=p_family and m.user_id=p_user and m.ended_at is null
93 )
94 $$;
95
96 create function is_family_admin(p_family uuid, p_user uuid) returns boolean
97 language sql stable as $$
98 select exists(
99 select 1 from family_membership m
100 where m.family_id=p_family and m.user_id=p_user and m.ended_at is null and m.is_admin
101 )
102 $$;
103
104 create function is_active_guardian(p_family uuid, p_guardian uuid, p_minor uuid) returns boolean
105 language sql stable as $$
106 select exists(
107 select 1 from guardian_relationship g
108 join family_membership gm
109 on gm.family_id=g.family_id
110 and gm.user_id=g.guardian_user_id
111 and gm.ended_at is null
112 and gm.role='parent_guardian'
113 join family_membership mm
114 on mm.family_id=g.family_id
115 and mm.user_id=g.minor_user_id
116 and mm.ended_at is null
117 and mm.role='teen_minor'
118 where g.family_id=p_family
119 and g.guardian_user_id=p_guardian
120 and g.minor_user_id=p_minor
121 and g.active
122 )
123 $$;
124
125 commit;