//Importing the libraries import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions, URLSearchParams } 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 { IContactDetails } from '../core/IContactDetails'; import { ICompanyDetails } from '../core/ICompanyDetails'; import { ICompanyContacts } from '../core/ICompanyContacts'; import { ICompanyDashboard } from '../core/ICompanyDashboard'; import { IOptions } from '../core/IOptions'; import { IFilterKey } from '../core/IFilterKey'; @Injectable({ providedIn: 'root' }) export class CompanyService { //API Calls private GetCompaniesUrl = globals.serverBase + '/api/ContactDetails/GetCompanies'; private GetCompanyContactsDetailsByIdUrl = globals.serverBase + '/api/ContactDetails/GetCompanyContactsDetailsById'; private GetContactDetailsByIdUrl = globals.serverBase + '/api/ContactDetails/GetContactDetailsById'; private GetContactDetailsByContactIdUrl = globals.serverBase + '/api/ContactDetails/GetCompanyContactsDetailsByContactId'; private GetCompanyDetailsByIdUrl = globals.serverBase + '/api/ContactDetails/GetCompanyDetailsById'; private SaveCompanyDetailsUrl = globals.serverBase + '/api/ContactDetails/SaveCompanyDetails'; private SaveCompanyContactDetailsUrl = globals.serverBase + '/api/ContactDetails/SaveCompanyContactDetails'; private AddNewCompanyUrl = globals.serverBase + '/api/ContactDetails/SetNewCompanyDetails'; private UpdateDefaultCompanyUrl = globals.serverBase + '/api/ContactDetails/UpdateDefaultCompany'; private GetCompanyHistoryDetailsByIdUrl = globals.serverBase + '/api/ContactDetails/GetCompanyHistoryDetailsById'; private GetCompanyContactsHistoryDetailsByIdUrl = globals.serverBase + '/api/ContactDetails/GetCompanyContactsHistoryDetailsById'; private DisableCompanyContactUrl = globals.serverBase + '/api/ContactDetails/DisableCompanyContacts'; private GetCompanyListWithFilterUrl = globals.serverBase + '/api/ContactDetails/GetCompanyListWithFilter'; //The webapi controller urls private AppUrl = globals.serverBase + '/api/ContactDetails'; headers: any; constructor(private http: Http) { this.headers = new Headers(); this.headers.append('Content-Type', 'application/x-www-form-urlencoded'); } GetCompanyListWithFilter(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.GetCompanyListWithFilterUrl, Options,requestOptions) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } // Get company related contact details GetCompanies(Options: IOptions): Observable { return this.http.post(this.GetCompaniesUrl, Options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get company related contact details GetCompanyContactsDetailsById(companyId: number) { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetCompanyContactsDetailsByIdUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get All contact details by Id GetContactDetailsById(contactId: number) { let myparams = new URLSearchParams(); myparams.append("contactId", contactId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetContactDetailsByIdUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } /** Get contacts details by company contact id */ GetContactDetailsByContactId(contactId: number) { let myparams = new URLSearchParams(); myparams.append("contactId", contactId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetContactDetailsByContactIdUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get Company details by Id GetCompanyDetailsById(companyId: number) { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetCompanyDetailsByIdUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Update Company details SaveCompanyDetails(company: ICompanyDetails) { return this.http.post(this.SaveCompanyDetailsUrl, company) .map((response: Response) => response.json()) .catch(this.handleError); } /** To save company contact details */ SaveCompanyContactDetails(companyContact: ICompanyContacts, staffId: number) { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('staffId', staffId.toString()); let body = urlSearchParams.toString() // SaveCompanyContactDetails return this.http.post(this.SaveCompanyContactDetailsUrl, companyContact, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } //Adding a new company AddNewCompany(companyName: string, contacId: number, loggedUserId: number) { let myparams = new URLSearchParams(); myparams.append("companyName", companyName); myparams.append("contacId", contacId.toString()); myparams.append("loggedUserId", loggedUserId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.AddNewCompanyUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Update Default Company UpdateDefaultCompany(defaultCompanyContactId: number, defaultCompanyId: number) { let myparams = new URLSearchParams(); myparams.append("companyContactId", defaultCompanyContactId.toString()); myparams.append("companyId", defaultCompanyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.UpdateDefaultCompanyUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get Company History GetCompanyHistoryDetailsById(companyId: number) { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetCompanyHistoryDetailsByIdUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Get Contact Company History GetCompanyContactsHistoryDetailsById(companyId: number) { let myparams = new URLSearchParams(); myparams.append("companyId", companyId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetCompanyContactsHistoryDetailsByIdUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } DisableCompanyContact(companyContactIds: number[]) { return this.http.post(this.DisableCompanyContactUrl, companyContactIds) .map((response: Response) => response.json()) .catch(this.handleError); } //----------- Common methods------------------// //The function of handling the error private handleError(error: Response) { //console.log("Error in contact service"); //console.log(error); return Observable.throw('Server error'); } }