@davoodmehraban89 / LifeMate / commits / b19160c
Fork 0

LifeMate — Personal, Family & Learning Companion

feat: add phase 3 planner school and family schema

davoodmehraban89 committed Oct 5, 2026 at 15:04 UTC b19160c85ca9466821f13a3a3571fcd8e26f44c5
1 file changed +214
backend/migrations/0003_planner_school_family.sql new
+214
@@ -0,0 +1,214 @@
1 +begin;
2 +
3 +create type life_context_kind as enum ('student','work','personal');
4 +create type plan_item_kind as enum ('task','event','routine','goal','assignment','exam','study_session');
5 +create type plan_item_status as enum ('planned','in_progress','completed','cancelled');
6 +create type plan_priority as enum ('low','normal','high','urgent');
7 +create type sharing_scope as enum ('private','selected_members','parent_guardian','family');
8 +create type reminder_status as enum ('scheduled','claimed','sent','cancelled','failed');
9 +
10 +create table life_context (
11 + id uuid primary key default gen_random_uuid(),
12 + user_id uuid not null references app_user(id) on delete cascade,
13 + kind life_context_kind not null,
14 + title text not null check (char_length(title) between 1 and 120),
15 + active boolean not null default true,
16 + created_at timestamptz not null default now()
17 +);
18 +create index life_context_user_idx on life_context(user_id) where active;
19 +
20 +create table academic_year (
21 + id uuid primary key default gen_random_uuid(),
22 + student_user_id uuid not null references app_user(id) on delete cascade,
23 + life_context_id uuid not null references life_context(id) on delete cascade,
24 + title text not null,
25 + starts_on date not null,
26 + ends_on date not null,
27 + active boolean not null default true,
28 + created_at timestamptz not null default now(),
29 + check (ends_on >= starts_on)
30 +);
31 +create index academic_year_student_idx on academic_year(student_user_id, starts_on desc);
32 +
33 +create table academic_term (
34 + id uuid primary key default gen_random_uuid(),
35 + academic_year_id uuid not null references academic_year(id) on delete cascade,
36 + title text not null,
37 + starts_on date not null,
38 + ends_on date not null,
39 + created_at timestamptz not null default now(),
40 + check (ends_on >= starts_on)
41 +);
42 +
43 +create table subject (
44 + id uuid primary key default gen_random_uuid(),
45 + student_user_id uuid not null references app_user(id) on delete cascade,
46 + academic_term_id uuid not null references academic_term(id) on delete cascade,
47 + name text not null check (char_length(name) between 1 and 120),
48 + teacher_name text,
49 + color_key text,
50 + created_at timestamptz not null default now()
51 +);
52 +create index subject_student_term_idx on subject(student_user_id, academic_term_id);
53 +
54 +create table class_session (
55 + id uuid primary key default gen_random_uuid(),
56 + subject_id uuid not null references subject(id) on delete cascade,
57 + weekday smallint not null check (weekday between 1 and 7),
58 + starts_at time not null,
59 + ends_at time not null,
60 + location text,
61 + recurrence_until date,
62 + created_at timestamptz not null default now(),
63 + check (ends_at > starts_at)
64 +);
65 +create index class_session_subject_weekday_idx on class_session(subject_id, weekday, starts_at);
66 +
67 +create table plan_item (
68 + id uuid primary key default gen_random_uuid(),
69 + owner_user_id uuid not null references app_user(id) on delete cascade,
70 + family_id uuid references family_workspace(id) on delete cascade,
71 + subject_id uuid references subject(id) on delete set null,
72 + kind plan_item_kind not null,
73 + title text not null check (char_length(title) between 1 and 240),
74 + notes text,
75 + status plan_item_status not null default 'planned',
76 + priority plan_priority not null default 'normal',
77 + visibility sharing_scope not null default 'private',
78 + starts_at timestamptz,
79 + due_at timestamptz,
80 + duration_minutes integer check (duration_minutes is null or duration_minutes between 1 and 1440),
81 + recurrence_rule text,
82 + parent_item_id uuid references plan_item(id) on delete set null,
83 + grade_points numeric(8,2),
84 + grade_out_of numeric(8,2),
85 + completed_at timestamptz,
86 + created_at timestamptz not null default now(),
87 + updated_at timestamptz not null default now(),
88 + check (due_at is null or starts_at is null or due_at >= starts_at),
89 + check ((grade_points is null and grade_out_of is null) or (grade_points is not null and grade_out_of is not null and grade_out_of > 0))
90 +);
91 +create index plan_item_owner_time_idx on plan_item(owner_user_id, coalesce(starts_at,due_at), status);
92 +create index plan_item_family_time_idx on plan_item(family_id, coalesce(starts_at,due_at)) where family_id is not null;
93 +create index plan_item_subject_idx on plan_item(subject_id, kind, due_at) where subject_id is not null;
94 +
95 +create table sharing_grant (
96 + resource_type text not null check (resource_type in ('plan_item')),
97 + resource_id uuid not null,
98 + grantee_user_id uuid not null references app_user(id) on delete cascade,
99 + granted_by uuid not null references app_user(id) on delete cascade,
100 + created_at timestamptz not null default now(),
101 + primary key (resource_type, resource_id, grantee_user_id)
102 +);
103 +create index sharing_grant_grantee_idx on sharing_grant(grantee_user_id, resource_type, resource_id);
104 +
105 +create table reminder (
106 + id uuid primary key default gen_random_uuid(),
107 + plan_item_id uuid not null references plan_item(id) on delete cascade,
108 + owner_user_id uuid not null references app_user(id) on delete cascade,
109 + minutes_before integer not null check (minutes_before between 0 and 10080),
110 + scheduled_for timestamptz not null,
111 + status reminder_status not null default 'scheduled',
112 + claimed_at timestamptz,
113 + delivered_at timestamptz,
114 + failure_reason text,
115 + created_at timestamptz not null default now(),
116 + unique(plan_item_id, owner_user_id, minutes_before)
117 +);
118 +create index reminder_due_idx on reminder(scheduled_for) where status='scheduled';
119 +
120 +create table notification_preference (
121 + user_id uuid primary key references app_user(id) on delete cascade,
122 + reminders_enabled boolean not null default true,
123 + quiet_start time,
124 + quiet_end time,
125 + timezone text not null default 'Asia/Tehran',
126 + updated_at timestamptz not null default now()
127 +);
128 +
129 +create table notification_outbox (
130 + id bigint generated always as identity primary key,
131 + reminder_id uuid not null unique references reminder(id) on delete cascade,
132 + user_id uuid not null references app_user(id) on delete cascade,
133 + payload jsonb not null,
134 + available_at timestamptz not null default now(),
135 + delivered_at timestamptz,
136 + attempts integer not null default 0,
137 + last_error text
138 +);
139 +create index notification_outbox_pending_idx on notification_outbox(available_at) where delivered_at is null;
140 +
141 +create table sync_mutation (
142 + id uuid primary key,
143 + user_id uuid not null references app_user(id) on delete cascade,
144 + entity_type text not null,
145 + entity_id uuid,
146 + operation text not null check (operation in ('create','update','complete','reschedule')),
147 + client_updated_at timestamptz not null,
148 + payload jsonb not null,
149 + accepted_at timestamptz not null default now(),
150 + unique(user_id,id)
151 +);
152 +
153 +create function can_view_student_academic(p_viewer uuid, p_student uuid) returns boolean
154 +language sql stable as $$
155 + select p_viewer=p_student or exists(
156 + select 1
157 + from guardian_relationship g
158 + join family_membership gm
159 + on gm.family_id=g.family_id and gm.user_id=g.guardian_user_id and gm.ended_at is null
160 + join family_membership sm
161 + on sm.family_id=g.family_id and sm.user_id=g.minor_user_id and sm.ended_at is null
162 + where g.guardian_user_id=p_viewer
163 + and g.minor_user_id=p_student
164 + and g.active
165 + )
166 +$$;
167 +
168 +create function can_view_plan_item(p_viewer uuid, p_item uuid) returns boolean
169 +language sql stable as $$
170 + select exists(
171 + select 1
172 + from plan_item p
173 + where p.id=p_item
174 + and (
175 + p.owner_user_id=p_viewer
176 + or exists(
177 + select 1 from sharing_grant sg
178 + where sg.resource_type='plan_item'
179 + and sg.resource_id=p.id
180 + and sg.grantee_user_id=p_viewer
181 + )
182 + or (
183 + p.visibility='family'
184 + and p.family_id is not null
185 + and is_active_family_member(p.family_id,p_viewer)
186 + )
187 + or (
188 + p.visibility='parent_guardian'
189 + and p.family_id is not null
190 + and is_active_guardian(p.family_id,p_viewer,p.owner_user_id)
191 + )
192 + )
193 + )
194 +$$;
195 +
196 +create function recompute_reminder_schedule(p_plan_item uuid) returns void
197 +language plpgsql as $$
198 +declare
199 + base_time timestamptz;
200 +begin
201 + select coalesce(starts_at,due_at) into base_time from plan_item where id=p_plan_item;
202 + if base_time is null then
203 + update reminder set status='cancelled' where plan_item_id=p_plan_item and status in ('scheduled','claimed');
204 + return;
205 + end if;
206 + update reminder
207 + set scheduled_for = base_time - make_interval(mins => minutes_before),
208 + status = case when status in ('sent','failed') then status else 'scheduled' end,
209 + claimed_at = null
210 + where plan_item_id=p_plan_item
211 + and status <> 'cancelled';
212 +end $$;
213 +
214 +commit;