blob: ec019a918c54dddc44b21e5a1ea049fdbb7c2f2c [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 {Actions, createEffect, ofType} from "@ngrx/effects";
import {Action} from "@ngrx/store";
import {concat, EMPTY, merge, Observable} from "rxjs";
import {endWith, exhaustMap, filter, map, retry, startWith} from "rxjs/operators";
import {IAPIDepartmentGroups} from "../../../../core/api/settings";
import {IAPIWorkflowData, StatementsApiService} from "../../../../core/api/statements";
import {emitOnComplete} from "../../../../util/rxjs";
import {arrayJoin} from "../../../../util/store";
import {ProcessTaskEffect} from "../../../process/effects";
import {setStatementLoadingAction, submitWorkflowDataFormAction, updateStatementEntityAction} from "../../actions";
import {IWorkflowFormValue} from "../../model";
@Injectable({providedIn: "root"})
export class SubmitWorkflowFormEffect {
public readonly submit$ = createEffect(() => this.actions.pipe(
ofType(submitWorkflowDataFormAction),
filter((action) => action.statementId != null && action.taskId != null && action.data != null),
exhaustMap((action) => this.submitWorkflowForm(action.statementId, action.taskId, action.data, action.completeTask))
));
public constructor(
private readonly actions: Actions,
private readonly statementsApiService: StatementsApiService,
private readonly taskEffect: ProcessTaskEffect
) {
}
private submitWorkflowForm(
statementId: number,
taskId: string,
data: IWorkflowFormValue,
completeTask?: boolean
): Observable<Action> {
return concat(
merge(
this.postWorkflowData(statementId, taskId, data),
this.postParentIds(statementId, taskId, data)
).pipe(emitOnComplete()),
completeTask ? this.taskEffect.completeTask(statementId, taskId, {}, true) : EMPTY
).pipe(
startWith(setStatementLoadingAction({loading: {submittingWorkflowData: true}})),
endWith(setStatementLoadingAction({loading: {submittingWorkflowData: false}}))
);
}
private postWorkflowData(
statementId: number,
taskId: string,
data: IWorkflowFormValue
): Observable<Action> {
const body: IAPIWorkflowData = {
mandatoryDepartments: data.departments.selected
.reduce<IAPIDepartmentGroups>((current, value) => {
return {
...current,
[value.groupName]: arrayJoin(current[value.groupName], [value.name])
};
}, {}),
optionalDepartments: data.departments.indeterminate
.reduce<IAPIDepartmentGroups>((current, value) => {
return {
...current,
[value.groupName]: arrayJoin(current[value.groupName], [value.name])
};
}, {}),
geoPosition: ""
};
return this.statementsApiService.postWorkflowData(statementId, taskId, body).pipe(
map(() => updateStatementEntityAction({statementId, entity: {workflow: body}})),
retry(3)
);
}
private postParentIds(
statementId: number,
taskId: string,
data: IWorkflowFormValue
): Observable<Action> {
const parentIds = data.parentIds;
return this.statementsApiService.postParentIds(statementId, taskId, parentIds).pipe(
map(() => updateStatementEntityAction({statementId, entity: {parentIds}})),
retry(3)
);
}
}