[ACS-3887] Rule set listing, including linked & inherited rules (#2780)

* [ACS-3887] Rule set listing to include linked & inherited rules

* Handled rules & rule sets reloading after a create/update/delete operation

* Linting

* Start rewrite of folder rules service unit tests

* Rules service and rule sets service unit tests

* Readd rules services create, update & delete unit tests

* rule set list ui component unit tests

* Manage rules component unit tests

* Remove & modify comments
This commit is contained in:
Thomas Hunter 2022-11-16 16:29:26 +00:00 committed by GitHub
parent cc9af931c6
commit c75091bf59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 1220 additions and 465 deletions

View File

@ -105,6 +105,15 @@
"TITLE": "Delete rule", "TITLE": "Delete rule",
"MESSAGE": "Are you sure you want to delete this rule?" "MESSAGE": "Are you sure you want to delete this rule?"
} }
},
"RULE_LIST": {
"OWNED_BY_THIS_FOLDER": "Owned by this folder",
"LINKED_FROM": "Linked from",
"INHERITED_FROM": "Inherited from",
"LOAD_MORE_RULE_SETS": "Load more rule sets",
"LOADING_RULE_SETS": "Loading rule sets",
"LOAD_MORE_RULES": "Load more rules",
"LOADING_RULES": "Loading rules"
} }
} }
} }

View File

