diff --git a/lib/process-services-cloud/src/lib/task/public-api.ts b/lib/process-services-cloud/src/lib/task/public-api.ts index 34f0abdf78..e880b3e7d6 100644 --- a/lib/process-services-cloud/src/lib/task/public-api.ts +++ b/lib/process-services-cloud/src/lib/task/public-api.ts @@ -23,5 +23,6 @@ export * from './task-form/public-api'; export * from './directives/public-api'; export * from './services/task-cloud.service'; +export * from './services/start-task-cloud.service'; export * from './task-cloud.module'; diff --git a/lib/process-services-cloud/src/lib/task/services/start-task-cloud.service.ts b/lib/process-services-cloud/src/lib/task/services/start-task-cloud.service.ts new file mode 100644 index 0000000000..e75dcd7aba --- /dev/null +++ b/lib/process-services-cloud/src/lib/task/services/start-task-cloud.service.ts @@ -0,0 +1,75 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from '@angular/core'; +import { AlfrescoApiService, AppConfigService, LogService } from '@alfresco/adf-core'; +import { Observable, from, throwError } from 'rxjs'; +import { map, catchError } from 'rxjs/operators'; +import { StartTaskCloudRequestModel } from '../start-task/models/start-task-cloud-request.model'; +import { TaskDetailsCloudModel, StartTaskCloudResponseModel } from '../start-task/models/task-details-cloud.model'; +import { BaseCloudService } from '../../services/base-cloud.service'; + +@Injectable() +export class StartTaskCloudService extends BaseCloudService { + + constructor( + private apiService: AlfrescoApiService, + private appConfigService: AppConfigService, + private logService: LogService + ) { super(); } + + /** + * @deprecated in 3.5.0, use TaskCloudService instead. + * Creates a new standalone task. + * @param taskDetails Details of the task to create + * @returns Details of the newly created task + */ + createNewTask(taskDetails: TaskDetailsCloudModel): Observable { + const queryUrl = this.buildCreateTaskUrl(taskDetails.appName); + const bodyParam = JSON.stringify(this.buildRequestBody(taskDetails)); + const pathParams = {}, queryParams = {}, headerParams = {}, + formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; + + return from( + this.apiService + .getInstance() + .oauth2Auth.callCustomApi( + queryUrl, 'POST', pathParams, queryParams, + headerParams, formParams, bodyParam, + contentTypes, accepts, null, null) + ).pipe( + map((response: StartTaskCloudResponseModel) => { + return new TaskDetailsCloudModel(response.entry); + }), + catchError((err) => this.handleError(err)) + ); + } + + private buildCreateTaskUrl(appName: string): any { + this.contextRoot = this.appConfigService.get('bpmHost'); + return `${this.getBasePath(appName)}/rb/v1/tasks`; + } + + private buildRequestBody(taskDetails: any) { + return new StartTaskCloudRequestModel(taskDetails); + } + + private handleError(error: any) { + this.logService.error(error); + return throwError(error || 'Server error'); + } +}