1
|
import {Component, OnInit, Input} from '@angular/core';
|
2
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
3
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
4
|
import {ActivatedRoute, Router} from '@angular/router';
|
5
|
|
6
|
import {HelpContentService} from "../../../services/help-content.service";
|
7
|
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
|
8
|
import {SubscribeService} from "../../../openaireLibrary/utils/subscribe/subscribe.service";
|
9
|
import {EmailService} from "../../../openaireLibrary/utils/email/email.service";
|
10
|
|
11
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
12
|
|
13
|
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
|
14
|
import {Email} from "../../../openaireLibrary/utils/email/email";
|
15
|
|
16
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
17
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
18
|
|
19
|
@Component({
|
20
|
selector: 'community-edit-form',
|
21
|
templateUrl: './community-edit-form.component.html',
|
22
|
})
|
23
|
|
24
|
export class CommunityEditFormComponent implements OnInit{
|
25
|
|
26
|
@Input('group')
|
27
|
myForm: FormGroup;
|
28
|
|
29
|
public showLoading: boolean = true;
|
30
|
public errorMessage: string = '';
|
31
|
public updateErrorMessage: string = '';
|
32
|
|
33
|
public successfulSaveMessage: string = '';
|
34
|
public successfulResetMessage: string = '';
|
35
|
|
36
|
public hasChanged: boolean = false;
|
37
|
|
38
|
public res=[];
|
39
|
|
40
|
params: any;
|
41
|
|
42
|
public communityId = null;
|
43
|
public community = null;
|
44
|
public firstVersionOfManagers: string[];
|
45
|
public newManagersToSubscribe: string[];
|
46
|
|
47
|
public email: Email;
|
48
|
|
49
|
public emailToInform: Email;
|
50
|
|
51
|
public note: string = '';
|
52
|
|
53
|
public properties:EnvProperties = null;
|
54
|
|
55
|
constructor (private route: ActivatedRoute,
|
56
|
private _router: Router,
|
57
|
public _fb: FormBuilder,
|
58
|
private _helpContentService: HelpContentService,
|
59
|
private _communityService: CommunityService,
|
60
|
private _subscribeService: SubscribeService,
|
61
|
private _emailService: EmailService){ }
|
62
|
|
63
|
|
64
|
ngOnInit() {
|
65
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
66
|
this.properties = data.envSpecific;
|
67
|
this.route.queryParams.subscribe(
|
68
|
communityId => {
|
69
|
this.communityId = communityId['communityId'];
|
70
|
|
71
|
this.email = {body: "", subject: "", recipients: []};
|
72
|
this.emailToInform = {body: "", subject: "", recipients: []};
|
73
|
|
74
|
if(!Session.isLoggedIn()){
|
75
|
console.info(this._router.url);
|
76
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
77
|
} else {
|
78
|
if (this.communityId != null && this.communityId != '') {
|
79
|
this.showLoading = true;
|
80
|
this.updateErrorMessage = "";
|
81
|
this.errorMessage = "";
|
82
|
|
83
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
84
|
community => {
|
85
|
this.community = community;
|
86
|
this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
|
87
|
this.firstVersionOfManagers = community.managers.slice();
|
88
|
console.log("First version of managers " + this.firstVersionOfManagers);
|
89
|
console.log(community);
|
90
|
this.showLoading = false;
|
91
|
},
|
92
|
error => this.handleError('System error retrieving community profile', error)
|
93
|
);
|
94
|
}
|
95
|
}
|
96
|
});
|
97
|
});
|
98
|
}
|
99
|
|
100
|
public addManager() {
|
101
|
this.community.managers.push("");
|
102
|
}
|
103
|
|
104
|
public removeManager(i : any) {
|
105
|
this.community.managers.splice(i,1);
|
106
|
}
|
107
|
|
108
|
// public addSubject() {
|
109
|
// this.community.subjects.push("");
|
110
|
// }
|
111
|
//
|
112
|
// public removeSubject(i : any) {
|
113
|
// this.community.subjects.splice(i,1);
|
114
|
// }
|
115
|
|
116
|
public resetForm(communityId: string) {
|
117
|
if(!Session.isLoggedIn()){
|
118
|
console.info(this._router.url);
|
119
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
120
|
} else {
|
121
|
if (communityId != null && communityId != '') {
|
122
|
this.showLoading = true;
|
123
|
this.updateErrorMessage = "";
|
124
|
this.errorMessage = "";
|
125
|
|
126
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+communityId).subscribe (
|
127
|
community => {
|
128
|
this.community = community;
|
129
|
this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
|
130
|
this.showLoading = false;
|
131
|
this.handleSuccessfulReset('Form reseted!')
|
132
|
},
|
133
|
error => this.handleError('System error retrieving community profile', error)
|
134
|
);
|
135
|
}
|
136
|
this.resetChange();
|
137
|
}
|
138
|
}
|
139
|
|
140
|
public updateCommunity() {
|
141
|
if(!Session.isLoggedIn()){
|
142
|
console.info(this._router.url);
|
143
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
144
|
} else {
|
145
|
if (this.communityId != null && this.communityId != '') {
|
146
|
this.showLoading = true;
|
147
|
var community = this.parseUpdatedCommunity();
|
148
|
|
149
|
// TODO delete after testing
|
150
|
console.log(community);
|
151
|
|
152
|
if (!this.hasValidEmail(community) || !this.hasFilled(community["name"])) {
|
153
|
this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
|
154
|
} else {
|
155
|
var newManagers = this.getNewManagers();
|
156
|
this._communityService.updateCommunity(this.properties.communityAPI+this.communityId, community).subscribe(
|
157
|
|
158
|
community => {
|
159
|
// TODO delete after testing
|
160
|
console.log("New managers in component: " + newManagers);
|
161
|
if (newManagers !== null) {
|
162
|
this.sendMailToNewManagers(newManagers);
|
163
|
console.log("Old managers in component: " + this.firstVersionOfManagers);
|
164
|
this.informOldManagersForTheNewOnes(this.firstVersionOfManagers);
|
165
|
for (let i = 0; i < newManagers.length; i++) {
|
166
|
console.log(newManagers[i]);
|
167
|
this._subscribeService.subscribeToCommunity(this.communityId, newManagers[i], this.properties.adminToolsAPIURL).subscribe(
|
168
|
res => {
|
169
|
console.log(res);
|
170
|
}
|
171
|
);
|
172
|
this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe(
|
173
|
res => {
|
174
|
console.log(res);
|
175
|
}
|
176
|
);
|
177
|
}
|
178
|
}
|
179
|
this.handleSuccessfulSave('Community saved!')
|
180
|
},
|
181
|
error => {
|
182
|
this.handleUpdateError('System error updating community profile', error)
|
183
|
},
|
184
|
() => { this.firstVersionOfManagers = this.community["managers"].slice(); }
|
185
|
);
|
186
|
this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
|
187
|
}
|
188
|
}
|
189
|
this.resetChange();
|
190
|
}
|
191
|
}
|
192
|
|
193
|
|
194
|
private parseUpdatedCommunity() : {} {
|
195
|
var community = {};
|
196
|
|
197
|
community["name"] = this.community.title;
|
198
|
community["shortName"] = this.community.shortTitle;
|
199
|
community["status"] = this.community.status;
|
200
|
community["description"] = this.community.description;
|
201
|
community["logoUrl"] = this.community.logoUrl;
|
202
|
|
203
|
community['managers'] = new Array<string>();
|
204
|
this.community.managers = this.getNonEmptyItems(this.community.managers);
|
205
|
community['managers'] = this.community.managers;
|
206
|
|
207
|
return community;
|
208
|
}
|
209
|
|
210
|
private getNewManagers(): Array<string> {
|
211
|
|
212
|
let newManagers = null;
|
213
|
for (let i = 0; i < this.community['managers'].length; i++) {
|
214
|
if (!this.firstVersionOfManagers.includes(this.community['managers'][i])) {
|
215
|
if(newManagers === null){
|
216
|
newManagers = new Array<string>();
|
217
|
}
|
218
|
newManagers.push(this.community['managers'][i]);
|
219
|
}
|
220
|
}
|
221
|
console.log("New managers are: " + newManagers);
|
222
|
return newManagers;
|
223
|
}
|
224
|
|
225
|
private sendMailToNewManagers(managers: any) {
|
226
|
this._emailService.sendEmail("http://scoobydoo.di.uoa.gr:8080/uoa-admin-tools/sendMail", this.composeEmailForNewManager(this.community.title, managers)).subscribe(
|
227
|
res => {
|
228
|
console.log("The email has been sent successfully!")
|
229
|
},
|
230
|
error => console.log(error)
|
231
|
);
|
232
|
}
|
233
|
|
234
|
private informOldManagersForTheNewOnes(managers: any) {
|
235
|
this._emailService.sendEmail("http://scoobydoo.di.uoa.gr:8080/uoa-admin-tools/notifyForNewManagers/"+ this.communityId, this.composeEmailToInformOldManagersForTheNewOnes(this.community.title, this.communityId, this.firstVersionOfManagers)).subscribe(
|
236
|
res => {
|
237
|
console.log("The email has been sent successfully!")
|
238
|
},
|
239
|
error => console.log(error)
|
240
|
);
|
241
|
}
|
242
|
|
243
|
// TODO find the right place to write it
|
244
|
private composeEmailForNewManager(communityName: string, newCommunityManagers: any): Email {
|
245
|
this.email.subject = "[OpenAIRE-Connect] " + communityName + ": Managers list notification";
|
246
|
this.email.body = "<p>There are updates in the managers list for " + communityName + " community.<br>"
|
247
|
+ "The list of managers is: " + newCommunityManagers + "</p><br><br>" +
|
248
|
"OpenAIRE team<br>" + "<a href='https://www.openaire.eu'>www.openaire.eu</a>";
|
249
|
this.email.recipients = newCommunityManagers;
|
250
|
|
251
|
console.log(this.email);
|
252
|
|
253
|
return this.email;
|
254
|
}
|
255
|
|
256
|
// TODO find the right place to write it
|
257
|
private composeEmailToInformOldManagersForTheNewOnes(communityName: string, communityId: string, firstVersionOfManagers: any) : Email {
|
258
|
this.emailToInform.subject = "[OpenAIRE-Connect]" + communityName + ": New subscriber notification";
|
259
|
this.emailToInform.body = "<p>There is a new subscriber for " + communityName +
|
260
|
"community. Click <a href='https://beta.admin.connect.openaire.eu/manage-subscribers?communityId=" +
|
261
|
communityId + "'>here</a> to manage the subscibers list.</p><br><br>" +
|
262
|
"OpenAIRE team<br>"+ "<a href='https://www.openaire.eu'>www.openaire.eu</a>";
|
263
|
this.emailToInform.recipients = firstVersionOfManagers;
|
264
|
|
265
|
return this.emailToInform;
|
266
|
}
|
267
|
|
268
|
private subscribeNewManagers(newManagers: string[]): boolean {
|
269
|
return true;
|
270
|
}
|
271
|
|
272
|
private getNonEmptyItems(data: string[]): string[] {
|
273
|
let length = data.length;
|
274
|
let arrayNonEmpty = new Array<string>();
|
275
|
|
276
|
let j = 0;
|
277
|
for (let i = 0; i < length; i++) {
|
278
|
if (this.isEmpty(data[i])) {
|
279
|
//console.log(data[i]);
|
280
|
} else if (this.isNonEmpty(data[i])) {
|
281
|
arrayNonEmpty[j] = data[i];
|
282
|
j++;
|
283
|
//console.log(data[i]);
|
284
|
}
|
285
|
}
|
286
|
return arrayNonEmpty;
|
287
|
}
|
288
|
|
289
|
private hasFilled(data: any): boolean {
|
290
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
291
|
// TODO remove console message after final testing
|
292
|
console.log("NAME IS NOT EMPTY");
|
293
|
return true;
|
294
|
}
|
295
|
// TODO remove console message after final testing
|
296
|
console.log("NAME IS EMPTY");
|
297
|
return false;
|
298
|
}
|
299
|
|
300
|
private isEmpty(data: string): boolean {
|
301
|
if (data != undefined && !data.replace(/\s/g, '').length)
|
302
|
return true;
|
303
|
else
|
304
|
return false;
|
305
|
}
|
306
|
|
307
|
private isNonEmpty(data: string): boolean {
|
308
|
if (data != undefined && data != null)
|
309
|
return true;
|
310
|
else
|
311
|
return false;
|
312
|
}
|
313
|
|
314
|
private hasValidEmail(data: any): boolean {
|
315
|
let length = data['managers'].length;
|
316
|
|
317
|
for(let i = 0; i < length; i++) {
|
318
|
if (!this.emailValidator(data['managers'][i])){
|
319
|
// TODO remove console message after final testing
|
320
|
console.log("INVALID EMAIL");
|
321
|
return false;
|
322
|
}
|
323
|
}
|
324
|
// TODO remove console message after final testing
|
325
|
console.log("ALL EMAILS ARE VALID");
|
326
|
return true;
|
327
|
}
|
328
|
|
329
|
private emailValidator(email : any): boolean {
|
330
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
331
|
return true;
|
332
|
else
|
333
|
return false;
|
334
|
}
|
335
|
|
336
|
private change() {
|
337
|
this.hasChanged = true;
|
338
|
this.successfulSaveMessage = '';
|
339
|
this.successfulResetMessage = '';
|
340
|
// TODO remove after testing
|
341
|
console.log('I have changed: I AM TRUE');
|
342
|
}
|
343
|
|
344
|
private resetChange() {
|
345
|
this.hasChanged = false;
|
346
|
// TODO remove after testing
|
347
|
console.log('I have changed: I AM FALSE');
|
348
|
}
|
349
|
|
350
|
public get form() {
|
351
|
return this._fb.group({
|
352
|
_id : '',
|
353
|
name : ['', Validators.required]
|
354
|
});
|
355
|
}
|
356
|
|
357
|
public reset() {
|
358
|
this.myForm.patchValue({
|
359
|
name : '',
|
360
|
_id : ''
|
361
|
});
|
362
|
}
|
363
|
|
364
|
handleUpdateError(message: string, error) {
|
365
|
this.updateErrorMessage = message;
|
366
|
console.log('Server responded: ' +error);
|
367
|
|
368
|
this.showLoading = false;
|
369
|
}
|
370
|
|
371
|
handleError(message: string, error) {
|
372
|
this.errorMessage = message;
|
373
|
console.log('Server responded: ' + error);
|
374
|
|
375
|
this.showLoading = false;
|
376
|
}
|
377
|
|
378
|
handleSuccessfulSave(message) {
|
379
|
this.showLoading = false;
|
380
|
this.successfulSaveMessage = message;
|
381
|
}
|
382
|
|
383
|
handleSuccessfulReset(message) {
|
384
|
this.showLoading = false;
|
385
|
this.successfulResetMessage = message;
|
386
|
}
|
387
|
|
388
|
trackByFn(index: any, item: any) {
|
389
|
return index;
|
390
|
}
|
391
|
}
|