mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-10-01 14:41:32 +00:00
[ACS-7427] Process Services improvements and cleanup (#9664)
This commit is contained in:
@@ -18,54 +18,48 @@
|
||||
import { AlfrescoApiService, DateFnsUtils, FormValues } from '@alfresco/adf-core';
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
TasksApi,
|
||||
FormDefinitionRepresentation,
|
||||
ProcessDefinitionsApi,
|
||||
ProcessInstancesApi,
|
||||
RestVariable,
|
||||
ProcessInstanceAuditInfoRepresentation,
|
||||
ProcessInstanceQueryRepresentation,
|
||||
ProcessInstanceRepresentation,
|
||||
ProcessInstanceVariablesApi
|
||||
ProcessInstancesApi,
|
||||
ProcessInstanceVariablesApi,
|
||||
RestVariable,
|
||||
ResultListDataRepresentationProcessInstanceRepresentation,
|
||||
TasksApi,
|
||||
ProcessDefinitionRepresentation
|
||||
} from '@alfresco/js-api';
|
||||
import { Observable, from, throwError, of } from 'rxjs';
|
||||
import { from, Observable } from 'rxjs';
|
||||
import { TaskDetailsModel } from '../../task-list';
|
||||
import { ProcessFilterParamRepresentationModel } from '../models/filter-process.model';
|
||||
import { ProcessDefinitionRepresentation } from '../models/process-definition.model';
|
||||
import { ProcessInstanceVariable } from '../models/process-instance-variable.model';
|
||||
import { ProcessInstance } from '../models/process-instance.model';
|
||||
import { ProcessListModel } from '../models/process-list.model';
|
||||
import { map, catchError } from 'rxjs/operators';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { DatePipe } from '@angular/common';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ProcessService {
|
||||
|
||||
private _tasksApi: TasksApi;
|
||||
get tasksApi(): TasksApi {
|
||||
this._tasksApi = this._tasksApi ?? new TasksApi(this.alfrescoApiService.getInstance());
|
||||
return this._tasksApi;
|
||||
return (this._tasksApi ||= new TasksApi(this.alfrescoApiService.getInstance()));
|
||||
}
|
||||
|
||||
private _processDefinitionsApi: ProcessDefinitionsApi;
|
||||
get processDefinitionsApi(): ProcessDefinitionsApi {
|
||||
this._processDefinitionsApi = this._processDefinitionsApi ?? new ProcessDefinitionsApi(this.alfrescoApiService.getInstance());
|
||||
return this._processDefinitionsApi;
|
||||
return (this._processDefinitionsApi ||= new ProcessDefinitionsApi(this.alfrescoApiService.getInstance()));
|
||||
}
|
||||
|
||||
private _processInstancesApi: ProcessInstancesApi;
|
||||
get processInstancesApi(): ProcessInstancesApi {
|
||||
this._processInstancesApi = this._processInstancesApi ?? new ProcessInstancesApi(this.alfrescoApiService.getInstance());
|
||||
return this._processInstancesApi;
|
||||
return (this._processInstancesApi ||= new ProcessInstancesApi(this.alfrescoApiService.getInstance()));
|
||||
}
|
||||
|
||||
private _processInstanceVariablesApi: ProcessInstanceVariablesApi;
|
||||
get processInstanceVariablesApi(): ProcessInstanceVariablesApi {
|
||||
this._processInstanceVariablesApi = this._processInstanceVariablesApi ?? new ProcessInstanceVariablesApi(this.alfrescoApiService.getInstance());
|
||||
return this._processInstanceVariablesApi;
|
||||
return (this._processInstanceVariablesApi ||= new ProcessInstanceVariablesApi(this.alfrescoApiService.getInstance()));
|
||||
}
|
||||
|
||||
constructor(private alfrescoApiService: AlfrescoApiService) {
|
||||
}
|
||||
constructor(private alfrescoApiService: AlfrescoApiService) {}
|
||||
|
||||
/**
|
||||
* Gets process instances for a filter and optionally a process definition.
|
||||
@@ -74,20 +68,18 @@ export class ProcessService {
|
||||
* @param processDefinitionKey Limits returned instances to a process definition
|
||||
* @returns List of process instances
|
||||
*/
|
||||
getProcessInstances(requestNode: ProcessFilterParamRepresentationModel, processDefinitionKey?: string): Observable<ProcessListModel> {
|
||||
return from(this.processInstancesApi.getProcessInstances(requestNode))
|
||||
.pipe(
|
||||
map((res: any) => {
|
||||
if (processDefinitionKey) {
|
||||
const filtered = res.data.filter((process) => process.processDefinitionKey === processDefinitionKey);
|
||||
res.data = filtered;
|
||||
return res;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
getProcessInstances(
|
||||
requestNode: ProcessInstanceQueryRepresentation,
|
||||
processDefinitionKey?: string
|
||||
): Observable<ResultListDataRepresentationProcessInstanceRepresentation> {
|
||||
return from(this.processInstancesApi.getProcessInstances(requestNode)).pipe(
|
||||
map((res) => {
|
||||
if (processDefinitionKey) {
|
||||
res.data = res.data.filter((process) => process.processDefinitionKey === processDefinitionKey);
|
||||
}
|
||||
return res;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,18 +89,19 @@ export class ProcessService {
|
||||
* @param processDefinitionKey Limits returned instances to a process definition
|
||||
* @returns List of processes
|
||||
*/
|
||||
getProcesses(requestNode: ProcessFilterParamRepresentationModel, processDefinitionKey?: string): Observable<ProcessListModel> {
|
||||
return this.getProcessInstances(requestNode, processDefinitionKey)
|
||||
.pipe(
|
||||
map(response => ({
|
||||
...response,
|
||||
data: (response.data || []).map(instance => {
|
||||
instance.name = this.getProcessNameOrDescription(instance, 'medium');
|
||||
return instance;
|
||||
})
|
||||
})),
|
||||
catchError(() => of(new ProcessListModel({})))
|
||||
);
|
||||
getProcesses(
|
||||
requestNode: ProcessInstanceQueryRepresentation,
|
||||
processDefinitionKey?: string
|
||||
): Observable<ResultListDataRepresentationProcessInstanceRepresentation> {
|
||||
return this.getProcessInstances(requestNode, processDefinitionKey).pipe(
|
||||
map((response) => {
|
||||
response.data = (response.data || []).map((instance) => {
|
||||
instance.name = this.getProcessNameOrDescription(instance, 'medium');
|
||||
return instance;
|
||||
});
|
||||
return response;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,10 +111,7 @@ export class ProcessService {
|
||||
* @returns Binary PDF data
|
||||
*/
|
||||
fetchProcessAuditPdfById(processId: string): Observable<Blob> {
|
||||
return from(this.processInstancesApi.getProcessAuditPdf(processId))
|
||||
.pipe(
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
return from(this.processInstancesApi.getProcessAuditPdf(processId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,11 +120,8 @@ export class ProcessService {
|
||||
* @param processId ID of the target process
|
||||
* @returns JSON data
|
||||
*/
|
||||
fetchProcessAuditJsonById(processId: string): Observable<any> {
|
||||
return from(this.processInstancesApi.getTaskAuditLog(processId))
|
||||
.pipe(
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
fetchProcessAuditJsonById(processId: string): Observable<ProcessInstanceAuditInfoRepresentation> {
|
||||
return from(this.processInstancesApi.getTaskAuditLog(processId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,12 +130,8 @@ export class ProcessService {
|
||||
* @param processInstanceId ID of the target process
|
||||
* @returns Metadata for the instance
|
||||
*/
|
||||
getProcess(processInstanceId: string): Observable<ProcessInstance> {
|
||||
return from(this.processInstancesApi.getProcessInstance(processInstanceId))
|
||||
.pipe(
|
||||
map(this.toJson),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
getProcess(processInstanceId: string): Observable<ProcessInstanceRepresentation> {
|
||||
return from(this.processInstancesApi.getProcessInstance(processInstanceId)).pipe(map(this.toJson));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,36 +141,26 @@ export class ProcessService {
|
||||
* @returns Form definition
|
||||
*/
|
||||
getStartFormDefinition(processId: string): Observable<any> {
|
||||
return from(this.processDefinitionsApi.getProcessDefinitionStartForm(processId))
|
||||
.pipe(
|
||||
map(this.toJson),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
return from(this.processDefinitionsApi.getProcessDefinitionStartForm(processId)).pipe(map(this.toJson));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the start form instance for a given process.
|
||||
*
|
||||
* @param processId Process definition ID
|
||||
* @returns Form definition
|
||||
*/
|
||||
getStartFormInstance(processId: string): Observable<any> {
|
||||
return from(this.processInstancesApi.getProcessInstanceStartForm(processId))
|
||||
.pipe(
|
||||
map(this.toJson),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
getStartFormInstance(processId: string): Observable<FormDefinitionRepresentation> {
|
||||
return from(this.processInstancesApi.getProcessInstanceStartForm(processId)).pipe(map(this.toJson));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a JSON representation of form data.
|
||||
*
|
||||
* @param res Object representing form data
|
||||
* @returns JSON data
|
||||
*/
|
||||
toJson(res: any) {
|
||||
private toJson(res: any) {
|
||||
if (res) {
|
||||
return res || {};
|
||||
}
|
||||
@@ -202,21 +175,23 @@ export class ProcessService {
|
||||
* @returns Array of task instance details
|
||||
*/
|
||||
getProcessTasks(processInstanceId: string, state?: string): Observable<TaskDetailsModel[]> {
|
||||
const taskOpts = state ? {
|
||||
processInstanceId,
|
||||
state
|
||||
} : {
|
||||
processInstanceId
|
||||
};
|
||||
return from(this.tasksApi.listTasks(taskOpts))
|
||||
.pipe(
|
||||
map(this.extractData),
|
||||
map((tasks) => tasks.map((task: any) => {
|
||||
const taskOpts = state
|
||||
? {
|
||||
processInstanceId,
|
||||
state
|
||||
}
|
||||
: {
|
||||
processInstanceId
|
||||
};
|
||||
return from(this.tasksApi.listTasks(taskOpts)).pipe(
|
||||
map(this.extractData),
|
||||
map((tasks) =>
|
||||
tasks.map((task: any) => {
|
||||
task.created = DateFnsUtils.formatDate(new Date(task.created), 'YYYY-MM-DD');
|
||||
return task;
|
||||
})),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,20 +201,15 @@ export class ProcessService {
|
||||
* @returns Array of process definitions
|
||||
*/
|
||||
getProcessDefinitions(appId?: number): Observable<ProcessDefinitionRepresentation[]> {
|
||||
const opts = appId ? {
|
||||
latest: true,
|
||||
appDefinitionId: appId
|
||||
} : {
|
||||
latest: true
|
||||
};
|
||||
return from(
|
||||
this.processDefinitionsApi.getProcessDefinitions(opts)
|
||||
)
|
||||
.pipe(
|
||||
map(this.extractData),
|
||||
map((processDefs) => processDefs.map((pd) => new ProcessDefinitionRepresentation(pd))),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
const opts = appId
|
||||
? {
|
||||
latest: true,
|
||||
appDefinitionId: appId
|
||||
}
|
||||
: {
|
||||
latest: true
|
||||
};
|
||||
return from(this.processDefinitionsApi.getProcessDefinitions(opts)).pipe(map(this.extractData));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,7 +222,13 @@ export class ProcessService {
|
||||
* @param variables Array of process instance variables
|
||||
* @returns Details of the process instance just started
|
||||
*/
|
||||
startProcess(processDefinitionId: string, name: string, outcome?: string, startFormValues?: FormValues, variables?: ProcessInstanceVariable[]): Observable<ProcessInstance> {
|
||||
startProcess(
|
||||
processDefinitionId: string,
|
||||
name: string,
|
||||
outcome?: string,
|
||||
startFormValues?: FormValues,
|
||||
variables?: RestVariable[]
|
||||
): Observable<ProcessInstanceRepresentation> {
|
||||
const startRequest: any = {
|
||||
name,
|
||||
processDefinitionId
|
||||
@@ -266,13 +242,7 @@ export class ProcessService {
|
||||
if (variables) {
|
||||
startRequest.variables = variables;
|
||||
}
|
||||
return from(
|
||||
this.processInstancesApi.startNewProcessInstance(startRequest)
|
||||
)
|
||||
.pipe(
|
||||
map((pd) => new ProcessInstance(pd)),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
return from(this.processInstancesApi.startNewProcessInstance(startRequest));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,12 +252,7 @@ export class ProcessService {
|
||||
* @returns Null response notifying when the operation is complete
|
||||
*/
|
||||
cancelProcess(processInstanceId: string): Observable<void> {
|
||||
return from(
|
||||
this.processInstancesApi.deleteProcessInstance(processInstanceId)
|
||||
)
|
||||
.pipe(
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
return from(this.processInstancesApi.deleteProcessInstance(processInstanceId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,14 +261,8 @@ export class ProcessService {
|
||||
* @param processInstanceId ID of the target process
|
||||
* @returns Array of instance variable info
|
||||
*/
|
||||
getProcessInstanceVariables(processInstanceId: string): Observable<ProcessInstanceVariable[]> {
|
||||
return from(
|
||||
this.processInstanceVariablesApi.getProcessInstanceVariables(processInstanceId)
|
||||
)
|
||||
.pipe(
|
||||
map((processVars: any[]) => processVars.map((currentProcessVar) => new ProcessInstanceVariable(currentProcessVar))),
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
getProcessInstanceVariables(processInstanceId: string): Observable<RestVariable[]> {
|
||||
return from(this.processInstanceVariablesApi.getProcessInstanceVariables(processInstanceId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,12 +272,8 @@ export class ProcessService {
|
||||
* @param variables Variables to update
|
||||
* @returns Array of instance variable info
|
||||
*/
|
||||
createOrUpdateProcessInstanceVariables(processInstanceId: string, variables: RestVariable[]): Observable<ProcessInstanceVariable[]> {
|
||||
return from(
|
||||
this.processInstanceVariablesApi.createOrUpdateProcessInstanceVariables(processInstanceId, variables)
|
||||
).pipe(
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
createOrUpdateProcessInstanceVariables(processInstanceId: string, variables: RestVariable[]): Observable<RestVariable[]> {
|
||||
return from(this.processInstanceVariablesApi.createOrUpdateProcessInstanceVariables(processInstanceId, variables));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,28 +284,18 @@ export class ProcessService {
|
||||
* @returns Null response notifying when the operation is complete
|
||||
*/
|
||||
deleteProcessInstanceVariable(processInstanceId: string, variableName: string): Observable<void> {
|
||||
return from(
|
||||
this.processInstanceVariablesApi.deleteProcessInstanceVariable(processInstanceId, variableName)
|
||||
)
|
||||
.pipe(
|
||||
catchError((err) => this.handleProcessError(err))
|
||||
);
|
||||
return from(this.processInstanceVariablesApi.deleteProcessInstanceVariable(processInstanceId, variableName));
|
||||
}
|
||||
|
||||
private extractData(res: any) {
|
||||
return res.data || {};
|
||||
}
|
||||
|
||||
private handleProcessError(error: any) {
|
||||
return throwError(error || 'Server error');
|
||||
}
|
||||
|
||||
private getProcessNameOrDescription(processInstance: ProcessInstanceRepresentation, dateFormat: string): string {
|
||||
let name = '';
|
||||
|
||||
if (processInstance) {
|
||||
name = processInstance.name ||
|
||||
processInstance.processDefinitionName + ' - ' + this.getFormatDate(processInstance.started, dateFormat);
|
||||
name = processInstance.name || processInstance.processDefinitionName + ' - ' + this.getFormatDate(processInstance.started, dateFormat);
|
||||
}
|
||||
|
||||
return name;
|
||||
|
Reference in New Issue
Block a user