import type { APIRoute } from 'astro'; import { getUserIdFromToken } from './auth'; /* ── In-memory groups store ────────────────────────────────── */ type Group = { id: string; name: string; description: string; members: Set; }; export const groups = new Map(); // Seed groups groups.set('family', { id: 'family', name: 'Family', description: 'The whole family', members: new Set(['junwon', 'sungho', 'mikyung', 'soojin']), }); groups.set('parents', { id: 'parents', name: 'Parents', description: 'Mom & Dad', members: new Set(['sungho', 'mikyung']), }); groups.set('siblings', { id: 'siblings', name: 'Siblings', description: 'Junwon & Soojin', members: new Set(['junwon', 'soojin']), }); /* ── GET /family/api/groups?token=TOKEN ────────────────────── */ export const GET: APIRoute = async ({ url }) => { const token = url.searchParams.get('token'); const userId = getUserIdFromToken(token); if (!userId) { return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401, headers: { 'Content-Type': 'application/json' }, }); } const result = Array.from(groups.values()).map((g) => ({ id: g.id, name: g.name, description: g.description, member_count: g.members.size, is_member: g.members.has(userId), })); return new Response(JSON.stringify(result), { status: 200, headers: { 'Content-Type': 'application/json' }, }); }; /* ── POST /family/api/groups — join a group ────────────────── */ export const POST: APIRoute = async ({ request }) => { const body = await request.json(); const { token, groupId } = body; const userId = getUserIdFromToken(token); if (!userId) { return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401, headers: { 'Content-Type': 'application/json' }, }); } const group = groups.get(groupId); if (!group) { return new Response(JSON.stringify({ error: 'Group not found' }), { status: 404, headers: { 'Content-Type': 'application/json' }, }); } group.members.add(userId); return new Response(JSON.stringify({ ok: true }), { status: 200, headers: { 'Content-Type': 'application/json' }, }); };