# All files / modules/auth/decorators tenant.decorator.ts

15.38% Statements 2/13
0% Branches 0/8
0% Functions 0/1
15.38% Lines 2/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    282x

    2x

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

    export const CurrentTenant = createParamDecorator(
      (data: unknown, ctx: ExecutionContext): string => {
        const request = ctx.switchToHttp().getRequest();

        // Get tenant from user (if authenticated)
        Iif (request.user?.tenant_id) {
          return request.user.tenant_id;
        }

        // Get tenant from header (for public routes)
        const tenantId = request.headers['x-tenant-id'];
        Iif (tenantId) {
          return tenantId as string;
        }

        // Get tenant from subdomain (optional)
        const host = request.headers.host || '';
        const subdomain = host.split('.')[0];
        Iif (subdomain && subdomain !== 'www' && subdomain !== 'api') {
          return subdomain;
        }

        return '';
      },
    );