[ACS-3511] Update rules (#2701)

* rebase

* fixed a bug conserning isEnabled checkbox

* removed addFakeApect, added unit tests

* linting

* renamed openNewRuleDialog to openCreateUpdateRuleDialog
This commit is contained in:
Nikita Maliarchuk 2022-10-11 10:24:37 +02:00 committed by GitHub
parent 07f3c99b33
commit a804dceade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 30 deletions

View File

@ -90,7 +90,8 @@
"RULES": "rules"
},
"ACTIONS": {
"CREATE_RULE": "Create rule"
"CREATE_RULE": "Create rule",
"EDIT_RULE": "Edit"
}
},
"EMPTY_RULES_LIST": {

View File

@ -25,7 +25,7 @@
class="aca-manage-rules__actions-bar__title__breadcrumb"></adf-breadcrumb>
</adf-toolbar-title>
<button mat-flat-button color="primary" (click)="openNewRuleDialog()">{{ 'ACA_FOLDER_RULES.MANAGE_RULES.TOOLBAR.ACTIONS.CREATE_RULE' | translate }}</button>
<button mat-flat-button color="primary" (click)="openCreateUpdateRuleDialog()">{{ 'ACA_FOLDER_RULES.MANAGE_RULES.TOOLBAR.ACTIONS.CREATE_RULE' | translate }}</button>
</adf-toolbar>
<mat-divider></mat-divider>
@ -40,6 +40,9 @@
<button mat-icon-button (click)="onRuleDelete()" id="delete-rule-btn">
<mat-icon>delete_outline</mat-icon>
</button>
<button mat-stroked-button (click)="onRuleUpdate()" id="edit-rule-btn">
{{ 'ACA_FOLDER_RULES.MANAGE_RULES.TOOLBAR.ACTIONS.EDIT_RULE' | translate }}
</button>
</div>
</div>
<p>{{ selectedRule.description }}</p>

View File

@ -103,10 +103,13 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
this.selectedRule = rule;
}
openNewRuleDialog() {
openCreateUpdateRuleDialog(model = {}) {
const dialogRef = this.matDialogService.open(EditRuleDialogSmartComponent, {
width: '90%',
panelClass: 'aca-edit-rule-dialog-container'
panelClass: 'aca-edit-rule-dialog-container',
data: {
model
}
});
this.onSubmitRuleDialog(dialogRef);
@ -115,7 +118,11 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
onSubmitRuleDialog(dialogRef) {
dialogRef.componentInstance.submitted.subscribe(async (rule) => {
try {
await this.folderRulesService.createRule(this.nodeId, rule);
if (rule.id) {
await this.folderRulesService.updateRule(this.nodeId, rule.id, rule);
} else {
await this.folderRulesService.createRule(this.nodeId, rule);
}
this.folderRulesService.loadRules(this.nodeId);
dialogRef.close();
} catch (error) {
@ -140,4 +147,8 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
}
});
}
onRuleUpdate(): void {
this.openCreateUpdateRuleDialog(this.selectedRule);
}
}

View File

@ -47,12 +47,12 @@ export class EditRuleDialogSmartComponent implements OnInit {
actionDefinitions$ = this.actionsService.actionDefinitionsListing$;
loading$ = this.actionsService.loading$;
constructor(@Inject(MAT_DIALOG_DATA) public options: EditRuleDialogOptions, private actionsService: ActionsService) {
this.model = this.options?.model || {};
constructor(@Inject(MAT_DIALOG_DATA) public data: EditRuleDialogOptions, private actionsService: ActionsService) {
this.model = this.data?.model || {};
}
get isUpdateMode(): boolean {
return !!this.options?.model?.id;
return !!this.data?.model?.id;
}
get title(): string {

View File

@ -25,11 +25,10 @@
</mat-checkbox>
<mat-checkbox
[attr.data-automation-id]="'rule-option-checkbox-enabled'"
[checked]="!form.value.isEnabled" *ngIf="!preview"
[checked]="!form.get('isEnabled').value" *ngIf="!preview"
(change)="form.get('isEnabled').setValue(!$event.checked)">
{{ 'ACA_FOLDER_RULES.RULE_DETAILS.OPTIONS.DISABLE_RULE' | translate }}
</mat-checkbox>
</div>
</div>

View File

@ -62,6 +62,7 @@ export class RuleDetailsUiComponent implements OnInit, OnDestroy {
}
set value(newValue: Partial<Rule>) {
newValue = {
id: newValue.id || FolderRulesService.emptyRule.id,
name: newValue.name || FolderRulesService.emptyRule.name,
description: newValue.description || FolderRulesService.emptyRule.description,
triggers: newValue.triggers || FolderRulesService.emptyRule.triggers,
@ -69,7 +70,7 @@ export class RuleDetailsUiComponent implements OnInit, OnDestroy {
isAsynchronous: newValue.isAsynchronous || FolderRulesService.emptyRule.isAsynchronous,
errorScript: newValue.errorScript || FolderRulesService.emptyRule.errorScript,
isInheritable: newValue.isInheritable || FolderRulesService.emptyRule.isInheritable,
isEnabled: newValue.isEnabled || FolderRulesService.emptyRule.isEnabled,
isEnabled: typeof newValue.isInheritable == 'boolean' ? newValue.isEnabled : FolderRulesService.emptyRule.isEnabled,
actions: newValue.actions || FolderRulesService.emptyRule.actions
};
if (this.form) {
@ -118,6 +119,7 @@ export class RuleDetailsUiComponent implements OnInit, OnDestroy {
ngOnInit() {
this.form = new UntypedFormGroup({
id: new UntypedFormControl(this.value.id),
name: new UntypedFormControl(this.value.name || '', Validators.required),
description: new UntypedFormControl(this.value.description || ''),
triggers: new UntypedFormControl(this.value.triggers || ['inbound'], Validators.required),

View File

@ -151,4 +151,13 @@ describe('FolderRulesService', () => {
expect(result).toEqual(dummyRules[0]);
});
});
it('should send correct PUT request to update rule and return it', async () => {
spyOn<any>(folderRulesService, 'apiCall')
.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', paramsWithBody)
.and.returnValue(Promise.resolve(dummyRules[0]));
const result = await folderRulesService.updateRule(nodeId, ruleId, dummyRules[0]);
expect(result).toEqual(dummyRules[0]);
});
});

View File

@ -118,7 +118,19 @@ export class FolderRulesService {
{},
{},
{},
{ ...this.addFakeAction(rule) },
{ ...rule },
['application/json'],
['application/json']
]);
}
updateRule(nodeId: string, ruleId: string, rule: Rule, ruleSetId: string = '-default-') {
return this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', [
{},
{},
{},
{},
{ ...rule },
['application/json'],
['application/json']
]);
@ -169,24 +181,6 @@ export class FolderRulesService {
});
}
private addFakeAction(rule): Partial<Rule> {
if (rule.actions) {
return rule;
} else {
return {
...rule,
actions: [
{
actionDefinitionId: 'add-features',
params: {
'aspect-name': 'ai:creativeWorks'
}
}
]
};
}
}
private apiCall(path: string, httpMethod: string, params?: any[]): Promise<any> {
return this.apiService.getInstance().contentClient.callApi(path, httpMethod, ...params);
}