import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { Store } from '@ngrx/store'; import { Client } from '../../client/models/client.model'; import { CustomerInvoiceSetting } from '@app/invoices/models/customer-invoice-setting.model'; @Injectable() export class ClientService { private readonly clientURL = '/clients'; constructor( private store: Store<{}>, private http: HttpClient ) { } loadClients(options?: LoadClientOps): Observable { return this.http.post(this.clientURL + '/search', options); } getClient(id: string): Observable { return this.http.get(`${this.clientURL}/${id}`); } getClientWithInvoiceSetting(options?: LoadClientOps): Observable { return this.http.post(`${this.clientURL}/searchWithSettings`, options); } saveClient(client: Client): Observable { return client._id !== '0' ? this.updateClient(client) : this.createClient(client); } private createClient(client: Client): Observable { return this.http.post(this.clientURL, client); } private updateClient(client: Client): Observable { return this.http.put(`${this.clientURL}/${client._id}`, client); } deleteClient(client: Client): Observable { return this.http.delete(`${this.clientURL}/${client._id}`); } searchWithSettings(byPuid: string): Observable { return this.http.post(`${this.clientURL}/searchWithSettings`, { byPuid }); } } export interface LoadClientOps { byPuid: string; } export interface ClientWithSetting extends Client { invoiceSettings: CustomerInvoiceSetting; }