calcom/cal.diy · error · UnauthorizedException

GetOrgId decorator: Organization ID not found

Error message

GetOrgId decorator: Organization ID not found

What it means

UnauthorizedException from the @GetOrgId() param decorator when request.organizationId is falsy at the time the route handler resolves. The decorator expects a prior middleware/guard to have attached organizationId onto the request; if none did, no org-scoped route can run. This is a wiring/config error, not a user-input error.

Source

Thrown at apps/api/v2/src/modules/auth/decorators/get-org-id/get-org-id.decorator.ts:8

import { createParamDecorator, ExecutionContext, UnauthorizedException } from "@nestjs/common";

export const GetOrgId = createParamDecorator((_: unknown, ctx: ExecutionContext): number => {
  const request = ctx.switchToHttp().getRequest();
  const organizationId = request.organizationId;

  if (!organizationId) {
    throw new UnauthorizedException("GetOrgId decorator: Organization ID not found");
  }

  return organizationId;
});

View on GitHub (pinned to 176037d0af)

Solutions

  1. Ensure the controller/module registers the guard or middleware that sets req.organizationId (e.g. OrganizationGuard / ParseOrgIdMiddleware) before the handler runs.
  2. Confirm the client is sending the org-identifying header/param the middleware reads (e.g. cal-api-version, org slug, or path orgId).
  3. In tests, mock req.organizationId on the ExecutionContext before invoking the handler.

Example fix

// before — handler runs with no org guard
@Get('members')
as members(@GetOrgId() orgId: number) { ... }

// after — ensure the guard populates request.organizationId first
@UseGuards(OrganizationGuard)
@Get('members')
as members(@GetOrgId() orgId: number) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// In tests or middleware stacks, ensure req.organizationId is set
const orgId = request.organizationId;
if (!orgId) throw new UnauthorizedException('GetOrgId decorator: Organization ID not found');

Type guard

function hasOrgIdOnRequest(req: unknown): req is { organizationId: number } {
  return typeof req === 'object' && req !== null &&
    typeof (req as any).organizationId === 'number' && (req as any).organizationId > 0;
}

Prevention

When it happens

Trigger: Hitting any controller method that uses @GetOrgId() without an upstream OrganizationGuard/middleware (e.g. ParseOrgIdMiddleware) that sets req.organizationId — typically because the route module omits the guard, the org id header/param is missing, or the guard ran but set nothing.

Common situations: New route added with @GetOrgId() but the APP_GUARD or per-controller guard that populates organizationId isn't wired; the request path bypasses the org middleware (wrong module scope); test harness invokes the handler directly without simulating the middleware; orgId header was removed.

Related errors


AI-assisted analysis of calcom/cal.diy@176037d0af (2026-08-12). Data as JSON: /api/errors/c5ad3e2def159671. Report an issue: GitHub.