diff --git a/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.spec.ts b/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.spec.ts index aaa7ce0784..3f1345dc35 100644 --- a/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.spec.ts +++ b/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.spec.ts @@ -39,6 +39,20 @@ describe('ProcessListCloudService', () => { isEcmLoggedIn: () => false }); + const returnCallOperation = (): any => ({ + oauth2Auth: { + callCustomApi: (_queryUrl, operation, _context, _queryParams) => Promise.resolve(operation) + }, + isEcmLoggedIn: () => false + }); + + const returnCallBody = (): any => ({ + oauth2Auth: { + callCustomApi: (_queryUrl, _operation, _context, _queryParams, _headerParams, _formParams, bodyParam) => Promise.resolve(bodyParam) + }, + isEcmLoggedIn: () => false + }); + setupTestBed({ imports: [ ProcessServiceCloudTestingModule @@ -99,4 +113,87 @@ describe('ProcessListCloudService', () => { } ); }); + + describe('getAdminProcessRequest', () => { + + it('should append to the call all the parameters', async () => { + const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service' } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallQueryParameters); + const request = await service.getAdminProcessByRequest(processRequest).toPromise(); + + expect(request).toBeDefined(); + expect(request).not.toBeNull(); + expect(request.skipCount).toBe(0); + expect(request.maxItems).toBe(20); + expect(request.service).toBe('fake-service'); + }); + + it('should concat the app name to the request url', async () => { + const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service' } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallUrl); + const requestUrl = await service.getAdminProcessByRequest(processRequest).toPromise(); + + expect(requestUrl).toBeDefined(); + expect(requestUrl).not.toBeNull(); + expect(requestUrl).toContain('/fakeName/query/admin/v1/process-instances'); + }); + + it('should concat the sorting to append as parameters', async () => { + const processRequest = { + appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service', + sorting: [{ orderBy: 'NAME', direction: 'DESC' }, { orderBy: 'TITLE', direction: 'ASC' }] + } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallQueryParameters); + const request = await service.getAdminProcessByRequest(processRequest).toPromise(); + + expect(request).toBeDefined(); + expect(request).not.toBeNull(); + expect(request.sort).toBe('NAME,DESC&TITLE,ASC'); + }); + + it('should return an error when app name is not specified', async () => { + const processRequest = { appName: null } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallUrl); + + try { + await service.getAdminProcessByRequest(processRequest).toPromise(); + + fail('Should have thrown error'); + } catch(error) { + expect(error).toBe('Appname not configured'); + } + }); + + it('should make post request', async () => { + const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service' } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallOperation); + const requestMethod = await service.getAdminProcessByRequest(processRequest).toPromise(); + expect(requestMethod).toBeDefined(); + expect(requestMethod).not.toBeNull(); + expect(requestMethod).toBe('POST'); + }); + + it('should not have variable keys as part of query parameters', async () => { + const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service', variableKeys: ['test-one', 'test-two'] } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallQueryParameters); + const requestParams = await service.getAdminProcessByRequest(processRequest).toPromise(); + + expect(requestParams).toBeDefined(); + expect(requestParams).not.toBeNull(); + expect(requestParams.variableKeys).not.toBeDefined(); + }); + + it('should send right variable keys as post body', async () => { + const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service', variableKeys: ['test-one', 'test-two'] } as ProcessQueryCloudRequestModel; + spyOn(alfrescoApiService, 'getInstance').and.callFake(returnCallBody); + const requestBodyParams = await service.getAdminProcessByRequest(processRequest).toPromise(); + + expect(requestBodyParams).toBeDefined(); + expect(requestBodyParams).not.toBeNull(); + expect(requestBodyParams.variableKeys).toBeDefined(); + expect(requestBodyParams.variableKeys.length).toBe(2); + expect(requestBodyParams.variableKeys[0]).toBe('test-one'); + expect(requestBodyParams.variableKeys[1]).toBe('test-two'); + }); + }); }); diff --git a/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.ts b/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.ts index e253f7ee1b..16609585f4 100644 --- a/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.ts +++ b/lib/process-services-cloud/src/lib/process/process-list/services/process-list-cloud.service.ts @@ -32,23 +32,21 @@ export class ProcessListCloudService extends BaseCloudService { super(apiService, appConfigService); } - /** - * Finds a process using an object with optional query properties. - * - * @param requestNode Query object - * @param queryUrl Query url - * @returns Process information - */ - getProcessByRequest(requestNode: ProcessQueryCloudRequestModel, queryUrl?: string): Observable { + private getProcess( + callback: (queryUrl: string, queryParams: any) => Observable, + defaultQueryUrl: string, + requestNode: ProcessQueryCloudRequestModel, + queryUrl?: string + ): Observable { if (requestNode.appName || requestNode.appName === '') { - queryUrl = queryUrl || `${this.getBasePath(requestNode.appName)}/query/v1/process-instances`; + queryUrl = queryUrl || `${this.getBasePath(requestNode.appName)}/${defaultQueryUrl}`; const queryParams = this.buildQueryParams(requestNode); const sortingParams = this.buildSortingParam(requestNode.sorting); if (sortingParams) { queryParams['sort'] = sortingParams; } - return this.get(queryUrl, queryParams).pipe( + return callback(queryUrl, queryParams).pipe( map((response: any) => { const entries = response.list && response.list.entries; if (entries) { @@ -63,6 +61,51 @@ export class ProcessListCloudService extends BaseCloudService { } } + /** + * Finds a process using an object with optional query properties. + * + * @param requestNode Query object + * @param queryUrl Query url + * @returns Process information + */ + getProcessByRequest(requestNode: ProcessQueryCloudRequestModel, queryUrl?: string): Observable { + const callback = (url: string, queryParams: any) => this.get(url, queryParams); + const defaultQueryUrl = 'query/v1/process-instances'; + + return this.getProcess(callback, defaultQueryUrl, requestNode, queryUrl); + } + + /** + * Finds a process using an object with optional query properties in admin app. + * + * @param requestNode Query object + * @param queryUrl Query url + * @returns Process information + */ + getAdminProcessByRequest(requestNode: ProcessQueryCloudRequestModel, queryUrl?: string): Observable { + const callback = (url: string, params: any) => { + const postBody = { + variableKeys: this.getVariableKeysFromQueryParams(params) + }; + + delete params['variableKeys']; + + return this.post(url, postBody, params); + }; + + const defaultQueryUrl = 'query/admin/v1/process-instances'; + + return this.getProcess(callback, defaultQueryUrl, requestNode, queryUrl); + } + + private getVariableKeysFromQueryParams(queryParams: any): string[] { + if (!queryParams['variableKeys'] || queryParams['variableKeys'].length <= 0) { + return []; + } + + return queryParams['variableKeys'].split(','); + } + protected isPropertyValueValid(requestNode: any, property: string): boolean { return requestNode[property] !== '' && requestNode[property] !== null && requestNode[property] !== undefined; } diff --git a/lib/process-services-cloud/src/lib/services/base-cloud.service.ts b/lib/process-services-cloud/src/lib/services/base-cloud.service.ts index 0bba387765..c951b432f5 100644 --- a/lib/process-services-cloud/src/lib/services/base-cloud.service.ts +++ b/lib/process-services-cloud/src/lib/services/base-cloud.service.ts @@ -52,13 +52,14 @@ export class BaseCloudService { : this.contextRoot; } - protected post(url: string, data?: T): Observable { + protected post(url: string, data?: T, queryParams?: any): Observable { return from( this.callApi({ ...this.defaultParams, path: url, httpMethod: 'POST', - bodyParam: data + bodyParam: data, + queryParams }) ); }