import { Injectable } from '@angular/core'; import { AppConf } from '../app-conf/app-conf'; import { AppUtil } from '../app-util/app-util'; import { HttpClient } from '@angular/common/http'; @Injectable() export class RemoteService { constructor(public appConf: AppConf, public appUtil: AppUtil, public http: HttpClient) { } public invokeRemoteAction(actionConfig, actionParams) { let errorMessage, hasLoginParam = true; let promise = new Promise((resolve, reject) => { if (actionParams) { let allkeys = Object.keys(actionParams); if (allkeys.length > 0) { hasLoginParam = actionParams[allkeys[0]].hasOwnProperty("ADLoginRequest"); if (!hasLoginParam) { let ADLoginRequest = this.appConf.getLoginRequest(); if (ADLoginRequest == null) { errorMessage = "Error - login expired"; this.appUtil.popUp("Login Again? Require Internet", "Login Expired", "Login", "Ignore").then((ok) => { //$state.go("appLogin"); }, (cancel) => { console.error("RemoteService -> self.invokeRemoteAction -> Skipped the Authentication"); }); reject(errorMessage); } else { actionParams[allkeys[0]].ADLoginRequest = ADLoginRequest; } } } } if (!actionConfig.actionType || typeof actionConfig.actionType !== 'string') { errorMessage = "Error - Could not invoke remote action: action type invalid!"; reject(errorMessage); } let req = { method: actionConfig.method, url: this.getActionURL(actionConfig), data: this.getReqData(actionParams), dataType: '', headers: { } } if (actionConfig.dataType) { req.dataType = actionConfig.dataType; req.headers = { accepts: 'application/' + actionConfig.dataType, contentType: 'application/' + actionConfig.dataType + '; charset=UTF-8' }; } else if (actionConfig.headers) { req.headers = actionConfig.headers; } else { req.headers = { accepts: 'application/json', contentType: 'application/json; charset=UTF-8' }; } // const myheader = new HttpHeaders().set('Content-Type', 'application/json; charset=UTF-8'); // myheader.append('accepts','application/json'); return this.http.post(req.url, req.data) .subscribe(data => { this.resolver(data).then((res) => { resolve(res); }, (err) => { reject(err); }) }, (err) => reject(err)); }); return promise; } public getActionURL(actionConfig) { if (actionConfig.actionType) { return this.appConf.getFullUrl() + actionConfig.actionType; } return this.appConf.getFullUrl(); } public getReqData(actionParams) { let param = actionParams ? actionParams : {}; return param; } public resolver(response) { let promise = new Promise((resolve, reject) => { let res; if (response.WindowTabData) { res = response.WindowTabData; } else if (response.RunProcessResponse) { res = response.RunProcessResponse; } if (res && res.ADLoginResponse) { if (this.validateLoginResponse(res.ADLoginResponse)) { resolve(res); } } res.isLoginError = true; resolve(res); }); return promise; } public validateLoginResponse(response) { if (response.roles == 0) { return false; } else { return true; } } }