diff --git a/projects/aca-folder-rules/src/lib/manage-rules/manage-rules.smart-component.ts b/projects/aca-folder-rules/src/lib/manage-rules/manage-rules.smart-component.ts index 9c624ae41..c7b5d4e28 100644 --- a/projects/aca-folder-rules/src/lib/manage-rules/manage-rules.smart-component.ts +++ b/projects/aca-folder-rules/src/lib/manage-rules/manage-rules.smart-component.ts @@ -34,6 +34,7 @@ import { tap } from 'rxjs/operators'; import { EditRuleDialogSmartComponent } from '../rule-details/edit-rule-dialog.smart-component'; import { MatDialog } from '@angular/material/dialog'; import { ConfirmDialogComponent } from '@alfresco/adf-content-services'; +import { NotificationService } from '@alfresco/adf-core'; @Component({ selector: 'aca-manage-rules', @@ -49,12 +50,14 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy { selectedRule: Rule = null; nodeId: string = null; deletedRuleSubscription$: Subscription; + ruleDialogOnSubmitSubscription$: Subscription; constructor( private location: Location, private folderRulesService: FolderRulesService, private route: ActivatedRoute, - private matDialogService: MatDialog + private matDialogService: MatDialog, + private notificationService: NotificationService ) {} ngOnInit(): void { @@ -93,10 +96,24 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy { } openNewRuleDialog() { - this.matDialogService.open(EditRuleDialogSmartComponent, { + const dialogRef = this.matDialogService.open(EditRuleDialogSmartComponent, { width: '90%', panelClass: 'aca-edit-rule-dialog-container' }); + + this.onSubmitRuleDialog(dialogRef); + } + + onSubmitRuleDialog(dialogRef) { + dialogRef.componentInstance.submitted.subscribe(async (rule) => { + try { + await this.folderRulesService.createRule(this.nodeId, rule); + this.folderRulesService.loadRules(this.nodeId); + dialogRef.close(); + } catch (error) { + this.notificationService.showError(error.response.body.error.errorKey); + } + }); } onRuleDelete(): void { diff --git a/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.html b/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.html index b904f9238..b6f2c9a41 100644 --- a/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.html +++ b/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.html @@ -8,10 +8,10 @@ - + - + diff --git a/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.ts b/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.ts index 57c33de4d..9eeb06541 100644 --- a/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.ts +++ b/projects/aca-folder-rules/src/lib/rule-details/edit-rule-dialog.smart-component.ts @@ -23,7 +23,7 @@ * along with Alfresco. If not, see . */ -import { Component, Inject, ViewEncapsulation } from '@angular/core'; +import { Component, EventEmitter, Inject, Output, ViewEncapsulation } from '@angular/core'; import { MAT_DIALOG_DATA } from '@angular/material/dialog'; import { Rule } from '../model/rule.model'; @@ -41,6 +41,8 @@ export interface EditRuleDialogOptions { export class EditRuleDialogSmartComponent { formValid = false; model: Partial; + formValue: Partial; + @Output() submitted = new EventEmitter>(); constructor(@Inject(MAT_DIALOG_DATA) public options: EditRuleDialogOptions) { this.model = this.options?.model || {}; @@ -57,4 +59,8 @@ export class EditRuleDialogSmartComponent { get submitLabel(): string { return 'ACA_FOLDER_RULES.EDIT_RULE_DIALOG.' + (this.isUpdateMode ? 'UPDATE' : 'CREATE'); } + + onSubmit() { + this.submitted.emit(this.formValue); + } } diff --git a/projects/aca-folder-rules/src/lib/services/folder-rules.service.spec.ts b/projects/aca-folder-rules/src/lib/services/folder-rules.service.spec.ts index 5c918bc5e..87f33740a 100644 --- a/projects/aca-folder-rules/src/lib/services/folder-rules.service.spec.ts +++ b/projects/aca-folder-rules/src/lib/services/folder-rules.service.spec.ts @@ -50,6 +50,7 @@ describe('FolderRulesService', () => { const ruleId = '********-fake-rule-****-********'; const ruleSetId = '-default-'; const params = [{}, {}, {}, {}, {}, ['application/json'], ['application/json']]; + const paramsWithBody = [{}, {}, {}, {}, dummyRules[0], ['application/json'], ['application/json']]; beforeEach(async () => { TestBed.configureTestingModule({ @@ -111,7 +112,6 @@ describe('FolderRulesService', () => { }); describe('toggleRule', () => { - const paramsWithBody = [{}, {}, {}, {}, dummyRules[0], ['application/json'], ['application/json']]; beforeEach(async () => { apiCallSpy = spyOn(folderRulesService, 'apiCall') .withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', paramsWithBody) @@ -138,4 +138,19 @@ describe('FolderRulesService', () => { expect(apiCallSpy).toHaveBeenCalledWith(`/aspects`, 'GET', params); }); }); + + describe('createRule', () => { + beforeEach(async () => { + spyOn(folderRulesService, 'apiCall') + .withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'POST', paramsWithBody) + .and.returnValue(Promise.resolve(dummyRules[0])); + }); + + it('should send correct POST request and return created rule', function () { + folderRulesService.createRule(nodeId, dummyRules[0]).then((result) => { + expect(folderRulesService.createRule).toHaveBeenCalledWith(nodeId, dummyRules[0]); + expect(result).toEqual(dummyRules[0]); + }); + }); + }); }); diff --git a/projects/aca-folder-rules/src/lib/services/folder-rules.service.ts b/projects/aca-folder-rules/src/lib/services/folder-rules.service.ts index 2bb47ccc2..9b61b90a8 100644 --- a/projects/aca-folder-rules/src/lib/services/folder-rules.service.ts +++ b/projects/aca-folder-rules/src/lib/services/folder-rules.service.ts @@ -112,6 +112,18 @@ export class FolderRulesService { ); } + createRule(nodeId: string, rule: Partial, ruleSetId: string = '-default-') { + return this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'POST', [ + {}, + {}, + {}, + {}, + { ...this.addFakeAction(rule) }, + ['application/json'], + ['application/json'] + ]); + } + deleteRule(nodeId: string, ruleId: string, ruleSetId: string = '-default-'): void { this.loadingSource.next(true); from( @@ -157,6 +169,24 @@ export class FolderRulesService { }); } + private addFakeAction(rule): Partial { + 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 { return this.apiService.getInstance().contentClient.callApi(path, httpMethod, ...params); }