Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Use subquery for org membership check

Richie McIlroyd597c0cf6750
diff --git a/apps/web/app/api/desktop/[...route]/root.ts b/apps/web/app/api/desktop/[...route]/root.ts
index bc191642d..955aa7041 100644
--- a/apps/web/app/api/desktop/[...route]/root.ts
+++ b/apps/web/app/api/desktop/[...route]/root.ts
@@ -10,7 +10,7 @@ import {
import { buildEnv, serverEnv } from "@cap/env";
import { stripe, userIsPro } from "@cap/utils";
import { zValidator } from "@hono/zod-validator";
-import { and, eq, isNull, or } from "drizzle-orm";
+import { and, eq, inArray, isNull, or } from "drizzle-orm";
import { Hono } from "hono";
import { PostHog } from "posthog-node";
import type Stripe from "stripe";
@@ -363,6 +363,11 @@ app.get("/plan", withAuth, async (c) => {
app.get("/organizations", withAuth, async (c) => {
const user = c.get("user");
+ const memberOrgIds = db()
+ .select({ id: organizationMembers.organizationId })
+ .from(organizationMembers)
+ .where(eq(organizationMembers.userId, user.id));
+
const orgs = await db()
.select({
id: organizations.id,
@@ -370,20 +375,15 @@ app.get("/organizations", withAuth, async (c) => {
ownerId: organizations.ownerId,
})
.from(organizations)
- .leftJoin(
- organizationMembers,
- eq(organizations.id, organizationMembers.organizationId),
- )
.where(
and(
isNull(organizations.tombstoneAt),
or(
eq(organizations.ownerId, user.id),
- eq(organizationMembers.userId, user.id),
+ inArray(organizations.id, memberOrgIds),
),
),
- )
- .groupBy(organizations.id);
+ );
return c.json(orgs);
});