import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { BehaviorSubject, catchError, map, Observable, of, tap } from 'rxjs'; import Transaction from '@model/transaction' import { SendRequest, SendResponse } from '@message/Send'; import { LoginResponse } from '@message/Login'; import Account from '@model/user'; @Injectable({ providedIn: 'root', }) export class APIService { private apiUrl = '/api'; private isAuthenticatedSubject = new BehaviorSubject(false); isAuthenticated$ = this.isAuthenticatedSubject.asObservable(); // data holding loggedInUser!: Account; currentUser!: Account; ownedAccounts!: Account[]; constructor(private http: HttpClient){} login(username: string, password: string): Observable{ return this.http.post(`${this.apiUrl}/auth/login`,{ 'username': username, 'password': password}).pipe( tap({ next: (resp) => { this.isAuthenticatedSubject.next(true); this.loggedInUser = resp.user; this.currentUser = this.loggedInUser; this.ownedAccounts = resp.ownedAccounts; this.ownedAccounts.push(this.loggedInUser); }, error: () => this.isAuthenticatedSubject.next(false) }) ); } logout(): Observable{ return this.http.post(`${this.apiUrl}/auth/logout`, {}).pipe( tap({ next: () => this.isAuthenticatedSubject.next(false), error: () => this.isAuthenticatedSubject.next(false), }) ); } clearAuthState() { this.isAuthenticatedSubject.next(false); } checkAuthStatus(): Observable { return this.http.get(`${this.apiUrl}/auth/status`).pipe( map(() => true), catchError(() => of(false)), tap( authenticated => { this.isAuthenticatedSubject.next(authenticated); }) ); } getTransactions(): Observable{ return this.http.get(`${this.apiUrl}/transactions`); } send(amount: number, recipientID: string, reference: string = ""): Observable{ let request: SendRequest = {senderID: this.currentUser.id, amount, recipientID, reference}; return this.http.post(`${this.apiUrl}/send`, request); } }