//Importing libraries import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs'; 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 { IContactPositions } from '../core/IContactPositions'; import { ContactHistory } from '../core/contactHistory'; import { ClientDetails } from '../core/IClientDetails'; import { ICompanyDetails } from '../core/ICompanyDetails'; import { ICountry } from '../core/ICountry'; import { IState } from '../core/IState'; import { ExportImportContacts } from '../core/ExportImportContacts'; @Injectable({ providedIn: 'root' }) export class ContactService { //API calls private UpdateAvatarUrl = globals.serverBase + '/api/ContactDetails/UpdateAvatar'; private GetPositionHistoriesUrl = globals.serverBase + '/api/ContactDetails/GetContactPositionHistoryByIds'; private UpdateContactPositionurl = globals.serverBase + '/api/ContactDetails/UpdateContactPositionsById'; private GetHistoryRecordsUrl = globals.serverBase + '/api/ContactDetails/GetAllContactHistoryList'; private GetAllClientsByStaffId = globals.serverBase + '/api/ContactDetails/GetClientList'; private GetAllCompanyDetailsByIDUrl = globals.serverBase + '/api/ContactDetails/GetAllCompanyDetailsByID'; private GetAllStatesUrl = globals.serverBase + '/api/ContactDetails/GetAllStates'; private InsertNewClientUrl = globals.serverBase + '/api/ContactDetails/SaveClient'; private GetAllCountriesUrl = globals.serverBase + '/api/ContactDetails/GetAllCountries'; private GetExportContactsUrl = globals.serverBase + '/api/ContactDetails/GetExportContacts'; private UpdateContactUrl = globals.serverBase + '/api/ContactDetails/UpdateContact'; headers: any; constructor(private http: Http) { } // Update Avatar UpdateAvatar(frmData: any, contactId: number) { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('contactId', contactId.toString()); let body = urlSearchParams.toString() return this.http.post(this.UpdateAvatarUrl, frmData, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } //Getting the contact position history GetPositionHistories(contactPositionsId:number){ let urlSearchParams = new URLSearchParams(); urlSearchParams.append('contactPositionsId', contactPositionsId.toString()); let body = urlSearchParams.toString() return this.http.get(this.GetPositionHistoriesUrl,{ params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } //Updating the company position of the contact UpdateContactPosition(positionId:number, position:string, loginStaffId:number){ let urlSearchParams = new URLSearchParams(); urlSearchParams.append('positionId', positionId.toString()); urlSearchParams.append('loginStaffId', loginStaffId.toString()); urlSearchParams.append('position', position); let body = urlSearchParams.toString() return this.http.get(this.UpdateContactPositionurl,{ params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } //Getting the history for contact records GetHistoryRecords(contactId:number, historyType:string){ let urlSearchParams = new URLSearchParams(); urlSearchParams.append('contactId', contactId.toString()); urlSearchParams.append('historyType', historyType); let body = urlSearchParams.toString() return this.http.get(this.GetHistoryRecordsUrl,{ params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get All Client Details by id getAllClientsByStaffId(staffId: any) { const urlSearchParams = new URLSearchParams(); urlSearchParams.append('staffId', staffId.toString()); const body = urlSearchParams.toString(); return this.http.get(this.GetAllClientsByStaffId, { params: body }) .map((response: Response) => response.json() as ClientDetails[]) .catch(this.handleError); } getAllCompanyDetailsByID(companyId: number) { const urlSearchParams = new URLSearchParams(); urlSearchParams.append('companyId', companyId.toString()); const body = urlSearchParams.toString(); return this.http.get(this.GetAllCompanyDetailsByIDUrl, { params: body }) .map((response: Response) => response.json() as ICompanyDetails) .catch(this.handleError); } getAllStates(countryID: number) { const urlSearchParams = new URLSearchParams(); urlSearchParams.append('countryID', countryID.toString()); const body = urlSearchParams.toString(); return this.http.get(this.GetAllStatesUrl, { params: body }) .map((response: Response) => response.json() as IState[]) .catch(this.handleError); } // Save contact insertNewClientDetails(companyDetails: ICompanyDetails) { return this.http.post(this.InsertNewClientUrl, companyDetails) .map((response: Response) => response.json() as boolean) .catch(this.handleError); } getAllCountries() { return this.http.get(this.GetAllCountriesUrl) .map((response: Response) => response.json() as ICountry[]) .catch(this.handleError); } // Get Export contacts GetExportContacts(contactIdList: string[], companyID:number) { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('companyID', companyID.toString()); let body = urlSearchParams.toString() return this.http.post(this.GetExportContactsUrl, contactIdList, { params: body }) .map((response: Response) => response.json() as ExportImportContacts[]) .catch(this.handleError); } // Update Contact UpdateContact(frmData: any) { return this.http.post(this.UpdateContactUrl, frmData) .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'); } errorHandler(error: Response) { // console.log(error); } }