Project

General

Profile

1
import {Injectable} from "@angular/core";
2
import {HttpClient} from "@angular/common/http";
3
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
4

    
5
import {UserNotificationsRights} from './userNotificationsRights';
6
import {CustomOptions} from '../../openaireLibrary/services/servicesUtils/customOptions.class';
7
import {map} from "rxjs/operators";
8

    
9
@Injectable()
10
export class ManageUserNotificationsService {
11

    
12
    constructor(private http: HttpClient) {
13
    }
14

    
15
    getUserNotifications(properties: EnvProperties, pid: string, email: string) {
16
      let url: string = properties.adminToolsAPIURL +  properties.adminToolsPortalType + '/'+ pid + '/notifications';
17
        return this.http.get(url)//.map(res => <any> res.json())
18
                                 .pipe(map(res => this.parseUserNotifications(res, email)));
19
    }
20

    
21
    updateUserNotifications(properties: EnvProperties, pid: string, userNotificationsRights: any) {
22
        //let headers = new Headers({'Content-Type': 'application/json'});
23
        //let options = new RequestOptions({headers: headers});
24
        let body = JSON.stringify(userNotificationsRights);
25
        let url: string = properties.adminToolsAPIURL +  properties.adminToolsPortalType + '/' + pid + '/notifications';
26
        return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody());
27
                        //.do(request => console.log("Insert Response:"+request.status));
28
    }
29

    
30
    parseUserNotifications(data:any, email:string): UserNotificationsRights {
31

    
32
        var notificationRights: UserNotificationsRights = new UserNotificationsRights();
33

    
34
        for (let i = 0; i < data.length; i++) {
35
            if (email == data[i].managerEmail) {
36
                notificationRights['notifyForNewManagers'] = data[i].notifyForNewManagers;
37
                notificationRights['notifyForNewSubscribers'] = data[i].notifyForNewSubscribers;
38
                notificationRights['managerEmail'] = data[i].managerEmail;
39
            }
40
        }
41
        return notificationRights;
42
    }
43
  }
(5-5/6)