| /******************************************************************************** |
| * 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 {Router} from "@angular/router"; |
| import {Actions, createEffect, ofType} from "@ngrx/effects"; |
| import {Action} from "@ngrx/store"; |
| import {concat, defer, EMPTY, Observable} from "rxjs"; |
| import {catchError, endWith, exhaustMap, filter, map, startWith, switchMap} from "rxjs/operators"; |
| import {EAPIProcessTaskDefinitionKey, ProcessApiService, StatementsApiService} from "../../../../core"; |
| import {ignoreError} from "../../../../util/rxjs"; |
| import {SubmitAttachmentsEffect} from "../../../attachments/effects/submit"; |
| import {CompleteTaskEffect} from "../../../process/effects"; |
| import {setStatementLoadingAction, submitStatementInformationFormAction} from "../../actions"; |
| import {IStatementInformationFormValue} from "../../model"; |
| |
| @Injectable({providedIn: "root"}) |
| export class SubmitStatementInformationFormEffect { |
| |
| public readonly submit$ = createEffect(() => this.actions.pipe( |
| ofType(submitStatementInformationFormAction), |
| filter((action) => action.value != null), |
| filter((action) => action.new || action.statementId != null && action.taskId != null), |
| exhaustMap((action) => { |
| return action.new ? |
| this.submitNewStatement(action.value, action.responsible) : |
| this.submit(action.statementId, action.taskId, action.value, action.responsible); |
| }) |
| )); |
| |
| public constructor( |
| private readonly actions: Actions, |
| private readonly router: Router, |
| private readonly submitAttachmentsEffect: SubmitAttachmentsEffect, |
| private readonly completeTaskEffect: CompleteTaskEffect, |
| private readonly processApiService: ProcessApiService, |
| private readonly statementsApiService: StatementsApiService, |
| ) { |
| |
| } |
| |
| public submit( |
| statementId: number, |
| taskId: string, |
| value: IStatementInformationFormValue, |
| responsible?: boolean |
| ): Observable<Action> { |
| return this.updateStatement(statementId, taskId, value).pipe( |
| switchMap(() => { |
| const submitAttachments$ = this.submitAttachmentsEffect.submit(statementId, taskId, value?.attachments).pipe( |
| catchError(() => { |
| responsible = undefined; |
| return EMPTY; |
| }) |
| ); |
| |
| return concat( |
| submitAttachments$, |
| defer(() => this.finalizeSubmit(statementId, taskId, responsible)) |
| ); |
| }), |
| ignoreError(), |
| startWith(setStatementLoadingAction({loading: {submittingStatementInformation: true}})), |
| endWith(setStatementLoadingAction({loading: {submittingStatementInformation: false}})) |
| ); |
| } |
| |
| public submitNewStatement( |
| value: IStatementInformationFormValue, |
| responsible?: boolean |
| ): Observable<Action> { |
| let statementId: number; |
| let taskId: string; |
| return this.createStatement(value).pipe( |
| map((info) => statementId = info.id), |
| switchMap(() => this.completeTaskEffect.claimNextTask(statementId, EAPIProcessTaskDefinitionKey.ADD_BASIC_INFO_DATA)), |
| switchMap((task) => { |
| taskId = task?.taskId; |
| const submitAttachments$ = this.submitAttachmentsEffect.submit(statementId, taskId, value?.attachments).pipe( |
| catchError(() => { |
| responsible = undefined; |
| return EMPTY; |
| }) |
| ); |
| |
| return concat( |
| submitAttachments$, |
| defer(() => this.finalizeSubmit(statementId, taskId, responsible)) |
| ); |
| }), |
| catchError(() => { |
| if (statementId == null) { |
| return EMPTY; |
| } |
| return this.completeTaskEffect.navigateToStatement(statementId).pipe(switchMap(() => EMPTY)); |
| }), |
| ignoreError(), |
| startWith(setStatementLoadingAction({loading: {submittingStatementInformation: true}})), |
| endWith(setStatementLoadingAction({loading: {submittingStatementInformation: false}})) |
| ); |
| } |
| |
| public finalizeSubmit(statementId: number, taskId: string, responsible?: boolean): Observable<Action> { |
| return defer(() => { |
| if (taskId != null && responsible) { |
| return this.completeTaskEffect |
| .completeTask(statementId, taskId, {responsible: {type: "Boolean", value: true}}, true); |
| } else { |
| const queryParams = taskId != null && responsible === false ? {negative: true} : {}; |
| return this.completeTaskEffect.navigateToStatement(statementId, taskId, queryParams).pipe( |
| switchMap(() => EMPTY) |
| ); |
| } |
| }).pipe( |
| ignoreError() |
| ); |
| } |
| |
| public createStatement(value: IStatementInformationFormValue) { |
| return this.statementsApiService.putStatement({ |
| title: value.title, |
| dueDate: value.dueDate, |
| receiptDate: value.receiptDate, |
| typeId: value.typeId, |
| city: value.city, |
| district: value.district, |
| contactId: value.contactId |
| }); |
| } |
| |
| public updateStatement(statementId: number, taskId: string, value: IStatementInformationFormValue) { |
| return this.statementsApiService.postStatement(statementId, taskId, { |
| title: value.title, |
| dueDate: value.dueDate, |
| receiptDate: value.receiptDate, |
| typeId: value.typeId, |
| city: value.city, |
| district: value.district, |
| contactId: value.contactId |
| }); |
| } |
| |
| } |