|
| 1 | +/*! |
| 2 | + * @license |
| 3 | + * Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { Injectable } from '@angular/core'; |
| 19 | +import { Observable, throwError } from 'rxjs'; |
| 20 | +import { BaseCloudService } from '../../../services/base-cloud.service'; |
| 21 | +import { map } from 'rxjs/operators'; |
| 22 | +import { TaskQueryCloudRequestModel } from '../../../models/filter-cloud-model'; |
| 23 | +import { TaskCloudNodePaging } from '../../../models/task-cloud.model'; |
| 24 | +import { TaskListCloudSortingModel } from '../../../models/task-list-sorting.model'; |
| 25 | + |
| 26 | +@Injectable({ providedIn: 'root' }) |
| 27 | +export class ProcessTaskListCloudService extends BaseCloudService { |
| 28 | + /** |
| 29 | + * Finds a task using an object with optional query properties. |
| 30 | + * |
| 31 | + * @param requestNode Query object |
| 32 | + * @param queryUrl Query url |
| 33 | + * @returns Task information |
| 34 | + */ |
| 35 | + getTaskByRequest(requestNode: TaskQueryCloudRequestModel, queryUrl?: string): Observable<any> { |
| 36 | + if (requestNode.appName || requestNode.appName === '') { |
| 37 | + queryUrl = queryUrl || `${this.getBasePath(requestNode.appName)}/query/v1/process-instances/${requestNode.processInstanceId}/tasks`; |
| 38 | + const queryParams = this.buildQueryParams(requestNode); |
| 39 | + const sortingParams = this.buildSortingParam(requestNode.sorting); |
| 40 | + if (sortingParams) { |
| 41 | + queryParams['sort'] = sortingParams; |
| 42 | + } |
| 43 | + return this.get<TaskCloudNodePaging>(queryUrl, queryParams).pipe( |
| 44 | + map((response) => { |
| 45 | + const entries = response.list?.entries; |
| 46 | + if (entries) { |
| 47 | + // TODO: this is a hack of the model and should be revisited |
| 48 | + response.list.entries = entries.map((entryData: any) => entryData.entry); |
| 49 | + } |
| 50 | + return response; |
| 51 | + }) |
| 52 | + ); |
| 53 | + } else { |
| 54 | + return throwError('Appname not configured'); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + protected buildQueryParams(requestNode: TaskQueryCloudRequestModel): any { |
| 59 | + const queryParam: any = {}; |
| 60 | + for (const property in requestNode) { |
| 61 | + if ( |
| 62 | + Object.prototype.hasOwnProperty.call(requestNode, property) && |
| 63 | + !this.isExcludedField(property) && |
| 64 | + this.isPropertyValueValid(requestNode, property) |
| 65 | + ) { |
| 66 | + queryParam[property] = requestNode[property]; |
| 67 | + } |
| 68 | + } |
| 69 | + return queryParam; |
| 70 | + } |
| 71 | + |
| 72 | + protected isExcludedField(property: string): boolean { |
| 73 | + return property === 'appName' || property === 'sorting'; |
| 74 | + } |
| 75 | + |
| 76 | + protected isPropertyValueValid(requestNode: TaskQueryCloudRequestModel, property: string): boolean { |
| 77 | + return requestNode[property] !== '' && requestNode[property] !== null && requestNode[property] !== undefined; |
| 78 | + } |
| 79 | + |
| 80 | + protected buildSortingParam(models: TaskListCloudSortingModel[]): string { |
| 81 | + let finalSorting: string = ''; |
| 82 | + if (models) { |
| 83 | + for (const sort of models) { |
| 84 | + if (!finalSorting) { |
| 85 | + finalSorting = `${sort.orderBy},${sort.direction}`; |
| 86 | + } else { |
| 87 | + finalSorting = `${finalSorting}&${sort.orderBy},${sort.direction}`; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return encodeURI(finalSorting); |
| 92 | + } |
| 93 | +} |
0 commit comments