import { Injectable } from '@angular/core'; import * as global from '../../core/common'; import { Http, RequestOptions, URLSearchParams, Response } from '@angular/http'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import 'rxjs/add/observable/throw'; import { Observable, throwError } from 'rxjs' import { Coupons } from '../../core/products/Coupons'; import { DiscountTypes } from '../../core/products/DiscountTypes'; @Injectable({ providedIn: 'root' }) export class CouponsService { private GetCoupons = global.serverBase + '/api/Products/GetCoupons'; private GetDiscountTypes = global.serverBase + '/api/Products/GetDiscountTypes'; private SetUsageLimits = global.serverBase + '/api/Products/CreateCouponUsageLimits'; private CreateCategoriesOfProduct = global.serverBase + '/api/Products/CreateCategoriesOfCoupons'; private CreateCouponCode = global.serverBase + '/api/Products/CreateCouponCode'; private CopyCoupon = global.serverBase + '/api/Products/CopyCoupons'; private DeleteCouponById = global.serverBase + '/api/Products/DeleteCoupons'; private SetShippingDetails = global.serverBase + '/api/Products/CreateShippingDetails'; constructor(private http: Http) { } // Get all coupons GetAllCoupons(companyId: number, actionType: string = "All") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetCoupons, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Get dicount types GetAllDiscountTypes(companyId: number, actionType: string = "All") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetDiscountTypes, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Save usage limits SaveUsageLimits(usageLimits, companyId: number, actionType: string = "SET") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetUsageLimits, usageLimits, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Create categories of coupons CreateCategoriesOfCoupons(categoryOfCoupon, companyId: number, actionType: string = "SET") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.CreateCategoriesOfProduct, categoryOfCoupon, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Save coupon SaveCouponCodes(coupons, companyId: number, actionType: string) { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); let options = new RequestOptions({ params: myparams }); return this.http.post(this.CreateCouponCode, coupons, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Copy coupon CopyBulkDiscount(id: number, companyId: number, actionType: string = "COPY") { const myparams = new URLSearchParams(); myparams.append('id', id.toString()); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.CopyCoupon, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Delete coupon code DeleteCouponCode(id: number, companyId: number, actionType: string = "DELETE") { const myparams = new URLSearchParams(); myparams.append('id', id.toString()); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.delete(this.DeleteCouponById, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Save shipping details SaveShippingDetails(shipping, companyId: number, actionType: string) { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetShippingDetails, shipping, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } private handleError(error): Observable { const message = error.statusText || 'Server error'; return throwError(error); } }