import { Injectable , Inject } from '@angular/core' ; import { Observable } from 'rxjs' ; import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { map } from 'rxjs/operators' ; import { NavService } from '../shared/nav.service' ; @ Injectable ( ) export class CheckoutService { baseUrl : string ; constructor ( private http , private navService , @ Inject ( 'BASE_URL' ) baseUrl ) { this . baseUrl = baseUrl ; } getCartById ( ) { const productUrl = `${ this . baseUrl }/api/user/checkout` ; return this . http . get ( productUrl ) ; } sendCheckout ( value ) { const headers = new HttpHeaders ( { Accept : 'application/json' , 'Content-Type' : 'application/json' , } ) ; const checkoutUrl = `/api/user/checkout/confirm` ; return this . http . post ( checkoutUrl , value , { headers } ) . pipe ( map ( ( res ) => { if ( res . success ) { localStorage . setItem ( 'id_cart' , res . cart || 0 ) ; this . navService . changeCart ( res . cart || 0 ) ; return true ; } return false ; } ) ) ; } }