import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { ErrorLoggerService } from '../../core/error-logger.service'; import { Table } from '../../core/table'; import { Bill } from './bill'; import { VoucherType } from './voucher-type'; const url = '/api/voucher'; const urlMoveTable = '/api/move-table'; const urlMoveKot = '/api/move-kot'; const urlSplitBill = '/api/split-bill'; const serviceName = 'VoucherService'; @Injectable({ providedIn: 'root' }) export class VoucherService { constructor(private http: HttpClient, private log: ErrorLoggerService) {} get(id: string): Observable { return this.http .get(`${url}/from-id/${id}`) .pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable; } getFromTable(tableId: string, voucherId: string | null, guestId: string | null): Observable { let params = new HttpParams(); if (voucherId !== null) { params = params.set('v', voucherId); } if (guestId !== null) { params = params.set('g', guestId); } return this.http .get(`${url}/from-table/${tableId}`, { params }) .pipe( catchError( this.log.handleError( serviceName, `getFromTable tableId=${tableId} voucherId=${voucherId} guestId=${guestId}`, ), ), ) as Observable; } getFromBill(billId: string): Observable { return this.http .get(`${url}/from-bill/${billId}`) .pipe(catchError(this.log.handleError(serviceName, `getFromBill billId=${billId}`))) as Observable; } save(voucher: Bill, voucherType: VoucherType, guestBookId: string | null, updateTable: boolean): Observable { const options = { params: new HttpParams().set('p', voucherType.toString()).set('u', updateTable.toString()), }; if (guestBookId !== null) { options.params = options.params.set('g', guestBookId); } return this.http .post(`${url}/save`, voucher, options) .pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable; } update( voucher: Bill, voucherType: VoucherType, guestBookId: string | null, updateTable: boolean, ): Observable { const options = { params: new HttpParams().set('p', voucherType.toString()).set('u', updateTable.toString()), }; if (guestBookId !== null) { options.params = options.params.set('g', guestBookId); } return this.http .put(`${url}/update/${voucher.id}`, voucher, options) .pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable; } change( voucher: Bill, voucherType: VoucherType, guestBookId: string | null, updateTable: boolean, ): Observable { const options = { params: new HttpParams().set('u', updateTable.toString()) }; if (guestBookId !== null) { options.params = options.params.set('g', guestBookId); } return this.http .put(`${url}/change/${voucher.id}`, voucher, options) .pipe(catchError(this.log.handleError(serviceName, 'change'))) as Observable; } saveOrUpdate( voucher: Bill, voucherType: VoucherType, guestBookId: string | null, updateTable: boolean, ): Observable { if (!voucher.id) { return this.save(voucher, voucherType, guestBookId, updateTable); } if (voucher.voucherType === VoucherType.Kot) { return this.update(voucher, voucherType, guestBookId, updateTable); } return this.change(voucher, voucherType, guestBookId, updateTable); } receivePayment( id: string, amounts: { id: number; name: string; amount: number }[], reason: string, updateTable: boolean, ): Observable { const options = { params: new HttpParams().set('u', updateTable.toString()) }; return this.http .post(`${url}/receive-payment/${id}`, { reason, amounts }, options) .pipe(catchError(this.log.handleError(serviceName, 'receivePayment'))) as Observable; } cancelBill(id: string, reason: string, updateTable: boolean): Observable { const options = { params: new HttpParams().set('reason', reason).set('u', updateTable.toString()), }; return this.http .post(`${url}/cancel-bill/${id}`, {}, options) .pipe(catchError(this.log.handleError(serviceName, 'cancelBill'))) as Observable; } moveTable(id: string, table: Table): Observable { return this.http .post(`${urlMoveTable}/move`, { voucherId: id, tableId: table.id, }) .pipe(catchError(this.log.handleError(serviceName, 'moveTable'))) as Observable; } mergeTable(id: string, table: Table): Observable { return this.http .post(`${urlMoveTable}/merge`, { voucherId: id, tableId: table.id, newVoucherId: table.voucherId, }) .pipe(catchError(this.log.handleError(serviceName, 'mergeTable'))) as Observable; } moveKotToNewTable(id: string, kotId: string, table: Table): Observable { return this.http .post(`${urlMoveKot}/move`, { voucherId: id, kotId, tableId: table.id, }) .pipe(catchError(this.log.handleError(serviceName, 'moveKotToNewTable'))) as Observable; } mergeKotWithOldBill(id: string, kotId: string, table: Table): Observable { return this.http .post(`${urlMoveKot}/merge`, { voucherId: id, kotId, tableId: table.id, newVoucherId: table.voucherId, }) .pipe(catchError(this.log.handleError(serviceName, 'mergeKotWithOldBill'))) as Observable; } splitBill(id: string, inventoriesToMove: string[], table: Table, updateTable: boolean) { const options = { params: new HttpParams().set('u', updateTable.toString()) }; return this.http .post( `${urlSplitBill}/${id}`, { inventories: inventoriesToMove, tableId: table.id, }, options, ) .pipe(catchError(this.log.handleError(serviceName, 'splitBill'))) as Observable; } }