Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import {Injectable} from '@angular/core';
5
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
6
import {Observable, throwError} from 'rxjs';
7
import {Page} from "../utils/entities/adminTool/page";
8
import {PageHelpContent} from "../utils/entities/adminTool/page-help-content";
9
import {Portal} from "../utils/entities/adminTool/portal";
10
import {Entity} from "../utils/entities/adminTool/entity";
11
import {DivId} from "../utils/entities/adminTool/divId";
12
import {DivHelpContent} from "../utils/entities/adminTool/div-help-content";
13
import {StatisticsDisplay, StatisticsSummary} from '../connect/statistics/statisticsEntities';
14
import {CustomOptions} from './servicesUtils/customOptions.class';
15
import {catchError, map} from "rxjs/operators";
16
import {properties} from "../../../environments/environment";
17

    
18
@Injectable()
19
export class HelpContentService {
20

    
21
  constructor(private http:HttpClient) {
22
  }
23

    
24
  static removeNulls(obj){
25
    var isArray = obj instanceof Array;
26
    for (var k in obj){
27
      if (obj[k]===null || obj[k]==='') isArray ? obj.splice(k,1) : delete obj[k];
28
      else if (typeof obj[k]=="object") HelpContentService.removeNulls(obj[k]);
29
    }
30
  }
31

    
32
  getEntities(helpContentUrl:string) {
33
    return this.http.get<Array<Entity>>(helpContentUrl + 'entity')
34
      .pipe(catchError(this.handleError));
35
  }
36

    
37
  saveEntity(entity: Entity, helpContentUrl:string) {
38
    HelpContentService.removeNulls(entity);
39

    
40
    return this.http.post<Entity>(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
41
      .pipe(catchError(this.handleError));
42
  }
43

    
44
  updateEntity(entity: Entity, helpContentUrl:string) {
45
    HelpContentService.removeNulls(entity);
46

    
47
    return this.http.post<Entity>(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
48
      .pipe(catchError(this.handleError));
49
  }
50

    
51
  deleteEntities(ids : string[], helpContentUrl:string) {
52
    return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
53
      .pipe(catchError(this.handleError));
54
  }
55

    
56
  // toggleEntity(selectedCommunityId: string, id : string,status : boolean) {
57
  //     let headers = new Headers({'Content-Type': 'application/json'});
58
  //     let options = new RequestOptions({headers: headers});
59
  //
60
  //     return this.http.post(helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options)
61
  //         .catch(this.handleError);
62
  // }
63

    
64
  getCommunityEntities(pid: string, helpContentUrl:string) {
65
    return this.http.get<Array<Entity>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/entities')
66
      .pipe(catchError(this.handleError));
67
  }
68

    
69
  toggleEntities(pid: string, ids : string[],status : boolean, helpContentUrl:string) {
70

    
71
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+pid+ '/entity/toggle?status='+ status.toString(),
72
      JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
73
      .pipe(catchError(this.handleError));
74
  }
75

    
76
  // unused
77
  toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
78
    return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(),
79
      CustomOptions.getAuthOptionsWithBody())
80
      .pipe(catchError(this.handleError));
81
  }
82

    
83
  // getDivIdsFull(page_id: string, helpContentUrl:string, pid: string = null) {
84
  //   let parameters: string = "";
85
  //   if(page_id || pid) {
86
  //     parameters = "?";
87
  //     if(page_id) {
88
  //       parameters += "&page="+page_id;
89
  //     }
90
  //     if(pid) {
91
  //       parameters += "&portal="+pid;
92
  //     }
93
  //   }
94
  //
95
  //   return this.http.get<Array<DivId>>(helpContentUrl + 'div/full'+parameters)
96
  //       //.map(res => <Array<DivId>> res.json())
97
  //       .pipe(catchError(this.handleError));
98
  // }
99

    
100
  // Replacing getDivIdsFull
101
  getAllDivIdsFull(helpContentUrl:string) {
102
    return this.http.get<Array<DivId>>(helpContentUrl + 'div/full')
103
    //.map(res => <Array<DivId>> res.json())
104
      .pipe(catchError(this.handleError));
105
  }
106

    
107
  getDivIdsFullByPortal(page_id: string, helpContentUrl:string, pid: string) {
108
    let parameters: string = page_id ? "?&page="+page_id : "";
109

    
110
    return this.http.get<Array<DivId>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid + '/div/full'+parameters)
111
      .pipe(catchError(this.handleError));
112
  }
