Project

General

Profile

1
import {Injectable} from "@angular/core";
2
import {HttpClient} from "@angular/common/http";
3
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
4
import {Indicator, Section, Stakeholder, StakeholderInfo, Visibility} from "../entities/stakeholder";
5
import {HelperFunctions} from "../../utils/HelperFunctions.class";
6
import {map} from "rxjs/operators";
7
import {ActivatedRoute} from "@angular/router";
8
import {properties} from "../../../../environments/environment";
9
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
10
import {StringUtils} from "../../utils/string-utils.class";
11

    
12
let maps: string[] = ['parameters', 'filters'];
13

    
14
export interface Reorder {
15
  action: 'moved' | 'added' | 'removed',
16
  target: string,
17
  ids: string[];
18
}
19

    
20
@Injectable({
21
  providedIn: "root"
22
})
23
export class StakeholderService {
24
  
25
  private stakeholderSubject: BehaviorSubject<Stakeholder> = null;
26
  private promise: Promise<any>;
27
  private sub;
28
  
29
  constructor(private http: HttpClient, private route: ActivatedRoute) {
30
    this.stakeholderSubject = new BehaviorSubject<Stakeholder>(null);
31
  }
32
  
33
  ngOnDestroy() {
34
    this.clearSubscriptions();
35
  }
36
  
37
  clearSubscriptions() {
38
    if (this.sub instanceof Subscriber) {
39
      this.sub.unsubscribe();
40
    }
41
  }
42
  
43
  getStakeholder(alias: string): Observable<Stakeholder> {
44
    if (!this.stakeholderSubject.value || this.stakeholderSubject.value.alias !== alias) {
45
      this.promise = new Promise<any>((resolve, reject) => {
46
        this.sub = this.http.get<Stakeholder>(properties.monitorServiceAPIURL + '/stakeholder/' + encodeURIComponent(alias), CustomOptions.registryOptions()).pipe(map(stakeholder => {
47
          return this.formalize(this.checkIsUpload(stakeholder));
48
        })).subscribe(stakeholder => {
49
          this.stakeholderSubject.next(stakeholder);
50
          resolve();
51
        }, error => {
52
          this.stakeholderSubject.next(null);
53
          resolve();
54
        });
55
      });
56
    }
57
    return from(this.getStakeholderAsync());
58
  }
59
  
60
  async getStakeholderAsync() {
61
    await this.promise;
62
    this.clearSubscriptions();
63
    return this.stakeholderSubject.getValue();
64
  }
65
  
66
  getAlias(url: string): Observable<string[]> {
67
    return this.http.get<Stakeholder[]>(url + '/stakeholder/alias', CustomOptions.registryOptions()).pipe(map(stakeholders => {
68
      return this.formalize(stakeholders);
69
    }));
70
  }
71
  
72
  getStakeholders(url: string, type: string = null, defaultId: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
73
    return this.http.get<Stakeholder[]>(url + '/stakeholder' + ((type) ? ('?type=' + type) : '') + ((!type && defaultId)? ('?defaultId=' + defaultId):''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
74
      return this.formalize(this.checkIsUpload(stakeholders));
75
    }));
76
  }
77
  
78
  getMyStakeholders(url: string, type: string = null): Observable<Stakeholder[]> {
79
    return this.http.get<Stakeholder[]>(url + '/my-stakeholder' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
80
      return this.formalize(this.checkIsUpload(stakeholders));
81
    }));
82
  }
83
  
84
  getDefaultStakeholders(url: string, type: string = null): Observable<Stakeholder[]> {
85
    return this.http.get<Stakeholder[]>(url + '/stakeholder/default' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
86
      return this.formalize(this.checkIsUpload(stakeholders));
87
    }));
88
  }
89
  
90
  buildStakeholder(url: string, stakeholder: Stakeholder): Observable<Stakeholder> {
91
    return this.http.post<Stakeholder>(url + '/build-stakeholder', stakeholder, CustomOptions.registryOptions()).pipe(map(stakeholder => {
92
      return this.formalize(this.checkIsUpload(stakeholder));
93
    }));
94
  }
95
  
96
  changeVisibility(url: string, path: string[], visibility: Visibility): Observable<any> {
97
    return this.http.post<Visibility>(url + '/' + path.join('/') + '/change-visibility' + '?visibility=' + visibility, null, CustomOptions.registryOptions());
98
  }
99
  
100
  saveElement(url: string, element: any, path: string[] = []): Observable<any> {
101
    path = HelperFunctions.encodeArray(path);
102
    return this.http.post<any>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
103
      '/save', element, CustomOptions.registryOptions()).pipe(map(element => {
104
        if(path.length === 0) {
105
          return this.formalize(this.checkIsUpload(element));
106
        } else {
107
          return this.formalize(element);
108
        }
109
    }));
110
  }
111
  
112
  saveSection(url: string, element: any, path: string[] = [], index: number = -1): Observable<Section> {
113
    path = HelperFunctions.encodeArray(path);
114
    return this.http.post<Section>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
115
      '/save/' + index, element, CustomOptions.registryOptions()).pipe(map(element => {
116
      return this.formalize(element);
117
    }));
118
  }
119
  
120
  deleteElement(url: string, path: string[], childrenAction: string = null): Observable<any> {
121
    path = HelperFunctions.encodeArray(path);
122
    let params: string = "";
123
    if (childrenAction) {
124
      params = "?children=" + childrenAction;
125
    }
126
    return this.http.delete<any>(url + '/' + path.join('/') + '/delete' + params, CustomOptions.registryOptions());
127
  }
128
  
129
  reorderIndicators(url: string, path: string[], reorder: Reorder, type: string = 'chart'): Observable<Indicator[]> {
130
    path = HelperFunctions.encodeArray(path);
131
    return this.http.post<Indicator[]>(url + '/' + path.join('/') + '/' + type + '/reorder', reorder, CustomOptions.registryOptions()).pipe(map(indicators => {
132
      return this.formalize(indicators);
133
    }));
134
  }
135
  
136
  getStakeholderAsObservable(): Observable<Stakeholder> {
137
    return this.stakeholderSubject.asObservable();
138
  }
139
  
140
  setStakeholder(stakeholder: Stakeholder) {
141
    this.stakeholderSubject.next(stakeholder);
142
  }
143
  
144
  private checkIsUpload(response: Stakeholder | Stakeholder[]): any | any[] {
145
    if(Array.isArray(response)) {
146
      response.forEach(value => {
147
        value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
148
      });
149
    } else {
150
      response.isUpload = response.logoUrl && !StringUtils.isValidUrl(response.logoUrl);
151
    }
152
    return response;
153
  }
154
  
155
  private formalize(element: any) {
156
    return HelperFunctions.copy(element);
157
  }
158
}
    (1-1/1)