diff --git a/lib/process-services-cloud/src/lib/task/services/task-cloud.service.spec.ts b/lib/process-services-cloud/src/lib/task/services/task-cloud.service.spec.ts index ce80d6c446..425cd2b695 100644 --- a/lib/process-services-cloud/src/lib/task/services/task-cloud.service.spec.ts +++ b/lib/process-services-cloud/src/lib/task/services/task-cloud.service.spec.ts @@ -480,6 +480,63 @@ describe('Task Cloud Service', () => { ); }); + describe('nextTask', () => { + const appName = 'task-app'; + const nextTaskResponse = { + entry: { + appName: 'task-app', + id: 'next-task-id', + name: 'Next Task', + status: 'CREATED', + assignee: null + } + }; + + it('should return the next task details when called with a valid appName', (done) => { + requestSpy.and.returnValue(Promise.resolve(nextTaskResponse)); + + service.nextTask(appName).subscribe((res) => { + expect(res).toBeDefined(); + expect(res.id).toBe('next-task-id'); + expect(res.appName).toBe('task-app'); + done(); + }); + }); + + it('should call the API without strategy when strategy is not provided', (done) => { + requestSpy.and.returnValue(Promise.resolve(nextTaskResponse)); + + service.nextTask(appName).subscribe(() => { + const [url, options] = requestSpy.calls.mostRecent().args; + expect(url).toContain('/rb/v1/tasks/next'); + expect(options?.queryParams?.strategy).toBeUndefined(); + done(); + }); + }); + + it('should pass strategy query param when strategy is provided', (done) => { + requestSpy.and.returnValue(Promise.resolve(nextTaskResponse)); + + service.nextTask(appName, 'FIFO').subscribe(() => { + const [url, options] = requestSpy.calls.mostRecent().args; + expect(url).toContain('/rb/v1/tasks/next'); + expect(options?.queryParams?.strategy).toBe('FIFO'); + done(); + }); + }); + + it('should emit dataChangesDetected$ when next task is returned', (done) => { + requestSpy.and.returnValue(Promise.resolve(nextTaskResponse)); + const dataChangeSpy = jasmine.createSpy('dataChangesDetected$'); + service.dataChangesDetected$.subscribe(dataChangeSpy); + + service.nextTask(appName).subscribe(() => { + expect(dataChangeSpy).toHaveBeenCalledWith(nextTaskResponse); + done(); + }); + }); + }); + describe('wasTaskCompletedByCurrentUser', () => { it('should return true when task was completed by current user', () => { const completedTaskByCurrentUser = { diff --git a/lib/process-services-cloud/src/lib/task/services/task-cloud.service.ts b/lib/process-services-cloud/src/lib/task/services/task-cloud.service.ts index 0ed69ab973..b7ee44e57a 100644 --- a/lib/process-services-cloud/src/lib/task/services/task-cloud.service.ts +++ b/lib/process-services-cloud/src/lib/task/services/task-cloud.service.ts @@ -17,16 +17,16 @@ import { inject, Injectable } from '@angular/core'; import { CardViewArrayItem, TranslationService } from '@alfresco/adf-core'; -import { throwError, Observable, of, Subject } from 'rxjs'; +import { Observable, of, Subject, throwError } from 'rxjs'; import { map } from 'rxjs/operators'; import { - TaskDetailsCloudModel, StartTaskCloudResponseModel, TASK_ASSIGNED_STATE, TASK_CLAIM_PERMISSION, + TASK_COMPLETED_STATE, TASK_CREATED_STATE, TASK_RELEASE_PERMISSION, - TASK_COMPLETED_STATE + TaskDetailsCloudModel } from '../models/task-details-cloud.model'; import { BaseCloudService } from '../../services/base-cloud.service'; import { StartTaskCloudRequestModel } from '../models/start-task-cloud-request.model'; @@ -130,6 +130,26 @@ export class TaskCloudService extends BaseCloudService { ); } + /** + * Returns the next recommended task to process. + * + * @param appName Name of the app + * @param strategy The task identification strategy + * @returns Details of the returned task + */ + nextTask(appName: string, strategy?: string): Observable { + if (!appName || appName === '') { + return throwError(() => 'AppName not configured'); + } + + return this.post(`${this.getBasePath(appName)}/rb/v1/tasks/next`, null, strategy ? { strategy } : null).pipe( + map((res: any) => { + this.dataChangesDetected$.next(res); + return res.entry; + }) + ); + } + /** * Claims a task for an assignee. *