111 lines
3.9 KiB
TypeScript
111 lines
3.9 KiB
TypeScript
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<Voucher> {
|
|
const endpoint = type.replace(/ /g, '-').toLowerCase();
|
|
return this.http
|
|
.get<Voucher>(`${url}/${endpoint}/${id}`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'Get Voucher'))) as Observable<Voucher>;
|
|
}
|
|
|
|
getOfType(type: string, account?: string): Observable<Voucher> {
|
|
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<Voucher>(`${url}/${endpoint}`, options)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, 'Get Voucher of Type')),
|
|
) as Observable<Voucher>;
|
|
}
|
|
|
|
getIncentive(date: string): Observable<Voucher> {
|
|
const options = { params: new HttpParams().set('d', date) };
|
|
return this.http
|
|
.get<Voucher>(`${url}/incentive`, options)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'Get Incentive'))) as Observable<Voucher>;
|
|
}
|
|
|
|
post(id: string): Observable<Voucher> {
|
|
return this.http
|
|
.post<Voucher>(`${url}/post-voucher/${id}`, {})
|
|
.pipe(catchError(this.log.handleError(serviceName, 'Post Voucher'))) as Observable<Voucher>;
|
|
}
|
|
|
|
delete(id: string): Observable<Voucher> {
|
|
return this.http
|
|
.delete<Voucher>(`${url}/delete/${id}`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'Delete Voucher'))) as Observable<Voucher>;
|
|
}
|
|
|
|
saveOrUpdate(voucher: Voucher): Observable<Voucher> {
|
|
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<Voucher> {
|
|
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<Voucher>(`${url}/${endpoint}`, fd)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'Save Voucher'))) as Observable<Voucher>;
|
|
}
|
|
|
|
update(voucher: Voucher, endpoint: string): Observable<Voucher> {
|
|
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<Voucher>(`${url}/${endpoint}/${voucher.id}`, fd)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'Update Voucher'))) as Observable<Voucher>;
|
|
}
|
|
}
|