Skip to content

Commit

Permalink
Added support for extracting subdomain name from multi-level subdomains
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepsuvit committed Sep 9, 2020
1 parent 0d4cfd8 commit 1d9aa8e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

[Mongoose](http://mongoosejs.com/) multitenancy module for [Nest](https://github.com/nestjs/nest).

### Note
Currently supported only with Nest Express adaptor.

## Installation

```bash
Expand Down
31 changes: 23 additions & 8 deletions lib/tenancy-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicModule, Global, HttpException, HttpStatus, Module, OnApplicationShutdown, Provider, Scope } from '@nestjs/common';
import { BadRequestException, DynamicModule, Global, Module, OnApplicationShutdown, Provider, Scope } from '@nestjs/common';
import { Type } from '@nestjs/common/interfaces';
import { ModuleRef, REQUEST } from '@nestjs/core';
import { Request } from 'express';
Expand Down Expand Up @@ -162,7 +162,7 @@ export class TenancyCoreModule implements OnApplicationShutdown {
let tenantId = '';

if (!moduleOptions) {
throw new HttpException(`Tenant options are mandatory`, HttpStatus.BAD_REQUEST);
throw new BadRequestException(`Tenant options are mandatory`);
}

// Extract the tenant idetifier
Expand All @@ -173,24 +173,27 @@ export class TenancyCoreModule implements OnApplicationShutdown {

// Pull the tenant id from the subdomain
if (isTenantFromSubdomain) {
tenantId = req.subdomains[0] || '';
// Check for multi-level subdomains and return only the first name
if (req.subdomains instanceof Array && req.subdomains.length > 0) {
tenantId = req.subdomains[req.subdomains.length - 1];
}

// Validate if tenant identifier token is present
if (tenantId === '') {
throw new HttpException(`Tenant ID is mandatory`, HttpStatus.BAD_REQUEST);
if (this.isEmpty(tenantId)) {
throw new BadRequestException(`Tenant ID is mandatory`);
}
} else {
// Validate if tenant identifier token is present
if (!tenantIdentifier) {
throw new HttpException(`${tenantIdentifier} is mandatory`, HttpStatus.BAD_REQUEST);
throw new BadRequestException(`${tenantIdentifier} is mandatory`);
}

// Get the tenant id from the request
tenantId = req.get(`${tenantIdentifier}`) || '';

// Validate if tenant id is present
if (tenantId === '') {
throw new HttpException(`${tenantIdentifier} is not supplied`, HttpStatus.BAD_REQUEST);
if (this.isEmpty(tenantId)) {
throw new BadRequestException(`${tenantIdentifier} is not supplied`);
}
}

Expand Down Expand Up @@ -358,4 +361,16 @@ export class TenancyCoreModule implements OnApplicationShutdown {
inject,
};
}

/**
* Check if the object is empty or not
*
* @private
* @param {*} obj
* @returns
* @memberof TenancyCoreModule
*/
private static isEmpty(obj: any) {
return !obj || !Object.keys(obj).some(x => obj[x] !== void 0);
}
}

0 comments on commit 1d9aa8e

Please sign in to comment.