Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response, Headers, RequestOptions} from '@angular/http';
3

    
4
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
5

    
6
import {UserNotificationsRights} from './userNotificationsRights';
7

    
8
@Injectable()
9
export class ManageUserNotificationsService {
10

    
11
    constructor(private http: Http) {
12
    }
13

    
14
    getUserNotifications(url: string, email: string) {
15
        return this.http.get(url).map(res => <any> res.json())
16
                                 .map(res => this.parseUserNotifications(res, email));
17
    }
18

    
19
    updateUserNotifications(url: string, userNotificationsRights: any) {
20
        let headers = new Headers({'Content-Type': 'application/json'});
21
        let options = new RequestOptions({headers: headers});
22
        let body = JSON.stringify(userNotificationsRights);
23

    
24
        console.log(body);
25

    
26
        return this.http.post(url, body, options)
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
  }
(4-4/5)