diff --git a/geonode_mapstore_client/client/js/epics/gnsearch.js b/geonode_mapstore_client/client/js/epics/gnsearch.js index ec83a92a56..c6d792c194 100644 --- a/geonode_mapstore_client/client/js/epics/gnsearch.js +++ b/geonode_mapstore_client/client/js/epics/gnsearch.js @@ -28,8 +28,7 @@ import { import { resourceLoading, setResource, - resourceError, - processResources + resourceError } from '@js/actions/gnresource'; import { LOCATION_CHANGE, @@ -41,10 +40,15 @@ import { } from '@js/utils/SearchUtils'; import url from 'url'; import { getCustomMenuFilters } from '@js/selectors/config'; -import { STOP_ASYNC_PROCESS, stopAsyncProcess } from '@js/actions/resourceservice'; +import { + STOP_ASYNC_PROCESS, + stopAsyncProcess, + startAsyncProcess +} from '@js/actions/resourceservice'; import { ProcessTypes, - ProcessStatus + ProcessStatus, + extractExecutionsFromResources } from '@js/utils/ResourceServiceUtils'; import { getCurrentProcesses } from '@js/selectors/resourceservice'; import { userSelector } from '@mapstore/framework/selectors/security'; @@ -143,12 +147,10 @@ const requestResourcesObservable = ({ isNextPageAvailable }) => { const currentUser = userSelector(state); - const processingResources = resources.filter((resource) => (resource.executions?.length > 0 && resource.executions[0].status_url && resource.executions[0].user === currentUser?.info?.preferred_username) && { ...resource, executions: resource.executions[0] }); + const preferredUsername = currentUser?.info?.preferred_username; + const pendingExecutions = extractExecutionsFromResources(resources, preferredUsername); return Observable.of( - ...processingResources.map((resource) => { - const process = `${resource?.executions[0].func_name}Resource`; - return processResources(process, [resource], false); - }), + ...pendingExecutions.map((payload) => startAsyncProcess(payload)), updateResources(resources, reset), updateResourcesMetadata({ isNextPageAvailable, diff --git a/geonode_mapstore_client/client/js/utils/ResourceServiceUtils.js b/geonode_mapstore_client/client/js/utils/ResourceServiceUtils.js index b495a2f1d6..ee43fc71dc 100644 --- a/geonode_mapstore_client/client/js/utils/ResourceServiceUtils.js +++ b/geonode_mapstore_client/client/js/utils/ResourceServiceUtils.js @@ -6,6 +6,8 @@ * LICENSE file in the root directory of this source tree. */ +import flatten from 'lodash/flatten'; + export const ProcessTypes = { DELETE_RESOURCE: 'deleteResource', COPY_RESOURCE: 'copyResource', @@ -35,3 +37,30 @@ export const actionButtons = { isControlled: true } }; + +export const extractExecutionsFromResources = (resources, username) => { + const processingResources = resources + .filter((resource) => ( + resource.executions?.length > 0 + && resource.executions.find(({ status_url: statusUrl, user }) => + statusUrl && user && user === username + ) + )); + return flatten(processingResources.map((resource) => { + return resource.executions + .filter(({ + func_name: funcName, + status_url: statusUrl, + user + }) => + funcName === 'copy' + && statusUrl && user && user === username + ).map((output) => { + return { + resource, + output, + processType: ProcessTypes.COPY_RESOURCE + }; + }); + })); +}; diff --git a/geonode_mapstore_client/client/js/utils/__tests__/ResourceServiceUtils-test.js b/geonode_mapstore_client/client/js/utils/__tests__/ResourceServiceUtils-test.js new file mode 100644 index 0000000000..b687ffe653 --- /dev/null +++ b/geonode_mapstore_client/client/js/utils/__tests__/ResourceServiceUtils-test.js @@ -0,0 +1,71 @@ + +/* + * Copyright 2021, GeoSolutions Sas. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +import expect from 'expect'; +import { + ProcessTypes, + extractExecutionsFromResources +} from '../ResourceServiceUtils'; + +describe('Test Resource Service Utils', () => { + it('should extract executions from resources', () => { + const executions = extractExecutionsFromResources([ + { + pk: 1, + executions: [ + { + status_url: 'status_url', + user: 'admin', + func_name: 'copy' + }, + { + status_url: 'status_url', + user: 'user', + func_name: 'copy' + }, + { + status_url: 'status_url', + user: 'user' + } + ] + }, + { + pk: 2, + executions: [] + } + ], 'admin'); + expect(executions).toEqual([{ + resource: { + pk: 1, + executions: [ + { + status_url: 'status_url', + user: 'admin', + func_name: 'copy' + }, + { + status_url: 'status_url', + user: 'user', + func_name: 'copy' + }, + { + status_url: 'status_url', + user: 'user' + } + ] + }, + output: { + status_url: 'status_url', + user: 'admin', + func_name: 'copy' + }, + processType: ProcessTypes.COPY_RESOURCE + }]); + }); +});