feat: add CashboxExportApi
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { CashboxExportApi } from './cashbox-export-api';
|
||||
|
||||
describe('CashboxExportApi', () => {
|
||||
let api: CashboxExportApi;
|
||||
let httpMock: HttpTestingController;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [provideHttpClient(), provideHttpClientTesting()],
|
||||
});
|
||||
api = TestBed.inject(CashboxExportApi);
|
||||
httpMock = TestBed.inject(HttpTestingController);
|
||||
});
|
||||
|
||||
afterEach(() => httpMock.verify());
|
||||
|
||||
it('downloads the cashbox export as a blob with query params', () => {
|
||||
api.exportCashbox(5, '2026-08-01', '2026-08-31', 'csv').subscribe();
|
||||
const request = httpMock.expectOne(
|
||||
`${environment.apiUrl}cashbox-export/5?from=2026-08-01&to=2026-08-31&format=csv`,
|
||||
);
|
||||
expect(request.request.method).toBe('GET');
|
||||
expect(request.request.responseType).toBe('blob');
|
||||
request.flush(new Blob(['csv content']));
|
||||
});
|
||||
|
||||
it('loads the subscription', () => {
|
||||
api.getSubscription(5).subscribe();
|
||||
const request = httpMock.expectOne(`${environment.apiUrl}cashbox-export/5/subscription`);
|
||||
expect(request.request.method).toBe('GET');
|
||||
request.flush({ recipients: [], interval: 'monthly', active: false, nextRunDate: null });
|
||||
});
|
||||
|
||||
it('updates the subscription', () => {
|
||||
const update = { recipients: ['a@example.com'], interval: 'monthly' as const, active: true };
|
||||
api.updateSubscription(5, update).subscribe();
|
||||
const request = httpMock.expectOne(`${environment.apiUrl}cashbox-export/5/subscription`);
|
||||
expect(request.request.method).toBe('PUT');
|
||||
expect(request.request.body).toEqual(update);
|
||||
request.flush({ ...update, nextRunDate: '2026-09-01T00:00:00.000Z' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import {
|
||||
CashboxExportFormat,
|
||||
CashboxExportSubscription,
|
||||
UpdateCashboxExportSubscription,
|
||||
} from '../../models/cashbox-export.model';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class CashboxExportApi {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly baseUrl = `${environment.apiUrl}cashbox-export`;
|
||||
|
||||
exportCashbox(
|
||||
teamId: number,
|
||||
from: string,
|
||||
to: string,
|
||||
format: CashboxExportFormat,
|
||||
): Observable<Blob> {
|
||||
const params = new HttpParams().set('from', from).set('to', to).set('format', format);
|
||||
return this.http.get(`${this.baseUrl}/${teamId}`, { params, responseType: 'blob' });
|
||||
}
|
||||
|
||||
getSubscription(teamId: number): Observable<CashboxExportSubscription> {
|
||||
return this.http.get<CashboxExportSubscription>(`${this.baseUrl}/${teamId}/subscription`);
|
||||
}
|
||||
|
||||
updateSubscription(
|
||||
teamId: number,
|
||||
dto: UpdateCashboxExportSubscription,
|
||||
): Observable<CashboxExportSubscription> {
|
||||
return this.http.put<CashboxExportSubscription>(`${this.baseUrl}/${teamId}/subscription`, dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user