//Importing the libraries import { Injectable } from '@angular/core'; import { Http, Response, RequestOptions, URLSearchParams, ResponseContentType } from '@angular/http'; import { Observable } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import 'rxjs/add/observable/throw'; //Importing core classes import * as globals from '../core/common'; import { IAutomation } from '../core/automation/IAutomation'; import { INotes } from '../core/automation/INotes'; import { ICriteria } from '../core/automation/ICriteria'; import { IStep } from '../core/automation/IStep'; import { IStaff } from '../core/automation/IStaff'; import { IRole } from '../core/automation/IRole'; import { INotification } from '../core/automation/INotification'; import { IEmail } from '../core/automation/IEmail'; import { IInitiator } from '../core/automation/IInitiator'; import { CombineModels } from '../core/automation/CombineModels'; import { IAutomationFlow } from '../core/automation/IAutomationFlow'; @Injectable({ providedIn: 'root' }) export class AutomationService { //API Calls private GetAutomationsUrl = globals.automationServerBase + '/api/Automation/GetAutomationList'; private PinAutomationUrl = globals.automationServerBase + '/api/Automation/PinAutomations'; private ApplyAllUrl = globals.automationServerBase + '/api/Automation/ApplyForAll'; private GetCriteriaUrl = globals.automationServerBase + '/api/Criteria/GetAllCriteria'; private SaveNotesDetailsUrl = globals.automationServerBase + '/api/Criteria/SaveNotesDetails'; private SaveNotificationDetailsUrl = globals.automationServerBase + '/api/Criteria/SaveNotificationDetails'; private SaveEmailNotificationDetailsUrl = globals.automationServerBase + '/api/Criteria/EmailNotificationDetails'; private GetStepsUrl = globals.automationServerBase + '/api/StepDetails/GetAllSteps'; private getStaffUrl = globals.staffServerBase + '/api/Staff/GetStaff'; private getRoleUrl = globals.staffServerBase + '/api/Role/GetRoles'; private GetAllInitiatorUrl = globals.automationServerBase + '/api/Criteria/GetAllInitiator'; private SaveAutomationUrl = globals.automationServerBase + '/api/Automation/CreateAutomation'; private GetAutomationUrl = globals.automationServerBase + '/api/Automation/GetAutomation'; private UpdateAutomationUrl = globals.automationServerBase + '/api/Automation/UpdateAutomation'; constructor(private http: Http) { } // Get company related automation details GetAutomations(companyId: number, siteType: string, automationId: number, automationType: number): Observable { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); myparams.append("siteType", siteType.toString()); myparams.append("automationId", automationId.toString()); myparams.append("automationType", automationType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetAutomationsUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Pin automation in the top of the list PinAutomations(companyId: number, automationId: number, siteType: string, isPin: boolean): Observable { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); myparams.append("automationId", automationId.toString()); myparams.append("siteType", siteType.toString()); myparams.append("isPin", isPin.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.PinAutomationUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } GetInitiators(companyId: number){ let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetAllInitiatorUrl,options) .map((response: Response) => response.json()) .catch(this.handleError); } // Make the automation as general rules ApplyForAll(companyId: number,automationId: number, isApplyAll: boolean): Observable { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); myparams.append("automationId", automationId.toString()); myparams.append("isApplyAll", isApplyAll.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.ApplyAllUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get Criteria by initiator Id GetCriteria(initiatorCode: string,companyId:number): Observable { let myparams = new URLSearchParams(); myparams.append("initiatorCode", initiatorCode.toString()); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetCriteriaUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get Steps by Company Type GetStepList(stepType: string,companyId: number): Observable { let myparams = new URLSearchParams(); myparams.append("stepType", stepType.toString()); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetStepsUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Save Notes SaveNotesDetails(companyId: number,INotes: INotes, task: boolean, bussinessDates: number, associateRecords: number) { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); myparams.append("businessDays", bussinessDates.toString()); myparams.append("associateRecords", associateRecords.toString()); myparams.append("taskCreate", task.toString()); let options = new RequestOptions({ params: myparams }); //let options = myparams.toString(); return this.http.post(this.SaveNotesDetailsUrl, INotes, options) .map((response: Response) => response.json()).catch(this.handleError); } getStaffList(id: string): Observable { let myparams = new URLSearchParams(); myparams.append("id", id); let options = new RequestOptions({ params: myparams }); return this.http.get(this.getStaffUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)); } getRoleList(organisationId:number):Observable{ let myparams = new URLSearchParams(); myparams.append("organisationId", organisationId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.getRoleUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } SaveNotificationDetails(notificationObject: INotification) { return this.http.post(this.SaveNotificationDetailsUrl, notificationObject) .map((response: Response) => response.json()).catch(this.handleError); } SaveEmailNotificationDetails(notificationObject: IEmail) { return this.http.post(this.SaveEmailNotificationDetailsUrl, notificationObject) .map((response: Response) => response.json()).catch(this.handleError); } SaveAutomationDetails(combineModel: CombineModels[], automationName,companyId:number):Observable { const options = new RequestOptions({ params: { "automationName":automationName, "companyId": companyId }, responseType: ResponseContentType.Json, withCredentials: false }); return this.http.post(this.SaveAutomationUrl, combineModel, options) .map((response: Response) => response.json()).catch(this.handleError); } // Edit automation EditAutomationDetails(combineModel: CombineModels[], automationName:string, automationId:number,companyId:number):Observable { const options = new RequestOptions({ params: { "automationName":automationName, "automationId":automationId, "companyId": companyId }, responseType: ResponseContentType.Json, withCredentials: false }); return this.http.post(this.UpdateAutomationUrl, combineModel, options) .map((response: Response) => response.json()).catch(this.handleError); } // Get automation flowchart details GetAutomation(automationId: number,companyId:number): Observable { let myparams = new URLSearchParams(); myparams.append("automationId", automationId.toString()); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetAutomationUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } //----------- Common methods------------------// //The function of handling the error private handleError() { //console.log("Error in contact service"); //console.log(error); return Observable.throw('Server error'); } }