diff --git a/myteamwallet_frontend_modern/src/app/core/team/cashbox-export-api.spec.ts b/myteamwallet_frontend_modern/src/app/core/team/cashbox-export-api.spec.ts new file mode 100644 index 0000000..40730aa --- /dev/null +++ b/myteamwallet_frontend_modern/src/app/core/team/cashbox-export-api.spec.ts @@ -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' }); + }); +}); diff --git a/myteamwallet_frontend_modern/src/app/core/team/cashbox-export-api.ts b/myteamwallet_frontend_modern/src/app/core/team/cashbox-export-api.ts new file mode 100644 index 0000000..21cf9ff --- /dev/null +++ b/myteamwallet_frontend_modern/src/app/core/team/cashbox-export-api.ts @@ -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 { + 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 { + return this.http.get(`${this.baseUrl}/${teamId}/subscription`); + } + + updateSubscription( + teamId: number, + dto: UpdateCashboxExportSubscription, + ): Observable { + return this.http.put(`${this.baseUrl}/${teamId}/subscription`, dto); + } +}