2019-03-05 15:53:50 +01:00
|
|
|
import {Component, OnInit, Input, ElementRef} from '@angular/core';
|
|
|
|
import {FormGroup, FormBuilder, Validators} from '@angular/forms';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
import {HelpContentService} from '../../../services/help-content.service';
|
|
|
|
import {CommunityService} from '../../../openaireLibrary/connect/community/community.service';
|
|
|
|
import {SubscribeService} from '../../../openaireLibrary/utils/subscribe/subscribe.service';
|
|
|
|
import {EmailService} from '../../../openaireLibrary/utils/email/email.service';
|
2018-11-20 16:53:54 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
import {Email} from '../../../openaireLibrary/utils/email/email';
|
|
|
|
import {Composer} from '../../../openaireLibrary/utils/email/composer';
|
|
|
|
import {Validator} from '../../../openaireLibrary/utils/email/validator';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
2019-04-24 13:44:46 +02:00
|
|
|
import {HelperFunctions} from "../../../openaireLibrary/utils/HelperFunctions.class";
|
2020-04-01 15:40:48 +02:00
|
|
|
import {Title} from '@angular/platform-browser';
|
2018-11-13 16:26:49 +01:00
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'community-edit-form',
|
|
|
|
templateUrl: './community-edit-form.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class CommunityEditFormComponent implements OnInit{
|
|
|
|
|
|
|
|
@Input('group')
|
|
|
|
myForm: FormGroup;
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public showLoading = true;
|
|
|
|
public errorMessage = '';
|
|
|
|
public updateErrorMessage = '';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public successfulSaveMessage = '';
|
|
|
|
public successfulResetMessage = '';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public hasChanged = false;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public res = [];
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
params: any;
|
|
|
|
|
|
|
|
public communityId = null;
|
|
|
|
public community = null;
|
|
|
|
public firstVersionOfManagers: string[];
|
|
|
|
public newManagersToSubscribe: string[];
|
|
|
|
|
2018-11-20 16:53:54 +01:00
|
|
|
public email: Email;
|
|
|
|
|
2018-11-20 17:30:53 +01:00
|
|
|
public emailToInform: Email;
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public note = '';
|
2018-11-22 17:23:07 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public properties: EnvProperties = null;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2018-12-03 12:33:03 +01:00
|
|
|
constructor (private element: ElementRef,
|
2020-04-01 15:40:48 +02:00
|
|
|
private title: Title,
|
2018-12-03 12:33:03 +01:00
|
|
|
private route: ActivatedRoute,
|
2018-10-30 16:31:16 +01:00
|
|
|
private _router: Router,
|
|
|
|
public _fb: FormBuilder,
|
|
|
|
private _helpContentService: HelpContentService,
|
|
|
|
private _communityService: CommunityService,
|
2018-11-20 16:53:54 +01:00
|
|
|
private _subscribeService: SubscribeService,
|
2019-03-05 15:53:50 +01:00
|
|
|
private _emailService: EmailService) { }
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.route.queryParams.subscribe(
|
|
|
|
communityId => {
|
2019-04-24 13:44:46 +02:00
|
|
|
HelperFunctions.scroll();
|
2020-04-01 15:40:48 +02:00
|
|
|
this.title.setTitle('Administration Dashboard | Community Profile');
|
2019-04-24 13:44:46 +02:00
|
|
|
this.communityId = communityId['communityId'];
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
this.email = {body: '', subject: '', recipients: []};
|
|
|
|
this.emailToInform = {body: '', subject: '', recipients: []};
|
2018-11-20 16:53:54 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (this.communityId != null && this.communityId !== '') {
|
2018-11-13 16:26:49 +01:00
|
|
|
this.showLoading = true;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.updateErrorMessage = '';
|
|
|
|
this.errorMessage = '';
|
2018-11-13 16:26:49 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
this._communityService.getCommunity(this.properties,
|
|
|
|
this.properties.communityAPI + this.communityId).subscribe (
|
2018-11-13 16:26:49 +01:00
|
|
|
community => {
|
|
|
|
this.community = community;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.params = {community: encodeURIComponent(
|
|
|
|
'"' + community.queryId + '"')};
|
2018-11-13 16:26:49 +01:00
|
|
|
this.firstVersionOfManagers = community.managers.slice();
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving community profile', error)
|
|
|
|
);
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public addManager() {
|
2019-03-05 15:53:50 +01:00
|
|
|
this.community.managers.push('');
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public removeManager(i: any) {
|
|
|
|
this.community.managers.splice(i, 1);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// public addSubject() {
|
|
|
|
// this.community.subjects.push("");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public removeSubject(i : any) {
|
|
|
|
// this.community.subjects.splice(i,1);
|
|
|
|
// }
|
|
|
|
|
|
|
|
public resetForm(communityId: string) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams:
|
|
|
|
{ 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (communityId != null && communityId !== '') {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.updateErrorMessage = '';
|
|
|
|
this.errorMessage = '';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
this._communityService.getCommunity(this.properties,
|
|
|
|
this.properties.communityAPI + communityId).subscribe (
|
2018-10-30 16:31:16 +01:00
|
|
|
community => {
|
|
|
|
this.community = community;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.params = {community: encodeURIComponent(
|
|
|
|
'"' + community.queryId + '"')};
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = false;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.handleSuccessfulReset('Form reset!');
|
2018-10-30 16:31:16 +01:00
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving community profile', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.resetChange();
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public updateCommunity() {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams:
|
|
|
|
{ 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (this.communityId != null && this.communityId !== '') {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
2019-03-05 15:53:50 +01:00
|
|
|
const community = this.parseUpdatedCommunity();
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Validator.hasValidEmails(community['managers']) || !this.hasFilled(community['name'])) {
|
|
|
|
this._router.navigate(['/community-edit-form'], {
|
|
|
|
queryParams: { 'communityId': this.communityId}});
|
2018-10-30 16:31:16 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
const newManagers = this.getNewManagers();
|
|
|
|
this._communityService.updateCommunity(
|
|
|
|
this.properties.communityAPI + this.communityId, community).subscribe(
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
community => {
|
|
|
|
if (newManagers !== null) {
|
2018-11-20 16:53:54 +01:00
|
|
|
this.sendMailToNewManagers(newManagers);
|
2019-03-05 15:53:50 +01:00
|
|
|
this.informOldManagersForTheNewOnes();
|
2018-10-30 16:31:16 +01:00
|
|
|
for (let i = 0; i < newManagers.length; i++) {
|
2019-03-05 15:53:50 +01:00
|
|
|
this._subscribeService.subscribeToCommunity(
|
|
|
|
this.communityId, newManagers[i], this.properties.adminToolsAPIURL).
|
|
|
|
subscribe(
|
2018-10-30 16:31:16 +01:00
|
|
|
res => {
|
2019-03-05 15:53:50 +01:00
|
|
|
// console.log(res);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
);
|
2019-03-05 15:53:50 +01:00
|
|
|
this._subscribeService.getCommunitySubscribers
|
2019-07-02 11:30:48 +02:00
|
|
|
(this.properties, this.communityId).subscribe(
|
2018-10-30 16:31:16 +01:00
|
|
|
res => {
|
2019-03-05 15:53:50 +01:00
|
|
|
// console.log(res);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 15:53:50 +01:00
|
|
|
this.handleSuccessfulSave('Community saved!');
|
2018-10-30 16:31:16 +01:00
|
|
|
},
|
|
|
|
error => {
|
2019-03-05 15:53:50 +01:00
|
|
|
this.handleUpdateError('System error updating community profile', error);
|
2018-10-30 16:31:16 +01:00
|
|
|
},
|
2019-03-05 15:53:50 +01:00
|
|
|
() => { this.firstVersionOfManagers = this.community['managers'].slice(); }
|
2018-10-30 16:31:16 +01:00
|
|
|
);
|
2019-03-05 15:53:50 +01:00
|
|
|
this._router.navigate(['/community-edit-form'], {
|
|
|
|
queryParams: { 'communityId': this.communityId}});
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.resetChange();
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
private parseUpdatedCommunity(): {} {
|
|
|
|
const community = {};
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
community['name'] = this.community.title;
|
|
|
|
community['shortName'] = this.community.shortTitle;
|
|
|
|
community['status'] = this.community.status;
|
|
|
|
community['description'] = this.community.description;
|
|
|
|
community['logoUrl'] = this.community.logoUrl;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
community['managers'] = new Array<string>();
|
|
|
|
this.community.managers = this.getNonEmptyItems(this.community.managers);
|
|
|
|
community['managers'] = this.community.managers;
|
|
|
|
|
|
|
|
return community;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getNewManagers(): Array<string> {
|
|
|
|
|
|
|
|
let newManagers = null;
|
|
|
|
for (let i = 0; i < this.community['managers'].length; i++) {
|
|
|
|
if (!this.firstVersionOfManagers.includes(this.community['managers'][i])) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if(newManagers === null) {
|
2018-10-30 16:31:16 +01:00
|
|
|
newManagers = new Array<string>();
|
|
|
|
}
|
|
|
|
newManagers.push(this.community['managers'][i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newManagers;
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:53:54 +01:00
|
|
|
private sendMailToNewManagers(managers: any) {
|
2020-04-07 19:29:12 +02:00
|
|
|
this._emailService.sendEmail(this.properties,
|
2019-03-05 15:53:50 +01:00
|
|
|
Composer.composeEmailForNewManager(this.communityId,
|
|
|
|
this.community.title, managers)).subscribe(
|
2018-11-20 16:53:54 +01:00
|
|
|
res => {
|
2019-03-05 15:53:50 +01:00
|
|
|
// console.log("The email has been sent successfully!")
|
2018-11-20 16:53:54 +01:00
|
|
|
},
|
|
|
|
error => console.log(error)
|
2018-12-12 14:08:02 +01:00
|
|
|
);
|
2018-11-20 16:53:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
private informOldManagersForTheNewOnes() {
|
2020-04-07 19:29:12 +02:00
|
|
|
this._emailService.notifyForNewManagers(
|
|
|
|
this.properties, this.communityId,
|
2018-12-12 14:08:02 +01:00
|
|
|
Composer.composeEmailToInformOldManagersForTheNewOnes(this.community.title, this.communityId,
|
|
|
|
this.firstVersionOfManagers,
|
|
|
|
this.community.managers)).subscribe(
|
2018-11-20 17:30:53 +01:00
|
|
|
res => {
|
2019-03-05 15:53:50 +01:00
|
|
|
// console.log("The email has been sent successfully!")
|
2018-11-20 17:30:53 +01:00
|
|
|
},
|
|
|
|
error => console.log(error)
|
2018-12-12 14:08:02 +01:00
|
|
|
);
|
2018-11-20 16:53:54 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
private subscribeNewManagers(newManagers: string[]): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getNonEmptyItems(data: string[]): string[] {
|
2019-03-05 15:53:50 +01:00
|
|
|
const length = data.length;
|
|
|
|
const arrayNonEmpty = new Array<string>();
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
let j = 0;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
if (this.isEmpty(data[i])) {
|
2019-03-05 15:53:50 +01:00
|
|
|
// console.log(data[i]);
|
2018-10-30 16:31:16 +01:00
|
|
|
} else if (this.isNonEmpty(data[i])) {
|
|
|
|
arrayNonEmpty[j] = data[i];
|
|
|
|
j++;
|
2019-03-05 15:53:50 +01:00
|
|
|
// console.log(data[i]);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return arrayNonEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasFilled(data: any): boolean {
|
|
|
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private isEmpty(data: string): boolean {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (data !== undefined && !data.replace(/\s/g, '').length) {
|
2018-10-30 16:31:16 +01:00
|
|
|
return true;
|
2019-03-05 15:53:50 +01:00
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
return false;
|
2019-03-05 15:53:50 +01:00
|
|
|
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private isNonEmpty(data: string): boolean {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (data !== undefined && data != null) {
|
2018-10-30 16:31:16 +01:00
|
|
|
return true;
|
2019-03-05 15:53:50 +01:00
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
return false;
|
2019-03-05 15:53:50 +01:00
|
|
|
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private change() {
|
|
|
|
this.hasChanged = true;
|
|
|
|
this.successfulSaveMessage = '';
|
|
|
|
this.successfulResetMessage = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
private resetChange() {
|
|
|
|
this.hasChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
_id : '',
|
|
|
|
name : ['', Validators.required]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
name : '',
|
|
|
|
_id : ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
this.updateErrorMessage = message;
|
2019-03-05 15:53:50 +01:00
|
|
|
console.log('Server responded: ' + error);
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
this.errorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulSave(message) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.successfulSaveMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulReset(message) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.successfulResetMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
trackByFn(index: any, item: any) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|