user.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { Observable, BehaviorSubject } from 'rxjs';
  4. import { User } from '../../models/user.model';
  5. @Injectable({
  6. providedIn: 'root'
  7. })
  8. export class UserService {
  9. private baseUrl = 'http://localhost:8085/api/users';
  10. // Reactive session stream holding the active parsed user object or null if guest
  11. private currentSessionSubject = new BehaviorSubject<any>(null);
  12. public currentSession$ = this.currentSessionSubject.asObservable();
  13. constructor(private http: HttpClient) {
  14. // Automatically initialize stream status from storage on application startup boot phase
  15. this.loadInitialSession();
  16. }
  17. private loadInitialSession(): void {
  18. const activeSessionStr = localStorage.getItem('nexus_mock_session');
  19. if (activeSessionStr) {
  20. try {
  21. this.currentSessionSubject.next(JSON.parse(activeSessionStr));
  22. } catch (e) {
  23. console.error('Initial session recovery failed:', e);
  24. }
  25. }
  26. }
  27. // Notifies all reactive stream observers across the system that a new identity is confirmed
  28. updateSessionState(sessionData: any): void {
  29. localStorage.setItem('nexus_mock_session', JSON.stringify(sessionData));
  30. this.currentSessionSubject.next(sessionData);
  31. }
  32. // Clear tracking tokens and broadcast the null context downstream to trigger logout transitions
  33. clearSessionState(): void {
  34. localStorage.removeItem('nexus_mock_session');
  35. this.currentSessionSubject.next(null);
  36. }
  37. register(userData: User): Observable<User> {
  38. return this.http.post<User>(`${this.baseUrl}/register`, userData);
  39. }
  40. login(credentials: { email: any; password: any }): Observable<User> {
  41. return this.http.post<User>(`${this.baseUrl}/login`, {
  42. email: credentials.email,
  43. password: credentials.password
  44. });
  45. }
  46. getUserById(userId: number): Observable<User> {
  47. return this.http.get<User>(`${this.baseUrl}/${userId}`);
  48. }
  49. // Initiates Stripe Checkout compiler sequences by fetching the dynamic mapping redirect URL
  50. createStripeSession(userId: number, amount: number): Observable<{ url: string }> {
  51. return this.http.post<{ url: string }>('http://localhost:8085/api/payment/create-checkout-session', {
  52. userId: userId,
  53. amount: amount
  54. });
  55. }
  56. // Requests the operational simulation pipeline wrapper to instantly verify balance records
  57. simulateWebhookPayment(userId: number, amount: number): Observable<any> {
  58. return this.http.post('http://localhost:8085/api/payment/simulate-success-webhook', {
  59. userId: userId,
  60. amount: amount
  61. });
  62. }
  63. }