//Importing the libraries import { Injectable } from '@angular/core'; import { Http, Response, URLSearchParams, RequestOptions } from '@angular/http'; import { Observable, Subject, BehaviorSubject } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import { environment } from '../../environments/environment'; //Importing the core classes import * as globals from '../core/common'; import { IContactDetails } from '../core/IContactDetails'; import { IOptions } from '../core/IOptions'; import { ICompanyContacts } from '../core/ICompanyContacts'; import { IFilterType } from '../core/IFilterType'; import { IFilterValues } from '../core/IFilterValues'; import { IFilterKey } from '../core/IFilterKey'; import { IRecentlyAdded } from '../core/IRecentlyAdded'; import { ISchedule } from '../core/ISchedule'; import { IDashboardCounts } from '../core/IDashboardCounts'; @Injectable({ providedIn: 'root' }) export class DashboardService { //API calls private GetContactListUrl = globals.serverBase + '/api/Dashboard/GetContactList'; private GetLeadListUrl = globals.serverBase + '/api/ContactDetails/GetLeads'; private SearchUrl = globals.serverBase + 'api/'; private getFilterTypesUrl = globals.serverBase + '/api/Dashboard/GetFilterTypes'; private getFilterValuesUrl = globals.serverBase + '/api/Dashboard/GetFilterValues'; private GetContactListWithFilterUrl = globals.serverBase + '/api/Dashboard/GetContactListWithFilter'; private GetLeadsListWithFilterUrl = globals.serverBase + '/api/ContactDetails/GetLeadsListWithFilter'; private GetAllDashboardReportCountsUrl = globals.serverBase + '/api/Dashboard/GetAllDashboardReportCounts'; private GetRecentlyAddedListUrl = globals.serverBase + '/api/Dashboard/GetAllRecentlyAddedList'; private GetScheduleListUrl = globals.serverBase + '/api/Dashboard/GetScheduleList'; constructor(private _http: Http) { } GetAllDashboardReportCounts(fromDate: string, toDate: string, selectionType:string, loggedUserId:number, CompanyId:number) { let myparams = new URLSearchParams(); myparams.append("fromDate", fromDate.toString()); myparams.append("toDate", toDate.toString()); myparams.append("selectionType", selectionType.toString()); myparams.append("loggedUserId", loggedUserId.toString()); myparams.append("CompanyId", CompanyId.toString()); let options = new RequestOptions({ params: myparams }); return this._http.get(this.GetAllDashboardReportCountsUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } GetScheduleList(fromDate: string, toDate: string, selectionType:string, loggedUserId:number) { let myparams = new URLSearchParams(); myparams.append("fromDate", fromDate.toString()); myparams.append("toDate", toDate.toString()); myparams.append("selectionType", selectionType.toString()); myparams.append("loggedUserId", loggedUserId.toString()); let options = new RequestOptions({ params: myparams }); return this._http.get(this.GetScheduleListUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } GetRecentlyAddedList(fromDate: string, toDate: string, selectionType:string, loggedUserId:number, CompanyId:number) { let myparams = new URLSearchParams(); myparams.append("fromDate", fromDate); myparams.append("toDate", toDate); myparams.append("selectionType", selectionType); myparams.append("loggedUserId", loggedUserId.toString()); myparams.append("CompanyId", CompanyId.toString()); let options = new RequestOptions({ params: myparams }); return this._http.get(this.GetRecentlyAddedListUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } GetFilterValues(filterType: string) { let myparams = new URLSearchParams(); myparams.append("filterType", filterType); let options = new RequestOptions({ params: myparams }); return this._http.get(this.getFilterValuesUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } getFilterTypes(section: string, companyID:number): Observable { let myparams = new URLSearchParams(); myparams.append("actionType", section); myparams.append("companyID", companyID.toString()); let options = new RequestOptions({ params: myparams }); return this._http.get(this.getFilterTypesUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } /** for to emit search data */ private searchResult = new BehaviorSubject([]); currentSearchResults = this.searchResult.asObservable(); emitSearchResults(results: any[]) { this.searchResult.next(results) } /** For change no of records in list tables */ private noOfRecords = new BehaviorSubject([]); currentNoOfRecords = this.noOfRecords.asObservable(); emitCurrentNoOfRecords(results: any[]) { this.noOfRecords.next(results) } getContactList(Options: IOptions, companyID: number): Observable { let myparams = new URLSearchParams(); //myparams.append("Options", Options); let requestOptions = new RequestOptions({ params: myparams }); let urlSearchParams = new URLSearchParams(); urlSearchParams.append('companyID', companyID.toString()); let body = urlSearchParams.toString() // ------------ uncomment and configure this to call api ----------- if (environment.useDummyData == false) { return this._http.post(this.GetContactListUrl, Options, { params: body }) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } return this._http.post('', Options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } GetContactListWithFilter(Options: IOptions, filterKeys: IFilterKey[]): Observable { let myparams = new URLSearchParams(); myparams.append("filterKeys", JSON.stringify(filterKeys)); let requestOptions = new RequestOptions({ params: myparams }); return this._http.post(this.GetContactListWithFilterUrl, Options, requestOptions) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } GetLeadsListWithFilter(Options: IOptions, filterKeys: IFilterKey[]): Observable { let myparams = new URLSearchParams(); myparams.append("filterKeys", JSON.stringify(filterKeys)); let requestOptions = new RequestOptions({ params: myparams }); return this._http.post(this.GetLeadsListWithFilterUrl, Options, requestOptions) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } getLeadList(Options: IOptions): Observable { // debugger // ------------ uncomment and configure this to call api ----------- if (environment.useDummyData == false) { return this._http.post(this.GetLeadListUrl, Options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } return this._http.post('', Options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } //The function of handling the error private handleError(error: Response) { //console.log("Error in dashboard service"); //console.log(error); return Observable.throw(error.json().error || 'Server error'); } /** search */ search(Options: IOptions, term: string, companyID: number): Observable { Options.SearchValue = term; let myparams = new URLSearchParams(); //myparams.append("Options", Options); let requestOptions = new RequestOptions({ params: myparams }); let urlSearchParams = new URLSearchParams(); urlSearchParams.append('companyID', companyID.toString()); let body = urlSearchParams.toString() // ------------ uncomment and configure this to call api ----------- if (environment.useDummyData == false) { return this._http.post(this.GetContactListUrl, Options, { params: body }) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } return this._http.post('', Options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } }