LifeMate — Personal, Family & Learning Companion
feat: add privacy-preserving family guidance for guardians
davoodmehraban89 committed
Oct 5, 2026 at 16:23 UTC
42b45e70a24ddec962ad2d6c3641a4e43e99ebfd
1 file changed
+110
backend/src/phase4.js
+110
@@ -212,6 +212,116 @@ export function createPhase4Router({ pool, auth }) {
212
});
213
});
214
215
+ router.post('/families/:familyId/children/:minorUserId/family-guidance', async (req, res) => {
216
+ const allowed = await guardianRelationship(
217
+ pool,
218
+ req.params.familyId,
219
+ req.identity.sub,
220
+ req.params.minorUserId,
221
+ );
222
+ if (!allowed) return res.status(403).json({ error: 'forbidden' });
223
+
224
+ const question = clampText(req.body.question, 2000);
225
+ if (!question) return res.status(400).json({ error: 'invalid_input' });
226
+
227
+ const [academic, wellbeing, safety] = await Promise.all([
228
+ pool.query(
229
+ `select
230
+ count(*) filter (
231
+ where kind in ('assignment','study_session')
232
+ and status not in ('completed','cancelled')
233
+ and due_at < now()
234
+ )::int as overdue,
235
+ count(*) filter (
236
+ where kind in ('assignment','study_session')
237
+ and status='completed'
238
+ and completed_at >= now()-interval '7 days'
239
+ )::int as completed_last_7_days,
240
+ coalesce(sum(duration_minutes) filter (
241
+ where kind='study_session'
242
+ and status='completed'
243
+ and completed_at >= now()-interval '7 days'
244
+ ),0)::int as study_minutes_last_7_days,
245
+ case when sum(grade_out_of) filter (
246
+ where grade_points is not null and grade_out_of is not null
247
+ ) > 0 then
248
+ round(
249
+ sum(grade_points) filter (where grade_points is not null and grade_out_of is not null)
250
+ /
251
+ sum(grade_out_of) filter (where grade_points is not null and grade_out_of is not null)
252
+ * 100,
253
+ 1
254
+ )
255
+ end as grade_percent
256
+ from plan_item
257
+ where owner_user_id=$1`,
258
+ [req.params.minorUserId],
259
+ ),
260
+ pool.query(
261
+ `select
262
+ count(*)::int as checkin_count,
263
+ round(avg(mood)::numeric,2) as mood_average,
264
+ round(avg(energy)::numeric,2) as energy_average,
265
+ round(avg(stress)::numeric,2) as stress_average
266
+ from wellbeing_checkin
267
+ where owner_user_id=$1
268
+ and visibility='guardian_summary'
269
+ and created_at >= now()-interval '14 days'`,
270
+ [req.params.minorUserId],
271
+ ),
272
+ pool.query(
273
+ `select count(*)::int as open_urgent_count
274
+ from wellbeing_safety_event
275
+ where owner_user_id=$1 and status='open'`,
276
+ [req.params.minorUserId],
277
+ ),
278
+ ]);
279
+
280
+ const context = {
281
+ academic: academic.rows[0],
282
+ wellbeing: wellbeing.rows[0],
283
+ safety: safety.rows[0],
284
+ privacy: {
285
+ rawNotesIncluded: false,
286
+ rawConversationIncluded: false,
287
+ },
288
+ };
289
+ const prompt = [
290
+ 'You are advising a parent/guardian using only authorized aggregate data.',
291
+ 'Do not diagnose the child and do not infer hidden mental state.',
292
+ 'Give concrete, low-pressure family support ideas and questions the parent can ask.',
293
+ 'If safety.open_urgent_count is above zero, prioritize immediate trusted-human support and local emergency resources.',
294
+ `Authorized summary: ${JSON.stringify(context)}`,
295
+ `Parent question: ${question}`,
296
+ ].join('\n');
297
+
298
+ const klass =
299
+ Number(context.safety.open_urgent_count ?? 0) > 0
300
+ ? 'urgent_review'
301
+ : safetyClass(question);
302
+ const advice = await providerGuide('wellbeing', prompt, klass);
303
+
304
+ await pool.query(
305
+ `insert into access_audit(actor_user_id,family_id,action,target_type,target_id,metadata)
306
+ values($1,$2,'parent.family_guidance.request','student',$3,$4::jsonb)`,
307
+ [
308
+ req.identity.sub,
309
+ req.params.familyId,
310
+ req.params.minorUserId,
311
+ JSON.stringify({ rawNotesIncluded: false, rawConversationIncluded: false }),
312
+ ],
313
+ );
314
+
315
+ res.json({
316
+ advice,
317
+ summary: context,
318
+ advisory: true,
319
+ medicalDiagnosis: false,
320
+ rawNotesIncluded: false,
321
+ rawConversationIncluded: false,
322
+ });
323
+ });
324
+
325
router.post('/ai/sessions', async (req, res) => {
326
const kind = ['study', 'planner', 'wellbeing'].includes(req.body.kind)
327
? req.body.kind