113
  // End of replacing getDivIdsFull
114

    
115
  // unused
116
  getDivId(divId: string, helpContentUrl:string) {
117
    return this.http.get<DivId>(helpContentUrl + 'div/'+divId)
118
      .pipe(catchError(this.handleError));
119
  }
120

    
121
  getDivIdFull(divId: string, helpContentUrl:string, pid: string) {
122
    return this.http.get<DivId>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/div/'+divId+"/full")
123
      .pipe(catchError(this.handleError));
124
  }
125

    
126
  updateDivId(divId: DivId, helpContentUrl:string) {
127
    HelpContentService.removeNulls(divId);
128

    
129
    return this.http.post<DivId>(helpContentUrl + 'div/update', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
130
      .pipe(catchError(this.handleError));
131
  }
132

    
133
  saveDivId(divId: DivId, helpContentUrl:string) {
134
    HelpContentService.removeNulls(divId);
135

    
136
    return this.http.post<DivId>(helpContentUrl + 'div/save', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
137
      .pipe(catchError(this.handleError));
138
  }
139

    
140
  deleteDivIds(ids : string[], helpContentUrl:string) {
141
    return this.http.post(helpContentUrl + 'div/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
142
      .pipe(catchError(this.handleError));
143
  }
144

    
145
  getPageIdsFromDivIds(pid: string, helpContentUrl:string) {
146
    return this.http.get<Array<string>>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/div/pages')
147
      .pipe(catchError(this.handleError));
148
  }
149

    
150
  getCommunityDivHelpContents(pid: string, helpContentUrl:string, pageId:string = null) {
151
    return this.http.get<Array<DivHelpContent>>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + (pageId ? '/' + pageId : '') + '/divhelpcontent')
152
      .pipe(catchError(this.handleError));
153
  }
154

    
155
  getDivHelpContent(id : string, helpContentUrl:string, pid: string) {
156
    return this.http.get<DivHelpContent>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/' + id)
157
      .pipe(catchError(this.handleError));
158
  }
159

    
160
  insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) {
161
    HelpContentService.removeNulls(divHelpContent);
162
    return this.http.post<DivHelpContent>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/' + (divHelpContent._id ? 'update' : 'save'),
163
      JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
164
      .pipe(catchError(this.handleError));
165
  }
166

    
167

    
168
  deleteDivHelpContents(ids : string[], helpContentUrl:string, pid: string) {
169
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/delete',
170
      JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
171
      .pipe(catchError(this.handleError));
172
  }
173

    
174
  toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) {
175

    
176
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/divhelpcontent/toggle?status='+ status.toString(),
177
      JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
178
      .pipe(catchError(this.handleError));
179
  }
180

    
181
  // unused
182
  getPageHelpContents(helpContentUrl:string) {
183
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
184
      .pipe(catchError(this.handleError));
185
  }
186

    
187

    
188
  getCommunityPageHelpContents(pid: string, helpContentUrl:string, pageId:string = null) {
189
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + (pageId ? '/' + pageId : '') + '/pagehelpcontent')
190
      .pipe(catchError(this.handleError));
191
  }
192
  countCommunityPageHelpContents(pid: string, helpContentUrl:string ,classContents:boolean=false) {
193
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + properties.adminToolsPortalType + '/' + pid  + (classContents?"/divhelpcontent":"/pagehelpcontent")+'/page/count')
194
      .pipe(catchError(this.handleError));
195
  }
196
  getPageHelpContent(id : string, helpContentUrl:string, pid: string) {
197
    return this.http.get<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/' + id)
198
    //.map(res => <PageHelpContent> res.json())
199
      .pipe(catchError(this.handleError));
200
  }
201

    
202
  savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) {
203
    HelpContentService.removeNulls(pageHelpContent);
204

    
205
    return this.http.post<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/' + (pageHelpContent._id ? 'update' : 'save'),
206
      JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
207
      .pipe(catchError(this.handleError));
208
  }
209

    
210
  updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) {
211
    HelpContentService.removeNulls(pageHelpContent);
212

    
213
    return this.http.post<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid +'/pagehelpcontent/update',
214
      JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
215
      .pipe(catchError(this.handleError));
216
  }
