2021-05-19 13:40:29 +02:00
|
|
|
import {Component, ElementRef, Input, OnInit} from '@angular/core';
|
2022-09-23 16:14:20 +02:00
|
|
|
import {UntypedFormBuilder, UntypedFormGroup} from '@angular/forms';
|
2018-10-30 16:31:16 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
2019-09-13 11:39:18 +02:00
|
|
|
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
|
2018-10-30 16:31:16 +01:00
|
|
|
import {ManageUserNotificationsService} from './manage-user-notifications.service';
|
|
|
|
import {UserNotificationsRights} from './userNotificationsRights';
|
2018-11-01 18:21:10 +01:00
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
2019-09-13 11:39:18 +02:00
|
|
|
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
|
2020-04-01 15:40:48 +02:00
|
|
|
import {Title} from '@angular/platform-browser';
|
2021-05-19 13:40:29 +02:00
|
|
|
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
|
|
|
|
import {properties} from '../../../environments/environment';
|
2022-06-28 20:15:05 +02:00
|
|
|
import {Subscriber} from 'rxjs';
|
2021-05-19 13:40:29 +02:00
|
|
|
import {MailPrefsService} from '../../openaireLibrary/connect/userEmailPreferences/mailPrefs.service';
|
2022-06-28 20:15:05 +02:00
|
|
|
import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo";
|
|
|
|
import {Option} from "../../openaireLibrary/sharedComponents/input/input.component";
|
2022-07-08 20:59:17 +02:00
|
|
|
import {NotificationHandler} from "../../openaireLibrary/utils/notification-handler";
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
@Component({
|
2019-09-13 11:39:18 +02:00
|
|
|
selector: 'manage-user-notifications',
|
|
|
|
templateUrl: './manage-user-notifications.component.html',
|
2018-10-30 16:31:16 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export class ManageUserNotificationsComponent implements OnInit {
|
2019-09-13 11:39:18 +02:00
|
|
|
@Input('group')
|
2022-09-23 16:14:20 +02:00
|
|
|
myForm: UntypedFormGroup;
|
2022-06-30 12:41:36 +02:00
|
|
|
public properties: EnvProperties = properties;
|
2022-06-28 20:15:05 +02:00
|
|
|
public community: CommunityInfo;
|
|
|
|
public userNotifications: UserNotificationsRights = null;
|
|
|
|
public initialUserNotifications: UserNotificationsRights = null;
|
2021-05-19 13:40:29 +02:00
|
|
|
public notifications = null;
|
|
|
|
public initialNotifications = [];
|
2019-09-13 11:39:18 +02:00
|
|
|
public userEmail = null;
|
|
|
|
public showLoading = true;
|
|
|
|
public hasChanged = false;
|
|
|
|
public user: User;
|
2021-05-19 13:40:29 +02:00
|
|
|
private subscriptions = [];
|
2022-06-28 20:15:05 +02:00
|
|
|
|
|
|
|
frequencyOptions: Option[] = [{label: "Daily", value: 24}, {label: "Every two days", value: 48}, {label: "Weekly", value: 168}]
|
|
|
|
|
2022-09-23 16:14:20 +02:00
|
|
|
constructor(private route: ActivatedRoute, private _router: Router, public _fb: UntypedFormBuilder,
|
2020-04-01 15:40:48 +02:00
|
|
|
private title: Title,
|
2022-06-28 20:15:05 +02:00
|
|
|
private manageUserNotificationsService: ManageUserNotificationsService,
|
|
|
|
private element: ElementRef, private userManagementService: UserManagementService,
|
|
|
|
private communityService: CommunityService,
|
|
|
|
private mailPrefsService: MailPrefsService) {
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
ngOnInit() {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
|
2022-06-28 20:15:05 +02:00
|
|
|
if (community) {
|
|
|
|
this.community = community;
|
|
|
|
this.title.setTitle(this.community.shortTitle.toUpperCase() + ' | User Notifications');
|
2021-05-19 13:40:29 +02:00
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
|
|
|
this.user = user;
|
|
|
|
if (this.user) {
|
|
|
|
this.userEmail = this.user.email;
|
2022-06-28 20:15:05 +02:00
|
|
|
this.subscriptions.push(this.manageUserNotificationsService.getUserNotifications(this.properties, this.community.communityId).subscribe(
|
2021-05-19 13:40:29 +02:00
|
|
|
userNotifications => {
|
|
|
|
this.initialUserNotifications = userNotifications;
|
|
|
|
if (this.initialUserNotifications['notifyForNewManagers'] == null ||
|
|
|
|
this.initialUserNotifications['notifyForNewSubscribers'] == null) {
|
2022-06-28 20:15:05 +02:00
|
|
|
if (Session.isManager("community", this.community.communityId, this.user)) {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.initialUserNotifications = new UserNotificationsRights(true, true, "");
|
2022-06-28 20:15:05 +02:00
|
|
|
} else {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.initialUserNotifications = new UserNotificationsRights(false, false, "");
|
|
|
|
}
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2021-05-19 13:40:29 +02:00
|
|
|
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
|
|
|
|
this.getClaimsNotifications();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
if (error.status === 404) {
|
2022-06-28 20:15:05 +02:00
|
|
|
if (Session.isManager("community", this.community.communityId, this.user)) {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.initialUserNotifications = new UserNotificationsRights(true, true, "");
|
2022-06-28 20:15:05 +02:00
|
|
|
} else {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.initialUserNotifications = new UserNotificationsRights(false, false, "");
|
|
|
|
}
|
|
|
|
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
|
|
|
|
} else {
|
|
|
|
this.handleError('System error retrieving user notifications', error);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
this.getClaimsNotifications();
|
|
|
|
}
|
|
|
|
));
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
|
|
|
getClaimsNotifications() {
|
|
|
|
this.subscriptions.push(this.mailPrefsService.getUserEmailPreferencesForCommunity(this.community.communityId, this.properties.claimsAPIURL).subscribe(
|
2021-05-19 13:40:29 +02:00
|
|
|
data => {
|
2022-06-28 20:15:05 +02:00
|
|
|
if (data.code != 204) {
|
|
|
|
this.initialNotifications = data.data;
|
|
|
|
} else {
|
|
|
|
if (Session.isManager("community", this.community.communityId, this.user)) {
|
|
|
|
this.initialNotifications = [{notify: true, frequency: 24, openaireId: this.community.communityId}];
|
|
|
|
} else {
|
|
|
|
this.initialNotifications = [{notify: false, frequency: 24, openaireId: this.community.communityId}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.notifications = JSON.parse(JSON.stringify(this.initialNotifications));
|
2021-05-19 13:40:29 +02:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
err => {
|
2022-06-28 20:15:05 +02:00
|
|
|
if (err.status === 404) {
|
|
|
|
if (Session.isManager("community", this.community.communityId, this.user)) {
|
|
|
|
this.initialNotifications = [{notify: true, frequency: 24, openaireId: this.community.communityId}];
|
|
|
|
} else {
|
|
|
|
this.initialNotifications = [{notify: false, frequency: 24, openaireId: this.community.communityId}];
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
this.notifications = JSON.parse(JSON.stringify(this.initialNotifications));
|
|
|
|
} else {
|
|
|
|
this.handleError("Error getting user email preferences for community with id: " + this.community.communityId, err);
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2021-05-19 13:40:29 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
2019-09-13 11:39:18 +02:00
|
|
|
});
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
public updateUserNotifications() {
|
2022-07-08 20:59:17 +02:00
|
|
|
if (this.community.communityId != null && this.community.communityId !== '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
const userNotifications = this.parseUpdatedUserNotifications();
|
|
|
|
this.subscriptions.push(this.manageUserNotificationsService.updateUserNotifications(this.properties, this.community.communityId, userNotifications).subscribe(
|
|
|
|
userNotifications => {
|
|
|
|
this.initialUserNotifications = JSON.parse(JSON.stringify(this.userNotifications));
|
|
|
|
this.handleSuccessfulSave('Notification settings saved!');
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error updating user notifications', error)
|
|
|
|
));
|
|
|
|
this.subscriptions.push(this.mailPrefsService.saveUserEmailPreferences(this.notifications[0], this.properties.claimsAPIURL).subscribe(
|
|
|
|
data => {
|
|
|
|
this.initialNotifications[0] = JSON.parse(JSON.stringify(this.notifications[0]));
|
|
|
|
this.handleSuccessfulSave('Claims notification settings saved!');
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
//console.log(err);
|
|
|
|
this.handleError("Error saving user email preferences: " + JSON.stringify(this.notifications[0]), err);
|
|
|
|
}
|
|
|
|
));
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
2022-07-08 20:59:17 +02:00
|
|
|
this.resetChange();
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
private parseUpdatedUserNotifications(): {} {
|
|
|
|
const userNotifications = {};
|
|
|
|
userNotifications['notifyForNewManagers'] = this.userNotifications.notifyForNewManagers;
|
|
|
|
userNotifications['notifyForNewSubscribers'] = this.userNotifications.notifyForNewSubscribers;
|
|
|
|
if (this.userNotifications.managerEmail) {
|
|
|
|
userNotifications['managerEmail'] = this.userNotifications.managerEmail;
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
2019-09-13 11:39:18 +02:00
|
|
|
return userNotifications;
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
public resetForm() {
|
2022-07-08 20:59:17 +02:00
|
|
|
this.notifications[0] = JSON.parse(JSON.stringify(this.initialNotifications[0]));
|
|
|
|
if (this.userNotifications && this.initialUserNotifications) {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
|
|
|
|
this.showLoading = false;
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
2022-07-08 20:59:17 +02:00
|
|
|
this.resetChange();
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
public changeValueForNewManagers(notifyForManagers: any) {
|
2022-07-08 20:59:17 +02:00
|
|
|
this.userNotifications.notifyForNewManagers = !notifyForManagers;
|
|
|
|
this.change();
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
public changeValueForNewSubscribers(notifyForSubscribers: any) {
|
2022-07-08 20:59:17 +02:00
|
|
|
this.userNotifications.notifyForNewSubscribers = !notifyForSubscribers;
|
|
|
|
this.change();
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2022-07-08 20:59:17 +02:00
|
|
|
public change() {
|
2019-09-13 11:39:18 +02:00
|
|
|
this.hasChanged = true;
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
private resetChange() {
|
|
|
|
this.hasChanged = false;
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
handleError(message: string, error) {
|
|
|
|
console.log('Server responded: ' + error);
|
2022-07-08 20:59:17 +02:00
|
|
|
NotificationHandler.rise(message, 'danger');
|
2019-09-13 11:39:18 +02:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
handleSuccessfulSave(message) {
|
|
|
|
this.showLoading = false;
|
2022-07-08 20:59:17 +02:00
|
|
|
NotificationHandler.rise(message);
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2021-05-19 13:40:29 +02:00
|
|
|
changeNotify(notification: any, checked: boolean, index: number) {
|
2022-06-28 20:15:05 +02:00
|
|
|
notification.notify = checked;
|
|
|
|
this.change();
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
2021-05-19 13:40:29 +02:00
|
|
|
changeFrequency() {
|
|
|
|
this.change();
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2022-06-28 20:15:05 +02:00
|
|
|
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|