All files / src/app/api/participants/me route.ts

92.85% Statements 13/14
50% Branches 1/2
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50        1x                   3x 3x   3x 3x                 2x   3x 3x     2x   1x         2x   1x 1x            
import { NextResponse } from "next/server";
import { edcClient } from "@/lib/edc";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
 
export const dynamic = "force-dynamic";
 
/**
 * GET /api/participants/me — Get the current user's tenant profile.
 *
 * Lists all tenants from CFM TenantManager. In a production system this
 * would look up the tenant matching the authenticated user's DID; in the
 * demo we return all tenants with their profiles.
 */
export async function GET() {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  try {
    const tenants = await edcClient.tenant<
      {
        id: string;
        version: number;
        properties: Record<string, string>;
      }[]
    >("/v1alpha1/tenants");
 
    // Enrich each tenant with its participant profiles
    const enriched = await Promise.all(
      tenants.map(async (t) => {
        try {
          const profiles = await edcClient.tenant<unknown[]>(
            `/v1alpha1/tenants/${t.id}/participant-profiles`,
          );
          return { ...t, participantProfiles: profiles };
        } catch {
          return { ...t, participantProfiles: [] };
        }
      }),
    );
 
    return NextResponse.json(enriched);
  } catch (err) {
    console.error("Failed to get participant profile:", err);
    return NextResponse.json(
      { error: "Failed to get participant profile" },
      { status: 502 },
    );
  }
}