|
| 1 | +import type { HttpErrorResponse } from '@angular/common/http' |
| 2 | +import { HttpClient, HttpParams } from '@angular/common/http' |
| 3 | +import { inject, Injectable } from '@angular/core' |
| 4 | +import type { Observable } from 'rxjs' |
| 5 | +import { catchError, map } from 'rxjs' |
| 6 | +import { ErrorService } from '@seed/services' |
| 7 | +import type { AggregatedReportDataResponse, ReportDataResponse } from './inventory-report.types' |
| 8 | + |
| 9 | +@Injectable({ providedIn: 'root' }) |
| 10 | +export class InventoryReportService { |
| 11 | + private _errorService = inject(ErrorService) |
| 12 | + private _httpClient = inject(HttpClient) |
| 13 | + |
| 14 | + getReportData( |
| 15 | + orgId: number, |
| 16 | + xVar: string, |
| 17 | + yVar: string, |
| 18 | + cycleIds: number[], |
| 19 | + accessLevelInstanceId: number | null, |
| 20 | + filterGroupId: number | null, |
| 21 | + ): Observable<ReportDataResponse['data']> { |
| 22 | + const url = `/api/v3/organizations/${orgId}/report/` |
| 23 | + let params = new HttpParams().set('x_var', xVar).set('y_var', yVar) |
| 24 | + for (const id of cycleIds) { |
| 25 | + params = params.append('cycle_ids', id) |
| 26 | + } |
| 27 | + if (accessLevelInstanceId != null) params = params.set('access_level_instance_id', accessLevelInstanceId) |
| 28 | + if (filterGroupId != null) params = params.set('filter_group_id', filterGroupId) |
| 29 | + |
| 30 | + return this._httpClient.get<ReportDataResponse>(url, { params }).pipe( |
| 31 | + map(({ data }) => data), |
| 32 | + catchError((error: HttpErrorResponse) => { |
| 33 | + return this._errorService.handleError(error, 'Error fetching report data') |
| 34 | + }), |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + getAggregatedReportData( |
| 39 | + orgId: number, |
| 40 | + xVar: string, |
| 41 | + yVar: string, |
| 42 | + cycleIds: number[], |
| 43 | + accessLevelInstanceId: number | null, |
| 44 | + filterGroupId: number | null, |
| 45 | + aggregationType: string, |
| 46 | + ): Observable<AggregatedReportDataResponse['aggregated_data']> { |
| 47 | + const url = `/api/v3/organizations/${orgId}/report_aggregated/` |
| 48 | + let params = new HttpParams().set('x_var', xVar).set('y_var', yVar).set('aggregationType', aggregationType) |
| 49 | + for (const id of cycleIds) { |
| 50 | + params = params.append('cycle_ids', id) |
| 51 | + } |
| 52 | + if (accessLevelInstanceId != null) params = params.set('access_level_instance_id', accessLevelInstanceId) |
| 53 | + if (filterGroupId != null) params = params.set('filter_group_id', filterGroupId) |
| 54 | + |
| 55 | + return this._httpClient.get<AggregatedReportDataResponse>(url, { params }).pipe( |
| 56 | + map(({ aggregated_data }) => aggregated_data), |
| 57 | + catchError((error: HttpErrorResponse) => { |
| 58 | + return this._errorService.handleError(error, 'Error fetching aggregated report data') |
| 59 | + }), |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + exportReportData( |
| 64 | + orgId: number, |
| 65 | + xVar: string, |
| 66 | + xLabel: string, |
| 67 | + yVar: string, |
| 68 | + yLabel: string, |
| 69 | + cycleIds: number[], |
| 70 | + filterGroupId: number | null, |
| 71 | + ): Observable<Blob> { |
| 72 | + const url = `/api/v3/organizations/${orgId}/report_export/` |
| 73 | + let params = new HttpParams().set('x_var', xVar).set('x_label', xLabel).set('y_var', yVar).set('y_label', yLabel) |
| 74 | + for (const id of cycleIds) { |
| 75 | + params = params.append('cycle_ids', id) |
| 76 | + } |
| 77 | + if (filterGroupId != null) params = params.set('filter_group_id', filterGroupId) |
| 78 | + |
| 79 | + return this._httpClient.get(url, { params, responseType: 'arraybuffer' }).pipe( |
| 80 | + map((buffer) => new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })), |
| 81 | + catchError((error: HttpErrorResponse) => { |
| 82 | + return this._errorService.handleError(error, 'Error exporting report data') |
| 83 | + }), |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
0 commit comments