@ -36,12 +36,13 @@ import { RuleDetailsUiComponent } from './rule-details/rule-details.ui-component
import { RuleSimpleConditionUiComponent } from './rule-details/conditions/rule-simple-condition.ui-component'; import { RuleSimpleConditionUiComponent } from './rule-details/conditions/rule-simple-condition.ui-component';
import { GenericErrorModule, PageLayoutModule } from '@alfresco/aca-shared'; import { GenericErrorModule, PageLayoutModule } from '@alfresco/aca-shared';
import { BreadcrumbModule, DocumentListModule } from '@alfresco/adf-content-services'; import { BreadcrumbModule, DocumentListModule } from '@alfresco/adf-content-services';
import { RuleListItemUiComponent } from './rules-list/rule/rule-list-item.ui-component'; import { RuleListItemUiComponent } from './rule-list/rule-list-item/rule-list-item.ui-component';
import { RuleListUiComponent } from './rules-list/rule-list.ui-component'; import { RuleListUiComponent } from './rule-list/rule-list/rule-list.ui-component';
import { RuleTriggersUiComponent } from './rule-details/triggers/rule-triggers.ui-component'; import { RuleTriggersUiComponent } from './rule-details/triggers/rule-triggers.ui-component';
import { RuleOptionsUiComponent } from './rule-details/options/rule-options.ui-component'; import { RuleOptionsUiComponent } from './rule-details/options/rule-options.ui-component';
import { RuleActionListUiComponent } from './rule-details/actions/rule-action-list.ui-component'; import { RuleActionListUiComponent } from './rule-details/actions/rule-action-list.ui-component';
import { RuleActionUiComponent } from './rule-details/actions/rule-action.ui-component'; import { RuleActionUiComponent } from './rule-details/actions/rule-action.ui-component';
import { RuleSetListUiComponent } from './rule-list/rule-set-list/rule-set-list.ui-component';
const routes: Routes = [ const routes: Routes = [
{ {
@ -69,9 +70,10 @@ const routes: Routes = [
RuleActionUiComponent, RuleActionUiComponent,
RuleCompositeConditionUiComponent, RuleCompositeConditionUiComponent,
RuleDetailsUiComponent, RuleDetailsUiComponent,
RuleSimpleConditionUiComponent,
RuleListUiComponent, RuleListUiComponent,
RuleListItemUiComponent, RuleListItemUiComponent,
RuleSetListUiComponent,
RuleSimpleConditionUiComponent,
RuleTriggersUiComponent, RuleTriggersUiComponent,
RuleOptionsUiComponent RuleOptionsUiComponent
] ]

View File

@ -12,7 +12,7 @@
<aca-page-layout-content> <aca-page-layout-content>
<div class="main-content"> <div class="main-content">
<ng-container *ngIf="(rulesLoading$ | async) || (actionsLoading$ | async); else onLoaded"> <ng-container *ngIf="(ruleSetsLoading$ | async) || (actionsLoading$ | async); else onLoaded">
<mat-progress-bar color="primary" mode="indeterminate"></mat-progress-bar> <mat-progress-bar color="primary" mode="indeterminate"></mat-progress-bar>
</ng-container> </ng-container>
@ -29,12 +29,23 @@
</adf-toolbar> </adf-toolbar>
<mat-divider></mat-divider> <mat-divider></mat-divider>
<div class="aca-manage-rules__container" *ngIf="(rules$ | async).length > 0 ; else emptyContent"> <div class="aca-manage-rules__container" *ngIf="(ruleSetListing$ | async).length > 0; else emptyContent">
<aca-rule-list [rules]="rules$ | async" (ruleSelected)="onRuleSelected($event)" <aca-rule-set-list
[selectedRule]="selectedRule" [nodeId]="nodeId"></aca-rule-list> [folderId]="nodeId"
[ruleSets]="ruleSetListing$ | async"
[hasMoreRuleSets]="hasMoreRuleSets$ | async"
[ruleSetsLoading]="ruleSetsLoading$ | async"
[selectedRule]="selectedRule$ | async"
(loadMoreRuleSets)="onLoadMoreRuleSets()"
(loadMoreRules)="onLoadMoreRules($event)"
(navigateToOtherFolder)="onNavigateToOtherFolder($event)"
(selectRule)="onSelectRule($event)"
(ruleEnabledChanged)="onRuleEnabledToggle($event[0], $event[1])">
</aca-rule-set-list>
<div class="aca-manage-rules__container__rule-details"> <div class="aca-manage-rules__container__rule-details">
<div class="aca-manage-rules__container__rule-details__header"> <div class="aca-manage-rules__container__rule-details__header" *ngIf="(selectedRule$ | async) as selectedRule">
<div class="aca-manage-rules__container__rule-details__header__title"> <div class="aca-manage-rules__container__rule-details__header__title">
<div class="aca-manage-rules__container__rule-details__header__title__name"> <div class="aca-manage-rules__container__rule-details__header__title__name">
{{ selectedRule.name }} {{ selectedRule.name }}
@ -45,16 +56,16 @@
</div> </div>
<div class="aca-manage-rules__container__rule-details__header__buttons"> <div class="aca-manage-rules__container__rule-details__header__buttons">
<button mat-stroked-button (click)="onRuleDelete()" id="delete-rule-btn"> <button mat-stroked-button (click)="onRuleDeleteButtonClicked(selectedRule)" id="delete-rule-btn">
<mat-icon>delete_outline</mat-icon> <mat-icon>delete_outline</mat-icon>
</button> </button>
<button mat-stroked-button (click)="onRuleUpdate()" id="edit-rule-btn"> <button mat-stroked-button (click)="openCreateUpdateRuleDialog(selectedRule)" id="edit-rule-btn">
{{ 'ACA_FOLDER_RULES.MANAGE_RULES.TOOLBAR.ACTIONS.EDIT_RULE' | translate }} {{ 'ACA_FOLDER_RULES.MANAGE_RULES.TOOLBAR.ACTIONS.EDIT_RULE' | translate }}
</button> </button>
</div> </div>
</div> </div>
<div class="aca-manage-rules__container__rule-details__content"> <div class="aca-manage-rules__container__rule-details__content" *ngIf="(selectedRule$ | async) as selectedRule">
<aca-rule-details <aca-rule-details
[actionDefinitions]="actionDefinitions$ | async" [actionDefinitions]="actionDefinitions$ | async"
[readOnly]="true" [readOnly]="true"

View File

@ -21,11 +21,12 @@
padding: 20px; padding: 20px;
gap: 12px; gap: 12px;
overflow-y: auto; overflow-y: auto;
flex: 1;
&__rule-details { &__rule-details {
border: 1px solid var(--theme-border-color); border: 1px solid var(--theme-border-color);
border-radius: 12px; border-radius: 12px;
overflow-y: scroll; overflow-y: auto;
&__header { &__header {
position: sticky; position: sticky;

View File

@ -23,74 +23,83 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AcaFolderRulesModule, ManageRulesSmartComponent } from '@alfresco/aca-folder-rules'; import { AcaFolderRulesModule, ManageRulesSmartComponent } from '@alfresco/aca-folder-rules';
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { CoreTestingModule } from '@alfresco/adf-core'; import { CoreTestingModule } from '@alfresco/adf-core';
import { FolderRulesService } from '../services/folder-rules.service'; import { FolderRulesService } from '../services/folder-rules.service';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { dummyRules } from '../mock/rules.mock'; import { ruleSetsMock } from '../mock/rule-sets.mock';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { dummyNodeInfo } from '../mock/node.mock'; import { owningFolderIdMock, owningFolderMock } from '../mock/node.mock';
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { ActionsService } from '../services/actions.service'; import { ActionsService } from '../services/actions.service';
import { FolderRuleSetsService } from '../services/folder-rule-sets.service';
import { ruleMock } from '../mock/rules.mock';
import { Store } from '@ngrx/store';
describe('ManageRulesSmartComponent', () => { describe('ManageRulesSmartComponent', () => {
let fixture: ComponentFixture<ManageRulesSmartComponent>; let fixture: ComponentFixture<ManageRulesSmartComponent>;
let component: ManageRulesSmartComponent; let component: ManageRulesSmartComponent;
let debugElement: DebugElement; let debugElement: DebugElement;
let folderRuleSetsService: FolderRuleSetsService;
let folderRulesService: FolderRulesService; let folderRulesService: FolderRulesService;
let actionsService: ActionsService; let actionsService: ActionsService;
beforeEach( beforeEach(() => {
waitForAsync(() => { TestBed.configureTestingModule({
const folderRulesServiceSpy = jasmine.createSpyObj('FolderRulesService', ['loadRules', 'deleteRule']); imports: [CoreTestingModule, AcaFolderRulesModule],
TestBed.configureTestingModule({ providers: [
imports: [CoreTestingModule, AcaFolderRulesModule], FolderRuleSetsService,
providers: [ FolderRulesService,
{ provide: FolderRulesService, useValue: folderRulesServiceSpy }, { provide: Store, useValue: { dispatch: () => {} } },
{ provide: ActivatedRoute, useValue: { params: of({ nodeId: 1 }) } } { provide: ActivatedRoute, useValue: { params: of({ nodeId: owningFolderIdMock }) } }
] ]
}) });
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(ManageRulesSmartComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
folderRulesService = TestBed.inject<FolderRulesService>(FolderRulesService);
actionsService = TestBed.inject<ActionsService>(ActionsService);
});
})
);
it('should display aca-rules-list and aca-rule-details', () => { fixture = TestBed.createComponent(ManageRulesSmartComponent);
folderRulesService.deletedRuleId$ = of(null); component = fixture.componentInstance;
folderRulesService.folderInfo$ = of(dummyNodeInfo); debugElement = fixture.debugElement;
folderRulesService.rulesListing$ = of(dummyRules);
folderRulesService.loading$ = of(false); folderRuleSetsService = TestBed.inject(FolderRuleSetsService);
folderRulesService = TestBed.inject(FolderRulesService);
actionsService = TestBed.inject(ActionsService);
spyOn(actionsService, 'loadActionDefinitions').and.stub();
});
it('should show a list of rule sets and rules', () => {
const loadRuleSetsSpy = spyOn(folderRuleSetsService, 'loadRuleSets').and.stub();
folderRuleSetsService.folderInfo$ = of(owningFolderMock);
folderRuleSetsService.ruleSetListing$ = of(ruleSetsMock);
folderRuleSetsService.isLoading$ = of(false);
folderRulesService.selectedRule$ = of(ruleMock('owned-rule-1'));
actionsService.loading$ = of(false); actionsService.loading$ = of(false);
fixture.detectChanges(); fixture.detectChanges();
expect(component).toBeTruthy(); expect(component).toBeTruthy();
expect(folderRulesService.loadRules).toHaveBeenCalledOnceWith(component.nodeId); expect(loadRuleSetsSpy).toHaveBeenCalledOnceWith(component.nodeId);
const ruleSets = debugElement.queryAll(By.css(`[data-automation-id="rule-set-list-item"]`));
const rules = debugElement.queryAll(By.css('.aca-rule-list-item')); const rules = debugElement.queryAll(By.css('.aca-rule-list-item'));
const ruleDetails = debugElement.queryAll(By.css('aca-rule-details')); const ruleDetails = debugElement.query(By.css('aca-rule-details'));
const deleteRuleBtn = debugElement.query(By.css('#delete-rule-btn')); const deleteRuleBtn = debugElement.query(By.css('#delete-rule-btn'));
expect(rules.length).toBe(2, 'unexpected number of aca-rule'); expect(ruleSets.length).toBe(3, 'unexpected number of rule sets');
expect(ruleDetails.length).toBeTruthy('aca-rule-details was not rendered'); expect(rules.length).toBe(6, 'unexpected number of aca-rule-list-item');
expect(ruleDetails).toBeTruthy('aca-rule-details was not rendered');
expect(deleteRuleBtn).toBeTruthy('no delete rule button'); expect(deleteRuleBtn).toBeTruthy('no delete rule button');
}); });
it('should only show adf-empty-content if provided node has no rules defined yet', () => { it('should only show adf-empty-content if node has no rules defined yet', () => {
folderRulesService.folderInfo$ = of(dummyNodeInfo); folderRuleSetsService.folderInfo$ = of(owningFolderMock);
folderRulesService.rulesListing$ = of([]); folderRuleSetsService.ruleSetListing$ = of([]);
folderRulesService.loading$ = of(false); folderRuleSetsService.isLoading$ = of(false);
folderRulesService.deletedRuleId$ = of(null);
actionsService.loading$ = of(false); actionsService.loading$ = of(false);
fixture.detectChanges(); fixture.detectChanges();
@ -98,19 +107,18 @@ describe('ManageRulesSmartComponent', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
const adfEmptyContent = debugElement.query(By.css('adf-empty-content')); const adfEmptyContent = debugElement.query(By.css('adf-empty-content'));
const rules = debugElement.query(By.css('.aca-rule')); const ruleSets = debugElement.queryAll(By.css(`[data-automation-id="rule-set-list-item"]`));
const ruleDetails = debugElement.query(By.css('aca-rule-details')); const ruleDetails = debugElement.query(By.css('aca-rule-details'));
expect(adfEmptyContent).toBeTruthy(); expect(adfEmptyContent).toBeTruthy();
expect(rules).toBeFalsy(); expect(ruleSets.length).toBe(0);
expect(ruleDetails).toBeFalsy(); expect(ruleDetails).toBeFalsy();
}); });
it('should only show aca-generic-error if the non-existing node was provided', () => { it('should only show aca-generic-error if the non-existing node was provided', () => {
folderRulesService.folderInfo$ = of(null); folderRuleSetsService.folderInfo$ = of(null);
folderRulesService.deletedRuleId$ = of(null); folderRuleSetsService.ruleSetListing$ = of([]);
folderRulesService.rulesListing$ = of([]); folderRuleSetsService.isLoading$ = of(false);
folderRulesService.loading$ = of(false);
actionsService.loading$ = of(false); actionsService.loading$ = of(false);
fixture.detectChanges(); fixture.detectChanges();
@ -118,7 +126,7 @@ describe('ManageRulesSmartComponent', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
const acaGenericError = debugElement.query(By.css('aca-generic-error')); const acaGenericError = debugElement.query(By.css('aca-generic-error'));
const rules = debugElement.query(By.css('.aca-rule')); const rules = debugElement.query(By.css('.aca-rule-list-item'));
const ruleDetails = debugElement.query(By.css('aca-rule-details')); const ruleDetails = debugElement.query(By.css('aca-rule-details'));
expect(acaGenericError).toBeTruthy(); expect(acaGenericError).toBeTruthy();
@ -127,10 +135,9 @@ describe('ManageRulesSmartComponent', () => {
}); });
it('should only show progress bar while loading', async () => { it('should only show progress bar while loading', async () => {
folderRulesService.folderInfo$ = of(null); folderRuleSetsService.folderInfo$ = of(null);
folderRulesService.deletedRuleId$ = of(null); folderRuleSetsService.ruleSetListing$ = of([]);
folderRulesService.rulesListing$ = of([]); folderRuleSetsService.isLoading$ = of(true);
folderRulesService.loading$ = of(true);
actionsService.loading$ = of(true); actionsService.loading$ = of(true);
fixture.detectChanges(); fixture.detectChanges();
@ -138,7 +145,7 @@ describe('ManageRulesSmartComponent', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
const matProgressBar = debugElement.query(By.css('mat-progress-bar')); const matProgressBar = debugElement.query(By.css('mat-progress-bar'));
const rules = debugElement.query(By.css('.aca-rule')); const rules = debugElement.query(By.css('.aca-rule-list-item'));
const ruleDetails = debugElement.query(By.css('aca-rule-details')); const ruleDetails = debugElement.query(By.css('aca-rule-details'));
expect(matProgressBar).toBeTruthy(); expect(matProgressBar).toBeTruthy();
@ -148,55 +155,40 @@ describe('ManageRulesSmartComponent', () => {
it('should call deleteRule() if confirmation dialog returns true', () => { it('should call deleteRule() if confirmation dialog returns true', () => {
const dialog = TestBed.inject(MatDialog); const dialog = TestBed.inject(MatDialog);
folderRuleSetsService.folderInfo$ = of(owningFolderMock);
folderRuleSetsService.ruleSetListing$ = of(ruleSetsMock);
folderRuleSetsService.isLoading$ = of(false);
folderRulesService.selectedRule$ = of(ruleMock('owned-rule-1'));
folderRulesService.deletedRuleId$ = of(null); folderRulesService.deletedRuleId$ = of(null);
folderRulesService.folderInfo$ = of(dummyNodeInfo);
folderRulesService.rulesListing$ = of(dummyRules);
folderRulesService.loading$ = of(false);
actionsService.loading$ = of(false); actionsService.loading$ = of(false);
spyOn(component, 'onRuleDelete').and.callThrough(); const onRuleDeleteButtonClickedSpy = spyOn(component, 'onRuleDeleteButtonClicked').and.callThrough();
const dialogResult: any = { const dialogResult: any = {
afterClosed: () => afterClosed: () => of(true)
of(true).subscribe((res) => {
if (res === true) {
folderRulesService.deleteRule(component.nodeId, component.selectedRule.id);
}
})
}; };
spyOn(dialog, 'open').and.returnValue(dialogResult); const dialogOpenSpy = spyOn(dialog, 'open').and.returnValue(dialogResult);
const deleteRuleSpy = spyOn(folderRulesService, 'deleteRule');
const onRuleDeleteSpy = spyOn(component, 'onRuleDelete').and.callThrough();
fixture.detectChanges(); fixture.detectChanges();
expect(component).toBeTruthy('expected component'); expect(component).toBeTruthy('expected component');
const rules = debugElement.queryAll(By.css('.aca-rule')); const rules = debugElement.queryAll(By.css('.aca-rule-list-item'));
const ruleDetails = debugElement.query(By.css('aca-rule-details')); const ruleDetails = debugElement.query(By.css('aca-rule-details'));
const deleteRuleBtn = fixture.debugElement.nativeElement.querySelector('#delete-rule-btn'); const deleteRuleBtn = fixture.debugElement.nativeElement.querySelector('#delete-rule-btn');
deleteRuleBtn.click(); deleteRuleBtn.click();
fixture.detectChanges(); fixture.detectChanges();
folderRulesService.deletedRuleId$ = of(component.selectedRule.id); folderRulesService.deletedRuleId$ = of('owned-rule-1-id');
expect(component.onRuleDelete).toHaveBeenCalled(); expect(onRuleDeleteButtonClickedSpy).toHaveBeenCalled();
expect(dialog.open).toHaveBeenCalled(); expect(dialogOpenSpy).toHaveBeenCalled();
expect(folderRulesService.deleteRule).toHaveBeenCalled(); expect(deleteRuleSpy).toHaveBeenCalled();
expect(folderRulesService.loadRules).toHaveBeenCalledTimes(1); expect(onRuleDeleteSpy).toHaveBeenCalledTimes(1);
expect(rules).toBeTruthy('expected rules'); expect(rules).toBeTruthy('expected rules');
expect(ruleDetails).toBeTruthy('expected ruleDetails'); expect(ruleDetails).toBeTruthy('expected ruleDetails');
expect(deleteRuleBtn).toBeTruthy(); expect(deleteRuleBtn).toBeTruthy();
}); });
it('should run loadRules() when deletedRuleId$ emits new value', () => {
folderRulesService.deletedRuleId$ = of('new-value');
folderRulesService.folderInfo$ = of(dummyNodeInfo);
folderRulesService.rulesListing$ = of(dummyRules);
folderRulesService.loading$ = of(false);
fixture.detectChanges();
expect(component).toBeTruthy();
expect(folderRulesService.loadRules).toHaveBeenCalledTimes(2);
});
}); });

View File

@ -23,20 +23,23 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { Component, OnInit, OnDestroy, ViewEncapsulation } from '@angular/core'; import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { Location } from '@angular/common'; import { Location } from '@angular/common';
import { FolderRulesService } from '../services/folder-rules.service'; import { FolderRulesService } from '../services/folder-rules.service';
import { Observable, Subscription } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { Rule } from '../model/rule.model'; import { Rule } from '../model/rule.model';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { NodeInfo } from '@alfresco/aca-shared/store'; import { AppStore, NavigateRouteAction, NodeInfo } from '@alfresco/aca-shared/store';
import { delay, tap } from 'rxjs/operators'; import { delay, takeUntil } from 'rxjs/operators';
import { EditRuleDialogSmartComponent } from '../rule-details/edit-rule-dialog.smart-component'; import { EditRuleDialogSmartComponent } from '../rule-details/edit-rule-dialog.smart-component';
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { ConfirmDialogComponent } from '@alfresco/adf-content-services'; import { ConfirmDialogComponent } from '@alfresco/adf-content-services';
import { NotificationService } from '@alfresco/adf-core'; import { NotificationService } from '@alfresco/adf-core';
import { ActionDefinitionTransformed } from '../model/rule-action.model'; import { ActionDefinitionTransformed } from '../model/rule-action.model';
import { ActionsService } from '../services/actions.service'; import { ActionsService } from '../services/actions.service';
import { FolderRuleSetsService } from '../services/folder-rule-sets.service';
import { Store } from '@ngrx/store';
import { RuleSet } from '../model/rule-set.model';
@Component({ @Component({
selector: 'aca-manage-rules', selector: 'aca-manage-rules',
@ -46,15 +49,18 @@ import { ActionsService } from '../services/actions.service';
host: { class: 'aca-manage-rules' } host: { class: 'aca-manage-rules' }
}) })
export class ManageRulesSmartComponent implements OnInit, OnDestroy { export class ManageRulesSmartComponent implements OnInit, OnDestroy {
rules$: Observable<Rule[]>;
rulesLoading$: Observable<boolean>;
actionsLoading$: Observable<boolean>;
folderInfo$: Observable<NodeInfo>;
actionDefinitions$: Observable<ActionDefinitionTransformed[]>;
selectedRule: Rule = null;
nodeId: string = null; nodeId: string = null;
deletedRuleSubscription$: Subscription;
ruleDialogOnSubmitSubscription$: Subscription; ruleSetListing$: Observable<RuleSet[]>;
selectedRule$: Observable<Rule>;
hasMoreRuleSets$: Observable<boolean>;
ruleSetsLoading$: Observable<boolean>;
folderInfo$: Observable<NodeInfo>;
actionsLoading$: Observable<boolean>;
actionDefinitions$: Observable<ActionDefinitionTransformed[]>;
private destroyed$ = new Subject<void>();
constructor( constructor(
private location: Location, private location: Location,
@ -62,45 +68,44 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
private route: ActivatedRoute, private route: ActivatedRoute,
private matDialogService: MatDialog, private matDialogService: MatDialog,
private notificationService: NotificationService, private notificationService: NotificationService,
private actionsService: ActionsService private actionsService: ActionsService,
private folderRuleSetsService: FolderRuleSetsService,
private store: Store<AppStore>
) {} ) {}
ngOnInit(): void { ngOnInit() {
this.actionDefinitions$ = this.actionsService.actionDefinitionsListing$; this.ruleSetListing$ = this.folderRuleSetsService.ruleSetListing$;
this.rules$ = this.folderRulesService.rulesListing$.pipe( this.selectedRule$ = this.folderRulesService.selectedRule$;
tap((rules) => { this.hasMoreRuleSets$ = this.folderRuleSetsService.hasMoreRuleSets$;
if (!rules.includes(this.selectedRule)) { this.ruleSetsLoading$ = this.folderRuleSetsService.isLoading$;
this.selectedRule = rules[0]; this.folderInfo$ = this.folderRuleSetsService.folderInfo$;
}
})
);
this.deletedRuleSubscription$ = this.folderRulesService.deletedRuleId$.subscribe((deletedRuleId) => {
if (deletedRuleId) {
this.folderRulesService.loadRules(this.nodeId);
}
});
this.rulesLoading$ = this.folderRulesService.loading$;
this.actionsLoading$ = this.actionsService.loading$.pipe(delay(0)); this.actionsLoading$ = this.actionsService.loading$.pipe(delay(0));
this.folderInfo$ = this.folderRulesService.folderInfo$; this.actionDefinitions$ = this.actionsService.actionDefinitionsListing$;
this.folderRulesService.deletedRuleId$.pipe(takeUntil(this.destroyed$)).subscribe((deletedRuleId) => this.onRuleDelete(deletedRuleId));
this.actionsService.loadActionDefinitions(); this.actionsService.loadActionDefinitions();
this.route.params.subscribe((params) => { this.route.params.subscribe((params) => {
this.nodeId = params.nodeId; this.nodeId = params.nodeId;
if (this.nodeId) { if (this.nodeId) {
this.folderRulesService.loadRules(this.nodeId); this.folderRuleSetsService.loadRuleSets(this.nodeId);
} }
}); });
} }
ngOnDestroy(): void { ngOnDestroy() {
this.deletedRuleSubscription$.unsubscribe(); this.destroyed$.next();
this.destroyed$.complete();
} }
goBack(): void { goBack(): void {
this.location.back(); this.location.back();
} }
onRuleSelected(rule: Rule): void { onSelectRule(rule: Rule) {
this.selectedRule = rule; this.folderRulesService.selectRule(rule);
} }
openCreateUpdateRuleDialog(model = {}) { openCreateUpdateRuleDialog(model = {}) {
@ -119,11 +124,10 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
dialogRef.componentInstance.submitted.subscribe(async (rule) => { dialogRef.componentInstance.submitted.subscribe(async (rule) => {
try { try {
if (rule.id) { if (rule.id) {
await this.folderRulesService.updateRule(this.nodeId, rule.id, rule); await this.onRuleUpdate(rule);
} else { } else {
await this.folderRulesService.createRule(this.nodeId, rule); await this.onRuleCreate(rule);
} }
this.folderRulesService.loadRules(this.nodeId);
dialogRef.close(); dialogRef.close();
} catch (error) { } catch (error) {
this.notificationService.showError(error.response.body.error.errorKey); this.notificationService.showError(error.response.body.error.errorKey);
@ -131,7 +135,28 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
}); });
} }
onRuleDelete(): void { async onRuleUpdate(rule: Rule) {
const ruleSet = this.folderRuleSetsService.getRuleSetFromRuleId(rule.id);
await this.folderRulesService.updateRule(this.nodeId, rule.id, rule, ruleSet.id);
this.folderRulesService.loadRules(ruleSet, 0, rule);
}
async onRuleCreate(ruleCreateParams: Partial<Rule>) {
await this.folderRulesService.createRule(this.nodeId, ruleCreateParams, '-default-');
const ruleSetToLoad = this.folderRuleSetsService.getOwnedOrLinkedRuleSet();
if (ruleSetToLoad) {
this.folderRulesService.loadRules(ruleSetToLoad, 0, 'last');
} else {
this.folderRuleSetsService.loadMoreRuleSets(true);
}
}
async onRuleEnabledToggle(rule: Rule, isEnabled: boolean) {
const ruleSet = this.folderRuleSetsService.getRuleSetFromRuleId(rule.id);
await this.folderRulesService.updateRule(this.nodeId, rule.id, { ...rule, isEnabled }, ruleSet.id);
}
onRuleDeleteButtonClicked(rule: Rule) {
this.matDialogService this.matDialogService
.open(ConfirmDialogComponent, { .open(ConfirmDialogComponent, {
data: { data: {
@ -143,12 +168,31 @@ export class ManageRulesSmartComponent implements OnInit, OnDestroy {
.afterClosed() .afterClosed()
.subscribe((result) => { .subscribe((result) => {
if (result) { if (result) {
this.folderRulesService.deleteRule(this.nodeId, this.selectedRule.id); this.folderRulesService.deleteRule(this.nodeId, rule.id);
} }
}); });
} }
onRuleUpdate(): void { onRuleDelete(deletedRuleId: string) {
this.openCreateUpdateRuleDialog(this.selectedRule); if (deletedRuleId) {
const folderToRefresh = this.folderRuleSetsService.getRuleSetFromRuleId(deletedRuleId);
if (folderToRefresh?.rules.length > 1) {
this.folderRulesService.loadRules(folderToRefresh, 0, 'first');
} else {
this.folderRuleSetsService.loadRuleSets(this.nodeId);
}
}
}
onNavigateToOtherFolder(nodeId) {
this.store.dispatch(new NavigateRouteAction(['nodes', nodeId, 'rules']));
}
onLoadMoreRuleSets() {
this.folderRuleSetsService.loadMoreRuleSets();
}
onLoadMoreRules(ruleSet: RuleSet) {
this.folderRulesService.loadRules(ruleSet);
} }
} }

View File

@ -23,44 +23,32 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
export const dummyGetNodeResponse = { import { NodeInfo } from '@alfresco/aca-shared/store';
import { NodeEntry } from '@alfresco/js-api';
export const getOwningFolderEntryMock: NodeEntry = {
entry: { entry: {
aspectNames: ['rule:rules', 'cm:titled', 'cm:auditable'], id: 'owning-folder-id',
createdAt: '2022-08-16T07:58:21.416+0000', name: 'owning-folder-name'
isFolder: true,
isFile: false,
createdByUser: {
id: 'username',
displayName: 'username'
},
modifiedAt: '2022-08-16T07:59:45.771+0000',
modifiedByUser: {
id: 'username',
displayName: 'username'
},
name: 'folder1',
id: '76659fe3-5f93-483d-948e-38b9e006cc94',
nodeType: 'cm:folder',
parentId: 'eb48d545-61f7-4ebd-861d-5fe5b072472f'
} }
} as NodeEntry;
export const getOtherFolderEntryMock: NodeEntry = {
entry: {
id: 'other-folder-id',
name: 'other-folder-name'
}
} as NodeEntry;
export const owningFolderIdMock = 'owning-folder-id';
export const otherFolderIdMock = 'other-folder-id';
export const owningFolderMock: NodeInfo = {
id: owningFolderIdMock,
name: 'owning-folder-name'
}; };
export const dummyNodeInfo = { export const otherFolderMock: NodeInfo = {
aspectNames: ['rule:rules', 'cm:titled', 'cm:auditable'], id: otherFolderIdMock,
createdAt: '2022-08-16T07:58:21.416+0000', name: 'other-folder-name'
isFolder: true,
isFile: false,
createdByUser: {
id: 'username',
displayName: 'username'
},
modifiedAt: '2022-08-16T07:59:45.771+0000',
modifiedByUser: {
id: 'username',
displayName: 'username'
},
name: 'folder1',
id: '76659fe3-5f93-483d-948e-38b9e006cc94',
nodeType: 'cm:folder',
parentId: 'eb48d545-61f7-4ebd-861d-5fe5b072472f'
}; };

View File

@ -0,0 +1,109 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { RuleSet } from '../model/rule-set.model';
import { otherFolderIdMock, otherFolderMock, owningFolderIdMock, owningFolderMock } from './node.mock';
import { Rule } from '../model/rule.model';
import { inheritedRulesMock, linkedRulesMock, ownedRulesMock } from './rules.mock';
export const getRuleSetsResponseMock = {
list: {
pagination: {
count: 3,
hasMoreItems: false,
totalItems: 3,
skipCount: 0,
maxItems: 100
},
entries: [
{
entry: {
linkedToBy: [],
owningFolder: otherFolderIdMock,
isLinkedTo: false,
id: 'inherited-rule-set'
}
},
{
entry: {
linkedToBy: [],
owningFolder: owningFolderIdMock,
isLinkedTo: false,
id: 'rule-set-no-links'
}
},
{
entry: {
linkedToBy: [owningFolderIdMock],
owningFolder: otherFolderIdMock,
isLinkedTo: true,
id: 'rule-set-with-link'
}
}
]
}
};
export const ruleSetMock = (rules: Rule[] = []): RuleSet => ({
id: 'rule-set-id',
isLinkedTo: false,
owningFolder: owningFolderMock,
linkedToBy: [],
rules: [...rules],
hasMoreRules: true,
loadingRules: false
});
const ruleSetWithNoLinksMock: RuleSet = {
id: 'rule-set-no-links',
isLinkedTo: false,
owningFolder: owningFolderMock,
linkedToBy: [],
rules: ownedRulesMock,
hasMoreRules: false,
loadingRules: false
};
const ruleSetWithLinkMock: RuleSet = {
id: 'rule-set-with-link',
isLinkedTo: true,
owningFolder: otherFolderMock,
linkedToBy: [owningFolderIdMock],
rules: linkedRulesMock,
hasMoreRules: false,
loadingRules: false
};
const inheritedRuleSetMock: RuleSet = {
id: 'inherited-rule-set',
isLinkedTo: false,
owningFolder: otherFolderMock,
linkedToBy: [],
rules: inheritedRulesMock,
hasMoreRules: false,
loadingRules: false
};
export const ruleSetsMock: RuleSet[] = [inheritedRuleSetMock, ruleSetWithNoLinksMock, ruleSetWithLinkMock];

View File

@ -25,7 +25,7 @@
import { Rule } from '../model/rule.model'; import { Rule } from '../model/rule.model';
export const dummyResponse = { export const getRulesResponseMock = {
list: { list: {
pagination: { pagination: {
count: 2, count: 2,
@ -40,17 +40,13 @@ export const dummyResponse = {
isShared: false, isShared: false,
isInheritable: false, isInheritable: false,
isAsynchronous: false, isAsynchronous: false,
name: 'rule1', name: 'rule1-name',
id: 'd388ed54-a522-410f-a158-6dbf5a833731', id: 'rule1-id',
triggers: ['inbound'], triggers: ['inbound'],
actions: [ actions: [
{ {
actionDefinitionId: 'copy', actionDefinitionId: 'counter',
params: { params: {}
'deep-copy': false,
'destination-folder': '279c65f0-912b-4563-affb-ed9dab8338e0',
actionContext: 'rule'
}
} }
], ],
isEnabled: true isEnabled: true
@ -61,16 +57,13 @@ export const dummyResponse = {
isShared: false, isShared: false,
isInheritable: false, isInheritable: false,
isAsynchronous: false, isAsynchronous: false,
name: 'rule2', name: 'rule2-name',
id: 'e0e645ca-e6c0-47d4-9936-1a8872a6c30b', id: 'rule2-id',
triggers: ['inbound'], triggers: ['inbound'],
actions: [ actions: [
{ {
actionDefinitionId: 'move', actionDefinitionId: 'counter',
params: { params: {}
'destination-folder': '279c65f0-912b-4563-affb-ed9dab8338e0',
actionContext: 'rule'
}
} }
], ],
isEnabled: true isEnabled: true
@ -80,58 +73,88 @@ export const dummyResponse = {
} }
}; };
export const dummyRules: Rule[] = [ export const getMoreRulesResponseMock = {
{ list: {
id: 'd388ed54-a522-410f-a158-6dbf5a833731', pagination: {
name: 'rule1', count: 2,
description: '', hasMoreItems: false,
isEnabled: true, totalItems: 2,
isInheritable: false, skipCount: 0,
isAsynchronous: false, maxItems: 100
errorScript: '',
isShared: false,
triggers: ['inbound'],
conditions: {
inverted: false,
booleanMode: 'and',
simpleConditions: [],
compositeConditions: []
}, },
actions: [ entries: [
{ {
actionDefinitionId: 'copy', entry: {
params: { isShared: false,
'deep-copy': false, isInheritable: false,
'destination-folder': '279c65f0-912b-4563-affb-ed9dab8338e0', isAsynchronous: false,
actionContext: 'rule' name: 'rule3-name',
id: 'rule3-id',
triggers: ['inbound'],
actions: [
{
actionDefinitionId: 'counter',
params: {}
}
],
isEnabled: true
} }
} },
]
},
{
id: 'e0e645ca-e6c0-47d4-9936-1a8872a6c30b',
name: 'rule2',
description: '',
isEnabled: true,
isInheritable: false,
isAsynchronous: false,
errorScript: '',
isShared: false,
triggers: ['inbound'],
conditions: {
inverted: false,
booleanMode: 'and',
simpleConditions: [],
compositeConditions: []
},
actions: [
{ {
actionDefinitionId: 'move', entry: {
params: { isShared: false,
'destination-folder': '279c65f0-912b-4563-affb-ed9dab8338e0', isInheritable: false,
actionContext: 'rule' isAsynchronous: false,
name: 'rule4-name',
id: 'rule4-id',
triggers: ['inbound'],
actions: [
{
actionDefinitionId: 'counter',
params: {}
}
],
isEnabled: true
} }
} }
] ]
} }
]; };
const genericRuleMock: Rule = {
id: '',
name: '',
description: '',
isEnabled: true,
isInheritable: false,
isAsynchronous: false,
errorScript: '',
isShared: false,
triggers: ['inbound'],
conditions: {
inverted: false,
booleanMode: 'and',
simpleConditions: [],
compositeConditions: []
},
actions: [
{
actionDefinitionId: 'counter',
params: {}
}
]
};
export const ruleMock = (unique: string): Rule => ({
...genericRuleMock,
id: `${unique}-id`,
name: `${unique}-name`
});
export const rulesMock: Rule[] = [ruleMock('rule1'), ruleMock('rule2')];
export const moreRulesMock: Rule[] = [ruleMock('rule3'), ruleMock('rule4')];
export const manyRulesMock: Rule[] = [ruleMock('rule1'), ruleMock('rule2'), ruleMock('rule3'), ruleMock('rule4'), ruleMock('rule5')];
export const ownedRulesMock: Rule[] = [ruleMock('owned-rule-1'), ruleMock('owned-rule-2')];
export const linkedRulesMock: Rule[] = [ruleMock('linked-rule-1'), ruleMock('linked-rule-2')];
export const inheritedRulesMock: Rule[] = [ruleMock('inherited-rule-1'), ruleMock('inherited-rule-2')];

View File

@ -23,23 +23,15 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { Rule } from './rule.model';
import { RuleListItemUiComponent } from './rule-list-item.ui-component'; import { NodeInfo } from '@alfresco/aca-shared/store';
import { CoreTestingModule } from '@alfresco/adf-core';
describe('RuleComponent', () => { export interface RuleSet {
let component: RuleListItemUiComponent; id: string;
let fixture: ComponentFixture<RuleListItemUiComponent>; isLinkedTo: boolean;
owningFolder: NodeInfo;
beforeEach(() => { linkedToBy: string[];
TestBed.configureTestingModule({ rules: Rule[];
imports: [CoreTestingModule] hasMoreRules: boolean;
}); loadingRules: boolean;
fixture = TestBed.createComponent(RuleListItemUiComponent); }
component = fixture.componentInstance;
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
});

View File

@ -1,5 +1,5 @@
<div class="aca-rule-list-item__header"> <div class="aca-rule-list-item__header">
<span class="aca-rule-list-item__header__name">{{ rule.name }}</span> <span class="aca-rule-list-item__header__name">{{ rule.name }}</span>
<mat-slide-toggle [(ngModel)]="rule.isEnabled" (click)="onToggleClick(!rule.isEnabled)"></mat-slide-toggle> <mat-slide-toggle [(ngModel)]="rule.isEnabled" (click)="onToggleClick(!rule.isEnabled, $event)"></mat-slide-toggle>
</div> </div>
<div class="aca-rule-list-item__description">{{ rule.description }}</div> <div class="aca-rule-list-item__description">{{ rule.description }}</div>

View File

@ -3,8 +3,6 @@
flex-direction: column; flex-direction: column;
gap: 4px; gap: 4px;
padding: 12px 20px; padding: 12px 20px;
border-radius: 12px;
margin-bottom: 8px;
cursor: pointer; cursor: pointer;
p { p {

View File

@ -23,9 +23,8 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { Component, HostBinding, Input, ViewEncapsulation } from '@angular/core'; import { Component, EventEmitter, HostBinding, Input, Output, ViewEncapsulation } from '@angular/core';
import { Rule } from '../../model/rule.model'; import { Rule } from '../../model/rule.model';
import { FolderRulesService } from '../../services/folder-rules.service';
@Component({ @Component({
selector: 'aca-rule-list-item', selector: 'aca-rule-list-item',
@ -38,14 +37,14 @@ export class RuleListItemUiComponent {
@Input() @Input()
rule: Rule; rule: Rule;
@Input() @Input()
nodeId: string;
@Input()
@HostBinding('class.selected') @HostBinding('class.selected')
isSelected: boolean; isSelected: boolean;
constructor(private folderRulesService: FolderRulesService) {} @Output()
enabledChanged = new EventEmitter<boolean>();
onToggleClick(isEnabled: boolean) { onToggleClick(isEnabled: boolean, event: Event) {
this.folderRulesService.toggleRule(this.nodeId, this.rule.id, { ...this.rule, isEnabled }); event.stopPropagation();
this.enabledChanged.emit(isEnabled);
} }
} }

View File

@ -1,10 +1,11 @@
<div class="aca-rules-list" > <div class="aca-rules-list" >
<aca-rule-list-item <aca-rule-list-item
matRipple matRippleColor="hsla(0,0%,0%,0.05)" matRipple matRippleColor="hsla(0,0%,0%,0.05)"
tabindex="0"
*ngFor="let rule of rules" *ngFor="let rule of rules"
[rule]="rule" [rule]="rule"
[isSelected]="isSelected(rule)" [isSelected]="isSelected(rule)"
[nodeId]="nodeId" (click)="onRuleClicked(rule)"
(click)="onRuleClicked(rule)"> (enabledChanged)="onEnabledChanged(rule, $event)">
</aca-rule-list-item> </aca-rule-list-item>
</div> </div>

View File

@ -1,5 +1,4 @@
.aca-rule-list { .aca-rule-list {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 4px;
} }

View File

@ -25,13 +25,13 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RuleListUiComponent } from './rule-list.ui-component'; import { RuleListUiComponent } from './rule-list.ui-component';
import { dummyRules } from '../mock/rules.mock'; import { rulesMock } from '../../mock/rules.mock';
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { CoreTestingModule } from '@alfresco/adf-core'; import { CoreTestingModule } from '@alfresco/adf-core';
import { AcaFolderRulesModule } from '@alfresco/aca-folder-rules'; import { AcaFolderRulesModule } from '@alfresco/aca-folder-rules';
describe('RuleListComponent', () => { describe('RuleListUiComponent', () => {
let component: RuleListUiComponent; let component: RuleListUiComponent;
let fixture: ComponentFixture<RuleListUiComponent>; let fixture: ComponentFixture<RuleListUiComponent>;
let debugElement: DebugElement; let debugElement: DebugElement;
@ -50,7 +50,7 @@ describe('RuleListComponent', () => {
it('should display the list of rules', () => { it('should display the list of rules', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
component.rules = dummyRules; component.rules = rulesMock;
fixture.detectChanges(); fixture.detectChanges();
@ -64,8 +64,8 @@ describe('RuleListComponent', () => {
const description = rule.query(By.css('.aca-rule-list-item__description')); const description = rule.query(By.css('.aca-rule-list-item__description'));
const toggleBtn = rule.query(By.css('mat-slide-toggle')); const toggleBtn = rule.query(By.css('mat-slide-toggle'));
expect(name.nativeElement.textContent).toBe(dummyRules[0].name); expect(name.nativeElement.textContent).toBe(rulesMock[0].name);
expect(toggleBtn).toBeTruthy(); expect(toggleBtn).toBeTruthy();
expect(description.nativeElement.textContent).toBe(dummyRules[0].description); expect(description.nativeElement.textContent).toBe(rulesMock[0].description);
}); });
}); });

View File

@ -24,7 +24,7 @@
*/ */
import { Component, EventEmitter, Input, Output, ViewEncapsulation } from '@angular/core'; import { Component, EventEmitter, Input, Output, ViewEncapsulation } from '@angular/core';
import { Rule } from '../model/rule.model'; import { Rule } from '../../model/rule.model';
@Component({ @Component({
selector: 'aca-rule-list', selector: 'aca-rule-list',
@ -35,23 +35,24 @@ import { Rule } from '../model/rule.model';
}) })
export class RuleListUiComponent { export class RuleListUiComponent {
@Input() @Input()
rules: Rule[]; rules: Rule[] = [];
@Input() @Input()
selectedRule: Rule; selectedRule: Rule = null;
@Input()
nodeId: string;
@Output() @Output()
ruleSelected = new EventEmitter<Rule>(); selectRule = new EventEmitter<Rule>();
@Output()
ruleEnabledChanged = new EventEmitter<[Rule, boolean]>();
onRuleClicked(rule: Rule): void { onRuleClicked(rule: Rule): void {
this.ruleSelected.emit(rule); this.selectRule.emit(rule);
} }
isSelected(rule): boolean { isSelected(rule): boolean {
if (this.selectedRule) { return rule.id === this.selectedRule?.id;
return rule.id === this.selectedRule.id; }
}
return false; onEnabledChanged(rule: Rule, isEnabled: boolean) {
this.ruleEnabledChanged.emit([rule, isEnabled]);
} }
} }

View File

@ -0,0 +1,90 @@
<div
*ngFor="let ruleSet of ruleSets"
class="aca-rule-set-list__item"
data-automation-id="rule-set-list-item"
[ngClass]="{ expanded: isRuleSetExpanded(ruleSet) }">
<div class="aca-rule-set-list__item__header">
<div
tabindex="0"
*ngIf="ruleSet.owningFolder.id !== folderId"
matRipple matRippleColor="hsla(0,0%,0%,0.05)"
class="aca-rule-set-list__item__header__navigate-button"
(click)="clickNavigateButton(ruleSet.owningFolder)"
(keyup.enter)="clickNavigateButton(ruleSet.owningFolder)">
<mat-icon>edit_note</mat-icon>
</div>
<div
tabindex="0"
class="aca-rule-set-list__item__header__title"
data-automation-id="rule-set-item-title"
matRipple matRippleColor="hsla(0,0%,0%,0.05)"
(click)="clickRuleSetHeader(ruleSet)"
(keyup.enter)="clickRuleSetHeader(ruleSet)">
<ng-container *ngIf="ruleSet.owningFolder.id === folderId; else nonOwnedRuleSet">
{{ 'ACA_FOLDER_RULES.RULE_LIST.OWNED_BY_THIS_FOLDER' | translate }}
</ng-container>
<ng-template #nonOwnedRuleSet>
<ng-container *ngIf="isRuleSetLinked(ruleSet); else inheritedRuleSet">
{{ 'ACA_FOLDER_RULES.RULE_LIST.LINKED_FROM' | translate }} {{ ruleSet.owningFolder.name }}
</ng-container>
<ng-template #inheritedRuleSet>
{{ 'ACA_FOLDER_RULES.RULE_LIST.INHERITED_FROM' | translate }} {{ ruleSet.owningFolder.name }}
</ng-template>
</ng-template>
<mat-icon class="aca-rule-set-list__item__header__icon">
{{ isRuleSetExpanded(ruleSet) ? 'expand_more' : 'chevron_right' }}
</mat-icon>
</div>
</div>
<ng-container *ngIf="isRuleSetExpanded(ruleSet)">
<aca-rule-list
[rules]="ruleSet.rules"
[selectedRule]="selectedRule"
(selectRule)="onSelectRule($event)"
(ruleEnabledChanged)="onRuleEnabledChanged($event)">
</aca-rule-list>
<div
*ngIf="ruleSet.hasMoreRules || ruleSet.loadingRules"
tabindex="0"
class="aca-rule-set-list__item__load-more load-more"
matRipple matRippleColor="hsla(0,0%,0%,0.05)"
(click)="clickLoadMoreRules(ruleSet)"
(keyup.enter)="clickLoadMoreRules(ruleSet)">
<ng-container *ngIf="!ruleSet.loadingRules; else rulesLoadingTemplate">
{{ 'ACA_FOLDER_RULES.RULE_LIST.LOAD_MORE_RULES' | translate }}
</ng-container>
<ng-template #rulesLoadingTemplate>
<mat-spinner mode="indeterminate" [diameter]="16"></mat-spinner>
{{ 'ACA_FOLDER_RULES.RULE_LIST.LOADING_RULES' | translate }}
</ng-template>
</div>
</ng-container>
</div>
<div
*ngIf="hasMoreRuleSets"
tabindex="0"
class="aca-rule-set-list__load-more load-more"
matRipple matRippleColor="hsla(0,0%,0%,0.05)"
[matRippleDisabled]="ruleSetsLoading"
(click)="clickLoadMoreRuleSets()"
(keyup.enter)="clickLoadMoreRuleSets()">
<ng-container *ngIf="!ruleSetsLoading; else ruleSetsLoadingTemplate">
{{ 'ACA_FOLDER_RULES.RULE_LIST.LOAD_MORE_RULE_SETS' | translate }}
</ng-container>
<ng-template #ruleSetsLoadingTemplate>
<mat-spinner mode="indeterminate" [diameter]="16"></mat-spinner>
{{ 'ACA_FOLDER_RULES.RULE_LIST.LOADING_RULE_SETS' | translate }}
</ng-template>
</div>

View File

@ -0,0 +1,78 @@
.aca-rule-set-list {
display: flex;
flex-direction: column;
overflow-y: auto;
gap: 8px;
&__item {
display: flex;
flex-direction: column;
border: 1px solid var(--theme-border-color);
border-radius: 12px;
overflow: hidden;
&__header {
display: flex;
flex-direction: row;
align-items: stretch;
cursor: pointer;
color: var(--theme-text-color);
user-select: none;
font-size: 0.9em;
& > * {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
&__title {
padding: 0.5em 1em;
flex: 1;
}
&__navigate-button {
border-right: 1px solid var(--theme-border-color);
padding: 0.5em;
mat-icon {
transform: scale(0.8);
transform-origin: center;
}
}
}
&__load-more {
border-top: 1px solid var(--theme-border-color);
padding: 0.5em 1em;
}
&.expanded {
.aca-rule-set-list__item__header {
border-bottom: 1px solid var(--theme-border-color);
}
}
}
&__load-more {
padding: 1em 2em;
border: 1px solid var(--theme-border-color);
border-radius: 12px;
}
.load-more {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
color: var(--theme-disabled-text-color);
font-style: italic;
cursor: pointer;
text-align: center;
.mat-spinner {
margin-right: 0.5em;
}
}
}

View File

@ -0,0 +1,76 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { RuleSetListUiComponent } from './rule-set-list.ui-component';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CoreTestingModule } from '@alfresco/adf-core';
import { RuleListUiComponent } from '../rule-list/rule-list.ui-component';
import { RuleListItemUiComponent } from '../rule-list-item/rule-list-item.ui-component';
import { ruleSetsMock } from '../../mock/rule-sets.mock';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
import { owningFolderIdMock } from '../../mock/node.mock';
describe('RuleSetListUiComponent', () => {
let fixture: ComponentFixture<RuleSetListUiComponent>;
let component: RuleSetListUiComponent;
let debugElement: DebugElement;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CoreTestingModule],
declarations: [RuleSetListUiComponent, RuleListUiComponent, RuleListItemUiComponent]
});
fixture = TestBed.createComponent(RuleSetListUiComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
component.folderId = owningFolderIdMock;
component.ruleSets = ruleSetsMock;
fixture.detectChanges();
});
it('should display a list of rule sets', () => {
const ruleSetElements = debugElement.queryAll(By.css(`[data-automation-id="rule-set-list-item"]`));
expect(ruleSetElements.length).toBe(3);
});
it('should show the right message for the right sort of rule set', () => {
const ruleSetTitleElements = debugElement.queryAll(By.css(`[data-automation-id="rule-set-item-title"]`));
const innerTextWithoutIcon = (element: HTMLDivElement): string => element.innerText.replace(/(expand_more|chevron_right)$/, '').trim();
expect(ruleSetTitleElements.length).toBe(3);
expect(innerTextWithoutIcon(ruleSetTitleElements[0].nativeElement as HTMLDivElement)).toBe(
'ACA_FOLDER_RULES.RULE_LIST.INHERITED_FROM other-folder-name'
);
expect(innerTextWithoutIcon(ruleSetTitleElements[1].nativeElement as HTMLDivElement)).toBe('ACA_FOLDER_RULES.RULE_LIST.OWNED_BY_THIS_FOLDER');
expect(innerTextWithoutIcon(ruleSetTitleElements[2].nativeElement as HTMLDivElement)).toBe(
'ACA_FOLDER_RULES.RULE_LIST.LINKED_FROM other-folder-name'
);
});
});

View File

@ -0,0 +1,107 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, EventEmitter, Input, Output, ViewEncapsulation } from '@angular/core';
import { RuleSet } from '../../model/rule-set.model';
import { NodeInfo } from '@alfresco/aca-shared/store';
import { Rule } from '../../model/rule.model';
@Component({
selector: 'aca-rule-set-list',
templateUrl: './rule-set-list.ui-component.html',
styleUrls: ['./rule-set-list.ui-component.scss'],
encapsulation: ViewEncapsulation.None,
host: { class: 'aca-rule-set-list' }
})
export class RuleSetListUiComponent {
@Input()
folderId = '';
private _ruleSets: RuleSet[] = [];
@Input()
get ruleSets(): RuleSet[] {
return this._ruleSets;
}
set ruleSets(value: RuleSet[]) {
this._ruleSets = value;
this.expandedRuleSets = [...value];
}
@Input()
hasMoreRuleSets = false;
@Input()
ruleSetsLoading = false;
@Input()
selectedRule = null;
@Output()
navigateToOtherFolder = new EventEmitter<string>();
@Output()
loadMoreRuleSets = new EventEmitter<void>();
@Output()
loadMoreRules = new EventEmitter<RuleSet>();
@Output()
selectRule = new EventEmitter<Rule>();
@Output()
ruleEnabledChanged = new EventEmitter<[Rule, boolean]>();
expandedRuleSets: RuleSet[] = [];
isRuleSetLinked(ruleSet: RuleSet): boolean {
return ruleSet.linkedToBy.indexOf(this.folderId) > -1;
}
isRuleSetExpanded(ruleSet: RuleSet): boolean {
return this.expandedRuleSets.indexOf(ruleSet) > -1;
}
clickRuleSetHeader(ruleSet: RuleSet) {
if (this.isRuleSetExpanded(ruleSet)) {
this.expandedRuleSets.splice(this.expandedRuleSets.indexOf(ruleSet), 1);
} else {
this.expandedRuleSets.push(ruleSet);
}
}
clickNavigateButton(folder: NodeInfo) {
if (folder && folder.id) {
this.navigateToOtherFolder.emit(folder.id);
}
}
clickLoadMoreRuleSets() {
this.loadMoreRuleSets.emit();
}
clickLoadMoreRules(ruleSet: RuleSet) {
this.loadMoreRules.emit(ruleSet);
}
onSelectRule(rule: Rule) {
this.selectRule.emit(rule);
}
onRuleEnabledChanged(event: [Rule, boolean]) {
this.ruleEnabledChanged.emit(event);
}
}

View File

@ -0,0 +1,112 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { FolderRuleSetsService } from './folder-rule-sets.service';
import { TestBed } from '@angular/core/testing';
import { CoreTestingModule } from '@alfresco/adf-core';
import { FolderRulesService } from './folder-rules.service';
import { ContentApiService } from '@alfresco/aca-shared';
import { getOtherFolderEntryMock, getOwningFolderEntryMock, otherFolderIdMock, owningFolderIdMock, owningFolderMock } from '../mock/node.mock';
import { of } from 'rxjs';
import { getRuleSetsResponseMock, ruleSetsMock } from '../mock/rule-sets.mock';
import { take } from 'rxjs/operators';
import { inheritedRulesMock, linkedRulesMock, ownedRulesMock, ruleMock } from '../mock/rules.mock';
describe('FolderRuleSetsService', () => {
let folderRuleSetsService: FolderRuleSetsService;
let folderRulesService: FolderRulesService;
let contentApiService: ContentApiService;
let callApiSpy: jasmine.Spy;
let getNodeSpy: jasmine.Spy;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CoreTestingModule],
providers: [FolderRuleSetsService, FolderRulesService, ContentApiService]
});
folderRuleSetsService = TestBed.inject(FolderRuleSetsService);
folderRulesService = TestBed.inject(FolderRulesService);
contentApiService = TestBed.inject(ContentApiService);
callApiSpy = spyOn<any>(folderRuleSetsService, 'callApi');
spyOn<any>(folderRulesService, 'getRules')
.withArgs(jasmine.anything(), 'rule-set-no-links')
.and.returnValue(of({ rules: ownedRulesMock, hasMoreRules: false }))
.withArgs(jasmine.anything(), 'rule-set-with-link')
.and.returnValue(of({ rules: linkedRulesMock, hasMoreRules: false }))
.withArgs(jasmine.anything(), 'inherited-rule-set')
.and.returnValue(of({ rules: inheritedRulesMock, hasMoreRules: false }));
getNodeSpy = spyOn(contentApiService, 'getNode')
.withArgs(owningFolderIdMock)
.and.returnValue(of(getOwningFolderEntryMock))
.withArgs(otherFolderIdMock)
.and.returnValue(of(getOtherFolderEntryMock));
});
it(`should load node info when loading the node's rule sets`, async () => {
callApiSpy.and.returnValue(of(getRuleSetsResponseMock));
// take(2), because: 1 = init of the BehaviourSubject, 2 = in subscribe
const folderInfoPromise = folderRuleSetsService.folderInfo$.pipe(take(2)).toPromise();
folderRuleSetsService.loadRuleSets(owningFolderIdMock);
const folderInfo = await folderInfoPromise;
expect(getNodeSpy).toHaveBeenCalledWith(owningFolderIdMock);
expect(folderInfo).toEqual(owningFolderMock);
});
it('should load rule sets of a node', async () => {
callApiSpy.and.returnValue(of(getRuleSetsResponseMock));
// take(3), because: 1 = init of the BehaviourSubject, 2 = reinitialise at beginning of loadRuleSets, 3 = in subscribe
const ruleSetListingPromise = folderRuleSetsService.ruleSetListing$.pipe(take(3)).toPromise();
const hasMoreRuleSetsPromise = folderRuleSetsService.hasMoreRuleSets$.pipe(take(3)).toPromise();
folderRuleSetsService.loadRuleSets(owningFolderIdMock);
const ruleSets = await ruleSetListingPromise;
const hasMoreRuleSets = await hasMoreRuleSetsPromise;
expect(callApiSpy).toHaveBeenCalledWith(
`/nodes/${owningFolderIdMock}/rule-sets?include=isLinkedTo,owningFolder,linkedToBy&skipCount=0&maxItems=100`,
'GET'
);
expect(ruleSets).toEqual(ruleSetsMock);
expect(hasMoreRuleSets).toEqual(false);
});
it('should select the first rule of the owned rule set of the folder', async () => {
callApiSpy.and.returnValue(of(getRuleSetsResponseMock));
const selectRuleSpy = spyOn(folderRulesService, 'selectRule');
// take(3), because: 1 = init of the BehaviourSubject, 2 = reinitialise at beginning of loadRuleSets, 3 = in subscribe
const ruleSetListingPromise = folderRuleSetsService.ruleSetListing$.pipe(take(3)).toPromise();
folderRuleSetsService.loadRuleSets(owningFolderIdMock);
await ruleSetListingPromise;
expect(selectRuleSpy).toHaveBeenCalledWith(ruleMock('owned-rule-1'));
});
});

View File

@ -0,0 +1,170 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { AlfrescoApiService } from '@alfresco/adf-core';
import { BehaviorSubject, combineLatest, from, Observable, of } from 'rxjs';
import { NodeInfo } from '@alfresco/aca-shared/store';
import { catchError, finalize, map, switchMap, tap } from 'rxjs/operators';
import { RuleSet } from '../model/rule-set.model';
import { ContentApiService } from '@alfresco/aca-shared';
import { NodeEntry } from '@alfresco/js-api';
import { FolderRulesService } from './folder-rules.service';
import { Rule } from '../model/rule.model';
@Injectable({
providedIn: 'root'
})
export class FolderRuleSetsService {
public static MAX_RULE_SETS_PER_GET = 100;
private currentFolder: NodeInfo = null;
private ruleSets: RuleSet[] = [];
private hasMoreRuleSets = true;
private ruleSetListingSource = new BehaviorSubject<RuleSet[]>([]);
private hasMoreRuleSetsSource = new BehaviorSubject<boolean>(true);
private folderInfoSource = new BehaviorSubject<NodeInfo>(null);
private isLoadingSource = new BehaviorSubject<boolean>(false);
ruleSetListing$: Observable<RuleSet[]> = this.ruleSetListingSource.asObservable();
hasMoreRuleSets$: Observable<boolean> = this.hasMoreRuleSetsSource.asObservable();
folderInfo$: Observable<NodeInfo> = this.folderInfoSource.asObservable();
isLoading$ = this.isLoadingSource.asObservable();
constructor(private apiService: AlfrescoApiService, private contentApi: ContentApiService, private folderRulesService: FolderRulesService) {}
private callApi(path: string, httpMethod: string, body: object = {}): Promise<any> {
// APIs used by this service are still private and not yet available for public use
const params = [{}, {}, {}, {}, body, ['application/json'], ['application/json']];
return this.apiService.getInstance().contentPrivateClient.callApi(path, httpMethod, ...params);
}
private getRuleSets(nodeId: string, skipCount = 0): Observable<RuleSet[]> {
return from(
this.callApi(
`/nodes/${nodeId}/rule-sets?include=isLinkedTo,owningFolder,linkedToBy&skipCount=${skipCount}&maxItems=${FolderRuleSetsService.MAX_RULE_SETS_PER_GET}`,
'GET'
)
).pipe(
tap((res) => {
if (res?.list?.pagination) {
this.hasMoreRuleSets = res.list.pagination.hasMoreItems;
}
}),
switchMap((res) => this.formatRuleSets(res))
);
}
loadRuleSets(nodeId: string) {
this.isLoadingSource.next(true);
this.ruleSets = [];
this.hasMoreRuleSets = true;
this.currentFolder = null;
this.ruleSetListingSource.next(this.ruleSets);
this.hasMoreRuleSetsSource.next(this.hasMoreRuleSets);
this.getNodeInfo(nodeId)
.pipe(
tap((nodeInfo: NodeInfo) => {
this.currentFolder = nodeInfo;
this.folderInfoSource.next(this.currentFolder);
}),
switchMap(() => this.getRuleSets(nodeId)),
finalize(() => this.isLoadingSource.next(false))
)
.subscribe((ruleSets: RuleSet[]) => {
this.ruleSets = ruleSets;
this.ruleSetListingSource.next(this.ruleSets);
this.hasMoreRuleSetsSource.next(this.hasMoreRuleSets);
this.folderRulesService.selectRule(this.getOwnedOrLinkedRuleSet()?.rules[0] ?? ruleSets[0]?.rules[0]);
});
}
loadMoreRuleSets(selectLastRule = false) {
this.isLoadingSource.next(true);
this.getRuleSets(this.currentFolder.id, this.ruleSets.length)
.pipe(finalize(() => this.isLoadingSource.next(false)))
.subscribe((ruleSets) => {
this.ruleSets.push(...ruleSets);
this.ruleSetListingSource.next(this.ruleSets);
this.hasMoreRuleSetsSource.next(this.hasMoreRuleSets);
if (selectLastRule) {
const ownedRuleSet = this.getOwnedOrLinkedRuleSet();
this.folderRulesService.selectRule(ownedRuleSet?.rules[ownedRuleSet.rules.length - 1]);
}
});
}
private getNodeInfo(nodeId: string): Observable<NodeInfo> {
if (nodeId) {
return this.contentApi.getNode(nodeId).pipe(
catchError((error) => {
if (error.status === 404) {
return of({ entry: null });
}
return of(error);
}),
map((entry: NodeEntry) => entry.entry)
);
} else {
return of(null);
}
}
private formatRuleSets(res: any): Observable<RuleSet[]> {
return res?.list?.entries && res.list.entries instanceof Array
? combineLatest((res.list.entries as Array<any>).map((entry) => this.formatRuleSet(entry.entry)))
: of([]);
}
private formatRuleSet(entry: any): Observable<RuleSet> {
return combineLatest(
this.currentFolder?.id === entry.owningFolder ? of(this.currentFolder) : this.getNodeInfo(entry.owningFolder || ''),
this.folderRulesService.getRules(entry.owningFolder || '', entry.id)
).pipe(
map(([owningFolderNodeInfo, getRulesRes]) => ({
id: entry.id,
isLinkedTo: entry.isLinkedTo || false,
owningFolder: owningFolderNodeInfo,
linkedToBy: entry.linkedToBy || [],
rules: getRulesRes.rules,
hasMoreRules: getRulesRes.hasMoreRules,
loadingRules: false
}))
);
}
getRuleSetFromRuleId(ruleId: string): RuleSet {
return this.ruleSets.find((ruleSet: RuleSet) => ruleSet.rules.findIndex((r: Rule) => r.id === ruleId) > -1) ?? null;
}
getOwnedOrLinkedRuleSet(): RuleSet {
return (
this.ruleSets.find(
(ruleSet: RuleSet) => ruleSet.owningFolder.id === this.currentFolder.id || ruleSet.linkedToBy.indexOf(this.currentFolder.id) > -1
) ?? null
);
}
}

View File

@ -25,126 +25,111 @@
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { CoreTestingModule } from '@alfresco/adf-core'; import { CoreTestingModule } from '@alfresco/adf-core';
import { take } from 'rxjs/operators';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { FolderRulesService } from './folder-rules.service'; import { FolderRulesService } from './folder-rules.service';
import { Rule } from '../model/rule.model'; import { getMoreRulesResponseMock, getRulesResponseMock, manyRulesMock, moreRulesMock, ruleMock, rulesMock } from '../mock/rules.mock';
import { dummyResponse, dummyRules } from '../mock/rules.mock'; import { ruleSetMock } from '../mock/rule-sets.mock';
import { NodeInfo } from '@alfresco/aca-shared/store'; import { expect } from '@angular/flex-layout/_private-utils/testing';
import { ContentApiService } from '@alfresco/aca-shared'; import { owningFolderIdMock } from '../mock/node.mock';
import { dummyGetNodeResponse, dummyNodeInfo } from '../mock/node.mock'; import { take } from 'rxjs/operators';
describe('FolderRulesService', () => { describe('FolderRulesService', () => {
let folderRulesService: FolderRulesService; let folderRulesService: FolderRulesService;
let contentApi: ContentApiService;
let rulesPromise: Promise<Partial<Rule>[]>;
let folderInfoPromise: Promise<NodeInfo>;
let deletedRulePromise: Promise<string>;
let rules: Partial<Rule>[];
let folderInfo: NodeInfo;
let deletedRule: string;
let apiCallSpy;
let getNodeSpy;
const nodeId = '********-fake-node-****-********'; let callApiSpy: jasmine.Spy;
const ruleId = '********-fake-rule-****-********';
const ruleSetId = '-default-';
const params = [{}, {}, {}, {}, {}, ['application/json'], ['application/json']];
const paramsWithBody = [{}, {}, {}, {}, dummyRules[0], ['application/json'], ['application/json']];
beforeEach(async () => { const nodeId = owningFolderIdMock;
const ruleSetId = 'rule-set-id';
const mockedRule = ruleMock('rule-mock');
const ruleId = mockedRule.id;
beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [CoreTestingModule], imports: [CoreTestingModule],
providers: [FolderRulesService, ContentApiService] providers: [FolderRulesService]
}); });
folderRulesService = TestBed.inject<FolderRulesService>(FolderRulesService); folderRulesService = TestBed.inject<FolderRulesService>(FolderRulesService);
callApiSpy = spyOn<any>(folderRulesService, 'callApi');
}); });
describe('loadRules', () => { it('should load some rules into a rule set', () => {
beforeEach(async () => { const ruleSet = ruleSetMock();
contentApi = TestBed.inject<ContentApiService>(ContentApiService); callApiSpy.and.returnValue(of(getRulesResponseMock));
apiCallSpy = spyOn<any>(folderRulesService, 'apiCall') expect(ruleSet.rules.length).toBe(0);
.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'GET', params) expect(ruleSet.hasMoreRules).toBeTrue();
.and.returnValue(of(dummyResponse) as any); expect(ruleSet.loadingRules).toBeFalse();
getNodeSpy = spyOn<any>(contentApi, 'getNode').and.returnValue(of(dummyGetNodeResponse) as any);
rulesPromise = folderRulesService.rulesListing$.pipe(take(2)).toPromise(); folderRulesService.loadRules(ruleSet);
folderInfoPromise = folderRulesService.folderInfo$.pipe(take(2)).toPromise();
folderRulesService.loadRules(nodeId, ruleSetId); expect(callApiSpy).toHaveBeenCalledWith(`/nodes/${ruleSet.owningFolder.id}/rule-sets/${ruleSet.id}/rules?skipCount=0&maxItems=100`, 'GET');
expect(ruleSet.rules.length).toBe(2);
rules = await rulesPromise; expect(ruleSet.rules).toEqual(rulesMock);
folderInfo = await folderInfoPromise; expect(ruleSet.hasMoreRules).toBeFalse();
});
it('should format and set the data', async () => {
expect(rules).toBeTruthy('rulesListing$ is empty');
expect(folderInfo).toBeTruthy('folderInfo$ is empty');
expect(rules.length).toBe(2, 'rulesListing$ size is wrong');
expect(rules).toEqual(dummyRules, 'The list of rules is incorrectly formatted');
expect(folderInfo).toEqual(dummyNodeInfo, 'The node info is wrong');
expect(apiCallSpy).toHaveBeenCalledTimes(1);
expect(apiCallSpy).toHaveBeenCalledWith(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'GET', params);
expect(getNodeSpy).toHaveBeenCalledTimes(1);
});
}); });
describe('deleteRule', () => { it('should load more rules if it still has some more to load', () => {
beforeEach(async () => { const ruleSet = ruleSetMock(rulesMock);
apiCallSpy = spyOn<any>(folderRulesService, 'apiCall') callApiSpy.and.returnValue(of(getMoreRulesResponseMock));
.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'DELETE', params)
.and.returnValue(ruleId);
deletedRulePromise = folderRulesService.deletedRuleId$.pipe(take(2)).toPromise(); expect(ruleSet.rules.length).toBe(2);
expect(ruleSet.hasMoreRules).toBeTrue();
folderRulesService.deleteRule(nodeId, ruleId, ruleSetId); folderRulesService.loadRules(ruleSet);
deletedRule = await deletedRulePromise; expect(callApiSpy).toHaveBeenCalledWith(`/nodes/${ruleSet.owningFolder.id}/rule-sets/${ruleSet.id}/rules?skipCount=2&maxItems=100`, 'GET');
}); expect(ruleSet.rules.length).toBe(4);
expect(ruleSet.rules).toEqual([...rulesMock, ...moreRulesMock]);
it('should delete a rule and return its id', async () => { expect(ruleSet.hasMoreRules).toBeFalse();
expect(deletedRule).toBeTruthy('rule has not been deleted');
expect(deletedRule).toBe(ruleId, 'wrong id of deleted rule');
expect(apiCallSpy).toHaveBeenCalledTimes(1);
expect(apiCallSpy).toHaveBeenCalledWith(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'DELETE', params);
});
}); });
describe('toggleRule', () => { it('should select the right rule rule after loading', () => {
beforeEach(async () => { const ruleSet = ruleSetMock(rulesMock);
apiCallSpy = spyOn<any>(folderRulesService, 'apiCall') spyOn(folderRulesService, 'getRules').and.returnValue(of({ rules: manyRulesMock, hasMoreRules: false }));
.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', paramsWithBody) const selectedRuleSourceSpy = spyOn(folderRulesService['selectedRuleSource'], 'next');
.and.returnValue([]);
folderRulesService.toggleRule(nodeId, ruleId, dummyRules[0]); folderRulesService.loadRules(ruleSet, 0);
}); expect(selectedRuleSourceSpy).not.toHaveBeenCalled();
it('should send correct PUT request', async () => { folderRulesService.loadRules(ruleSet, 0, 'first');
expect(apiCallSpy).toHaveBeenCalled(); expect(selectedRuleSourceSpy).toHaveBeenCalledWith(ruleMock('rule1'));
expect(apiCallSpy).toHaveBeenCalledWith(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', paramsWithBody); selectedRuleSourceSpy.calls.reset();
});
folderRulesService.loadRules(ruleSet, 0, 'last');
expect(selectedRuleSourceSpy).toHaveBeenCalledWith(ruleMock('rule5'));
selectedRuleSourceSpy.calls.reset();
selectedRuleSourceSpy.calls.reset();
folderRulesService.loadRules(ruleSet, 0, ruleMock('rule3'));
expect(selectedRuleSourceSpy).toHaveBeenCalledWith(ruleMock('rule3'));
}); });
describe('createRule', () => { it('should delete a rule and return its id', async () => {
beforeEach(async () => { callApiSpy.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'DELETE').and.returnValue(ruleId);
spyOn<any>(folderRulesService, 'apiCall') const deletedRulePromise = folderRulesService.deletedRuleId$.pipe(take(2)).toPromise();
.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', async () => { folderRulesService.deleteRule(nodeId, ruleId, ruleSetId);
const result = await folderRulesService.createRule(nodeId, dummyRules[0]); const deletedRule = await deletedRulePromise;
expect(result).toEqual(dummyRules[0]);
}); expect(deletedRule).toBeTruthy('rule has not been deleted');
expect(deletedRule).toBe(ruleId, 'wrong id of deleted rule');
expect(callApiSpy).toHaveBeenCalledTimes(1);
expect(callApiSpy).toHaveBeenCalledWith(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'DELETE');
});
it('should send correct POST request and return created rule', async () => {
callApiSpy.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'POST', mockedRule).and.returnValue(Promise.resolve(mockedRule));
const result = await folderRulesService.createRule(nodeId, mockedRule, ruleSetId);
expect(result).toEqual(mockedRule);
}); });
it('should send correct PUT request to update rule and return it', async () => { it('should send correct PUT request to update rule and return it', async () => {
spyOn<any>(folderRulesService, 'apiCall') callApiSpy.withArgs(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', mockedRule).and.returnValue(Promise.resolve(mockedRule));
.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]); const result = await folderRulesService.updateRule(nodeId, ruleId, mockedRule, ruleSetId);
expect(result).toEqual(dummyRules[0]); expect(result).toEqual(mockedRule);
}); });
}); });

View File

@ -25,18 +25,24 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { AlfrescoApiService } from '@alfresco/adf-core'; import { AlfrescoApiService } from '@alfresco/adf-core';
import { BehaviorSubject, forkJoin, from, Observable, of } from 'rxjs'; import { BehaviorSubject, from, Observable } from 'rxjs';
import { catchError, finalize, map } from 'rxjs/operators'; import { finalize, map } from 'rxjs/operators';
import { Rule, RuleForForm, RuleOptions } from '../model/rule.model'; import { Rule, RuleForForm, RuleOptions } from '../model/rule.model';
import { ContentApiService } from '@alfresco/aca-shared';
import { NodeInfo } from '@alfresco/aca-shared/store';
import { RuleCompositeCondition } from '../model/rule-composite-condition.model'; import { RuleCompositeCondition } from '../model/rule-composite-condition.model';
import { RuleSimpleCondition } from '../model/rule-simple-condition.model'; import { RuleSimpleCondition } from '../model/rule-simple-condition.model';
import { RuleSet } from '../model/rule-set.model';
interface GetRulesResult {
rules: Rule[];
hasMoreRules: boolean;
}
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class FolderRulesService { export class FolderRulesService {
public static MAX_RULES_PER_GET = 100;
public static get emptyCompositeCondition(): RuleCompositeCondition { public static get emptyCompositeCondition(): RuleCompositeCondition {
return { return {
inverted: false, inverted: false,
@ -79,119 +85,77 @@ export class FolderRulesService {
return value; return value;
} }
private rulesListingSource = new BehaviorSubject<Rule[]>([]); private selectedRuleSource = new BehaviorSubject<Rule>(null);
rulesListing$: Observable<Rule[]> = this.rulesListingSource.asObservable();
private folderInfoSource = new BehaviorSubject<NodeInfo>(null);
folderInfo$: Observable<NodeInfo> = this.folderInfoSource.asObservable();
private loadingSource = new BehaviorSubject<boolean>(false);
loading$ = this.loadingSource.asObservable();
private deletedRuleIdSource = new BehaviorSubject<string>(null); private deletedRuleIdSource = new BehaviorSubject<string>(null);
selectedRule$ = this.selectedRuleSource.asObservable();
deletedRuleId$: Observable<string> = this.deletedRuleIdSource.asObservable(); deletedRuleId$: Observable<string> = this.deletedRuleIdSource.asObservable();
constructor(private apiService: AlfrescoApiService, private contentApi: ContentApiService) {} constructor(private apiService: AlfrescoApiService) {}
loadRules(nodeId: string, ruleSetId: string = '-default-'): void { private callApi(path: string, httpMethod: string, body: object = {}): Promise<any> {
this.loadingSource.next(true); // APIs used by this service are still private and not yet available for public use
forkJoin([ const params = [{}, {}, {}, {}, body, ['application/json'], ['application/json']];
from( return this.apiService.getInstance().contentPrivateClient.callApi(path, httpMethod, ...params);
this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'GET', [{}, {}, {}, {}, {}, ['application/json'], ['application/json']]) }
).pipe(
map((res) => this.formatRules(res)), getRules(owningFolderId: string, ruleSetId: string, skipCount = 0): Observable<GetRulesResult> {
catchError((error) => { return from(
if (error.status === 404) { this.callApi(
return of([]); `/nodes/${owningFolderId}/rule-sets/${ruleSetId}/rules?skipCount=${skipCount}&maxItems=${FolderRulesService.MAX_RULES_PER_GET}`,
} 'GET'
return of(error);
})
),
this.contentApi.getNode(nodeId).pipe(
catchError((error) => {
if (error.status === 404) {
return of({ entry: null });
}
return of(error);
})
) )
]) ).pipe(
.pipe(finalize(() => this.loadingSource.next(false))) map((res) => ({
.subscribe( rules: this.formatRules(res),
([rules, nodeInfo]) => { hasMoreRules: !!res?.list?.pagination?.hasMoreItems
this.rulesListingSource.next(rules); }))
this.folderInfoSource.next(nodeInfo.entry); );
},
(error) => {
this.rulesListingSource.next([]);
this.folderInfoSource.next(error);
}
);
} }
createRule(nodeId: string, rule: Partial<Rule>, ruleSetId: string = '-default-') { loadRules(ruleSet: RuleSet, skipCount = ruleSet.rules.length, selectRule: 'first' | 'last' | Rule = null) {
return this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'POST', [ if (ruleSet && !ruleSet.loadingRules) {
{}, ruleSet.loadingRules = true;
{}, this.getRules(ruleSet.owningFolder.id, ruleSet.id, skipCount)
{}, .pipe(
{}, finalize(() => {
{ ...rule }, ruleSet.loadingRules = false;
['application/json'], })
['application/json'] )
]); .subscribe((res: GetRulesResult) => {
ruleSet.hasMoreRules = res.hasMoreRules;
ruleSet.rules.splice(skipCount);
ruleSet.rules.push(...res.rules);
if (selectRule === 'first') {
this.selectRule(ruleSet.rules[0]);
} else if (selectRule === 'last') {
this.selectRule(ruleSet.rules[ruleSet.rules.length - 1]);
} else if (selectRule) {
this.selectRule(selectRule);
}
});
}
} }
updateRule(nodeId: string, ruleId: string, rule: Rule, ruleSetId: string = '-default-') { createRule(nodeId: string, rule: Partial<Rule>, ruleSetId: string): Promise<unknown> {
return this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', [ return this.callApi(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules`, 'POST', { ...rule });
{},
{},
{},
{},
{ ...rule },
['application/json'],
['application/json']
]);
} }
deleteRule(nodeId: string, ruleId: string, ruleSetId: string = '-default-'): void { updateRule(nodeId: string, ruleId: string, rule: Rule, ruleSetId: string): Promise<unknown> {
this.loadingSource.next(true); return this.callApi(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', { ...rule });
from( }
this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'DELETE', [
{}, deleteRule(nodeId: string, ruleId: string, ruleSetId: string = '-default-') {
{}, from(this.callApi(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'DELETE')).subscribe(
{},
{},
{},
['application/json'],
['application/json']
])
).subscribe(
() => { () => {
this.deletedRuleIdSource.next(ruleId); this.deletedRuleIdSource.next(ruleId);
}, },
(error) => { (error) => {
this.deletedRuleIdSource.next(error); this.deletedRuleIdSource.next(error);
this.loadingSource.next(false);
} }
); );
} }
toggleRule(nodeId: string, ruleId: string, rule: Rule, ruleSetId: string = '-default-'): void {
from(
this.apiCall(`/nodes/${nodeId}/rule-sets/${ruleSetId}/rules/${ruleId}`, 'PUT', [
{},
{},
{},
{},
{ ...rule },
['application/json'],
['application/json']
])
).subscribe({ error: (error) => console.error(error) });
}
private apiCall(path: string, httpMethod: string, params?: any[]): Promise<any> {
// APIs used by this service are still private and not yet available for public use
return this.apiService.getInstance().contentPrivateClient.callApi(path, httpMethod, ...params);
}
private formatRules(res): Rule[] { private formatRules(res): Rule[] {
return res.list.entries.map((entry) => this.formatRule(entry.entry)); return res.list.entries.map((entry) => this.formatRule(entry.entry));
} }
@ -239,4 +203,8 @@ export class FolderRulesService {
parameter: obj.parameter || '' parameter: obj.parameter || ''
}; };
} }
selectRule(rule: Rule) {
this.selectedRuleSource.next(rule);
}
} }