import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; import * as constants from "bpm-core"; import {CoreService, Messages} from "bpm-core"; import {environment} from "../../../environments/environment"; @Injectable({ providedIn: 'any', }) export class StateMachineService { private readonly transitions: { [key: string]: { [key: string]: Function } }; private state: string; constructor(private http: HttpClient, private coreStateMachineService: CoreService) { this.state = constants.STATE_MACHINE_STATUS_IDLE; this.transitions = { idle: { generalApi: function (serviceName: string, ...args) { let body = { method: constants.HTTP_METHOD_GET, //GET - PUT - POST destination: constants.TARGET_SERVER_WM, serviceName: serviceName, //, formName: environment.formName, queryParameters: { } } let options = this.coreStateMachineService.getRequestOptions(); let succ = 'serviceResponce'; return this.callPost(body, succ, options); } }, fetching: { serviceResponce: function (...args: any) { let [payload] = args; let result = payload; this.state = constants.STATE_MACHINE_STATUS_IDLE; return result; }, failure: function (...args) { let [errors] = args; this.state = constants.STATE_MACHINE_STATUS_ERROR; return this.dispatch(constants.STATE_MACHINE_ACTION_HANDLE_ERROR, errors); } }, error: { handleError: function (...args) { let [errors] = args; let result = new Messages( {}, [], [], [] ); if (errors?.meta?.messages) { errors.meta.messages.forEach((message) => { result[message.type].push(message.message); if (message.payloadReference) { let payloadReferenceTokens = message.payloadReference.split('/'); payloadReferenceTokens.slice(1, payloadReferenceTokens.length - 1); let element = result.errorFields; payloadReferenceTokens.forEach((token) => { element[token] = {}; element = element[token]; }) element = true; } }); } else if (errors?.errors) { errors.errors.forEach((message) => { result['error'].push(message.message); }); } this.state = constants.STATE_MACHINE_STATUS_IDLE; return result; } }, sending: { failure: function (...args) { let [errors] = args; this.state = constants.STATE_MACHINE_STATUS_ERROR; return this.dispatch(constants.STATE_MACHINE_ACTION_HANDLE_ERROR, errors); } } } } public dispatch(actionName, ...args) { const actions = this.transitions[this.state]; const action = actions[actionName]; if (action) { return action.apply(this, args); } } public callPost(body, succ, options) { let apiUrl = environment.proxyServiceBaseUrl + (window as any).wmConfig.language + environment.proxyFullAddress + environment.proxyServiceExecute; return this.http.post(apiUrl, encodeURIComponent(JSON.stringify(body)), options).toPromise() .then( (responseObject: any) => { let payload = responseObject; this.state = constants.STATE_MACHINE_STATUS_FETCHING; return this.dispatch(succ, payload); }, ).catch( (httpError: any) => { let responseError = httpError.error; if (responseError) { this.state = constants.STATE_MACHINE_STATUS_FETCHING; return this.dispatch(constants.STATE_MACHINE_ACTION_FAILURE, responseError); } else { this.state = constants.STATE_MACHINE_STATUS_IDLE; throw httpError; } } ); } }