From 480cc46768469d16b727e1e18ab00ef539d4c930 Mon Sep 17 00:00:00 2001 From: Bernt Christian Egeland Date: Mon, 2 Sep 2024 17:44:49 +0000 Subject: [PATCH] include description for all networks --- .../v1/org/[orgid]/network/[nwid]/index.ts | 2 +- src/pages/api/v1/org/[orgid]/network/index.ts | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/pages/api/v1/org/[orgid]/network/[nwid]/index.ts b/src/pages/api/v1/org/[orgid]/network/[nwid]/index.ts index 85771caa..f7102ccd 100644 --- a/src/pages/api/v1/org/[orgid]/network/[nwid]/index.ts +++ b/src/pages/api/v1/org/[orgid]/network/[nwid]/index.ts @@ -151,7 +151,7 @@ export const GET_network = SecuredOrganizationApiRoute( try { const network = await prisma.network.findUnique({ where: { nwid: networkId }, - select: { authorId: true, description: true }, + select: { description: true }, }); if (!network) { diff --git a/src/pages/api/v1/org/[orgid]/network/index.ts b/src/pages/api/v1/org/[orgid]/network/index.ts index 12cab7ca..08941750 100644 --- a/src/pages/api/v1/org/[orgid]/network/index.ts +++ b/src/pages/api/v1/org/[orgid]/network/index.ts @@ -6,6 +6,7 @@ import { handleApiErrors } from "~/utils/errors"; import rateLimit from "~/utils/rateLimit"; import * as ztController from "~/utils/ztApi"; import { createNetworkContextSchema } from "./_schema"; +import { prisma } from "~/server/db"; // Number of allowed requests per minute const limiter = rateLimit({ @@ -74,6 +75,17 @@ export const GET_orgUserNetworks = SecuredOrganizationApiRoute( const organization = await caller.org .getOrgById({ organizationId: orgId }) .then(async (org) => { + // Fetch all network descriptions in a single query + const networkDescriptions = await prisma.network.findMany({ + where: { nwid: { in: org.networks.map((n) => n.nwid) } }, + select: { nwid: true, description: true }, + }); + + // Create a map for quick lookup + const descriptionMap = new Map( + networkDescriptions.map((n) => [n.nwid, { description: n.description }]), + ); + // Use Promise.all to wait for all network detail fetches to complete const networksDetails = await Promise.all( org.networks.map(async (network) => { @@ -82,7 +94,13 @@ export const GET_orgUserNetworks = SecuredOrganizationApiRoute( ctx, network.nwid, ); - return controller.network; + const dbInfo = descriptionMap.get(network.nwid) || { + description: null, + }; + return { + ...dbInfo, + ...controller.network, + }; }), ); return networksDetails;