61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
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<Client[]> {
|
|
return this.http.post<Client[]>(this.clientURL + '/search', options);
|
|
}
|
|
|
|
getClient(id: string): Observable<Client> {
|
|
return this.http.get<Client>(`${this.clientURL}/${id}`);
|
|
}
|
|
|
|
getClientWithInvoiceSetting(options?: LoadClientOps): Observable<ClientWithSetting[]> {
|
|
return this.http.post<ClientWithSetting[]>(`${this.clientURL}/searchWithSettings`, options);
|
|
}
|
|
|
|
saveClient(client: Client): Observable<Client> {
|
|
return client._id !== '0' ? this.updateClient(client) : this.createClient(client);
|
|
}
|
|
|
|
private createClient(client: Client): Observable<Client> {
|
|
return this.http.post<Client>(this.clientURL, client);
|
|
}
|
|
|
|
private updateClient(client: Client): Observable<Client> {
|
|
return this.http.put<Client>(`${this.clientURL}/${client._id}`, client);
|
|
}
|
|
|
|
deleteClient(client: Client): Observable<Client> {
|
|
return this.http.delete<Client>(`${this.clientURL}/${client._id}`);
|
|
}
|
|
|
|
searchWithSettings(byPuid: string): Observable<Client[]> {
|
|
return this.http.post<Client[]>(`${this.clientURL}/searchWithSettings`, { byPuid });
|
|
}
|
|
}
|
|
|
|
export interface LoadClientOps {
|
|
byPuid: string;
|
|
}
|
|
|
|
export interface ClientWithSetting extends Client {
|
|
invoiceSettings: CustomerInvoiceSetting;
|
|
}
|