import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Observable , of } from 'rxjs' ; import { environment } from '../../../../environments/environment' ; import { Contact } from '../models/contact' ; import { map , switchMap , tap } from 'rxjs/operators' ; import { FormGroup , ValidationErrors } from '@angular/forms' ; import { BasicInfoFormComponent } from '../contact-form/basic-info-form/basic-info-form.component' ; const baseUrl = environment . baseCustomerServiceUrl ; @ Injectable ( { providedIn : 'root' } ) export class ContactService { constructor ( private http ) { } convertContactToCustomer ( contactId ) { this . fetchById ( contactId ) . pipe ( switchMap ( contact => { contact . status = 'CUSTOMER' ; return this . update ( contact ) ; } ) ) . subscribe ( ( contact ) => console . log ( "Contact status updated successfully" , contact ) , ( err ) => console . error ( err ) ) ; } fetchMyContacts ( ) { let url = `${ baseUrl }/contact` ; return this . http . get < Contact [ ] > ( url ) ; } create ( contact ) { let url = `${ baseUrl }/contact` ; return this . http . post < Contact > ( url , contact ) ; } update ( contact ) { let url = `${ baseUrl }/contact/${ contact . id }` ; return this . http . put < Contact > ( url , contact ) ; } fetchById ( id ) { if ( id ) { let url = `${ baseUrl }/contact/${ id }` ; return this . http . get < Contact > ( url ) ; } else { return of ( null ) ; } } doesEmailExist ( email ) { let url = `${ baseUrl }/contact/emailcheck` ; let content = { } ; content . email = email ; let response$ = this . http . post < boolean > ( url , content ) ; return response$ ; } }