//Importing the libraries import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions } 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 { EmailDetails } from '../core/emailDetails'; import { IEmailAddress } from '../core/IEmailAddress'; import { IEmailSignature } from '../core/IEmailSignature'; import { IEmailTemplateCategories } from '../core/IEmailTemplateCategories'; import { IEmailTemplate } from '../core/IEmailTemplate'; import { IStaffs } from '../core/IStaffs'; import { IInternelEmailType } from '../core/IInternelEmailType'; import { IAssociateRecords } from '../core/IAssociateRecords '; import { IEmailDetails } from '../core/IEmailDetails'; @Injectable({ providedIn: 'root' }) export class EmailsService { // Email API Calls private getAllAvailableEmailDetailsUrl = globals.serverBase + '/api/EmailDetails/GetAllAvailableEmailDetails'; private getAllEmailTemplateCategories = globals.serverBase + '/api/EmailDetails/GetAllEmailTemplateCategories'; private getAllEmailTemplates = globals.serverBase + '/api/EmailDetails/GetAllEmailTemplates'; private postEmailSignatureDetailsUrl = globals.serverBase + '/api/EmailDetails/SaveEmailSignatures'; private sendNewEmailUrl = globals.serverBase + '/api/EmailDetails/SendNewEmail'; private uploadAttachmentsUrl = globals.serverBase + '/api/EmailDetails/UploadAttachment'; private LoggedUserEmailUrl = globals.serverBase + '/api/EmailDetails/GetLoggedUserEmailDetails'; private GetAllSignaturesUrl = globals.serverBase + '/api/EmailDetails/GetAllSignaturesByStaffId'; private GetEmailTemplateByCategoryIdUrl = globals.serverBase + '/api/EmailDetails/GetEmailTemplatesByCategoryId'; private GetEmailTemplateDetailsByTemplateIdUrl = globals.serverBase + '/api/EmailDetails/GetEmailTemplateDetailsByTemplateId'; private SaveEmailTemplateCategoryUrl = globals.serverBase + '/api/EmailDetails/SaveEmailTemplateCategory'; private SaveEmailTemplateUrl = globals.serverBase + '/api/EmailDetails/SaveEmailTemplate'; // Contact API Calls private GetAssociatesUrl = globals.serverBase + '/api/ContactDetails/GetAssociateRecords'; private GetEmailAssociateRecordsUrl = globals.serverBase + '/api/ContactDetails/GetEmailAssociateRecords'; private ChangeOrderOfEmailsUrl = globals.serverBase + '/api/ContactDetails/ChangeOrderOfEmails'; private GetAllEmailsByContactIdUrl = globals.serverBase + '/api/ContactDetails/GetAllEmailsByContactId'; private ViewEmailThreadUrl = globals.serverBase + '/api/ContactDetails/GetViewEmailThread'; private GetAllFilterEmailsDetailsUrl = globals.serverBase + '/api/ContactDetails/GetAllFilterEmailsDetails'; constructor(private http: Http) { } //Getting the available email details getAllAvailableEmailDetails(): Observable { return this.http.get(this.getAllAvailableEmailDetailsUrl) .map((response: Response) => response.json()) .catch(this.handleError); } //Getting the all email template categories getEmailTemplateCategories(): Observable { return this.http.get(this.getAllEmailTemplateCategories) .map((response: Response) => response.json()) .catch(this.handleError); } //Getting the all email template by category id getEmailTemplateByCategoryId(categoryId: number): Observable { //debugger let urlSearchParams = new URLSearchParams(); urlSearchParams.append('categoryId', categoryId.toString()); let body = urlSearchParams.toString() let options = new RequestOptions({ params: urlSearchParams }); return this.http.get(this.GetEmailTemplateByCategoryIdUrl, { params: body }) //return this.http.get(this.AppUrl + '/GetEmailTemplatesByCategoryId', { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } //Getting the all email template by category id GetEmailTemplateDetailsByTemplateId(templateId: number): Observable { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('templateId', templateId.toString()); let body = urlSearchParams.toString() return this.http.get(this.GetEmailTemplateDetailsByTemplateIdUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } //Save email template category SaveEmailTemplateCategory(templateCategory: IEmailTemplateCategories) { return this.http.post(this.SaveEmailTemplateCategoryUrl, templateCategory) .map((response: Response) => response.json()) .catch(this.handleError); } //Save new email template SaveEmailTemplate(emailTemplate: IEmailTemplate) { return this.http.post(this.SaveEmailTemplateUrl, emailTemplate) .map((response: Response) => response.json()) .catch(this.handleError); } // Send email SendNewEmail(internelEmailObject: IInternelEmailType) { return this.http.post(this.sendNewEmailUrl, internelEmailObject) .map((response: Response) => response.json()) .catch(this.handleError); } UploadAttachment(frmData: any) { return this.http.post(this.uploadAttachmentsUrl, frmData) .map((response: Response) => response.json()) .catch(this.handleError); } // Save signature SaveEmailSignature(emailSignature: IEmailSignature) { return this.http.post(this.postEmailSignatureDetailsUrl, emailSignature) .map((response: Response) => response.json()) .catch(this.handleError); } // Get Logged User details GetLoggedUserEmailDetails(staffId: number) { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('staffId', staffId.toString()); let body = urlSearchParams.toString() return this.http.get(this.LoggedUserEmailUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get All Signatures GetAllSignaturesByStaffId(staffId: number) { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('staffId', staffId.toString()); let body = urlSearchParams.toString() return this.http.get(this.GetAllSignaturesUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // Associate Records GetAssociateRecords(contactId: number) { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('contactId', contactId.toString()); let body = urlSearchParams.toString(); return this.http.get(this.GetAssociatesUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // Email Associated Records GetEmailAssociateRecords(emailId: any) { let myparams = new URLSearchParams(); myparams.append("emailId", emailId.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetEmailAssociateRecordsUrl, options) .map((response: Response) => response.json()) .catch(this.handleError); } // Email Pin Records ChangeOrderOfEmails(emailId: any, loginStaffId: number) { let myparams = new URLSearchParams(); myparams.append("emailId", emailId.toString()); myparams.append("loginStaffId", loginStaffId.toString()); let body = myparams.toString(); // let options = new RequestOptions({ params: myparams }); return this.http.get(this.ChangeOrderOfEmailsUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get All Emails By ContactId GetAllEmailsByContactId(contactId: number) { let myparams = new URLSearchParams(); myparams.append("contactId", contactId.toString()); let body = myparams.toString(); let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetAllEmailsByContactIdUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // View Theread GetViewEmailThread(replyId: any) { let myparams = new URLSearchParams(); myparams.append("replyId", replyId.toString()); let body = myparams.toString(); let options = new RequestOptions({ params: myparams }); return this.http.get(this.ViewEmailThreadUrl, { params: body }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get All Filter Email data GetAllFilterEmailsDetails(dateFrom: string, dateTo: string, ownerId: number, flag: number, subjectTitle: string, contactId: number) { let myparams = new URLSearchParams(); if (subjectTitle == null && subjectTitle == undefined) { subjectTitle = ""; } if (ownerId == null && ownerId == undefined) { ownerId = 0; } myparams.append("dateFrom", dateFrom.toString()); myparams.append("dateTo", dateTo.toString()); myparams.append("ownerId", ownerId.toString()); myparams.append("flag", flag.toString()); myparams.append("subjectTitle", subjectTitle.toString()); myparams.append("contactId", contactId.toString()); let body = myparams.toString(); //let options = new RequestOptions({ params: myparams }); return this.http.get(this.GetAllFilterEmailsDetailsUrl, { params: body }) .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(error.json().error || 'Server error'); } }