diff --git a/packages/host-service/src/trpc/router/host/host.ts b/packages/host-service/src/trpc/router/host/host.ts index ca3ec0f757a..922854f0c92 100644 --- a/packages/host-service/src/trpc/router/host/host.ts +++ b/packages/host-service/src/trpc/router/host/host.ts @@ -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", }); } @@ -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(), diff --git a/packages/trpc/src/router/organization/organization.ts b/packages/trpc/src/router/organization/organization.ts index 9258916ae7d..a99e1518f2c 100644 --- a/packages/trpc/src/router/organization/organization.ts +++ b/packages/trpc/src/router/organization/organization.ts @@ -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; + }), + getInvitation: protectedProcedure .input(z.uuid()) .query(async ({ ctx, input }) => {