All files / src/app/api/assets route.ts

96.42% Statements 54/56
93.87% Branches 46/49
100% Functions 8/8
100% Lines 51/51

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198      2x     2x                                       8x 8x                             10x 10x 10x 4x   6x                         14x 14x   14x   14x 14x   4x                 4x       10x         8x 9x     14x 14x 8x 8x                 7x           1x                 8x 8x 8x 3x   1x 1x 1x 2x 1x       2x       8x   2x   2x 2x         6x 6x   6x 6x               6x   6x 3x           3x                                 6x           2x   1x 1x            
import { NextRequest, NextResponse } from "next/server";
import { edcClient, EDC_CONTEXT } from "@/lib/edc";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { promises as fs } from "fs";
import path from "path";
 
export const dynamic = "force-dynamic";
 
interface ParticipantAssets {
  participantId: string;
  identity: string;
  assets: Record<string, unknown>[];
}
 
/**
 * Normalise an asset object returned by the EDC-V v5alpha Management API.
 *
 * v5alpha nests properties under a `properties` sub-object:
 *   { properties: { name, description, contenttype, ... } }
 *
 * Older EDC versions (v3) used `edc:name`, `edc:description`, etc. at the top
 * level.  Many UI components still read the `edc:*` form, so we promote both
 * plain *and* `edc:*` fields for maximum compatibility.
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function normaliseAsset(raw: any): Record<string, unknown> {
  const props = raw?.properties ?? {};
  return {
    ...raw,
    // Plain names (preferred going forward)
    name: props.name ?? raw["edc:name"] ?? raw["@id"],
    description: props.description ?? raw["edc:description"] ?? "",
    contenttype: props.contenttype ?? raw["edc:contenttype"] ?? "",
    // Legacy edc:* names (consumed by existing pages)
    "edc:name": props.name ?? raw["edc:name"] ?? raw["@id"],
    "edc:description": props.description ?? raw["edc:description"] ?? "",
    "edc:contenttype": props.contenttype ?? raw["edc:contenttype"] ?? "",
  };
}
 
/** Load demo assets from the bundled mock JSON file. */
async function loadMockAssets(): Promise<ParticipantAssets[]> {
  try {
    const mockPath = path.join(process.cwd(), "public", "mock", "assets.json");
    const raw = await fs.readFile(mockPath, "utf-8");
    return JSON.parse(raw) as ParticipantAssets[];
  } catch {
    return [];
  }
}
 
/**
 * GET /api/assets — List data assets for a participant context.
 * Query param: ?participantId=<ctx_id>
 *
 * POST /api/assets — Create a new data asset.
 * Body: { participantId, assetId, name, description, contentType, dataAddress }
 */
 
export async function GET(req: NextRequest) {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  const participantId = req.nextUrl.searchParams.get("participantId");
 
  try {
    if (participantId) {
      // List assets for a specific participant
      const assets = await edcClient.management<Record<string, unknown>[]>(
        `/v5alpha/participants/${participantId}/assets/request`,
        "POST",
        {
          "@context": [EDC_CONTEXT],
          "@type": "QuerySpec",
          filterExpression: [],
        },
      );
      return NextResponse.json((assets ?? []).map(normaliseAsset));
    }
 
    // List all participant contexts, then aggregate assets
    const participants = await edcClient.management<
      { "@id": string; identity: string; state?: string }[]
    >("/v5alpha/participants");
 
    // Only include ACTIVATED participants — skip stale CREATED contexts
    const active = (Array.isArray(participants) ? participants : []).filter(
      (p) => (p.state ?? "ACTIVATED") === "ACTIVATED",
    );
 
    const allAssets: ParticipantAssets[] = [];
    for (const p of active) {
      try {
        const assets = await edcClient.management<Record<string, unknown>[]>(
          `/v5alpha/participants/${p["@id"]}/assets/request`,
          "POST",
          {
            "@context": [EDC_CONTEXT],
            "@type": "QuerySpec",
            filterExpression: [],
          },
        );
        allAssets.push({
          participantId: p["@id"],
          identity: p.identity,
          assets: (assets ?? []).map(normaliseAsset),
        });
      } catch {
        allAssets.push({
          participantId: p["@id"],
          identity: p.identity,
          assets: [],
        });
      }
    }
 
    // Merge with demo assets so the discover page is always demonstrable
    const mockAssets = await loadMockAssets();
    const realIdentities = new Set(allAssets.map((a) => a.identity));
    for (const mock of mockAssets) {
      if (realIdentities.has(mock.identity)) {
        // Merge mock assets into existing participant, dedup by @id
        const real = allAssets.find((a) => a.identity === mock.identity)!;
        const existingIds = new Set(real.assets.map((a) => a["@id"] as string));
        for (const ma of mock.assets) {
          if (!existingIds.has(ma["@id"] as string)) {
            real.assets.push(ma);
          }
        }
      } else {
        allAssets.push(mock);
      }
    }
 
    return NextResponse.json(allAssets);
  } catch (err) {
    console.warn("Controlplane assets unavailable, using demo data:", err);
    // Fall back to mock data entirely
    const mockAssets = await loadMockAssets();
    return NextResponse.json(mockAssets);
  }
}
 
export async function POST(req: NextRequest) {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  try {
    const body = await req.json();
    const {
      participantId,
      assetId,
      name,
      description,
      contentType,
      dataAddress,
    } = body;
 
    if (!participantId || !assetId || !name) {
      return NextResponse.json(
        { error: "participantId, assetId, and name are required" },
        { status: 400 },
      );
    }
 
    const assetPayload = {
      "@context": [EDC_CONTEXT],
      "@type": "Asset",
      "@id": assetId,
      properties: {
        name,
        description: description || "",
        contenttype: contentType || "application/json",
        version: "1.0.0",
      },
      dataAddress: dataAddress || {
        "@type": "DataAddress",
        type: "HttpData",
        baseUrl: "http://neo4j-proxy:9090",
      },
    };
 
    const result = await edcClient.management(
      `/v5alpha/participants/${participantId}/assets`,
      "POST",
      assetPayload,
    );
 
    return NextResponse.json(result, { status: 201 });
  } catch (err) {
    console.error("Failed to create asset:", err);
    return NextResponse.json(
      { error: "Failed to create asset" },
      { status: 502 },
    );
  }
}