connect-admin/src/app/pages/usernotifications/manage-user-notifications.s...

45 lines
1.7 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {UserNotificationsRights} from './userNotificationsRights';
import {CustomOptions} from '../../openaireLibrary/services/servicesUtils/customOptions.class';
@Injectable()
export class ManageUserNotificationsService {
constructor(private http: Http) {
}
getUserNotifications(url: string, email: string) {
return this.http.get(url).map(res => <any> res.json())
.map(res => this.parseUserNotifications(res, email));
}
updateUserNotifications(url: string, userNotificationsRights: any) {
//let headers = new Headers({'Content-Type': 'application/json'});
//let options = new RequestOptions({headers: headers});
let body = JSON.stringify(userNotificationsRights);
console.log(body);
return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody())
.do(request => console.log("Insert Response:"+request.status));
}
parseUserNotifications(data:any, email:string): UserNotificationsRights {
var notificationRights: UserNotificationsRights = new UserNotificationsRights();
for (let i = 0; i < data.length; i++) {
if (email == data[i].managerEmail) {
notificationRights['notifyForNewManagers'] = data[i].notifyForNewManagers;
notificationRights['notifyForNewSubscribers'] = data[i].notifyForNewSubscribers;
notificationRights['managerEmail'] = data[i].managerEmail;
}
}
return notificationRights;
}
}