import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { catchError, retry } from 'rxjs/operators'; import { environment } from '../../environments/environment'; @Injectable({ providedIn: 'root' }) export class InvoicesService { private NEW_INVOICE_DEFAULTS = `${environment.apiUrl}/api/v1/invoices/newInvoiceDefaults`; constructor(private httpClient: HttpClient) { } public sendNewInvoiceDefaultsGetRequest() { return this.httpClient.get(this.NEW_INVOICE_DEFAULTS) } createNewInvoice(newInvoiceFormData) { const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; return this.httpClient.post(`${environment.apiUrl}/api/v1/invoices`,newInvoiceFormData,httpOptions) } getAllInvoicesByStates(stateId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/states/${stateId}`) } getAllInvoicesOfCustomer(customerId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/customers/${customerId}`) } getRecentInvoicesOfCustomers(customerId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/recentOrders/${customerId}`) } getAllInvDetailsOfInvoice(invoiceId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/invoiceDetails/${invoiceId}`) } deleteInvoices(invoiceIds) { return this.httpClient.delete(`${environment.apiUrl}/api/v1/invoices/${invoiceIds}`); } updateTheInvoice(invoice) { return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices`,invoice); } createTheCustomerIssue(logData) { const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; return this.httpClient.post(`${environment.apiUrl}/api/v1/customer_issue`,logData,httpOptions) } getAllInvoicePayments() { return this.httpClient.get(`${environment.apiUrl}/api/v1/customers/customer_payment`); } deleteTheInvoicePayment(paymentId) { return this.httpClient.delete(`${environment.apiUrl}/api/v1/customers/customer_payment/${paymentId}`); } private handleError(/* error: HttpErrorResponse */) : Observable{ /* if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. console.error('An error occurred:', error.error.message); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong, console.error( `Backend returned code ${error.status}, ` + `body was: ${error.error}`); } // return an observable with a user-facing error message */ console.warn("API is not callable") return throwError( 'Something bad happened; please try again later.'); }; }