File

src/app/pages/services/config.service.ts

Description

Config Service

Index

Methods

Constructor

constructor(http: HttpClient, toastService: ToastService)
Parameters :
Name Type Optional
http HttpClient No
toastService ToastService No

Methods

Async getConfigByProjectIdClientId
getConfigByProjectIdClientId(projectId, clientId)
Parameters :
Name Optional
projectId No
clientId No
Returns : {}
Private getConfigByProjectIdClientIdObservable
getConfigByProjectIdClientIdObservable(projectId, clientId)
Parameters :
Name Optional
projectId No
clientId No
Returns : Observable<Config>
Async getGlobalConfigByClientId
getGlobalConfigByClientId(clientId)
Parameters :
Name Optional
clientId No
Returns : {}
Private getGlobalConfigByClientIdObservable
getGlobalConfigByClientIdObservable(clientId)
Parameters :
Name Optional
clientId No
Returns : Observable<Config>
postConfigByProjectIdAndClientId
postConfigByProjectIdAndClientId(projectId, clientId, payload)
Parameters :
Name Optional
projectId No
clientId No
payload No
Returns : any
postConfigByProjectIdAndClientIdAndUserId
postConfigByProjectIdAndClientIdAndUserId(projectId, clientId, userId, payload)
Parameters :
Name Optional
projectId No
clientId No
userId No
payload No
Returns : any
postGlobalConfigByClientId
postGlobalConfigByClientId(clientId, payload)
Parameters :
Name Optional
clientId No
payload No
Returns : any
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Config} from '@app/pages/models/config';
import {ToastService} from '@app/shared/services/toast.service';
import {Observable} from 'rxjs';
import {environment} from "@environments/environment";

/**
 * Config Service
 */
@Injectable({
  providedIn: 'root'
})
export class ConfigService {

  constructor(private http: HttpClient, private toastService: ToastService) { }

  private getGlobalConfigByClientIdObservable(clientId): Observable<Config> {
    return this.http.get<Config>(`${environment.backendUrl}/global/config/${clientId}`);
  }

  async getGlobalConfigByClientId(clientId) {
    return await this.getGlobalConfigByClientIdObservable(clientId).toPromise()
      .then((data: any) => {
        const {config, defaults} = data;
        config.forEach(c => {
          if (defaults) {
            const defaultValues = defaults.filter(d => d.name === c.name && d.scope === 'global');
            if (defaultValues.length > 0) {
              c.default = defaultValues[0].value;
            }
          }
        });
        this.toastService.showSuccess(`Configurations of Application: ${clientId} loaded.`);
        return config;
      })
      .catch(e => {
        this.toastService.showError(e);
        console.log(e);
      })
      .finally(() => {});
  }


  private getConfigByProjectIdClientIdObservable(projectId, clientId): Observable<Config> {
    return this.http.get<Config>(`/api/projects/${projectId}/config/${clientId}`);
  }

  async getConfigByProjectIdClientId(projectId, clientId) {
    return await this.getConfigByProjectIdClientIdObservable(projectId, clientId).toPromise()
      .then((data: any) => {
        const {config, defaults} = data;
        config.forEach(c => {
          if (defaults) {
            const defaultValues = defaults.filter(d => d.name === c.name && d.scope === 'global');
            if (defaultValues.length > 0) {
              c.default = defaultValues[0].value;
            }
          }
        });
        this.toastService.showSuccess(`Configurations of Project: ${projectId} - Application: ${clientId} loaded.`);
        return config;
      })
      .catch(e => {
        this.toastService.showError(e);
        console.log(e);
      })
      .finally();
  }

  // private getConfigByProjectIdClientIdUserIdObservable(projectId, clientId, userId) {}
  // getConfigByProjectIdClientIdUserId(projectId, clientId, userId) {}



  postGlobalConfigByClientId(clientId, payload) {
    return this.http.post(`/api/global/config/${clientId}`, payload, {
      headers: {
        'Content-Type': 'application/json'
      }
    });
  }

  postConfigByProjectIdAndClientId(projectId, clientId, payload) {
    return this.http.post(`/api/projects/${projectId}/config/${clientId}`, payload, {
      headers: {
        'Content-Type': 'application/json'
      }
    });
  }

  postConfigByProjectIdAndClientIdAndUserId(projectId, clientId, userId, payload) {
    return this.http.post(`/api/projects/${projectId}/users/${userId}/config/${clientId}`, payload, {
      headers: {
        'Content-Type': 'application/json'
      }
    });
  }
}

result-matching ""

    No results matching ""