diff --git a/lib/process-services/src/lib/mock/process/process-filters.mock.ts b/lib/process-services/src/lib/mock/process/process-filters.mock.ts index a7c3562e722..7ef6b39b930 100644 --- a/lib/process-services/src/lib/mock/process/process-filters.mock.ts +++ b/lib/process-services/src/lib/mock/process/process-filters.mock.ts @@ -49,3 +49,55 @@ export const fakeProcessFiltersResponse = { filter: { sort: 'created-desc', name: '', state: 'running' } })] }; + +export const dummyRunningFilter = { + appId: 123, + name: 'Running', + filter: { sort: 'created-desc', name: '', state: 'running' }, + icon: 'fa-random', + id: 18, + index: 10, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyCompletedFilter = { + appId: 123, + name: 'Completed', + filter: { sort: 'created-desc', name: '', state: 'completed' }, + icon: 'fa-random', + id: 19, + index: 11, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyAllFilter = { + appId: 123, + name: 'All', + filter: { sort: 'created-desc', name: '', state: 'all' }, + icon: 'fa-random', + id: 20, + index: 12, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyDuplicateRunningFilter = { + appId: 123, + name: 'Running', + filter: { sort: 'created-desc', name: '', state: 'running' }, + icon: 'fa-random', + id: 21, + index: 13, + recent: false, + hasFilter: () => { + return true; + } +}; diff --git a/lib/process-services/src/lib/mock/task/task-filters.mock.ts b/lib/process-services/src/lib/mock/task/task-filters.mock.ts index 8430a72ff31..64962f3a103 100644 --- a/lib/process-services/src/lib/mock/task/task-filters.mock.ts +++ b/lib/process-services/src/lib/mock/task/task-filters.mock.ts @@ -102,3 +102,68 @@ export const fakeRepresentationFilter2: FilterRepresentationModel = new FilterRe dueBefore: null } }); + +export const dummyMyTasksFilter = { + appId: 101, + name: 'My Tasks', + filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-mytasks' }, + icon: 'fa-random', + id: 81, + index: 21, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyInvolvedTasksFilter = { + appId: 101, + name: 'Involved Tasks', + filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-involved' }, + icon: 'fa-random', + id: 82, + index: 22, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyQueuedTasksFilter = { + appId: 101, + name: 'Queued Tasks', + filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-queued' }, + icon: 'fa-random', + id: 83, + index: 23, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyCompletedTasksFilter = { + appId: 101, + name: 'Completed', + filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-completed' }, + icon: 'fa-random', + id: 84, + index: 24, + recent: false, + hasFilter: () => { + return true; + } +}; + +export const dummyDuplicateMyTasksFilter = { + appId: 101, + name: 'My Tasks', + filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-mytasks' }, + icon: 'fa-random', + id: 85, + index: 25, + recent: false, + hasFilter: () => { + return true; + } +}; diff --git a/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts b/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts index 37a9694980f..90359ef961d 100644 --- a/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts +++ b/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts @@ -16,10 +16,12 @@ */ import { TestBed } from '@angular/core/testing'; -import { mockError, fakeProcessFiltersResponse } from '../../mock'; +import { mockError, fakeProcessFiltersResponse, dummyRunningFilter, dummyAllFilter, dummyCompletedFilter, dummyDuplicateRunningFilter } from '../../mock'; import { FilterProcessRepresentationModel } from '../models/filter-process.model'; import { ProcessFilterService } from './process-filter.service'; import { CoreTestingModule } from '@alfresco/adf-core'; +import { ProcessInstanceFilterRepresentation } from '@alfresco/js-api'; +import { of } from 'rxjs'; declare let jasmine: any; @@ -234,5 +236,78 @@ describe('Process filter', () => { }); }); + + describe('isFilterAlreadyExisting', () => { + let dummyProcessFilters: FilterProcessRepresentationModel[]; + let filterRepresentationData: ProcessInstanceFilterRepresentation; + + beforeEach(() => { + dummyProcessFilters = [ + { + appId: 0, + filter: filterRepresentationData, + icon: 'fa-random', + id: 8, + index: 0, + name: 'Running', + recent: false, + hasFilter: () => { + return true; + } + } + ]; + + filterRepresentationData = { + name : '', + sort : 'created-desc', + state : 'running' + }; + }); + + it('should return true if the process filter already exists', () => { + const processFilterName = 'Running'; + const result = service.isFilterAlreadyExisting(dummyProcessFilters, processFilterName); + expect(result).toBe(true); + }); + + it('should return false if the process filter does not exist', () => { + const processFilterName = 'All'; + const result = service.isFilterAlreadyExisting(dummyProcessFilters, processFilterName); + expect(result).toBe(false); + }); + }); + + describe('createDefaultFilters', () => { + + it('should return an array with unique process filters', (done) => { + const appId = 123; + + const runningFilter = dummyRunningFilter; + const completedFilter = dummyCompletedFilter; + const allFilter = dummyAllFilter; + const duplicateRunningFilter = dummyDuplicateRunningFilter; + + const runningObservable = of(runningFilter); + const completedObservable = of(completedFilter); + const allObservable = of(allFilter); + const duplicateRunningObservable = of(duplicateRunningFilter); + + spyOn(service, 'getRunningFilterInstance').and.returnValue(runningFilter); + spyOn(service, 'getCompletedFilterInstance').and.returnValue(completedFilter); + spyOn(service, 'getAllFilterInstance').and.returnValue(allFilter); + + spyOn(service, 'addProcessFilter').and.returnValues(runningObservable, completedObservable, allObservable, duplicateRunningObservable); + + service.createDefaultFilters(appId).subscribe((result) => { + expect(result).toEqual([ + new FilterProcessRepresentationModel({ ...runningFilter, filter: runningFilter.filter, appId }), + new FilterProcessRepresentationModel({ ...completedFilter, filter: completedFilter.filter, appId }), + new FilterProcessRepresentationModel({ ...allFilter, filter: allFilter.filter, appId }) + ]); + done(); + }); + }); + + }); }); }); diff --git a/lib/process-services/src/lib/process-list/services/process-filter.service.ts b/lib/process-services/src/lib/process-list/services/process-filter.service.ts index 61f26457a59..598efe98aa4 100644 --- a/lib/process-services/src/lib/process-list/services/process-filter.service.ts +++ b/lib/process-services/src/lib/process-list/services/process-filter.service.ts @@ -51,8 +51,10 @@ export class ProcessFilterService { map((response) => { const filters: FilterProcessRepresentationModel[] = []; response.data.forEach((filter) => { - const filterModel = new FilterProcessRepresentationModel(filter); - filters.push(filterModel); + if (!this.isFilterAlreadyExisting(filters, filter.name)) { + const filterModel = new FilterProcessRepresentationModel(filter); + filters.push(filterModel); + } }); return filters; }), @@ -116,12 +118,14 @@ export class ProcessFilterService { (res) => { const filters: FilterProcessRepresentationModel[] = []; res.forEach((filter) => { - if (filter.name === runningFilter.name) { - filters.push(new FilterProcessRepresentationModel({ ...filter, filter: runningFilter.filter, appId })); - } else if (filter.name === completedFilter.name) { - filters.push(new FilterProcessRepresentationModel({ ...filter, filter: completedFilter.filter, appId })); - } else if (filter.name === allFilter.name) { - filters.push(new FilterProcessRepresentationModel({ ...filter, filter: allFilter.filter, appId })); + if (!this.isFilterAlreadyExisting(filters, filter.name)) { + if (filter.name === runningFilter.name) { + filters.push(new FilterProcessRepresentationModel({ ...filter, filter: runningFilter.filter, appId })); + } else if (filter.name === completedFilter.name) { + filters.push(new FilterProcessRepresentationModel({ ...filter, filter: completedFilter.filter, appId })); + } else if (filter.name === allFilter.name) { + filters.push(new FilterProcessRepresentationModel({ ...filter, filter: allFilter.filter, appId })); + } } }); observer.next(filters); @@ -133,6 +137,17 @@ export class ProcessFilterService { }); } + /** + * Checks if a filter with the given name already exists in the list of filters. + * + * @param filters - An array of FilterProcessRepresentationModel objects representing the existing filters. + * @param filterName - The name of the filter to check for existence. + * @returns - True if a filter with the specified name already exists, false otherwise. + */ + isFilterAlreadyExisting(filters: FilterProcessRepresentationModel[], filterName: string): boolean { + return filters.some((existingFilter) => existingFilter.name === filterName); + } + /** * Creates and returns a filter that matches "running" process instances. * diff --git a/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts b/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts index d69f2a3ebf7..238ed05392e 100644 --- a/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts +++ b/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts @@ -17,11 +17,13 @@ import { TestBed } from '@angular/core/testing'; import { fakeAppPromise } from '../../mock'; -import { fakeFiltersResponse, fakeAppFilter } from '../../mock/task/task-filters.mock'; +import { fakeFiltersResponse, fakeAppFilter, dummyMyTasksFilter, dummyInvolvedTasksFilter, dummyQueuedTasksFilter, dummyCompletedTasksFilter, dummyDuplicateMyTasksFilter } from '../../mock/task/task-filters.mock'; import { FilterRepresentationModel } from '../models/filter.model'; import { TaskFilterService } from './task-filter.service'; import { CoreModule } from '@alfresco/adf-core'; import { ProcessTestingModule } from '../../testing/process.testing.module'; +import { TaskFilterRepresentation } from '@alfresco/js-api'; +import { of } from 'rxjs'; declare let jasmine: any; @@ -251,4 +253,81 @@ describe('Activiti Task filter Service', () => { }); }); }); + + describe('isFilterAlreadyExisting', () => { + let dummyTaskFilters: FilterRepresentationModel[]; + let filterRepresentationData: TaskFilterRepresentation; + + beforeEach(() => { + dummyTaskFilters = [ + { + appId: 0, + filter: filterRepresentationData, + icon: 'fa-random', + id: 9, + index: 0, + name: 'My Tasks', + recent: false, + hasFilter: () => { + return true; + } + } + ]; + + filterRepresentationData = { + name : '', + sort : 'created-desc', + state : 'running' + }; + }); + + it('should return true if the task filter already exists', () => { + const taskFilterName = 'My Tasks'; + const result = service.isFilterAlreadyExisting(dummyTaskFilters, taskFilterName); + expect(result).toBe(true); + }); + + it('should return false if the task filter does not exist', () => { + const taskFilterName = 'Involved Tasks'; + const result = service.isFilterAlreadyExisting(dummyTaskFilters, taskFilterName); + expect(result).toBe(false); + }); + }); + + describe('createDefaultFilters', () => { + + it('should return an array with unique task filters', (done) => { + const appId = 101; + + const myTasksFilter = dummyMyTasksFilter; + const involvedTasksFilter = dummyInvolvedTasksFilter; + const queuedTasksFilter = dummyQueuedTasksFilter; + const completedTasksFilter = dummyCompletedTasksFilter; + const duplicateMyTasksFilter = dummyDuplicateMyTasksFilter; + + const myTasksObservableObservable = of(myTasksFilter); + const involvedTasksObservable = of(involvedTasksFilter); + const queuedTasksObservable = of(queuedTasksFilter); + const completedTasksObservable = of(completedTasksFilter); + const duplicateMyTasksObservableObservable = of(duplicateMyTasksFilter); + + spyOn(service, 'getMyTasksFilterInstance').and.returnValue(myTasksFilter); + spyOn(service, 'getInvolvedTasksFilterInstance').and.returnValue(involvedTasksFilter); + spyOn(service, 'getQueuedTasksFilterInstance').and.returnValue(queuedTasksFilter); + spyOn(service, 'getCompletedTasksFilterInstance').and.returnValue(completedTasksFilter); + + spyOn(service, 'addFilter').and.returnValues(myTasksObservableObservable, involvedTasksObservable, queuedTasksObservable, completedTasksObservable, duplicateMyTasksObservableObservable); + + service.createDefaultFilters(appId).subscribe((result) => { + expect(result).toEqual([ + new FilterRepresentationModel({ ...myTasksFilter, filter: myTasksFilter.filter, appId }), + new FilterRepresentationModel({ ...involvedTasksFilter, filter: involvedTasksFilter.filter, appId }), + new FilterRepresentationModel({ ...queuedTasksFilter, filter: queuedTasksFilter.filter, appId }), + new FilterRepresentationModel({ ...completedTasksFilter, filter: completedTasksFilter.filter, appId }) + ]); + done(); + }); + }); + + }); }); diff --git a/lib/process-services/src/lib/task-list/services/task-filter.service.ts b/lib/process-services/src/lib/task-list/services/task-filter.service.ts index 0e1ceaeb11d..c8ef7a90713 100644 --- a/lib/process-services/src/lib/task-list/services/task-filter.service.ts +++ b/lib/process-services/src/lib/task-list/services/task-filter.service.ts @@ -67,30 +67,32 @@ export class TaskFilterService { (res) => { const filters: FilterRepresentationModel[] = []; res.forEach((filter) => { - if (filter.name === involvedTasksFilter.name) { - filters.push(new FilterRepresentationModel({ - ...filter, - filter: involvedTasksFilter.filter, - appId - })); - } else if (filter.name === myTasksFilter.name) { - filters.push(new FilterRepresentationModel({ - ...filter, - filter: myTasksFilter.filter, - appId - })); - } else if (filter.name === queuedTasksFilter.name) { - filters.push(new FilterRepresentationModel({ - ...filter, - filter: queuedTasksFilter.filter, - appId - })); - } else if (filter.name === completedTasksFilter.name) { - filters.push(new FilterRepresentationModel({ - ...filter, - filter: completedTasksFilter.filter, - appId - })); + if (!this.isFilterAlreadyExisting(filters, filter.name)) { + if (filter.name === involvedTasksFilter.name) { + filters.push(new FilterRepresentationModel({ + ...filter, + filter: involvedTasksFilter.filter, + appId + })); + } else if (filter.name === myTasksFilter.name) { + filters.push(new FilterRepresentationModel({ + ...filter, + filter: myTasksFilter.filter, + appId + })); + } else if (filter.name === queuedTasksFilter.name) { + filters.push(new FilterRepresentationModel({ + ...filter, + filter: queuedTasksFilter.filter, + appId + })); + } else if (filter.name === completedTasksFilter.name) { + filters.push(new FilterRepresentationModel({ + ...filter, + filter: completedTasksFilter.filter, + appId + })); + } } }); observer.next(filters); @@ -114,8 +116,10 @@ export class TaskFilterService { map((response: any) => { const filters: FilterRepresentationModel[] = []; response.data.forEach((filter: FilterRepresentationModel) => { - const filterModel = new FilterRepresentationModel(filter); - filters.push(filterModel); + if (!this.isFilterAlreadyExisting(filters, filter.name)) { + const filterModel = new FilterRepresentationModel(filter); + filters.push(filterModel); + } }); return filters; }), @@ -123,6 +127,17 @@ export class TaskFilterService { ); } + /** + * Checks if a filter with the given name already exists in the list of filters. + * + * @param filters - An array of `FilterRepresentationModel` objects representing the existing filters. + * @param filterName - The name of the filter to check for existence. + * @returns - True if a filter with the specified name already exists, false otherwise. + */ + isFilterAlreadyExisting(filters: FilterRepresentationModel[], filterName: string): boolean { + return filters.some((existingFilter) => existingFilter.name === filterName); + } + /** * Gets a task filter by ID. *