LifeMate — Personal, Family & Learning Companion
fix: model family admin as independent capability
davoodmehraban89 committed
Oct 5, 2026 at 07:39 UTC
6f4dd64dd1d15269df55766c06fae364a19ffef2
1 file changed
+20
-21
backend/src/server.js
+20
-21
@@ -84,12 +84,13 @@ async function createSession(client, userId) {
84
};
85
}
86
87
-async function requireFamilyRole(familyId, userId, roles) {
87
+async function familyAccess(familyId, userId, { roles = [], admin = false } = {}) {
88
const r = await pool.query(
89
- 'select role from family_membership where family_id=$1 and user_id=$2 and ended_at is null',
89
+ 'select role,is_admin from family_membership where family_id=$1 and user_id=$2 and ended_at is null',
90
[familyId, userId],
91
);
92
- return r.rowCount && roles.includes(r.rows[0].role);
92
+ if (!r.rowCount) return false;
93
+ return (admin && r.rows[0].is_admin) || roles.includes(r.rows[0].role);
94
}
95
96
app.get('/health', async (_req, res) => {
@@ -365,6 +366,9 @@ app.post('/v1/families', auth, async (req, res) => {
366
const name = String(req.body.name ?? '').trim();
367
if (!name) return res.status(400).json({ error: 'invalid_name' });
368
369
+ const creatorRole = ['parent_guardian', 'adult_member'].includes(req.body.role)
370
+ ? req.body.role
371
+ : 'adult_member';
372
const c = await pool.connect();
373
try {
374
await c.query('begin');
@@ -373,8 +377,8 @@ app.post('/v1/families', auth, async (req, res) => {
377
[name, req.identity.sub],
378
);
379
await c.query(
376
- "insert into family_membership(family_id,user_id,role) values($1,$2,'owner_admin')",
377
- [f.rows[0].id, req.identity.sub],
380
+ 'insert into family_membership(family_id,user_id,role,is_admin) values($1,$2,$3,true)',
381
+ [f.rows[0].id, req.identity.sub, creatorRole],
382
);
383
await c.query(
384
"insert into access_audit(actor_user_id,family_id,action,target_type,target_id) values($1,$2,'family.create','family',$2::text)",
@@ -391,15 +395,13 @@ app.post('/v1/families', auth, async (req, res) => {
395
});
396
397
app.get('/v1/families/:familyId/members', auth, async (req, res) => {
394
- const allowed = await requireFamilyRole(
395
- req.params.familyId,
396
- req.identity.sub,
397
- ['owner_admin', 'parent_guardian', 'teen_minor', 'adult_member'],
398
- );
398
+ const allowed = await familyAccess(req.params.familyId, req.identity.sub, {
399
+ roles: ['parent_guardian', 'teen_minor', 'adult_member'],
400
+ });
401
if (!allowed) return res.status(403).json({ error: 'forbidden' });
402
403
const r = await pool.query(
402
- `select m.user_id,m.role,p.display_name,p.theme_preference
404
+ `select m.user_id,m.role,m.is_admin,p.display_name,p.theme_preference
405
from family_membership m
406
join profile p on p.user_id=m.user_id
407
where m.family_id=$1 and m.ended_at is null
@@ -410,11 +412,10 @@ app.get('/v1/families/:familyId/members', auth, async (req, res) => {
412
});
413
414
app.post('/v1/families/:familyId/invitations', auth, async (req, res) => {
413
- const allowed = await requireFamilyRole(
414
- req.params.familyId,
415
- req.identity.sub,
416
- ['owner_admin', 'parent_guardian'],
417
- );
415
+ const allowed = await familyAccess(req.params.familyId, req.identity.sub, {
416
+ roles: ['parent_guardian'],
417
+ admin: true,
418
+ });
419
if (!allowed) return res.status(403).json({ error: 'forbidden' });
420
421
const email = normalizeEmail(req.body.email);
@@ -491,11 +492,9 @@ app.post('/v1/invitations/accept', auth, async (req, res) => {
492
});
493
494
app.post('/v1/families/:familyId/guardians', auth, async (req, res) => {
494
- const allowed = await requireFamilyRole(
495
- req.params.familyId,
496
- req.identity.sub,
497
- ['owner_admin'],
498
- );
495
+ const allowed = await familyAccess(req.params.familyId, req.identity.sub, {
496
+ admin: true,
497
+ });
498
if (!allowed) return res.status(403).json({ error: 'forbidden' });
499
500
const guardianUserId = String(req.body.guardianUserId ?? '');