File

src/app/modules/core/services/tenant/tenant.service.ts

Description

This service returns the organization details.

Extends

DataService

Index

Properties
Methods
Accessors

Constructor

constructor(http: HttpClient, config: ConfigService, cacheService: CacheService, learnerService: LearnerService, userService: UserService)

The constructor

Parameters :
Name Type Optional Description
http HttpClient No

Reference of HttpClient.

config ConfigService No

Reference of ConfigService.

cacheService CacheService No
learnerService LearnerService No
userService UserService No

Methods

Public getSlugDefaultTenantInfo
getSlugDefaultTenantInfo(slug)
Parameters :
Name Optional
slug No
Returns : any
Public getTenantConfig
getTenantConfig(slug: string)
Parameters :
Name Type Optional
slug string No
Returns : any
Public getTenantInfo
getTenantInfo(slug?: string)

API call to gather organization details.

Parameters :
Name Type Optional Description
slug string Yes

Organization details passed from main-header component.

Returns : void
Public initialize
initialize()
Returns : void
Public setTenantSettings
setTenantSettings(settingsResponse)
Parameters :
Name Optional
settingsResponse No
Returns : void
delete
delete(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:161

for making delete api calls

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

get
get(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:82

for making get api calls

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

Private getDateDiff
getDateDiff(serverdate)
Inherited from DataService
Defined in DataService:234
Parameters :
Name Optional
serverdate No
Returns : number
Private getHeader
getHeader(headers?)
Inherited from DataService
Defined in DataService:196

for preparing headers

Parameters :
Name Optional
headers Yes
getWithHeaders
getWithHeaders(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:60

for making get api calls which needs headers in response headers are fetched to get server time using Date attribute in header

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

patch
patch(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:143

for making patch api calls

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

post
post(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:123

for making post api calls

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

postWithHeaders
postWithHeaders(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:102

for making post api calls with headers in response object

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

Returns : Observable<any>
put
put(requestParam: RequestParam)
Inherited from DataService
Defined in DataService:180

for making PUT api calls

Parameters :
Name Type Optional Description
requestParam RequestParam No

interface

Properties

Private _defaultTenant
Type : string
Default value : ''

Variable that holds default tenant value

_tenantData$
Default value : new BehaviorSubject<ITenantInfo>(undefined)

BehaviorSubject containing tenant data.

_tenantSettings$
Default value : new BehaviorSubject<ITenantSettings>(undefined)

BehaviorSubject containing tenant settings data.

config
Type : ConfigService

reference of config service.

favicon
Type : string

This variable holds favicon details.

http
Type : HttpClient
Inherited from DataService
Defined in DataService:42

reference of http.

slugForIgot
Type : string
Default value : ''
tenantData
Type : ITenantData

Variable that holds tenant details.

Public Readonly tenantData$
Type : Observable<ITenantInfo>
Default value : this._tenantData$.asObservable() .pipe(skipWhile(data => data === undefined || data === null))

Read only observable containing tenant data.

Public Readonly tenantSettings$
Type : Observable<ITenantSettings>
Default value : this._tenantSettings$.asObservable() .pipe(skipWhile(data => data === undefined || data === null))

Read only observable containing tenant settings data.

appId
Type : string
Inherited from DataService
Defined in DataService:31

Contains appId

appVersion
Type : string
Inherited from DataService
Defined in DataService:48

Constructor

Parameters :
Name Description
http

HttpClient reference

baseUrl
Type : string
Inherited from DataService
Defined in DataService:39

Contains base Url for api end points

channelId
Type : string
Inherited from DataService
Defined in DataService:27

Contains channel Id

deviceId
Type : string
Inherited from DataService
Defined in DataService:35

Contains devoce Id

rootOrgId
Type : string
Inherited from DataService
Defined in DataService:23

Contains rootOrg Id

Static sessionId
Type : string
Inherited from DataService
Defined in DataService:19
Static userId
Type : string
Inherited from DataService
Defined in DataService:18

Accessors

defaultTenant
getdefaultTenant()
import { ITenantData, ITenantInfo, ITenantSettings } from './interfaces';
import { ConfigService, ServerResponse } from '@sunbird/shared';
import { BehaviorSubject ,  Observable, of, iif, combineLatest } from 'rxjs';
import { DataService } from '../data/data.service';
import { LearnerService } from './../learner/learner.service';
import { UserService } from './../user/user.service';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { skipWhile, map, catchError, mergeMap, tap } from 'rxjs/operators';
import { CacheService } from '../../../shared/services/cache-service/cache.service';
import * as _ from 'lodash-es';

/**
 * This service returns the organization details.
 */

@Injectable({
  providedIn: 'root'
})
export class TenantService extends DataService {
  /**
   * BehaviorSubject containing tenant data.
   */
  _tenantData$ = new BehaviorSubject<ITenantInfo>(undefined);
  /**
   * BehaviorSubject containing tenant settings data.
   */
  _tenantSettings$ = new BehaviorSubject<ITenantSettings>(undefined);
  /**
   * Read only observable containing tenant data.
   */
  public readonly tenantData$: Observable<ITenantInfo> = this._tenantData$.asObservable()
  .pipe(skipWhile(data => data === undefined || data === null));
  /**
   * Read only observable containing tenant settings data.
   */
  public readonly tenantSettings$: Observable<ITenantSettings> = this._tenantSettings$.asObservable()
  .pipe(skipWhile(data => data === undefined || data === null));
  /**
   * reference of http.
   */
  http: HttpClient;
  /**
   * reference of config service.
   */
  config: ConfigService;
  /**
   * Variable that holds tenant details.
   */
  tenantData: ITenantData;
  /**
   * This variable holds favicon details.
   */
  favicon: string;
  /**
   * Variable that holds default tenant value
   */
  private _defaultTenant = '';
  // TODO refactor the igot specific changes
  slugForIgot = '';
  /**
   * The constructor
   * @param {HttpClient} http Reference of HttpClient.
   * @param {ConfigService} config Reference of ConfigService.
   */
  constructor(http: HttpClient, config: ConfigService, private cacheService: CacheService,
    private learnerService: LearnerService, private userService: UserService) {
    super(http);
    this.config = config;
    this.baseUrl = this.config.urlConFig.URLS.TENANT_PREFIX;
    this._defaultTenant = (<HTMLInputElement>document.getElementById('defaultTenant'))
      ? (<HTMLInputElement>document.getElementById('defaultTenant')).value : null;
    this.slugForIgot = (<HTMLInputElement>document.getElementById('slugForProminentFilter')) ?
      (<HTMLInputElement>document.getElementById('slugForProminentFilter')).value : null;
  }

  /**
   * @returns default tenant value
   */
  get defaultTenant() {
    return this._defaultTenant;
  }

  public initialize() {
    if (this.cacheService.exists('orgSettings')) {
      const orgSettings = this.cacheService.get('orgSettings');
      const slug = this.userService.slug !== '' ? this.userService.slug : this._defaultTenant;
      if (orgSettings.id === slug) {
        this._tenantSettings$.next(JSON.parse(orgSettings.value));
      } else {
        this.getSlugDefaultTenantInfo(this.userService.slug).subscribe();
      }
    } else {
      this.getSlugDefaultTenantInfo(this.userService.slug).subscribe();
    }
  }

  /**
   * API call to gather organization details.
   * @param slug Organization details passed from main-header component.
   */
  public getTenantInfo(slug?: string) {
    const orgDetailsFromSlug = this.cacheService.get('orgDetailsFromSlug');
    // TODO: to rework igot.
    if (_.get(orgDetailsFromSlug, 'slug')) {
      slug = _.get(orgDetailsFromSlug, 'slug');
    }
    const url = `${this.config.urlConFig.URLS.TENANT.INFO + '/'}` + (slug ? slug : '');
    this.get({ url }).subscribe(
      (apiResponse: ServerResponse) => {
        this.tenantData = apiResponse.result;
        this._tenantData$.next({ err: null, tenantData: apiResponse.result });
      },
      (err: ServerResponse) => {
        this._tenantData$.next({ err: err, tenantData: undefined });
      }
    );
  }

  public getTenantConfig(slug: string) {
    if ((slug && slug !== '') && slug === this.slugForIgot) {
      slug = this.slugForIgot;
    } else {
      slug = this._defaultTenant;
    }
    const url = `${this.config.urlConFig.URLS.SYSTEM_SETTING.TENANT_CONFIG + '/'}` + slug;
    return this.learnerService.get({
      url: url
    }).pipe(map((data: ServerResponse) => {
      if (_.has(data, 'result.response')) {
        let configResponse = {};
        try {
          if (JSON.parse(data.result.response.value)) { configResponse = data; }
        } catch (parseJSONResponse) {
          // console.error('org settings parse error => ', parseJSONResponse);
        }
        return configResponse;
      } else {
        return {};
      }
    }), catchError((error) => {
      return of({});
    }));
  }

  public getSlugDefaultTenantInfo(slug) {
    return combineLatest([this.getTenantConfig(slug)])
    .pipe(
      mergeMap(([data]) => {
        return iif(() => _.isEmpty(data), this.getTenantConfig(this._defaultTenant), of(data));
      }),
      catchError(err => {
        console.error(err);
        return of({});
      }),
      tap(data => {
        this.setTenantSettings(data);
      })
    );
  }

  public setTenantSettings(settingsResponse) {
    const data = _.get(settingsResponse.result, 'response');
    this.cacheService.set('orgSettings', data , {
      maxAge: 86400
    });
    if (data) {
      this._tenantSettings$.next(JSON.parse(settingsResponse.result.response.value));
    }
  }
}

results matching ""

    No results matching ""