import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { Observable, catchError, throwError } from 'rxjs'; import { ExtractionResponse, ClaimRecord, UserAccount } from './extraction'; @Injectable({ providedIn: 'root' }) export class ExtractionService { private apiUrl = 'http://localhost:8000/api/v1'; constructor(private http: HttpClient) { } getUsers(): Observable { return this.http.get(`${this.apiUrl}/users`) .pipe( catchError(this.handleError) ); } createUser(user: UserAccount): Observable { return this.http.post(`${this.apiUrl}/users`, user) .pipe( catchError(this.handleError) ); } extractData(file: File, userName: string = 'Demo User', department: string = 'R&D'): Observable { const formData = new FormData(); formData.append('file', file); formData.append('user_name', userName); formData.append('department', department); return this.http.post(`${this.apiUrl}/extract`, formData) .pipe( catchError(this.handleError) ); } submitClaim(extractionData: ExtractionResponse, userId: string): Observable { const headers = new HttpHeaders({ 'user-id': userId }); return this.http.post(`${this.apiUrl}/claims`, extractionData, { headers }) .pipe( catchError(this.handleError) ); } getClaims(): Observable { return this.http.get(`${this.apiUrl}/claims`) .pipe( catchError(this.handleError) ); } private handleError(error: HttpErrorResponse) { let errorMessage = 'An unknown error occurred!'; if (error.error instanceof ErrorEvent) { errorMessage = `Error: ${error.error.message}`; } else { if (error.status === 504 || error.status === 0) { errorMessage = 'The request timed out. Please try again later.'; } else if (error.status === 402) { errorMessage = 'AI extraction credits exhausted. Please contact support.'; } else { errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`; } } return throwError(() => new Error(errorMessage)); } }