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) } getAllInvoicesByInvoiceStates(stateId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/invoiceStates/${stateId}`) } getAllInvoicesByOrderStatus(stateId){ return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/orderStatus/${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}`) } getInvoiceByInvoiceId(invoiceId){ return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/${invoiceId}`); } deleteInvoices(invoiceIds) { return this.httpClient.delete(`${environment.apiUrl}/api/v1/invoices/${invoiceIds}`); } cancelSalesOrder(invoiceIds){ return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/cancelSalesOrderAndUpdateCustomerWallet`, invoiceIds); } updateTheInvoice(invoice) { return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices`,invoice); } updateOrderStatusToDelivered(invoiceIds) { return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/orderstatus/delivered`,invoiceIds); } updateOrderStatusToInProgress(invoiceIds) { return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/orderstatus/inprogress`,invoiceIds); } updateInvoiceStateToPaid(invoiceId) { return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/invoicestate/paid`,invoiceId); } updateInvoiceStateToRefund(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/refund`,form); } updateInvoiceStateToPaidAndCustomerWallet(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/updateInvoiceStateToPaidAndCustomerWallet`,form); } assignOrder(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/invoices/assign`,form); } getCurrentOrderStatus(invoiceId){ return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/currentOrderStatus/${invoiceId}`); } getSalesDetailsByDate(fromDate, toDate){ return this.httpClient.get(`${environment.apiUrl}/api/v1/invoices/getSalesDetailsByDate/${fromDate}/${toDate}`); } 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}`); } getAllCustomerIssueByState(stateId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/customer_issue/state/${stateId}`); } updateCustomerIssue(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/customer_issue`, form); } 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.'); }; }