import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/internal/Observable'; import { catchError } from 'rxjs/operators'; import { ErrorLoggerService } from './error-logger.service'; import { Voucher } from './voucher'; const url = '/api'; const serviceName = 'VoucherService'; @Injectable({ providedIn: 'root', }) export class VoucherService { constructor(private http: HttpClient, private log: ErrorLoggerService) {} static dataURLtoBlob(dataURL: string) { const re = /^data:([\w/\-.]+);\w+,(.*)$/; const m = dataURL.match(re); const mimeString = (m as RegExpMatchArray)[1]; const byteString = atob((m as RegExpMatchArray)[2]); const ab = new ArrayBuffer(byteString.length); const dw = new DataView(ab); let i; for (i = 0; i < byteString.length; i += 1) { dw.setUint8(i, byteString.charCodeAt(i)); } return new Blob([ab], { type: mimeString }); } get(id: string, type: string): Observable { const endpoint = type.replace(/ /g, '-').toLowerCase(); return this.http .get(`${url}/${endpoint}/${id}`) .pipe(catchError(this.log.handleError(serviceName, 'Get Voucher'))) as Observable; } getOfType(type: string, account?: string): Observable { const endpoint = type.replace(/ /g, '-').toLowerCase(); let options = {}; if (account !== undefined && account !== null) { // eslint-disable-next-line @typescript-eslint/dot-notation options = { params: new HttpParams().set('a', account) }; } return this.http .get(`${url}/${endpoint}`, options) .pipe( catchError(this.log.handleError(serviceName, 'Get Voucher of Type')), ) as Observable; } getIncentive(date: string): Observable { const options = { params: new HttpParams().set('d', date) }; return this.http .get(`${url}/incentive`, options) .pipe(catchError(this.log.handleError(serviceName, 'Get Incentive'))) as Observable; } post(id: string): Observable { return this.http .post(`${url}/post-voucher/${id}`, {}) .pipe(catchError(this.log.handleError(serviceName, 'Post Voucher'))) as Observable; } delete(id: string): Observable { return this.http .delete(`${url}/delete/${id}`) .pipe(catchError(this.log.handleError(serviceName, 'Delete Voucher'))) as Observable; } saveOrUpdate(voucher: Voucher): Observable { const endpoint = voucher.type.replace(/ /g, '-').toLowerCase(); if (!voucher.id) { return this.save(voucher, endpoint); } return this.update(voucher, endpoint); } save(voucher: Voucher, endpoint: string): Observable { const fd = new FormData(); voucher.files .filter((x) => !x.id) .forEach((file) => { fd.append('i', VoucherService.dataURLtoBlob(file.resized)); fd.append('t', VoucherService.dataURLtoBlob(file.thumbnail)); }); voucher.files = voucher.files.filter((x) => x.id); fd.append('data', JSON.stringify(voucher)); return this.http .post(`${url}/${endpoint}`, fd) .pipe(catchError(this.log.handleError(serviceName, 'Save Voucher'))) as Observable; } update(voucher: Voucher, endpoint: string): Observable { const fd = new FormData(); voucher.files .filter((x) => !x.id) .forEach((file) => { fd.append('i', VoucherService.dataURLtoBlob(file.resized)); fd.append('t', VoucherService.dataURLtoBlob(file.thumbnail)); }); voucher.files = voucher.files.filter((x) => x.id); fd.append('data', JSON.stringify(voucher)); return this.http .put(`${url}/${endpoint}/${voucher.id}`, fd) .pipe(catchError(this.log.handleError(serviceName, 'Update Voucher'))) as Observable; } }