On create default stakeholder: add choose template in order to initialize from other default profile.

This commit is contained in:
Konstantinos Triantafyllou 2023-05-30 15:26:52 +03:00
parent ab13a41625
commit a30c492ffb
2 changed files with 109 additions and 132 deletions

View File

@ -90,17 +90,17 @@ import {error} from "protractor";
<!-- Full width error message --> <!-- Full width error message -->
<div *ngIf="uploadError" class="uk-text-danger uk-margin-small-top uk-width-1-1">{{uploadError}}</div> <div *ngIf="uploadError" class="uk-text-danger uk-margin-small-top uk-width-1-1">{{uploadError}}</div>
</div> </div>
<div [class]="canChooseType ? 'uk-width-1-3@m' : 'uk-width-1-2@m'"> <div [class]="canChooseTemplate ? 'uk-width-1-3@m' : 'uk-width-1-2@m'">
<div input [formInput]="stakeholderFb.get('visibility')" <div input [formInput]="stakeholderFb.get('visibility')"
[placeholder]="'Select a status'" [placeholder]="'Select a status'"
[options]="stakeholderUtils.statuses" type="select"></div> [options]="stakeholderUtils.statuses" type="select"></div>
</div> </div>
<div [class]="canChooseType ? 'uk-width-1-3@m' : 'uk-width-1-2@m'"> <div [class]="canChooseTemplate ? 'uk-width-1-3@m' : 'uk-width-1-2@m'">
<div input [formInput]="stakeholderFb.get('type')" <div input [formInput]="stakeholderFb.get('type')"
[placeholder]="'Select a type'" [placeholder]="'Select a type'"
[options]="types" type="select"></div> [options]="types" type="select"></div>
</div> </div>
<ng-container *ngIf="canChooseType"> <ng-container *ngIf="canChooseTemplate">
<div class="uk-width-1-3@m"> <div class="uk-width-1-3@m">
<div [placeholder]="'Select a template'" <div [placeholder]="'Select a template'"
input [formInput]="stakeholderFb.get('defaultId')" input [formInput]="stakeholderFb.get('defaultId')"
@ -140,7 +140,7 @@ export class EditStakeholderComponent implements OnDestroy {
public deleteCurrentPhoto: boolean = false; public deleteCurrentPhoto: boolean = false;
private maxsize: number = 200 * 1024; private maxsize: number = 200 * 1024;
user: User; user: User;
@ViewChild('notify', { static: true }) notify: NotifyFormComponent; @ViewChild('notify', {static: true}) notify: NotifyFormComponent;
private notification: Notification; private notification: Notification;
constructor(private fb: UntypedFormBuilder, constructor(private fb: UntypedFormBuilder,
@ -162,101 +162,99 @@ export class EditStakeholderComponent implements OnDestroy {
this.isDefault = isDefault; this.isDefault = isDefault;
this.isNew = isNew; this.isNew = isNew;
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => { this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
this.user = user; this.user = user;
if(this.isCurator) { if (this.isCurator) {
this.subscriptions.push(this.statsProfileService.getStatsProfiles().subscribe(statsProfiles => { this.subscriptions.push(this.statsProfileService.getStatsProfiles().subscribe(statsProfiles => {
this.statsProfiles = statsProfiles; this.statsProfiles = statsProfiles;
}, error => { }, error => {
this.statsProfiles = [];
}));
} else {
this.statsProfiles = []; this.statsProfiles = [];
} }));
this.types = this.stakeholderUtils.getTypesByUserRoles(this.user, this.stakeholder.alias); } else {
this.stakeholderFb = this.fb.group({ this.statsProfiles = [];
_id: this.fb.control(this.stakeholder._id), }
defaultId: this.fb.control(this.stakeholder.defaultId), this.types = this.stakeholderUtils.getTypesByUserRoles(this.user, this.stakeholder.alias);
name: this.fb.control(this.stakeholder.name, Validators.required), this.stakeholderFb = this.fb.group({
description: this.fb.control(this.stakeholder.description), _id: this.fb.control(this.stakeholder._id),
index_name: this.fb.control(this.stakeholder.index_name, Validators.required), defaultId: this.fb.control(this.stakeholder.defaultId),
index_id: this.fb.control(this.stakeholder.index_id, Validators.required), name: this.fb.control(this.stakeholder.name, Validators.required),
index_shortName: this.fb.control(this.stakeholder.index_shortName, Validators.required), description: this.fb.control(this.stakeholder.description),
statsProfile: this.fb.control(this.stakeholder.statsProfile, Validators.required), index_name: this.fb.control(this.stakeholder.index_name, Validators.required),
creationDate: this.fb.control(this.stakeholder.creationDate), index_id: this.fb.control(this.stakeholder.index_id, Validators.required),
alias: this.fb.control(this.stakeholder.alias, index_shortName: this.fb.control(this.stakeholder.index_shortName, Validators.required),
[ statsProfile: this.fb.control(this.stakeholder.statsProfile, Validators.required),
Validators.required, creationDate: this.fb.control(this.stakeholder.creationDate),
this.stakeholderUtils.aliasValidatorString( alias: this.fb.control(this.stakeholder.alias,
this.alias.filter(alias => alias !== this.stakeholder.alias) [
)] Validators.required,
), this.stakeholderUtils.aliasValidatorString(
isDefault: this.fb.control((this.isDefault)), this.alias.filter(alias => alias !== this.stakeholder.alias)
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), isDefault: this.fb.control((this.isDefault)),
isUpload: this.fb.control(this.stakeholder.isUpload), visibility: this.fb.control(this.stakeholder.visibility, Validators.required),
logoUrl: this.fb.control(this.stakeholder.logoUrl), type: this.fb.control(this.stakeholder.type, Validators.required),
}); topics: this.fb.control(this.stakeholder.topics),
if (this.stakeholder.isUpload) { 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').clearValidators();
this.stakeholderFb.get('logoUrl').updateValueAndValidity(); this.stakeholderFb.updateValueAndValidity();
} else { } else {
this.stakeholderFb.get('logoUrl').setValidators([StringUtils.urlValidator()]); 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.secure = (!this.stakeholderFb.get('logoUrl').value || this.stakeholderFb.get('logoUrl').value.includes('https://'));
this.stakeholderFb.get('logoUrl').clearValidators(); this.subscriptions.push(this.stakeholderFb.get('logoUrl').valueChanges.subscribe(value => {
this.stakeholderFb.updateValueAndValidity(); this.secure = (!value || value.includes('https://'));
} else { }));
this.stakeholderFb.get('logoUrl').setValidators([StringUtils.urlValidator()]); this.initPhoto();
this.stakeholderFb.updateValueAndValidity(); this.subscriptions.push(this.stakeholderFb.get('type').valueChanges.subscribe(value => {
} this.onTypeChange(value, defaultStakeholders);
})); }));
this.secure = (!this.stakeholderFb.get('logoUrl').value || this.stakeholderFb.get('logoUrl').value.includes('https://')); this.stakeholderFb.setControl('defaultId', this.fb.control(stakeholder.defaultId, (this.isDefault && !this.isNew)?[]:Validators.required));
this.subscriptions.push(this.stakeholderFb.get('logoUrl').valueChanges.subscribe(value => { if (!this.isNew) {
this.secure = (!value || value.includes('https://')); this.notification = NotificationUtils.editStakeholder(this.user.firstname + ' ' + this.user.lastname, this.stakeholder.name);
})); this.notify.reset(this.notification.message);
this.initPhoto(); if (this.isAdmin) {
if (!isDefault) { if (this.disableAlias) {
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);
}
setTimeout(() => { setTimeout(() => {
this.stakeholderFb.get('alias').disable(); this.stakeholderFb.get('alias').disable();
this.stakeholderFb.get('index_id').disable(); }, 0);
this.stakeholderFb.get('index_name').disable(); }
this.stakeholderFb.get('index_shortName').disable(); } else {
if (!this.isCurator) {
setTimeout(() => {
this.stakeholderFb.get('statsProfile').disable();
}, 0); }, 0);
} }
setTimeout(() => { setTimeout(() => {
this.stakeholderFb.get('type').disable(); this.stakeholderFb.get('alias').disable();
}, 0); this.stakeholderFb.get('index_id').disable();
} else { this.stakeholderFb.get('index_name').disable();
this.notification = NotificationUtils.createStakeholder(this.user.firstname + ' ' + this.user.lastname); this.stakeholderFb.get('index_shortName').disable();
this.notify.reset(this.notification.message);
setTimeout(() => {
this.stakeholderFb.get('type').enable();
}, 0); }, 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() { public get isAdmin() {
@ -277,8 +275,8 @@ export class EditStakeholderComponent implements OnDestroy {
return this.stakeholderFb && this.stakeholderFb.dirty; return this.stakeholderFb && this.stakeholderFb.dirty;
} }
public get canChooseType(): boolean { public get canChooseTemplate(): boolean {
return !this.stakeholderFb.get('isDefault').value && this.isNew && this.stakeholderFb.get('type').valid && !!this.defaultStakeholdersOptions; return this.isNew && this.stakeholderFb.get('type').valid && !!this.defaultStakeholdersOptions;
} }
reset() { reset() {
@ -292,7 +290,7 @@ export class EditStakeholderComponent implements OnDestroy {
} }
onTypeChange(value, defaultStakeholders: Stakeholder[]) { 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 = [{ this.defaultStakeholdersOptions = [{
label: 'New blank profile', label: 'New blank profile',
value: '-1' value: '-1'
@ -306,7 +304,7 @@ export class EditStakeholderComponent implements OnDestroy {
} }
public save(callback: Function, errorCallback: Function = null) { public save(callback: Function, errorCallback: Function = null) {
this.loading = true; this.loading = true;
if (this.file) { 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.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(); this.deletePhoto();
@ -327,12 +325,13 @@ export class EditStakeholderComponent implements OnDestroy {
public saveStakeholder(callback: Function, errorCallback: Function = null) { public saveStakeholder(callback: Function, errorCallback: Function = null) {
if (this.isNew) { if (this.isNew) {
if (!this.stakeholderFb.getRawValue().isDefault) { let defaultStakeholder = this.defaultStakeholders.find(value => value._id === this.stakeholderFb.getRawValue().defaultId);
let stakeholder = this.defaultStakeholders.find(value => value._id === this.stakeholderFb.getRawValue().defaultId); this.stakeholderFb.setValue(this.stakeholderUtils.createFunderFromDefaultProfile(this.stakeholderFb.getRawValue(),
this.stakeholderFb.setValue(this.stakeholderUtils.createFunderFromDefaultProfile(this.stakeholderFb.getRawValue(), (defaultStakeholder ? defaultStakeholder.topics : []), this.stakeholderFb.getRawValue().isDefault));
(stakeholder ? stakeholder.topics : [])));
}
this.removePhoto(); this.removePhoto();
if(this.stakeholderFb.getRawValue().isDefault) {
this.stakeholderFb.get('defaultId').setValue(null);
}
this.subscriptions.push(this.stakeholderService.buildStakeholder(this.properties.monitorServiceAPIURL, this.subscriptions.push(this.stakeholderService.buildStakeholder(this.properties.monitorServiceAPIURL,
this.stakeholderFb.getRawValue()).subscribe(stakeholder => { this.stakeholderFb.getRawValue()).subscribe(stakeholder => {
this.notification.entity = stakeholder._id; this.notification.entity = stakeholder._id;
@ -342,13 +341,13 @@ export class EditStakeholderComponent implements OnDestroy {
this.notify.sendNotification(this.notification); this.notify.sendNotification(this.notification);
NotificationHandler.rise(stakeholder.name + ' has been <b>successfully created</b>'); NotificationHandler.rise(stakeholder.name + ' has been <b>successfully created</b>');
callback(stakeholder); callback(stakeholder);
this.loading = false; this.loading = false;
}, error => { }, error => {
NotificationHandler.rise('An error has occurred. Please try again later', 'danger'); NotificationHandler.rise('An error has occurred. Please try again later', 'danger');
if (errorCallback) { if (errorCallback) {
errorCallback(error) errorCallback(error)
} }
this.loading = false; this.loading = false;
})); }));
} else { } else {
this.subscriptions.push(this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.stakeholderFb.getRawValue()).subscribe(stakeholder => { 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.notification.groups = [Role.curator(stakeholder.type), Role.manager(stakeholder.type, stakeholder.alias)];
this.notify.sendNotification(this.notification); this.notify.sendNotification(this.notification);
NotificationHandler.rise(stakeholder.name + ' has been <b>successfully saved</b>'); NotificationHandler.rise(stakeholder.name + ' has been <b>successfully saved</b>');
callback(stakeholder); callback(stakeholder);
this.loading = false; this.loading = false;
}, error => { }, error => {
NotificationHandler.rise('An error has occurred. Please try again later', 'danger'); NotificationHandler.rise('An error has occurred. Please try again later', 'danger');
if (errorCallback) { if (errorCallback) {
errorCallback(error) errorCallback(error)
} }
this.loading = false; this.loading = false;
})); }));
} }
} }

View File

@ -55,39 +55,31 @@ export class StakeholderUtils {
return types; return types;
} }
public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[]): Stakeholder { public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[], isDefault: boolean = false): Stakeholder {
funder.topics = HelperFunctions.copy(defaultTopics); funder.topics = HelperFunctions.copy(defaultTopics);
for (let topic of funder.topics) { for (let topic of funder.topics) {
topic.defaultId = topic._id; topic.defaultId = !isDefault?topic._id:null;
topic._id = null; topic._id = null;
for (let category of topic.categories) { for (let category of topic.categories) {
category.defaultId = category._id; category.defaultId = !isDefault?category._id:null;
category._id = null; category._id = null;
let subTokeep: SubCategory[] = []; let subTokeep: SubCategory[] = [];
for (let subCategory of category.subCategories) { for (let subCategory of category.subCategories) {
subCategory.defaultId = subCategory._id; subCategory.defaultId = !isDefault?subCategory._id:null;
subCategory._id = null; subCategory._id = null;
subTokeep.push(subCategory); subTokeep.push(subCategory);
for (let section of subCategory.charts) { for (let section of subCategory.charts) {
let chartsTokeep: Indicator[] = []; let chartsTokeep: Indicator[] = [];
section.defaultId = section._id; section.defaultId = !isDefault?section._id:null;
section.stakeholderAlias = funder.alias; section.stakeholderAlias = funder.alias;
section._id = null; section._id = null;
for (let indicator of section.indicators) { for (let indicator of section.indicators) {
indicator.defaultId = indicator._id; indicator.defaultId = !isDefault?indicator._id:null;
indicator._id = null; indicator._id = null;
chartsTokeep.push(indicator); chartsTokeep.push(indicator);
for (let indicatorPath of indicator.indicatorPaths) { for (let indicatorPath of indicator.indicatorPaths) {
if (indicatorPath.parameters) { if (indicatorPath.parameters) {
Object.keys(indicatorPath.parameters).forEach(key => { 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") { if (key == "index_name") {
indicatorPath.parameters[key] = funder.index_name; indicatorPath.parameters[key] = funder.index_name;
} else if (key == "index_id") { } else if (key == "index_id") {
@ -102,26 +94,12 @@ export class StakeholderUtils {
section.indicators = chartsTokeep; section.indicators = chartsTokeep;
} }
for (let section of subCategory.numbers) { for (let section of subCategory.numbers) {
section.defaultId = section._id; section.defaultId = !isDefault?section._id:null;
section.stakeholderAlias = funder.alias; section.stakeholderAlias = funder.alias;
section._id = null; section._id = null;
for (let indicator of section.indicators) { for (let indicator of section.indicators) {
indicator.defaultId = indicator._id; indicator.defaultId = !isDefault?indicator._id:null;
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()));
// }
// });
// }
}
} }
} }