From a43708c14e9a913d8025c6a7a6b2c9f49a6557f5 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 26 Nov 2026 12:20:25 -0600 Subject: [PATCH] manage-user: the roster sends overseer and lead, and set_flags sets a seat's roles PipeTooling's People screens are about to show who holds which role here, and its person desk is about to set them. The roster sent only the admin and twin flags, so an overseer read as a plain seat. - roster also sends is_overseer and is_lead. - set_flags { ct_user_id, is_admin?, is_overseer?, is_lead? } changes only the flags it is given. The last admin is never switched off. Behind the bridge secret like every verb here. Nothing changes until the function is deployed, and PipeTooling's proxy does not forward set_flags yet. Co-Authored-By: Claude Fable 5.2 --- supabase/functions/manage-user/index.ts | 40 ++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/supabase/functions/manage-user/index.ts b/supabase/functions/manage-user/index.ts index 71c25e67..c6a44889 100644 --- a/supabase/functions/manage-user/index.ts +++ b/supabase/functions/manage-user/index.ts @@ -22,6 +22,8 @@ import { corsHeaders } from '../_shared/cors.ts' // * `create` is idempotent: an existing email returns the existing uuid, so PT can // retry freely (fail-soft on the PT side). // * `roster` exists for PT's weekly drift audit — drift is caught, not prevented. +// * `set_flags` (2026-11-26) sets a seat's admin, overseer and lead flags from PT's person +// desk, and `roster` sends the three, so PT's People screens say who holds which. const bridgeCors = { ...corsHeaders, @@ -129,6 +131,39 @@ Deno.serve(async (req) => { if (error) return jsonRes(500, { error: `set_twin_flag failed: ${error.message}` }) return jsonRes(200, { ct_user_id: id, is_digital_twin: flag }) } + case 'set_flags': { + // 2026-11-26: PipeTooling's person desk sets a seat's roles here. Only the flags + // the caller names change. The overseer and lead flags are the ones Manage users + // toggles (admin_set_overseer / admin_set_lead); the admin flag had no door at all. + // The last admin is never switched off: the app would be left with nobody who can + // manage its users. + const id = String(body.ct_user_id ?? '') + if (!id) return jsonRes(400, { error: 'ct_user_id required' }) + const patch: Record = {} + for (const key of ['is_admin', 'is_overseer', 'is_lead'] as const) { + if (typeof body[key] === 'boolean') patch[key] = body[key] as boolean + } + if (Object.keys(patch).length === 0) return jsonRes(400, { error: 'name at least one of is_admin, is_overseer, is_lead (true or false)' }) + if (patch.is_admin === false) { + const { data: admins, error: adminsErr } = await admin.from('profiles').select('user_id').eq('is_admin', true) + if (adminsErr) return jsonRes(500, { error: `admins read failed: ${adminsErr.message}` }) + const others = (admins ?? []).filter((a) => a.user_id !== id) + if (others.length === 0) return jsonRes(409, { error: 'They are the only admin on CountTooling. Make someone else an admin first.' }) + } + const { data: saved, error } = await admin + .from('profiles') + .upsert({ user_id: id, ...patch }, { onConflict: 'user_id' }) + .select('user_id, is_admin, is_overseer, is_lead') + .maybeSingle() + if (error) return jsonRes(500, { error: `set_flags failed: ${error.message}` }) + console.log(`manage-user set_flags: ${id} ${JSON.stringify(patch)}`) + return jsonRes(200, { + ct_user_id: id, + is_admin: saved?.is_admin === true, + is_overseer: saved?.is_overseer === true, + is_lead: saved?.is_lead === true, + }) + } case 'update_email': { const id = String(body.ct_user_id ?? '') const email = String(body.email ?? '').trim().toLowerCase() @@ -147,7 +182,7 @@ Deno.serve(async (req) => { } case 'roster': { const users = await listAllUsers(admin) - const { data: profiles, error } = await admin.from('profiles').select('user_id, is_admin, is_digital_twin') + const { data: profiles, error } = await admin.from('profiles').select('user_id, is_admin, is_overseer, is_lead, is_digital_twin') if (error) return jsonRes(500, { error: `profiles read failed: ${error.message}` }) const profById = new Map((profiles ?? []).map((p) => [p.user_id as string, p])) const roster = users.map((u) => ({ @@ -155,6 +190,9 @@ Deno.serve(async (req) => { email: u.email ?? null, is_digital_twin: profById.get(u.id)?.is_digital_twin === true, is_admin: profById.get(u.id)?.is_admin === true, + // 2026-11-26: PipeTooling's People screens show the two roles a seat can hold here + is_overseer: profById.get(u.id)?.is_overseer === true, + is_lead: profById.get(u.id)?.is_lead === true, active: isActive(u), })) return jsonRes(200, { roster, count: roster.length })