217

    
218
  deletePageHelpContents(ids : string[], helpContentUrl:string, pid: string) {
219
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/delete',
220
      JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
221
      .pipe(catchError(this.handleError));
222
  }
223

    
224
  togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) {
225
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/toggle?status='+ status.toString(),
226
      JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
227
      .pipe(catchError(this.handleError));
228
  }
229

    
230
  getCommunityPagesWithDivId(pid: string, helpContentUrl:string) {
231
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?div=true')
232
      .pipe(catchError(this.handleError));
233
  }
234

    
235
  // getCommunityPages(pid: string, params: string, helpContentUrl:string) {
236
  //   return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'+params, CustomOptions.getAuthOptions())
237
  //   //.map(res => <Array<Page>> res.json())
238
  //     .pipe(catchError(this.handleError));
239
  // }
240

    
241
  // Replacing getCommunityPages
242
  getCommunityPagesByType(pid: string, type: string, helpContentUrl:string) {
243
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'
244
      + (type ? '?page_type='+type : ''))
245
      .pipe(catchError(this.handleError));
246
  }
247
  // End of replacing getCommunityPages
248

    
249
  // Replacing part of getPages (now getAllPages)
250
  getCommunityPagesWithPositions(pid: string, helpContentUrl:string) {
251
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?with_positions=true')
252
      .pipe(catchError(this.handleError));
253
  }
254
  // End of replacing part of getPages (now getAllPages)
255

    
256
  getAllPages(helpContentUrl:string) {//,pid:string,with_positions:boolean=null) {
257
    // let parameters: string = "";
258
    // if(pid || with_positions == true || with_positions == false) {
259
    //   parameters = "?";
260
    //   if(pid) {
261
    //     parameters += "&pid="+pid;
262
    //   }
263
    //   if(with_positions == true || with_positions == false) {
264
    //     parameters += "&with_positions="+with_positions;
265
    //   }
266
    // }
267
    return this.http.get<Array<Page>>(helpContentUrl + 'page')
268
    //.map(res => <Array<Page>> res.json())
269
      .pipe(catchError(this.handleError));
270
  }
271

    
272
  getAllPagesFull(helpContentUrl:string) {
273
    return this.http.get<Array<Page>>(helpContentUrl + 'page/full')//+(pid?("?pid="+pid):""))
274
    //.map(res => <Array<Page>> res.json())
275
      .pipe(catchError(this.handleError));
276
  }
277

    
278
  getPageByPortal(pageId:string, helpContentUrl:string, pid: string) {
279
    return this.http.get<Page>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/page/'+pageId)
280
      .pipe(catchError(this.handleError));
281
  }
282

    
283
  getCommunityPageByRoute(route:string, helpContentUrl:string, pid: string) {
284
    return this.http.get<Page>(helpContentUrl + properties.adminToolsPortalType +'/' + pid + '/page/?page_route='+route)
285
      .pipe(catchError(this.handleError));
286
  }
287

    
288
  savePage(page: Page, helpContentUrl:string) {
289
    HelpContentService.removeNulls(page);
290

    
291
    return this.http.post<Page>(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
292
    //.map(res => <Page> res.json())
293
      .pipe(catchError(this.handleError));
294
  }
295

    
296
  updatePage(page: Page, helpContentUrl:string) {
297

    
298
    HelpContentService.removeNulls(page);
299

    
300
    return this.http.post<Page>(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
301
      .pipe(catchError(this.handleError));
302
  }
303

    
304
  togglePages(selectedPortalPid: string, ids : string[],status : boolean, helpContentUrl:string) {
305

    
306
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+selectedPortalPid+'/page/toggle?status='+ status.toString(),
307
      JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
308
      .pipe(catchError(this.handleError));
309
  }
310

    
311
  deletePages(ids : string[], helpContentUrl:string) {
312

    
313
    return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
314
      .pipe(catchError(this.handleError));
315
  }
316

    
317
  // unused
318
  getCommunities( helpContentUrl:string) {
319
    return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType)
320
      .pipe(catchError(this.handleError));
321
  }
322

    
323
  getCommunity(pid: string, helpContentUrl:string) {
324
    return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+pid)
325
      .pipe(catchError(this.handleError));
326
  }
