Skip to content

Commit

Permalink
Merge pull request #432 from sinamics/duplicate-routes
Browse files Browse the repository at this point in the history
Bugfix, ensure routes array is non-empty before querying database for duplicate routes.
  • Loading branch information
sinamics authored Jun 4, 2024
2 parents 07a60bf + 3c6b868 commit 03c6ebc
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/server/api/routers/networkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,24 @@ export const networkRouter = createTRPCRouter({
name: string;
}

const duplicateRoutes: DuplicateRoutes[] = await ctx.prisma.$queryRaw`
SELECT "authorId", "routes", "name", "nwid"
FROM "network"
WHERE "authorId" = ${ctx.session.user.id}
AND EXISTS (
SELECT 1
FROM jsonb_array_elements("routes") as route
WHERE route->>'target' IN (${Prisma.join(targetIPs)})
)
AND "nwid" != ${input.nwid};
`;
// check if there are any other networks with the same routes.
let duplicateRoutes: DuplicateRoutes[] = [];
if (targetIPs.length > 0) {
duplicateRoutes = await ctx.prisma.$queryRaw<DuplicateRoutes[]>`
SELECT "authorId", "routes", "name", "nwid"
FROM "network"
WHERE "authorId" = ${ctx.session.user.id}
AND EXISTS (
SELECT 1
FROM jsonb_array_elements("routes") as route
WHERE route->>'target' IN (${Prisma.join(targetIPs)})
)
AND "nwid" != ${input.nwid};
`;
} else {
// Handle the case when targetIPs is empty
duplicateRoutes = [];
}

// Extract duplicated IPs
const duplicatedIPs = duplicateRoutes.flatMap((network) =>
Expand Down

0 comments on commit 03c6ebc

Please sign in to comment.