Skip to content

Commit

Permalink
include description for all networks
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Sep 2, 2024
1 parent ed8487e commit 480cc46
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/pages/api/v1/org/[orgid]/network/[nwid]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 19 additions & 1 deletion src/pages/api/v1/org/[orgid]/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down

0 comments on commit 480cc46

Please sign in to comment.