327

    
328
  getCommunitiesFull( helpContentUrl:string) {
329
    return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType + '/full')
330
      .pipe(catchError(this.handleError));
331
  }
332

    
333
  getPortalsFull( helpContentUrl:string) {
334
    return this.http.get<Array<Portal>>(helpContentUrl +  'portal/full')
335
    //.map(res => <Array<Portal>> res.json())
336
      .pipe(catchError(this.handleError));
337
  }
338
  getCommunityFull(portal_pid: string, helpContentUrl:string) {
339
    return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid+'/full')
340
      .pipe(catchError(this.handleError));
341
  }
342

    
343
  saveCommunity(portal: Portal, helpContentUrl:string) {
344
    // let headers = new Headers({'Content-Type': 'application/json'});
345
    // let options = new RequestOptions({headers: headers});
346

    
347
    HelpContentService.removeNulls(portal);
348

    
349
    return this.http.post<Portal>(helpContentUrl + portal.type + '/save', JSON.stringify(portal), CustomOptions.getAuthOptionsWithBody())
350
      .pipe(catchError(this.handleError));
351
  }
352

    
353
  updateCommunity(portal: Portal, helpContentUrl:string) {
354
    // let headers = new Headers({'Content-Type': 'application/json'});
355
    // let options = new RequestOptions({headers: headers});
356

    
357
    HelpContentService.removeNulls(portal);
358

    
359
    return this.http.post<Portal>(helpContentUrl + portal.type + '/update', JSON.stringify(portal),  CustomOptions.getAuthOptionsWithBody())
360
    //.map(res => <Portal> res.json())
361
      .pipe(catchError(this.handleError));
362
  }
363

    
364
  deleteCommunities(ids : string[], helpContentUrl:string) {
365
    // let headers = new Headers({'Content-Type': 'application/json'});
366
    // let options = new RequestOptions({headers: headers});
367

    
368
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
369
      .pipe(catchError(this.handleError));
370
  }
371

    
372
  private handleError(error: HttpErrorResponse) {
373
    // in a real world app, we may send the error to some remote logging infrastructure
374
    // instead of just logging it to the console
375
    console.error(error);
376
    return throwError(error.error || 'Server error');
377
  }
378

    
379
  // getDataProviders() {
380
  //     return this.http.get('https://beta.services.openaire.eu/search/v2/api/datasources?format=json').map(res => <any> res.json()).map(res => res.results).do(res => {console.log(res)}).catch(this.handleError);
381
  // }
382

    
383
  getCommunityStatistics(apiUrl: string, portalId: string): Observable<StatisticsSummary> {
384
    const url = `${apiUrl}communities/${portalId}`;
385
    //console.log(`getting statistics summary from: ${url}`);
386
    return this.http.get(url)
387
    //.map(res => <any>res.json())
388
      .pipe(map(res => res['statistics']));
389
  }
390

    
391
  getCommunityAdminStatisticsChoices(apiUrl: string, portalId: string): Observable<StatisticsDisplay> {
392
    const url = `${apiUrl}statistics/${portalId}`;
393
    //console.log(`getting admin choices for statistics from: ${url}`);
394
    return this.http.get<StatisticsDisplay>(url)
395
    //.map(stats => <StatisticsDisplay>stats.json())
396
      .pipe(catchError(this.handleError));
397
  }
398

    
399
  postCommunityAdminStatisticsChoices(apiUrl: string,
400
                                      portalId: string,
401
                                      entity: string,
402
                                      chartsOrNumbers: string,
403
                                      title: string,
404
                                      status: boolean,
405
                                      monitor: boolean): Observable<any> {
406
    const url = `${apiUrl}statistics/${portalId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
407
    //console.log(`getting admin choices for statistics from: ${url}`);
408

    
409
    return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
410
    //.map(stats => <any>stats.json())
411
      .pipe(catchError(this.handleError));
412
  }
413

    
414
  statisticsIsActiveToggle(apiURL: string, id: string): Observable<boolean> {
415
    const url = apiURL + '/statistics/' + encodeURIComponent(id) + '/toggle';
416
    return this.http.post<boolean>(url, {}, CustomOptions.getAuthOptionsWithBody()).pipe(catchError(this.handleError));
417
  }
418
}
(4-4/23)