blob: 2784052ca71f6b520b30b4e9cc1d21a13a365085 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {Injectable} from "@angular/core";
import {Params, Router} from "@angular/router";
import {Actions, createEffect, ofType} from "@ngrx/effects";
import {Action} from "@ngrx/store";
import {concat, defer, EMPTY, Observable, of} from "rxjs";
import {exhaustMap, filter, map, switchMap} from "rxjs/operators";
import {EAPIProcessTaskDefinitionKey} from "../../../core/api";
import {IAPIProcessObject, IAPIProcessTask, ProcessApiService} from "../../../core/api/process";
import {ignoreError} from "../../../util/rxjs";
import {arrayJoin} from "../../../util/store";
import {completeTaskAction, deleteTaskAction} from "../actions";
@Injectable({providedIn: "root"})
export class CompleteTaskEffect {
public readonly completeTask$ = createEffect(() => this.actions$.pipe(
ofType(completeTaskAction),
filter((action) => typeof action.statementId === "number" && typeof action.taskId === "string"),
exhaustMap((action) => this.completeTask(action.statementId, action.taskId, action.variables, action.claimNext, action.endWith))
));
public constructor(
private readonly actions$: Actions,
private readonly processApiService: ProcessApiService,
private readonly router: Router
) {
}
public completeTask(
statementId: number,
taskId: string,
variables: IAPIProcessObject,
claimNext?: boolean | EAPIProcessTaskDefinitionKey,
endWithActions?: Action[]
): Observable<Action> {
return concat(
this.processApiService.completeStatementTask(statementId, taskId, variables).pipe(
switchMap(() => {
endWithActions = arrayJoin([deleteTaskAction({statementId, taskId})], endWithActions);
return claimNext == null ? of<IAPIProcessTask>(null) : this.claimNextTask(statementId);
}),
switchMap((task) => {
return this.navigateToStatement(statementId, task?.taskId);
}),
switchMap(() => EMPTY),
ignoreError()
),
defer(() => of(...endWithActions))
);
}
public claimNextTask(statementId: number, key?: EAPIProcessTaskDefinitionKey): Observable<IAPIProcessTask> {
return this.processApiService.getStatementTasks(statementId).pipe(
map((taskList) => {
return taskList
.filter((task) => task?.taskId != null)
.filter((task) => key == null || task.taskDefinitionKey === key)[0];
}),
switchMap((task) => {
return task == null ? of(task) : this.processApiService.claimStatementTask(statementId, task.taskId);
})
);
}
public navigateToStatement(statementId: number, taskId?: string, queryParams: Params = {}): Observable<boolean> {
return defer(() => {
const route = statementId == null ? "/" : taskId == null ? "/details" : "/edit";
if (statementId != null) {
queryParams.id = statementId;
if (taskId != null) {
queryParams.taskId = taskId;
}
}
return this.router.navigate([route], {queryParams});
});
}
}