LifeMate — Personal, Family & Learning Companion

feat/aparat-learning-library
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 62 lines 4 KB
Raw
1 begin;
2 create table learning_assessment (
3 id uuid primary key default gen_random_uuid(),
4 unit_id uuid not null references learning_unit(id),
5 version integer not null default 1 check(version>0),
6 questions jsonb not null check(jsonb_typeof(questions)='array' and jsonb_array_length(questions) between 1 and 20),
7 content_hash text not null check(content_hash~'^[a-f0-9]{64}$'),
8 context_hash text not null check(context_hash~'^[a-f0-9]{64}$'),
9 publication_status text not null default 'draft' check(publication_status in('draft','published','withdrawn')),
10 review_record jsonb,
11 created_at timestamptz not null default now(),
12 check(publication_status<>'published' or coalesce(
13 jsonb_typeof(review_record)='object' and length(trim(review_record->>'reviewerName'))>0
14 and (review_record->>'accuracyApproved')='true' and (review_record->>'suitabilityApproved')='true'
15 and (review_record->>'editionAligned')='true' and (review_record->>'objectiveCoverageApproved')='true'
16 and (review_record->>'contentHash')=content_hash and (review_record->>'contextHash')=context_hash
17 and length(trim(review_record->>'reviewNotes'))>0,false))
18 );
19 create index learning_assessment_unit_idx on learning_assessment(unit_id,publication_status,version desc);
20 create function validate_learning_assessment_publication() returns trigger language plpgsql as $$
21 declare b record; reviewed timestamptz;
22 begin
23 if TG_OP='UPDATE' and new.questions is distinct from old.questions then
24 if new.publication_status='published' or new.version<=old.version or new.content_hash=old.content_hash then
25 raise exception 'changed assessment needs a new version/hash and draft review' using errcode='23514';
26 end if;
27 end if;
28 if new.publication_status='published' then
29 select t.book_code,t.edition_verification,s.school_year,u.unit_key into b
30 from learning_unit u join textbook_catalog t on t.id=u.textbook_id
31 join curriculum_subject s on s.id=t.curriculum_subject_id where u.id=new.unit_id and u.active;
32 if b.edition_verification is distinct from 'verified' or b.school_year='unknown'
33 or new.review_record->>'bookCode' is distinct from b.book_code
34 or new.review_record->>'schoolYear' is distinct from b.school_year
35 or new.review_record->>'unitKey' is distinct from b.unit_key then
36 raise exception 'assessment must match reviewed verified book edition and unit' using errcode='23514';
37 end if;
38 begin reviewed:=(new.review_record->>'reviewedAt')::timestamptz;
39 exception when others then raise exception 'invalid assessment review timestamp' using errcode='23514'; end;
40 if reviewed is null or reviewed>now() then raise exception 'invalid assessment review timestamp' using errcode='23514'; end if;
41 end if;
42 return new;
43 end $$;
44 create trigger learning_assessment_publication_guard before insert or update on learning_assessment
45 for each row execute function validate_learning_assessment_publication();
46 create table learning_assessment_attempt (
47 id uuid primary key default gen_random_uuid(),owner_user_id uuid not null references app_user(id) on delete cascade,
48 mutation_id uuid not null,assessment_id uuid not null references learning_assessment(id),
49 unit_id uuid not null references learning_unit(id),assessment_version integer not null,
50 request_hash text not null check(request_hash~'^[a-f0-9]{64}$'),answers jsonb not null,
51 confidence text not null check(confidence in('unsure','somewhat','confident')),
52 question_snapshot jsonb not null,acknowledgement jsonb not null,
53 created_at timestamptz not null default now(),unique(owner_user_id,mutation_id)
54 );
55 create index learning_attempt_owner_idx on learning_assessment_attempt(owner_user_id,unit_id,created_at desc);
56 create table learning_review_schedule (
57 owner_user_id uuid not null references app_user(id) on delete cascade,unit_id uuid not null references learning_unit(id),
58 last_attempt_id uuid not null references learning_assessment_attempt(id),next_review_at timestamptz not null,
59 schedule_policy text not null default 'pilot-1-or-3-days' check(schedule_policy='pilot-1-or-3-days'),
60 primary key(owner_user_id,unit_id)
61 );
62 commit;