diff --git a/src/app/general/edit-stakeholder/edit-stakeholder.component.ts b/src/app/general/edit-stakeholder/edit-stakeholder.component.ts index 5ff600e..2300843 100644 --- a/src/app/general/edit-stakeholder/edit-stakeholder.component.ts +++ b/src/app/general/edit-stakeholder/edit-stakeholder.component.ts @@ -90,17 +90,17 @@ import {error} from "protractor";
{{uploadError}}
-
+
-
+
- +
{ - this.user = user; - if(this.isCurator) { - this.subscriptions.push(this.statsProfileService.getStatsProfiles().subscribe(statsProfiles => { - this.statsProfiles = statsProfiles; - }, error => { - this.statsProfiles = []; - })); - } else { + this.user = user; + if (this.isCurator) { + this.subscriptions.push(this.statsProfileService.getStatsProfiles().subscribe(statsProfiles => { + this.statsProfiles = statsProfiles; + }, error => { this.statsProfiles = []; - } - this.types = this.stakeholderUtils.getTypesByUserRoles(this.user, this.stakeholder.alias); - this.stakeholderFb = this.fb.group({ - _id: this.fb.control(this.stakeholder._id), - defaultId: this.fb.control(this.stakeholder.defaultId), - name: this.fb.control(this.stakeholder.name, Validators.required), - description: this.fb.control(this.stakeholder.description), - index_name: this.fb.control(this.stakeholder.index_name, Validators.required), - index_id: this.fb.control(this.stakeholder.index_id, Validators.required), - index_shortName: this.fb.control(this.stakeholder.index_shortName, Validators.required), - statsProfile: this.fb.control(this.stakeholder.statsProfile, Validators.required), - creationDate: this.fb.control(this.stakeholder.creationDate), - alias: this.fb.control(this.stakeholder.alias, - [ - Validators.required, - this.stakeholderUtils.aliasValidatorString( - this.alias.filter(alias => alias !== this.stakeholder.alias) - )] - ), - isDefault: this.fb.control((this.isDefault)), - visibility: this.fb.control(this.stakeholder.visibility, Validators.required), - type: this.fb.control(this.stakeholder.type, Validators.required), - topics: this.fb.control(this.stakeholder.topics), - isUpload: this.fb.control(this.stakeholder.isUpload), - logoUrl: this.fb.control(this.stakeholder.logoUrl), - }); - if (this.stakeholder.isUpload) { + })); + } else { + this.statsProfiles = []; + } + this.types = this.stakeholderUtils.getTypesByUserRoles(this.user, this.stakeholder.alias); + this.stakeholderFb = this.fb.group({ + _id: this.fb.control(this.stakeholder._id), + defaultId: this.fb.control(this.stakeholder.defaultId), + name: this.fb.control(this.stakeholder.name, Validators.required), + description: this.fb.control(this.stakeholder.description), + index_name: this.fb.control(this.stakeholder.index_name, Validators.required), + index_id: this.fb.control(this.stakeholder.index_id, Validators.required), + index_shortName: this.fb.control(this.stakeholder.index_shortName, Validators.required), + statsProfile: this.fb.control(this.stakeholder.statsProfile, Validators.required), + creationDate: this.fb.control(this.stakeholder.creationDate), + alias: this.fb.control(this.stakeholder.alias, + [ + Validators.required, + this.stakeholderUtils.aliasValidatorString( + this.alias.filter(alias => alias !== this.stakeholder.alias) + )] + ), + isDefault: this.fb.control((this.isDefault)), + visibility: this.fb.control(this.stakeholder.visibility, Validators.required), + type: this.fb.control(this.stakeholder.type, Validators.required), + topics: this.fb.control(this.stakeholder.topics), + isUpload: this.fb.control(this.stakeholder.isUpload), + logoUrl: this.fb.control(this.stakeholder.logoUrl), + }); + if (this.stakeholder.isUpload) { + this.stakeholderFb.get('logoUrl').clearValidators(); + this.stakeholderFb.get('logoUrl').updateValueAndValidity(); + } else { + this.stakeholderFb.get('logoUrl').setValidators([StringUtils.urlValidator()]); + this.stakeholderFb.get('logoUrl').updateValueAndValidity(); + } + this.subscriptions.push(this.stakeholderFb.get('isUpload').valueChanges.subscribe(value => { + if (value == true) { this.stakeholderFb.get('logoUrl').clearValidators(); - this.stakeholderFb.get('logoUrl').updateValueAndValidity(); + this.stakeholderFb.updateValueAndValidity(); } else { this.stakeholderFb.get('logoUrl').setValidators([StringUtils.urlValidator()]); - this.stakeholderFb.get('logoUrl').updateValueAndValidity(); + this.stakeholderFb.updateValueAndValidity(); } - this.subscriptions.push(this.stakeholderFb.get('isUpload').valueChanges.subscribe(value => { - if (value == true) { - this.stakeholderFb.get('logoUrl').clearValidators(); - this.stakeholderFb.updateValueAndValidity(); - } else { - this.stakeholderFb.get('logoUrl').setValidators([StringUtils.urlValidator()]); - this.stakeholderFb.updateValueAndValidity(); - } - })); - this.secure = (!this.stakeholderFb.get('logoUrl').value || this.stakeholderFb.get('logoUrl').value.includes('https://')); - this.subscriptions.push(this.stakeholderFb.get('logoUrl').valueChanges.subscribe(value => { - this.secure = (!value || value.includes('https://')); - })); - this.initPhoto(); - if (!isDefault) { - this.subscriptions.push(this.stakeholderFb.get('type').valueChanges.subscribe(value => { - this.onTypeChange(value, defaultStakeholders); - })); - this.stakeholderFb.setControl('defaultId', this.fb.control(stakeholder.defaultId, Validators.required)); - } - if (!isNew) { - this.notification = NotificationUtils.editStakeholder(this.user.firstname + ' ' + this.user.lastname, this.stakeholder.name); - this.notify.reset(this.notification.message); - if (this.isAdmin) { - if (this.disableAlias) { - setTimeout(() => { - this.stakeholderFb.get('alias').disable(); - }, 0); - } - } else { - if(!this.isCurator) { - setTimeout(() => { - this.stakeholderFb.get('statsProfile').disable(); - }, 0); - } + })); + this.secure = (!this.stakeholderFb.get('logoUrl').value || this.stakeholderFb.get('logoUrl').value.includes('https://')); + this.subscriptions.push(this.stakeholderFb.get('logoUrl').valueChanges.subscribe(value => { + this.secure = (!value || value.includes('https://')); + })); + this.initPhoto(); + this.subscriptions.push(this.stakeholderFb.get('type').valueChanges.subscribe(value => { + this.onTypeChange(value, defaultStakeholders); + })); + this.stakeholderFb.setControl('defaultId', this.fb.control(stakeholder.defaultId, (this.isDefault && !this.isNew)?[]:Validators.required)); + if (!this.isNew) { + this.notification = NotificationUtils.editStakeholder(this.user.firstname + ' ' + this.user.lastname, this.stakeholder.name); + this.notify.reset(this.notification.message); + if (this.isAdmin) { + if (this.disableAlias) { setTimeout(() => { this.stakeholderFb.get('alias').disable(); - this.stakeholderFb.get('index_id').disable(); - this.stakeholderFb.get('index_name').disable(); - this.stakeholderFb.get('index_shortName').disable(); + }, 0); + } + } else { + if (!this.isCurator) { + setTimeout(() => { + this.stakeholderFb.get('statsProfile').disable(); }, 0); } setTimeout(() => { - this.stakeholderFb.get('type').disable(); - }, 0); - } else { - this.notification = NotificationUtils.createStakeholder(this.user.firstname + ' ' + this.user.lastname); - this.notify.reset(this.notification.message); - setTimeout(() => { - this.stakeholderFb.get('type').enable(); + this.stakeholderFb.get('alias').disable(); + this.stakeholderFb.get('index_id').disable(); + this.stakeholderFb.get('index_name').disable(); + this.stakeholderFb.get('index_shortName').disable(); }, 0); } - })); + setTimeout(() => { + this.stakeholderFb.get('type').disable(); + }, 0); + } else { + this.notification = NotificationUtils.createStakeholder(this.user.firstname + ' ' + this.user.lastname); + this.notify.reset(this.notification.message); + setTimeout(() => { + this.stakeholderFb.get('type').enable(); + }, 0); + } + })); } public get isAdmin() { return Session.isPortalAdministrator(this.user); } - + public get isCurator() { return this.stakeholder && (this.isAdmin || Session.isCurator(this.stakeholder.type, this.user)); } @@ -277,8 +275,8 @@ export class EditStakeholderComponent implements OnDestroy { return this.stakeholderFb && this.stakeholderFb.dirty; } - public get canChooseType(): boolean { - return !this.stakeholderFb.get('isDefault').value && this.isNew && this.stakeholderFb.get('type').valid && !!this.defaultStakeholdersOptions; + public get canChooseTemplate(): boolean { + return this.isNew && this.stakeholderFb.get('type').valid && !!this.defaultStakeholdersOptions; } reset() { @@ -292,7 +290,7 @@ export class EditStakeholderComponent implements OnDestroy { } onTypeChange(value, defaultStakeholders: Stakeholder[]) { - this.stakeholderFb.setControl('defaultId', this.fb.control(this.stakeholder.defaultId, Validators.required)); + this.stakeholderFb.setControl('defaultId', this.fb.control(this.stakeholder.defaultId, (this.isDefault && !this.isNew)?[]:Validators.required)); this.defaultStakeholdersOptions = [{ label: 'New blank profile', value: '-1' @@ -306,7 +304,7 @@ export class EditStakeholderComponent implements OnDestroy { } public save(callback: Function, errorCallback: Function = null) { - this.loading = true; + this.loading = true; if (this.file) { this.subscriptions.push(this.utilsService.uploadPhoto(this.properties.utilsService + "/upload/" + encodeURIComponent(this.stakeholderFb.getRawValue().type) + "/" + encodeURIComponent(this.stakeholderFb.getRawValue().alias), this.file).subscribe(res => { this.deletePhoto(); @@ -327,12 +325,13 @@ export class EditStakeholderComponent implements OnDestroy { public saveStakeholder(callback: Function, errorCallback: Function = null) { if (this.isNew) { - if (!this.stakeholderFb.getRawValue().isDefault) { - let stakeholder = this.defaultStakeholders.find(value => value._id === this.stakeholderFb.getRawValue().defaultId); - this.stakeholderFb.setValue(this.stakeholderUtils.createFunderFromDefaultProfile(this.stakeholderFb.getRawValue(), - (stakeholder ? stakeholder.topics : []))); - } + let defaultStakeholder = this.defaultStakeholders.find(value => value._id === this.stakeholderFb.getRawValue().defaultId); + this.stakeholderFb.setValue(this.stakeholderUtils.createFunderFromDefaultProfile(this.stakeholderFb.getRawValue(), + (defaultStakeholder ? defaultStakeholder.topics : []), this.stakeholderFb.getRawValue().isDefault)); this.removePhoto(); + if(this.stakeholderFb.getRawValue().isDefault) { + this.stakeholderFb.get('defaultId').setValue(null); + } this.subscriptions.push(this.stakeholderService.buildStakeholder(this.properties.monitorServiceAPIURL, this.stakeholderFb.getRawValue()).subscribe(stakeholder => { this.notification.entity = stakeholder._id; @@ -342,13 +341,13 @@ export class EditStakeholderComponent implements OnDestroy { this.notify.sendNotification(this.notification); NotificationHandler.rise(stakeholder.name + ' has been successfully created'); callback(stakeholder); - this.loading = false; + this.loading = false; }, error => { NotificationHandler.rise('An error has occurred. Please try again later', 'danger'); if (errorCallback) { errorCallback(error) } - this.loading = false; + this.loading = false; })); } else { this.subscriptions.push(this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.stakeholderFb.getRawValue()).subscribe(stakeholder => { @@ -358,14 +357,14 @@ export class EditStakeholderComponent implements OnDestroy { this.notification.groups = [Role.curator(stakeholder.type), Role.manager(stakeholder.type, stakeholder.alias)]; this.notify.sendNotification(this.notification); NotificationHandler.rise(stakeholder.name + ' has been successfully saved'); - callback(stakeholder); - this.loading = false; + callback(stakeholder); + this.loading = false; }, error => { NotificationHandler.rise('An error has occurred. Please try again later', 'danger'); if (errorCallback) { errorCallback(error) } - this.loading = false; + this.loading = false; })); } } diff --git a/src/app/utils/indicator-utils.ts b/src/app/utils/indicator-utils.ts index 2003efd..5212d34 100644 --- a/src/app/utils/indicator-utils.ts +++ b/src/app/utils/indicator-utils.ts @@ -55,39 +55,31 @@ export class StakeholderUtils { return types; } - public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[]): Stakeholder { + public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[], isDefault: boolean = false): Stakeholder { funder.topics = HelperFunctions.copy(defaultTopics); for (let topic of funder.topics) { - topic.defaultId = topic._id; + topic.defaultId = !isDefault?topic._id:null; topic._id = null; for (let category of topic.categories) { - category.defaultId = category._id; + category.defaultId = !isDefault?category._id:null; category._id = null; let subTokeep: SubCategory[] = []; for (let subCategory of category.subCategories) { - subCategory.defaultId = subCategory._id; + subCategory.defaultId = !isDefault?subCategory._id:null; subCategory._id = null; subTokeep.push(subCategory); for (let section of subCategory.charts) { let chartsTokeep: Indicator[] = []; - section.defaultId = section._id; + section.defaultId = !isDefault?section._id:null; section.stakeholderAlias = funder.alias; section._id = null; for (let indicator of section.indicators) { - indicator.defaultId = indicator._id; + indicator.defaultId = !isDefault?indicator._id:null; indicator._id = null; chartsTokeep.push(indicator); for (let indicatorPath of indicator.indicatorPaths) { if (indicatorPath.parameters) { Object.keys(indicatorPath.parameters).forEach(key => { - //TODO check before delete - /*if (indicatorPath.parameters[key].indexOf("_funder_name_") != -1) { - indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_name_", funder.index_name); - } else if (indicatorPath.parameters[key].indexOf("_funder_id_") != -1) { - indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_id_", funder.index_id); - } else if (indicatorPath.parameters[key].indexOf("_fsn_") != -1) { - indicatorPath.parameters[key] = indicatorPath.parameters[key].toString().replace("_fsn_", funder.index_shortName.toLowerCase()); - }*/ if (key == "index_name") { indicatorPath.parameters[key] = funder.index_name; } else if (key == "index_id") { @@ -102,26 +94,12 @@ export class StakeholderUtils { section.indicators = chartsTokeep; } for (let section of subCategory.numbers) { - section.defaultId = section._id; + section.defaultId = !isDefault?section._id:null; section.stakeholderAlias = funder.alias; section._id = null; for (let indicator of section.indicators) { - indicator.defaultId = indicator._id; + indicator.defaultId = !isDefault?indicator._id:null; indicator._id = null; - for (let indicatorPath of indicator.indicatorPaths) { - /* indicatorPath.url = indicatorPath.url.replace("index_id", encodeURIComponent(funder.index_id)); - indicatorPath.url = indicatorPath.url.replace("index_name", encodeURIComponent(funder.index_name)); - indicatorPath.url = indicatorPath.url.replace("index_shortName", encodeURIComponent(funder.index_shortName));*/ - // if(indicatorPath.parameters) { - // indicatorPath.parameters.forEach((value: string, key: string) => { - // if (value.indexOf("_funder_name_")!=-1) { - // indicatorPath.parameters.set(key,value.toString().replace("_funder_name_", funder.index_name)); - // }else if (value.indexOf("_fsn_")!=-1) { - // indicatorPath.parameters.set(key,value.toString().replace("_fsn_", funder.index_shortName.toLowerCase())); - // } - // }); - // } - } } }