import { Observable } from 'rxjs'; import { Lead } from './lead.model'; import { AuthHttp } from 'angular2-jwt'; import * as moment from 'moment'; import { Response } from '@angular/http'; import { LeadSerializer } from './lead.serializer'; import { LeadConverter } from './lead.converter'; import { LeadCollectionConverter } from './lead.collection.converter'; import { LeadMetric } from './lead-metric.model'; import { LeadMetricDeserializer } from './lead-metric.deserializer'; import { UrlParamBuilderService } from '../service/url-param-builder.service'; import { IErrorHandler } from '../error/error-handler.interface'; import { LeadCollection } from './lead.collection'; import { ApiPathComponent } from '../api-path-component'; export class LeadService { public constructor( private authHttp: AuthHttp, private leadConverter: LeadConverter, private collectionConverter: LeadCollectionConverter, private leadMetricDeserializer: LeadMetricDeserializer, private serializer: LeadSerializer, private baseUrl: string, private urlParamBuilderService: UrlParamBuilderService, private errorHandler: IErrorHandler ) {} public getMany(filters?: {[key: string]: any}, sort?: string[], page?: number, limit?: number): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads + '?' + this.urlParamBuilderService.build(filters, sort, page, limit) ; return this.authHttp.get(url) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.collectionConverter.toCollection(res.json())); } public getOne(id: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads + '/' + id; return this.authHttp.get(url) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.leadConverter.toOne(res.json())); } public create(lead: Lead): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads; return this.authHttp.post(url, this.serializer.serialize(lead)) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.leadConverter.toOne(res.json())); } public update(lead: Lead): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads + '/' + lead.id; return this.authHttp.put(url, this.serializer.serialize(lead)) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.leadConverter.toOne(res.json())); } public delete(id: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads + '/' + id; return this.authHttp.delete(url) .catch((error?: any) => this.errorHandler.handle(error)) .map(() => undefined); } public reportInboundCall(id: string, outcome: string): Observable { const url: string = this.buildActionUrl(id, 'inbound-call-reports'); return this.authHttp.post(url, {outcome}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public reportOutboundCall(id: string, outcome: string): Observable { const url: string = this.buildActionUrl(id, 'outbound-call-reports'); return this.authHttp.post(url, {outcome}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public scheduleCall(id: string, scheduledFor: Date): Observable { const url: string = this.buildActionUrl(id, 'scheduled-calls'); return this.authHttp.post(url, {scheduled_for: moment(scheduledFor).format()}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public scheduleCheckup(id: string, scheduledFor: Date): Observable { const url: string = this.buildActionUrl(id, 'scheduled-checkups'); return this.authHttp.post(url, {scheduled_for: moment(scheduledFor).format()}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public schedulePayment(id: string, scheduledFor: Date): Observable { const url: string = this.buildActionUrl(id, 'scheduled-payments'); return this.authHttp.post(url, {scheduled_for: moment(scheduledFor).format()}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public reportCheckup(id: string, outcome: string): Observable { const url: string = this.buildActionUrl(id, 'checkup-reports'); return this.authHttp.post(url, {outcome}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public reject(id: string, reason: string): Observable { const url: string = this.buildActionUrl(id, 'rejection'); return this.authHttp.put(url, {reason}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public reportOffer(id: string, offerValue: string, offerGiver: string): Observable { const url: string = this.buildActionUrl(id, 'offer-reports'); return this.authHttp.post(url, {offer_value: offerValue, offer_giver: offerGiver}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public reportPayment(id: string): Observable { const url: string = this.buildActionUrl(id, 'payment-reports'); return this.authHttp.post(url, {}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } public assign(leadId: string, userId: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads + '/' + leadId + '/' + ApiPathComponent.assignments; return this.authHttp.post(url, { user_id: userId }) .catch((error?: any) => this.errorHandler.handle(error)) .map(() => undefined); } public unassign(leadId: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.leads + '/' + leadId + '/' + ApiPathComponent.assignments; return this.authHttp.post(url, {}) .catch((error?: any) => this.errorHandler.handle(error)) .map(() => undefined); } public getMetrics(filters?: {[key: string]: any}): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.metrics + '/' + ApiPathComponent.leads + '?' + this.urlParamBuilderService.build(filters); return this.authHttp.get(url) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.leadMetricDeserializer.deserialize(res.json())); } private buildActionUrl(id: string, action: string): string { return this.baseUrl + '/' + ApiPathComponent.leads + '/' + id + '/' + action; } }