2018-10-30 16:31:16 +01:00
import { Component , OnInit , Input } from '@angular/core' ;
import { SimpleChanges , OnChanges } from '@angular/core' ;
import { FormGroup , FormArray , FormBuilder , Validators } from "@angular/forms" ;
import { ActivatedRoute , Router } from '@angular/router' ;
import { HelpContentService } from "../../../services/help-content.service" ;
import { CommunityService } from "../../../openaireLibrary/connect/community/community.service" ;
2018-11-20 16:53:54 +01:00
import { SubscribeService } from "../../../openaireLibrary/utils/subscribe/subscribe.service" ;
import { EmailService } from "../../../openaireLibrary/utils/email/email.service" ;
2018-10-30 16:31:16 +01:00
import { EnvProperties } from '../../../openaireLibrary/utils/properties/env-properties' ;
import { CommunityInfo } from '../../../openaireLibrary/connect/community/communityInfo' ;
2018-11-20 16:53:54 +01:00
import { Email } from "../../../openaireLibrary/utils/email/email" ;
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' ;
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 ;
public showLoading : boolean = true ;
public errorMessage : string = '' ;
public updateErrorMessage : string = '' ;
public successfulSaveMessage : string = '' ;
public successfulResetMessage : string = '' ;
public hasChanged : boolean = false ;
public res = [ ] ;
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 ;
2018-11-22 17:23:07 +01:00
public note : string = '' ;
2018-10-30 16:31:16 +01:00
public properties :EnvProperties = null ;
constructor ( private route : ActivatedRoute ,
private _router : Router ,
public _fb : FormBuilder ,
private _helpContentService : HelpContentService ,
private _communityService : CommunityService ,
2018-11-20 16:53:54 +01:00
private _subscribeService : SubscribeService ,
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 = > {
this . communityId = communityId [ 'communityId' ] ;
2018-11-20 16:53:54 +01:00
this . email = { body : "" , subject : "" , recipients : [ ] } ;
2018-11-20 17:30:53 +01:00
this . emailToInform = { body : "" , subject : "" , recipients : [ ] } ;
2018-11-20 16:53:54 +01:00
2018-11-13 16:26:49 +01:00
if ( ! Session . isLoggedIn ( ) ) {
console . info ( this . _router . url ) ;
this . _router . navigate ( [ '/user-info' ] , { queryParams : { "errorCode" : LoginErrorCodes . NOT_VALID , "redirectUrl" : this . _router . url } } ) ;
} else {
if ( this . communityId != null && this . communityId != '' ) {
this . showLoading = true ;
this . updateErrorMessage = "" ;
this . errorMessage = "" ;
this . _communityService . getCommunity ( this . properties , this . properties . communityAPI + this . communityId ) . subscribe (
community = > {
this . community = community ;
this . params = { community : encodeURIComponent ( '"' + community . queryId + '"' ) } ;
this . firstVersionOfManagers = community . managers . slice ( ) ;
console . log ( "First version of managers " + this . firstVersionOfManagers ) ;
console . log ( community ) ;
this . showLoading = false ;
} ,
error = > this . handleError ( 'System error retrieving community profile' , error )
) ;
}
2018-10-30 16:31:16 +01:00
}
} ) ;
} ) ;
}
public addManager() {
this . community . managers . push ( "" ) ;
}
public removeManager ( i : any ) {
this . community . managers . splice ( i , 1 ) ;
}
// public addSubject() {
// this.community.subjects.push("");
// }
//
// public removeSubject(i : any) {
// this.community.subjects.splice(i,1);
// }
public resetForm ( communityId : string ) {
2018-11-13 16:26:49 +01:00
if ( ! Session . isLoggedIn ( ) ) {
console . info ( this . _router . url ) ;
this . _router . navigate ( [ '/user-info' ] , { queryParams : { "errorCode" : LoginErrorCodes . NOT_VALID , "redirectUrl" : this . _router . url } } ) ;
} else {
2018-10-30 16:31:16 +01:00
if ( communityId != null && communityId != '' ) {
this . showLoading = true ;
this . updateErrorMessage = "" ;
this . errorMessage = "" ;
this . _communityService . getCommunity ( this . properties , this . properties . communityAPI + communityId ) . subscribe (
community = > {
this . community = community ;
this . params = { community : encodeURIComponent ( '"' + community . queryId + '"' ) } ;
this . showLoading = false ;
this . handleSuccessfulReset ( 'Form reseted!' )
} ,
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() {
2018-11-13 16:26:49 +01:00
if ( ! Session . isLoggedIn ( ) ) {
console . info ( this . _router . url ) ;
this . _router . navigate ( [ '/user-info' ] , { queryParams : { "errorCode" : LoginErrorCodes . NOT_VALID , "redirectUrl" : this . _router . url } } ) ;
} else {
2018-10-30 16:31:16 +01:00
if ( this . communityId != null && this . communityId != '' ) {
this . showLoading = true ;
var community = this . parseUpdatedCommunity ( ) ;
// TODO delete after testing
console . log ( community ) ;
if ( ! this . hasValidEmail ( community ) || ! this . hasFilled ( community [ "name" ] ) ) {
this . _router . navigate ( [ '/community-edit-form' ] , { queryParams : { "communityId" : this . communityId } } ) ;
} else {
var newManagers = this . getNewManagers ( ) ;
this . _communityService . updateCommunity ( this . properties . communityAPI + this . communityId , community ) . subscribe (
community = > {
// TODO delete after testing
console . log ( "New managers in component: " + newManagers ) ;
if ( newManagers !== null ) {
2018-11-20 16:53:54 +01:00
this . sendMailToNewManagers ( newManagers ) ;
2018-11-20 17:30:53 +01:00
console . log ( "Old managers in component: " + this . firstVersionOfManagers ) ;
this . informOldManagersForTheNewOnes ( this . firstVersionOfManagers ) ;
2018-10-30 16:31:16 +01:00
for ( let i = 0 ; i < newManagers . length ; i ++ ) {
console . log ( newManagers [ i ] ) ;
this . _subscribeService . subscribeToCommunity ( this . communityId , newManagers [ i ] , this . properties . adminToolsAPIURL ) . subscribe (
res = > {
console . log ( res ) ;
}
) ;
this . _subscribeService . getCommunitySubscribers ( this . communityId , this . properties . adminToolsAPIURL ) . subscribe (
res = > {
console . log ( res ) ;
}
) ;
}
}
this . handleSuccessfulSave ( 'Community saved!' )
} ,
error = > {
this . handleUpdateError ( 'System error updating community profile' , error )
} ,
( ) = > { this . firstVersionOfManagers = this . community [ "managers" ] . slice ( ) ; }
) ;
this . _router . navigate ( [ '/community-edit-form' ] , { queryParams : { "communityId" : this . communityId } } ) ;
}
}
this . resetChange ( ) ;
2018-11-13 16:26:49 +01:00
}
2018-10-30 16:31:16 +01:00
}
private parseUpdatedCommunity ( ) : { } {
var community = { } ;
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 ;
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 ] ) ) {
if ( newManagers === null ) {
newManagers = new Array < string > ( ) ;
}
newManagers . push ( this . community [ 'managers' ] [ i ] ) ;
}
}
console . log ( "New managers are: " + newManagers ) ;
return newManagers ;
}
2018-11-20 16:53:54 +01:00
private sendMailToNewManagers ( managers : any ) {
2018-11-23 18:25:58 +01:00
this . _emailService . sendEmail ( "http://duffy.di.uoa.gr:8080/uoa-admin-tools/sendMail" , this . composeEmailForNewManager ( this . community . title , managers ) ) . subscribe (
2018-11-20 16:53:54 +01:00
res = > {
console . log ( "The email has been sent successfully!" )
} ,
error = > console . log ( error )
) ;
}
private informOldManagersForTheNewOnes ( managers : any ) {
2018-11-23 18:25:58 +01:00
this . _emailService . sendEmail ( "http://duffy.di.uoa.gr:8080/uoa-admin-tools/notifyForNewManagers/" + this . communityId , this . composeEmailToInformOldManagersForTheNewOnes ( this . community . title , this . communityId , this . firstVersionOfManagers ) ) . subscribe (
2018-11-20 17:30:53 +01:00
res = > {
console . log ( "The email has been sent successfully!" )
} ,
error = > console . log ( error )
) ;
2018-11-20 16:53:54 +01:00
}
// TODO find the right place to write it
2018-11-23 18:25:58 +01:00
// private composeEmailForNewManager(communityName: string, newCommunityManagers: any): Email {
// this.email.subject = "[OpenAIRE-Connect] " + communityName + ": Managers list notification";
// this.email.body = "<p>There are updates in the managers list for " + communityName + " community.<br>"
// + "The list of managers is: " + newCommunityManagers + "</p><br><br>" +
// "OpenAIRE team<br>" + "<a href='https://www.openaire.eu'>www.openaire.eu</a>";
// this.email.recipients = newCommunityManagers;
//
// console.log(this.email);
//
// return this.email;
// }
2018-11-20 16:53:54 +01:00
private composeEmailForNewManager ( communityName : string , newCommunityManagers : any ) : Email {
this . email . subject = "[OpenAIRE-Connect] " + communityName + ": Managers list notification" ;
2018-11-23 18:25:58 +01:00
this . email . body = "<div style='font-size:14px;'><p>You are receiving this e-mail as you were assigned as manager of the community <a href='https://beta."
+ this . communityId + ".openaire.eu/'>" + "\"" + communityName + "\"" + "</a> dashboard. "
+ "In order to access the administration section of your community you must first login using one of the available options. "
+ "<br>The administrative rights are associated to the e-mail address, that was used to send you this message."
+ " If you login with an account associated to a different email, please <a href='mailto:helpdesk@openaire.eu'>contact us</a>"
+ " or your colleagues, that already have access to the administration tool, to update your e-mail. <br>"
+ "You can access the administration tool by clicking the \"Manage\" button that will be available in the top right corner of the dashboard."
+ "</p><br>"
+ "OpenAIRE team<br>" + "<a href='https://www.openaire.eu'>www.openaire.eu</a>"
+ "<p style='font-size:11px;'>If you are not responsible for this community, please "
+ "<a href='mailto:openaire.test@gmail.com'>contact us</a>.</p></div>" ;
2018-11-20 16:53:54 +01:00
this . email . recipients = newCommunityManagers ;
console . log ( this . email ) ;
return this . email ;
}
// TODO find the right place to write it
2018-11-20 17:30:53 +01:00
private composeEmailToInformOldManagersForTheNewOnes ( communityName : string , communityId : string , firstVersionOfManagers : any ) : Email {
this . emailToInform . subject = "[OpenAIRE-Connect]" + communityName + ": New subscriber notification" ;
this . emailToInform . body = "<p>There is a new subscriber for " + communityName +
"community. Click <a href='https://beta.admin.connect.openaire.eu/manage-subscribers?communityId=" +
communityId + "'>here</a> to manage the subscibers list.</p><br><br>" +
"OpenAIRE team<br>" + "<a href='https://www.openaire.eu'>www.openaire.eu</a>" ;
this . emailToInform . recipients = firstVersionOfManagers ;
return this . emailToInform ;
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 [ ] {
let length = data . length ;
let arrayNonEmpty = new Array < string > ( ) ;
let j = 0 ;
for ( let i = 0 ; i < length ; i ++ ) {
if ( this . isEmpty ( data [ i ] ) ) {
//console.log(data[i]);
} else if ( this . isNonEmpty ( data [ i ] ) ) {
arrayNonEmpty [ j ] = data [ i ] ;
j ++ ;
//console.log(data[i]);
}
}
return arrayNonEmpty ;
}
private hasFilled ( data : any ) : boolean {
if ( this . isNonEmpty ( data ) && ! this . isEmpty ( data ) ) {
// TODO remove console message after final testing
console . log ( "NAME IS NOT EMPTY" ) ;
return true ;
}
// TODO remove console message after final testing
console . log ( "NAME IS EMPTY" ) ;
return false ;
}
private isEmpty ( data : string ) : boolean {
if ( data != undefined && ! data . replace ( /\s/g , '' ) . length )
return true ;
else
return false ;
}
private isNonEmpty ( data : string ) : boolean {
if ( data != undefined && data != null )
return true ;
else
return false ;
}
private hasValidEmail ( data : any ) : boolean {
let length = data [ 'managers' ] . length ;
for ( let i = 0 ; i < length ; i ++ ) {
if ( ! this . emailValidator ( data [ 'managers' ] [ i ] ) ) {
// TODO remove console message after final testing
console . log ( "INVALID EMAIL" ) ;
return false ;
}
}
// TODO remove console message after final testing
console . log ( "ALL EMAILS ARE VALID" ) ;
return true ;
}
private emailValidator ( email : any ) : boolean {
if ( email . match ( "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" ) )
return true ;
else
return false ;
}
private change() {
this . hasChanged = true ;
this . successfulSaveMessage = '' ;
this . successfulResetMessage = '' ;
// TODO remove after testing
console . log ( 'I have changed: I AM TRUE' ) ;
}
private resetChange() {
this . hasChanged = false ;
// TODO remove after testing
console . log ( 'I have changed: I AM 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 ;
console . log ( 'Server responded: ' + error ) ;
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 ;
}
}