LifeMate — Personal, Family & Learning Companion
test/android-reminder-acceptance
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
| 1 | import crypto from 'node:crypto'; |
| 2 | import express from 'express'; |
| 3 | import jwt from 'jsonwebtoken'; |
| 4 | |
| 5 | export const SANDBOX_CONSENT_VERSION = 'sandbox-child-consent-v1-not-legal'; |
| 6 | export const SANDBOX_CONSENT_TEXT = 'SYNTHETIC TEST ONLY: I confirm these are invented test identities. This is not legal consent and authorizes no real child data, deployment or private-data access.'; |
| 7 | export const SANDBOX_CONSENT_DIGEST = crypto.createHash('sha256').update(SANDBOX_CONSENT_TEXT).digest('hex'); |
| 8 | const digest = value => crypto.createHash('sha256').update(value).digest('hex'); |
| 9 | const token = () => crypto.randomBytes(32).toString('base64url'); |
| 10 | const uuid = value => typeof value === 'string' && /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(value); |
| 11 | const exact = (body, fields) => body && typeof body === 'object' && !Array.isArray(body) && Object.keys(body).every(key => fields.includes(key)); |
| 12 | const bodyToken = body => exact(body, ['token']) && typeof body.token === 'string' && /^[A-Za-z0-9_-]{43}$/.test(body.token); |
| 13 | const failure = (status, error) => ({ status, body: { error } }); |
| 14 | const invalidInvitation = () => failure(400, 'invalid_or_expired_invitation'); |
| 15 | const send = (res, result) => res.status(result.status).json(result.body); |
| 16 | const date = value => typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value) && |
| 17 | Number(value.slice(0, 4)) >= 1900 && Number.isFinite(Date.parse(value + 'T00:00:00.000Z')) && |
| 18 | new Date(value + 'T00:00:00.000Z').toISOString().slice(0, 10) === value; |
| 19 | |
| 20 | async function sessionCurrent(client, identity, lock = false) { |
| 21 | if (lock) await client.query('select id from auth_session where id=$1 and user_id=$2 for share', [identity.sid, identity.sub]); |
| 22 | return (await client.query(`select 1 from auth_session s join app_user u on u.id=s.user_id |
| 23 | where s.id=$1 and s.user_id=$2 and s.revoked_at is null and s.expires_at>clock_timestamp() |
| 24 | and u.disabled_at is null and to_timestamp($3)>clock_timestamp()`, [identity.sid, identity.sub, identity.exp])).rowCount > 0; |
| 25 | } |
| 26 | async function transact(pool, identity, operation) { |
| 27 | const client = await pool.connect(); |
| 28 | try { |
| 29 | await client.query('begin'); |
| 30 | await client.query("set local lock_timeout='4s'"); |
| 31 | await client.query("set local statement_timeout='6s'"); |
| 32 | const result = await operation(client); |
| 33 | if (identity && !await sessionCurrent(client, identity, true)) { |
| 34 | await client.query('rollback'); return failure(401, 'session_invalid'); |
| 35 | } |
| 36 | if (result.status >= 400) await client.query('rollback'); |
| 37 | else await client.query('commit'); |
| 38 | return result; |
| 39 | } catch (error) { |
| 40 | await client.query('rollback').catch(() => {}); |
| 41 | // SQL/constraint details may contain profile information. Never return them. |
| 42 | if (['55P03', '57014', '40P01'].includes(error.code)) return failure(503, 'child_identity_busy'); |
| 43 | throw error; |
| 44 | } finally { client.release(); } |
| 45 | } |
| 46 | |
| 47 | async function context(client, familyId, participants) { |
| 48 | const family = await client.query('select id from family_workspace where id=$1 and archived_at is null for update', [familyId]); |
| 49 | if (!family.rowCount) return false; |
| 50 | const ids = [...new Set(participants)].sort(); |
| 51 | // Stable order shared with family lifecycle: family, users, profiles, |
| 52 | // memberships, managed child, consent, invitation, final actor session. |
| 53 | await client.query('select id from app_user where id=any($1::uuid[]) order by id for update', [ids]); |
| 54 | await client.query('select user_id from profile where user_id=any($1::uuid[]) order by user_id for update', [ids]); |
| 55 | await client.query('select user_id from family_membership where family_id=$1 and user_id=any($2::uuid[]) order by user_id for update', [familyId, ids]); |
| 56 | return true; |
| 57 | } |
| 58 | async function adultParent(client, familyId, userId, { admin = false } = {}) { |
| 59 | return (await client.query(`select 1 from family_membership m join app_user u on u.id=m.user_id |
| 60 | join profile p on p.user_id=m.user_id where m.family_id=$1 and m.user_id=$2 |
| 61 | and m.ended_at is null and m.role='parent_guardian' and (not $3::boolean or m.is_admin) |
| 62 | and u.disabled_at is null and not u.is_managed_child and p.birth_date is not null |
| 63 | and p.birth_date<=(current_date-interval '18 years')::date`, [familyId, userId, admin])).rowCount > 0; |
| 64 | } |
| 65 | async function activeChild(client, familyId, childId) { |
| 66 | await client.query('select user_id from managed_child where family_id=$1 and user_id=$2 for update', [familyId, childId]); |
| 67 | await client.query('select child_user_id from child_consent where child_user_id=$1 for update', [childId]); |
| 68 | return (await client.query(`select mc.* from managed_child mc join app_user u on u.id=mc.user_id |
| 69 | join profile p on p.user_id=mc.user_id join child_consent c on c.child_user_id=mc.user_id |
| 70 | join family_membership m on m.family_id=mc.family_id and m.user_id=mc.user_id |
| 71 | where mc.family_id=$1 and mc.user_id=$2 and u.is_managed_child and u.disabled_at is null |
| 72 | and m.role='teen_minor' and not m.is_admin and m.ended_at is null and c.revoked_at is null |
| 73 | and p.birth_date is not null and p.birth_date<=current_date |
| 74 | and p.birth_date>(current_date-interval '18 years')::date |
| 75 | and c.text_version=$3 and c.text_digest=$4`, [familyId, childId, SANDBOX_CONSENT_VERSION, SANDBOX_CONSENT_DIGEST])).rows[0]; |
| 76 | } |
| 77 | async function guardian(client, familyId, guardianId, childId) { |
| 78 | return await adultParent(client, familyId, guardianId) && |
| 79 | (await client.query('select is_active_guardian($1,$2,$3) active', [familyId, guardianId, childId])).rows[0].active; |
| 80 | } |
| 81 | async function audit(client, familyId, actorId, childId, action) { |
| 82 | await client.query(`insert into access_audit(actor_user_id,family_id,action,target_type,target_id,metadata) |
| 83 | values($1,$2,$3,'managed_child',$4,'{"synthetic":true}'::jsonb)`, [actorId, familyId, 'child.sandbox.' + action, childId]); |
| 84 | } |
| 85 | const validPath = req => uuid(req.params.familyId) && uuid(req.params.childId) && Object.keys(req.query).length === 0; |
| 86 | |
| 87 | export function createChildIdentityRouter({ pool, auth, jwtSecret, env = process.env }) { |
| 88 | const router = express.Router(); |
| 89 | // This router is mounted ahead of legacy /v1 routes. A use('/') gate would |
| 90 | // disable unrelated APIs; gate only these explicit identity paths. |
| 91 | const gate = (_req, res, next) => { |
| 92 | res.set('Cache-Control', 'no-store'); |
| 93 | if (!['test', 'development'].includes(env.NODE_ENV) || env.ENABLE_CHILD_IDENTITY_SANDBOX !== 'true') |
| 94 | return send(res, failure(503, 'child_identity_sandbox_disabled')); |
| 95 | next(); |
| 96 | }; |
| 97 | router.post('/families/:familyId/children', gate, auth, async (req, res) => { |
| 98 | const body = req.body; |
| 99 | if (!uuid(req.params.familyId) || Object.keys(req.query).length || !exact(body, |
| 100 | ['displayName', 'birthDate', 'profileCategory', 'consentVersion', 'consentAccepted', 'synthetic']) || |
| 101 | typeof body.displayName !== 'string' || body.displayName.trim().length < 1 || body.displayName.trim().length > 100 || |
| 102 | body.displayName.includes('\u0000') || |
| 103 | !date(body.birthDate) || !['girl_minor', 'boy_minor'].includes(body.profileCategory) || body.consentVersion !== SANDBOX_CONSENT_VERSION || |
| 104 | body.consentAccepted !== true || body.synthetic !== true) return send(res, failure(400, 'invalid_child_profile')); |
| 105 | send(res, await transact(pool, req.identity, async client => { |
| 106 | const age = (await client.query("select $1::date<=current_date and $1::date>(current_date-interval '18 years')::date valid", [body.birthDate])).rows[0].valid; |
| 107 | if (!age) return failure(400, 'invalid_child_profile'); |
| 108 | if (!await context(client, req.params.familyId, [req.identity.sub])) return failure(403, 'forbidden'); |
| 109 | if (!await adultParent(client, req.params.familyId, req.identity.sub, { admin: true })) return failure(403, 'forbidden'); |
| 110 | const childId = crypto.randomUUID(); |
| 111 | await client.query('insert into app_user(id,identity_subject,is_managed_child) values($1,$2,true)', [childId, 'managed-child:' + childId]); |
| 112 | await client.query('insert into profile(user_id,display_name,birth_date,profile_category,theme_preference) values($1,$2,$3,$4,$5)', |
| 113 | [childId, body.displayName.trim(), body.birthDate, body.profileCategory, body.profileCategory === 'girl_minor' ? 'girl_pink' : 'boy_blue']); |
| 114 | await client.query("insert into family_membership(family_id,user_id,role) values($1,$2,'teen_minor')", [req.params.familyId, childId]); |
| 115 | await client.query('insert into managed_child(user_id,family_id,creator_user_id) values($1,$2,$3)', [childId, req.params.familyId, req.identity.sub]); |
| 116 | await client.query('insert into child_consent(child_user_id,actor_user_id,text_version,text_digest) values($1,$2,$3,$4)', |
| 117 | [childId, req.identity.sub, SANDBOX_CONSENT_VERSION, SANDBOX_CONSENT_DIGEST]); |
| 118 | await client.query('insert into guardian_relationship(family_id,guardian_user_id,minor_user_id) values($1,$2,$3)', [req.params.familyId, req.identity.sub, childId]); |
| 119 | await audit(client, req.params.familyId, req.identity.sub, childId, 'create'); |
| 120 | return { status: 201, body: { child: { userId: childId, familyId: req.params.familyId, displayName: body.displayName.trim(), profileCategory: body.profileCategory }, |
| 121 | consent: { version: SANDBOX_CONSENT_VERSION, digest: SANDBOX_CONSENT_DIGEST, synthetic: true } } }; |
| 122 | })); |
| 123 | }); |
| 124 | |
| 125 | router.post('/families/:familyId/children/:childId/device-invitations', gate, auth, async (req, res) => { |
| 126 | if (!validPath(req) || !exact(req.body, ['synthetic']) || req.body.synthetic !== true) return send(res, failure(400, 'invalid_device_invitation')); |
| 127 | const { familyId, childId } = req.params; |
| 128 | send(res, await transact(pool, req.identity, async client => { |
| 129 | if (!await context(client, familyId, [req.identity.sub, childId])) return failure(403, 'forbidden'); |
| 130 | if (!await activeChild(client, familyId, childId) || !await guardian(client, familyId, req.identity.sub, childId)) return failure(403, 'forbidden'); |
| 131 | // Every parent-issued device is recovery-capable. Retire all prior devices |
| 132 | // before creating a replacement, even when this is the first issuance. |
| 133 | await client.query('update auth_session set revoked_at=clock_timestamp() where user_id=$1 and revoked_at is null', [childId]); |
| 134 | await client.query('update child_device_invitation set revoked_at=clock_timestamp() where child_user_id=$1 and used_at is null and revoked_at is null', [childId]); |
| 135 | const raw = token(); |
| 136 | const row = (await client.query(`insert into child_device_invitation(family_id,child_user_id,issuer_user_id,token_digest,expires_at) |
| 137 | values($1,$2,$3,$4,clock_timestamp()+interval '10 minutes') returning expires_at`, [familyId, childId, req.identity.sub, digest(raw)])).rows[0]; |
| 138 | await audit(client, familyId, req.identity.sub, childId, 'device_issue'); |
| 139 | return { status: 201, body: { token: raw, expiresAt: row.expires_at, accessScope: 'child_education', synthetic: true } }; |
| 140 | })); |
| 141 | }); |
| 142 | |
| 143 | router.post('/child-devices/accept', gate, async (req, res) => { |
| 144 | if (!bodyToken(req.body) || Object.keys(req.query).length) return send(res, invalidInvitation()); |
| 145 | const key = digest(req.body.token); |
| 146 | send(res, await transact(pool, null, async client => { |
| 147 | const preliminary = (await client.query('select family_id,child_user_id,issuer_user_id from child_device_invitation where token_digest=$1', [key])).rows[0]; |
| 148 | if (!preliminary || !await context(client, preliminary.family_id, [preliminary.child_user_id, preliminary.issuer_user_id])) return invalidInvitation(); |
| 149 | if (!await activeChild(client, preliminary.family_id, preliminary.child_user_id) || |
| 150 | !await guardian(client, preliminary.family_id, preliminary.issuer_user_id, preliminary.child_user_id)) return invalidInvitation(); |
| 151 | await client.query('select id from child_device_invitation where token_digest=$1 for update', [key]); |
| 152 | // Fresh SQL AFTER every lock: a token expiring while blocked is unusable. |
| 153 | const invitation = (await client.query(`select id from child_device_invitation where token_digest=$1 |
| 154 | and used_at is null and revoked_at is null and expires_at>clock_timestamp()`, [key])).rows[0]; |
| 155 | if (!invitation) return invalidInvitation(); |
| 156 | await client.query('update child_device_invitation set used_at=clock_timestamp() where id=$1', [invitation.id]); |
| 157 | const refreshToken = token(); |
| 158 | const session = (await client.query(`insert into auth_session(user_id,refresh_digest,expires_at,access_scope,child_issuer_user_id) |
| 159 | values($1,$2,clock_timestamp()+interval '30 days','child_education',$3) returning id`, [preliminary.child_user_id, digest(refreshToken), preliminary.issuer_user_id])).rows[0]; |
| 160 | await audit(client, preliminary.family_id, preliminary.child_user_id, preliminary.child_user_id, 'device_accept'); |
| 161 | return { status: 200, body: { accessToken: jwt.sign({ sub: preliminary.child_user_id, sid: session.id, scope: 'child_education', aud: 'lifemate-api' }, |
| 162 | jwtSecret, { issuer: 'lifemate', expiresIn: '15m' }), refreshToken, accessScope: 'child_education', synthetic: true } }; |
| 163 | })); |
| 164 | }); |
| 165 | |
| 166 | router.post('/families/:familyId/children/:childId/guardian-invitations', gate, auth, async (req, res) => { |
| 167 | if (!validPath(req) || !exact(req.body, ['guardianUserId']) || !uuid(req.body.guardianUserId)) return send(res, failure(400, 'invalid_guardian_invitation')); |
| 168 | const { familyId, childId } = req.params, recipient = req.body.guardianUserId; |
| 169 | send(res, await transact(pool, req.identity, async client => { |
| 170 | if (!await context(client, familyId, [req.identity.sub, childId, recipient])) return failure(403, 'forbidden'); |
| 171 | const child = await activeChild(client, familyId, childId); |
| 172 | if (!child || child.creator_user_id !== req.identity.sub || !await guardian(client, familyId, req.identity.sub, childId) || |
| 173 | recipient === req.identity.sub || !await adultParent(client, familyId, recipient)) return failure(403, 'forbidden'); |
| 174 | const raw = token(); |
| 175 | await client.query('update guardian_invitation set revoked_at=clock_timestamp() where child_user_id=$1 and guardian_user_id=$2 and used_at is null and revoked_at is null', [childId, recipient]); |
| 176 | const row = (await client.query(`insert into guardian_invitation(family_id,child_user_id,issuer_user_id,guardian_user_id,token_digest,expires_at) |
| 177 | values($1,$2,$3,$4,$5,clock_timestamp()+interval '10 minutes') returning expires_at`, [familyId, childId, req.identity.sub, recipient, digest(raw)])).rows[0]; |
| 178 | await audit(client, familyId, req.identity.sub, childId, 'guardian_invite'); |
| 179 | return { status: 201, body: { token: raw, expiresAt: row.expires_at, synthetic: true } }; |
| 180 | })); |
| 181 | }); |
| 182 | |
| 183 | router.post('/guardian-invitations/accept', gate, auth, async (req, res) => { |
| 184 | if (!bodyToken(req.body) || Object.keys(req.query).length) return send(res, invalidInvitation()); |
| 185 | const key = digest(req.body.token); |
| 186 | send(res, await transact(pool, req.identity, async client => { |
| 187 | const preliminary = (await client.query('select * from guardian_invitation where token_digest=$1', [key])).rows[0]; |
| 188 | if (!preliminary || preliminary.guardian_user_id !== req.identity.sub || |
| 189 | !await context(client, preliminary.family_id, [preliminary.child_user_id, preliminary.issuer_user_id, req.identity.sub])) return invalidInvitation(); |
| 190 | const child = await activeChild(client, preliminary.family_id, preliminary.child_user_id); |
| 191 | if (!child || child.creator_user_id !== preliminary.issuer_user_id || |
| 192 | !await guardian(client, preliminary.family_id, preliminary.issuer_user_id, preliminary.child_user_id) || |
| 193 | !await adultParent(client, preliminary.family_id, req.identity.sub)) return invalidInvitation(); |
| 194 | await client.query('select id from guardian_invitation where token_digest=$1 for update', [key]); |
| 195 | // Freeze logout before the last expiry gate. A session-row lock wait must |
| 196 | // not allow an invitation that expired during that wait to be consumed. |
| 197 | if (!await sessionCurrent(client, req.identity, true)) return failure(401, 'session_invalid'); |
| 198 | const current = (await client.query(`select id from guardian_invitation where token_digest=$1 |
| 199 | and used_at is null and revoked_at is null and expires_at>clock_timestamp()`, [key])).rows[0]; |
| 200 | if (!current) return invalidInvitation(); |
| 201 | await client.query('insert into guardian_relationship(family_id,guardian_user_id,minor_user_id) values($1,$2,$3) on conflict(family_id,guardian_user_id,minor_user_id) do update set active=true', |
| 202 | [preliminary.family_id, req.identity.sub, preliminary.child_user_id]); |
| 203 | await client.query('update guardian_invitation set used_at=clock_timestamp() where id=$1', [current.id]); |
| 204 | await audit(client, preliminary.family_id, req.identity.sub, preliminary.child_user_id, 'guardian_accept'); |
| 205 | return { status: 200, body: { accepted: true, synthetic: true } }; |
| 206 | })); |
| 207 | }); |
| 208 | |
| 209 | router.delete('/families/:familyId/children/:childId/consent', gate, auth, async (req, res) => { |
| 210 | if (!validPath(req) || (req.body !== undefined && !exact(req.body, []))) return send(res, failure(400, 'invalid_consent_revocation')); |
| 211 | const { familyId, childId } = req.params; |
| 212 | send(res, await transact(pool, req.identity, async client => { |
| 213 | if (!await context(client, familyId, [req.identity.sub, childId])) return failure(403, 'forbidden'); |
| 214 | const child = await activeChild(client, familyId, childId); |
| 215 | if (!child || child.creator_user_id !== req.identity.sub || !await adultParent(client, familyId, req.identity.sub)) return failure(403, 'forbidden'); |
| 216 | await client.query('update child_consent set revoked_at=clock_timestamp() where child_user_id=$1', [childId]); |
| 217 | await client.query('update auth_session set revoked_at=clock_timestamp() where user_id=$1 and revoked_at is null', [childId]); |
| 218 | await client.query('update child_device_invitation set revoked_at=clock_timestamp() where child_user_id=$1 and used_at is null and revoked_at is null', [childId]); |
| 219 | await client.query('update guardian_invitation set revoked_at=clock_timestamp() where child_user_id=$1 and used_at is null and revoked_at is null', [childId]); |
| 220 | await client.query('update guardian_relationship set active=false where family_id=$1 and minor_user_id=$2', [familyId, childId]); |
| 221 | await audit(client, familyId, req.identity.sub, childId, 'consent_revoke'); |
| 222 | return { status: 200, body: { revoked: true, synthetic: true } }; |
| 223 | })); |
| 224 | }); |
| 225 | |
| 226 | router.delete('/families/:familyId/children/:childId/guardians/:guardianId', gate, auth, async (req, res) => { |
| 227 | if (!validPath(req) || !uuid(req.params.guardianId) || (req.body !== undefined && !exact(req.body, []))) |
| 228 | return send(res, failure(400, 'invalid_guardian_revocation')); |
| 229 | const { familyId, childId, guardianId } = req.params; |
| 230 | send(res, await transact(pool, req.identity, async client => { |
| 231 | if (!await context(client, familyId, [req.identity.sub, childId, guardianId])) return failure(403, 'forbidden'); |
| 232 | const child = await activeChild(client, familyId, childId); |
| 233 | if (!child || child.creator_user_id !== req.identity.sub || !await guardian(client, familyId, req.identity.sub, childId)) |
| 234 | return failure(403, 'forbidden'); |
| 235 | if (guardianId === child.creator_user_id) return failure(409, 'creator_requires_consent_revocation'); |
| 236 | const relationship = await client.query(`update guardian_relationship set active=false |
| 237 | where family_id=$1 and guardian_user_id=$2 and minor_user_id=$3 returning guardian_user_id`, [familyId, guardianId, childId]); |
| 238 | if (!relationship.rowCount) return failure(403, 'forbidden'); |
| 239 | // Accepted invitation evidence must also be retired, preventing legacy |
| 240 | // relation reactivation from silently restoring a revoked grant. |
| 241 | await client.query('update guardian_invitation set revoked_at=clock_timestamp() where child_user_id=$1 and guardian_user_id=$2 and revoked_at is null', [childId, guardianId]); |
| 242 | await client.query('update auth_session set revoked_at=clock_timestamp() where user_id=$1 and child_issuer_user_id=$2 and revoked_at is null', [childId, guardianId]); |
| 243 | await client.query('update child_device_invitation set revoked_at=clock_timestamp() where child_user_id=$1 and issuer_user_id=$2 and used_at is null and revoked_at is null', [childId, guardianId]); |
| 244 | await audit(client, familyId, req.identity.sub, childId, 'guardian_revoke'); |
| 245 | return { status: 200, body: { revoked: true, synthetic: true } }; |
| 246 | })); |
| 247 | }); |
| 248 | |
| 249 | router.get('/child-visibility', gate, auth, async (req, res) => { |
| 250 | if (Object.keys(req.query).length) return send(res, failure(400, 'invalid_visibility_query')); |
| 251 | send(res, await transact(pool, req.identity, async client => { |
| 252 | const preliminary = (await client.query('select family_id from managed_child where user_id=$1', [req.identity.sub])).rows[0]; |
| 253 | if (!preliminary) return failure(403, 'forbidden'); |
| 254 | const guardianIds = (await client.query('select guardian_user_id from guardian_relationship where family_id=$1 and minor_user_id=$2', [preliminary.family_id, req.identity.sub])).rows.map(row => row.guardian_user_id); |
| 255 | if (!await context(client, preliminary.family_id, [req.identity.sub, ...guardianIds]) || !await activeChild(client, preliminary.family_id, req.identity.sub)) return failure(403, 'forbidden'); |
| 256 | const guardians = (await client.query(`select g.guardian_user_id "userId",p.display_name "displayName" from guardian_relationship g |
| 257 | join family_membership m on m.family_id=g.family_id and m.user_id=g.guardian_user_id |
| 258 | join profile p on p.user_id=g.guardian_user_id join app_user u on u.id=g.guardian_user_id |
| 259 | where g.family_id=$1 and g.minor_user_id=$2 and is_active_guardian(g.family_id,g.guardian_user_id,g.minor_user_id) |
| 260 | and m.ended_at is null and m.role='parent_guardian' |
| 261 | and u.disabled_at is null and p.birth_date<=(current_date-interval '18 years')::date |
| 262 | order by g.guardian_user_id`, [preliminary.family_id, req.identity.sub])).rows; |
| 263 | await audit(client, preliminary.family_id, req.identity.sub, req.identity.sub, 'visibility_read'); |
| 264 | return { status: 200, body: { guardians, accessScope: 'child_education', privateScopeGrantedByThisFlow: false, |
| 265 | guardianVisibility: ['explicitly_shared_academic_summary'], excluded: ['private_notes', 'private_chat', 'health', 'cycle'], |
| 266 | synthetic: true, notice: 'این محیط فقط برای هویت ساختگی آزمایشی است؛ دستگاه صادرشده توسط سرپرست به فضای خصوصی دسترسی ندارد.' } }; |
| 267 | })); |
| 268 | }); |
| 269 | return router; |
| 270 | } |