Browse Source

feat: use createTokenParser in multi tenancy utils

pull/4956/head
Arman Ozak 6 years ago
parent
commit
97ab8dc7d3
  1. 16
      npm/ng-packs/packages/core/src/lib/tests/multi-tenancy-utils.spec.ts
  2. 12
      npm/ng-packs/packages/core/src/lib/utils/multi-tenancy-utils.ts

16
npm/ng-packs/packages/core/src/lib/tests/multi-tenancy-utils.spec.ts

@ -5,7 +5,7 @@ import clone from 'just-clone';
import { BehaviorSubject } from 'rxjs';
import { FindTenantResultDto } from '../models/find-tenant-result-dto';
import { MultiTenancyService } from '../services/multi-tenancy.service';
import { getCurrentTenancyName, parseTenantFromUrl } from '../utils';
import { parseTenantFromUrl } from '../utils';
const environment = {
production: false,
@ -58,20 +58,6 @@ describe('MultiTenancyUtils', () => {
beforeEach(() => (spectator = createComponent()));
describe('#getCurrentTenancyName', () => {
test('should get tenancy name from href', async () => {
setHref('https://abp.volosoft.com/');
expect(getCurrentTenancyName('https://{0}.volosoft.com')).toBe('abp');
setHref('https://volosoft.com/');
expect(getCurrentTenancyName('https://{0}.com')).toBe('volosoft');
setHref('https://volosoft.com/abp/');
expect(getCurrentTenancyName('https://volosoft.com/{0}')).toBe('abp');
expect(getCurrentTenancyName('https://volosoft.com')).toBe(undefined);
});
});
describe('#parseTenantFromUrl', () => {
test('should get the tenancyName, set replaced environment and call the findTenantByName method of MultiTenancyService', async () => {
const injector = spectator.inject(Injector);

12
npm/ng-packs/packages/core/src/lib/utils/multi-tenancy-utils.ts

@ -1,21 +1,21 @@
import { Injector } from '@angular/core';
import { Store } from '@ngxs/store';
import clone from 'just-clone';
import { switchMap, tap } from 'rxjs/operators';
import { SetEnvironment } from '../actions';
import { Config } from '../models/config';
import { MultiTenancyService } from '../services/multi-tenancy.service';
import { ConfigState } from '../states/config.state';
import clone from 'just-clone';
import { createTokenParser } from './string-utils';
const tenancyPlaceholder = '{0}';
export function getCurrentTenancyName(appBaseUrl: string): string {
function getCurrentTenancyName(appBaseUrl: string): string {
if (appBaseUrl.charAt(appBaseUrl.length - 1) !== '/') appBaseUrl += '/';
const currentRootAddress = window.location.href;
const regex = appBaseUrl.replace(/\./g, '.').replace(tenancyPlaceholder, '(.+)');
return (currentRootAddress.match(regex) || []).slice(1)[0];
const parseTokens = createTokenParser(appBaseUrl);
const token = tenancyPlaceholder.replace(/[}{]/g, '');
return parseTokens(window.location.href)[token][0];
}
export async function parseTenantFromUrl(injector: Injector) {

Loading…
Cancel
Save