96 lines
3.4 KiB
TypeScript
96 lines
3.4 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs/internal/Observable';
|
|
import { catchError } from 'rxjs/operators';
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
|
import { ErrorLoggerService } from '../../core/error-logger.service';
|
|
import { Bill, PrintType } from './bill';
|
|
import { Table } from "../../core/table";
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
|
};
|
|
|
|
const url = '/v1/vouchers';
|
|
const serviceName = 'VoucherService';
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
export class VoucherService {
|
|
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {
|
|
}
|
|
|
|
get(id: string): Observable<Bill> {
|
|
return <Observable<Bill>>this.http.get<Bill>(`${url}/${id}`)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, `get id=${id}`))
|
|
);
|
|
}
|
|
|
|
getFromTable(tableId: string, voucherId: string, guestId: string): Observable<Bill> {
|
|
let params = new HttpParams();
|
|
if (tableId !== null) {
|
|
params = params.set('t', tableId);
|
|
}
|
|
if (voucherId !== null) {
|
|
params = params.set('v', voucherId);
|
|
}
|
|
if (guestId !== null) {
|
|
params = params.set('g', guestId);
|
|
}
|
|
|
|
return <Observable<Bill>>this.http.get<Bill>(`${url}/new`, {params: params})
|
|
.pipe(
|
|
catchError(this.log.handleError(
|
|
serviceName,
|
|
`getFromTable tableId=${tableId} voucherId=${voucherId} guestId=${guestId}`
|
|
))
|
|
);
|
|
}
|
|
|
|
save(voucher: Bill, printType: PrintType, guest_book_id: string, updateTable: boolean): Observable<Bill> {
|
|
const options = {params: new HttpParams().set('p', printType).set('u', updateTable.toString())};
|
|
if (guest_book_id !== null) {
|
|
options.params = options.params.set('g', guest_book_id)
|
|
}
|
|
return <Observable<Bill>>this.http.post<Bill>(`${url}/new`, voucher, options)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, 'save'))
|
|
);
|
|
}
|
|
|
|
update(voucher: Bill, printType: PrintType, guest_book_id: string, updateTable: boolean): Observable<Bill> {
|
|
const options = {params: new HttpParams().set('p', printType).set('u', updateTable.toString())};
|
|
if (guest_book_id !== null) {
|
|
options.params = options.params.set('g', guest_book_id)
|
|
}
|
|
return <Observable<Bill>>this.http.put<Bill>(`${url}/${voucher.id}`, voucher, options)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, 'update'))
|
|
);
|
|
}
|
|
|
|
saveOrUpdate(voucher: Bill, printType: PrintType, guest_book_id: string, updateTable: boolean): Observable<Bill> {
|
|
if (!voucher.id) {
|
|
return this.save(voucher, printType, guest_book_id, updateTable);
|
|
} else {
|
|
return this.update(voucher, printType, guest_book_id, updateTable);
|
|
}
|
|
}
|
|
|
|
receivePayment(id: string, amounts: { id: string; name: string; amount: number }[], updateTable: boolean): Observable<Bill> {
|
|
const options = {params: new HttpParams().set('r', "").set('u', updateTable.toString())};
|
|
return <Observable<Bill>>this.http.post<Bill>(`${url}/${id}`, amounts, options)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, 'receivePayment'))
|
|
);
|
|
}
|
|
|
|
moveTable(id: string, table: Table): Observable<boolean> {
|
|
const options = {params: new HttpParams().set('m', table.id)};
|
|
return <Observable<boolean>>this.http.post<boolean>(`${url}/${id}`, {}, options)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, 'moveTable'))
|
|
);
|
|
}
|
|
}
|