Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/host-service/src/trpc/router/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ let cachedOrganization: {

async function getOrganization(
api: ApiClient,
organizationId: string,
): Promise<{ id: string; name: string; slug: string }> {
if (
cachedOrganization &&
cachedOrganization.data.id === organizationId &&
Date.now() - cachedOrganization.cachedAt < ORGANIZATION_CACHE_TTL_MS
) {
return cachedOrganization.data;
}

const organization = await api.organization.getActiveFromJwt.query();
const organization = await api.organization.getByIdFromJwt.query({
id: organizationId,
});
if (!organization) {
throw new TRPCError({
code: "PRECONDITION_FAILED",
message: "No active organization",
message: "Organization not found or not accessible from JWT",
});
}

Expand All @@ -36,8 +40,7 @@ async function getOrganization(

export const hostRouter = router({
info: protectedProcedure.query(async ({ ctx }) => {
const api = (ctx as { api: ApiClient }).api;
const organization = await getOrganization(api);
const organization = await getOrganization(ctx.api, ctx.organizationId);

return {
hostId: getHashedDeviceId(),
Expand Down
20 changes: 20 additions & 0 deletions packages/trpc/src/router/organization/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ export const organizationRouter = {
return org ?? null;
}),

getByIdFromJwt: jwtProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
if (!ctx.organizationIds.includes(input.id)) return null;

const membership = await db.query.members.findFirst({
where: and(
eq(members.userId, ctx.userId),
eq(members.organizationId, input.id),
),
});
if (!membership) return null;

const org = await db.query.organizations.findFirst({
where: eq(organizations.id, input.id),
columns: { id: true, name: true, slug: true },
});
return org ?? null;
}),
Comment on lines +95 to +113
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Two sequential DB round-trips can be collapsed into one

getByIdFromJwt first queries members to verify membership exists, then queries organizations to fetch the org row. These are two separate database round-trips even though both conditions must be satisfied. A single join query would retrieve the org data while simultaneously confirming membership:

const result = await db
  .select({ id: organizations.id, name: organizations.name, slug: organizations.slug })
  .from(members)
  .innerJoin(organizations, eq(organizations.id, members.organizationId))
  .where(
    and(
      eq(members.userId, ctx.userId),
      eq(members.organizationId, input.id),
    ),
  )
  .limit(1);

return result[0] ?? null;

This mirrors the pattern used in getActive on the session-based path and halves the DB round-trips for every tray refresh.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/trpc/src/router/organization/organization.ts
Line: 95-113

Comment:
**Two sequential DB round-trips can be collapsed into one**

`getByIdFromJwt` first queries `members` to verify membership exists, then queries `organizations` to fetch the org row. These are two separate database round-trips even though both conditions must be satisfied. A single join query would retrieve the org data while simultaneously confirming membership:

```ts
const result = await db
  .select({ id: organizations.id, name: organizations.name, slug: organizations.slug })
  .from(members)
  .innerJoin(organizations, eq(organizations.id, members.organizationId))
  .where(
    and(
      eq(members.userId, ctx.userId),
      eq(members.organizationId, input.id),
    ),
  )
  .limit(1);

return result[0] ?? null;
```

This mirrors the pattern used in `getActive` on the session-based path and halves the DB round-trips for every tray refresh.

How can I resolve this? If you propose a fix, please make it concise.


getInvitation: protectedProcedure
.input(z.uuid())
.query(async ({ ctx, input }) => {
Expand Down
Loading