LifeMate — Personal, Family & Learning Companion

feat/private-planner-series-api
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
js 60 lines 2.99 KB
Raw
1 import express from 'express';
2
3 const categories = new Set(['girl_minor', 'boy_minor', 'adult']);
4 const themes = new Set(['girl_pink', 'boy_blue', 'adult_blue', 'custom']);
5
6 export function validBirthDate(value) {
7 if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value) || value.startsWith('0000-')) return false;
8 const date = new Date(`${value}T00:00:00Z`);
9 return Number.isFinite(date.getTime()) && date.toISOString().slice(0, 10) === value;
10 }
11
12 export function createProfileRouter({ pool, auth }) {
13 const router = express.Router();
14 router.use(auth);
15
16 router.get('/', async (req, res) => {
17 const result = await pool.query(
18 `select u.id as user_id,p.display_name,p.birth_date,p.theme_preference,p.profile_category,
19 u.email_normalized,u.email_verified_at,u.phone_normalized,u.phone_verified_at
20 from profile p join app_user u on u.id=p.user_id where p.user_id=$1`,
21 [req.identity.sub],
22 );
23 if (!result.rowCount) return res.status(404).json({ error: 'profile_not_found' });
24 if (req.identity.managedChild) {
25 res.setHeader('Cache-Control', 'no-store');
26 const row = result.rows[0];
27 return res.json({ user_id: row.user_id, display_name: row.display_name,
28 profile_category: row.profile_category, theme_preference: row.theme_preference,
29 accessScope: 'child_education' });
30 }
31 res.json(result.rows[0]);
32 });
33
34 router.patch('/', async (req, res) => {
35 const body = req.body ?? {};
36 const name = body.displayName == null ? null : String(body.displayName).trim();
37 const theme = body.themePreference == null ? null : String(body.themePreference);
38 const category = body.profileCategory;
39 const birthDate = body.birthDate;
40 if (Object.hasOwn(body, 'birthDate') && (await pool.query('select 1 from managed_child where user_id=$1', [req.identity.sub])).rowCount) {
41 return res.status(403).json({ error: 'managed_child_age_locked' });
42 }
43 if (name != null && (!name || name.length > 100)) return res.status(400).json({ error: 'invalid_display_name' });
44 if (theme != null && !themes.has(theme)) return res.status(400).json({ error: 'invalid_theme' });
45 if (Object.hasOwn(body, 'profileCategory') && !categories.has(category)) return res.status(400).json({ error: 'invalid_profile_category' });
46 if (birthDate != null && !validBirthDate(birthDate)) return res.status(400).json({ error: 'invalid_birth_date' });
47 const result = await pool.query(
48 `update profile set display_name=coalesce($2,display_name),
49 birth_date=coalesce($3::date,birth_date),
50 theme_preference=coalesce($4,theme_preference),
51 profile_category=coalesce($5,profile_category),updated_at=now()
52 where user_id=$1 returning display_name,birth_date,theme_preference,profile_category`,
53 [req.identity.sub, name, birthDate ?? null, theme, category ?? null],
54 );
55 if (!result.rowCount) return res.status(404).json({ error: 'profile_not_found' });
56 res.json(result.rows[0]);
57 });
58
59 return router;
60 }