All files / app/services stats.service.ts

100% Statements 34/34
100% Branches 10/10
100% Functions 8/8
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                    1x 15x     15x 15x             1x                         5x 5x 5x 5x   5x                   1x                 1x                 1x                         3x 3x 3x 3x   3x                                   2x 2x 2x 2x 2x   2x            
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../config/environment';
import { AuthService } from './auth.service';
import {
  StatsResponse,
  DemographicsStats,
  WinsStats,
  CodesStats,
  AdminStatsResponse,
  PageCode,
  PrizeType
} from '../models';
 
@Injectable({
  providedIn: 'root'
})
export class StatsService {
  private readonly API_URL = `${environment.apiUrl}/stats`;
 
  constructor(
    private http: HttpClient,
    private authService: AuthService
  ) {}
 
  /**
   * Obtenir les statistiques globales (sans filtres)
   */
  getGlobalStats(): Observable<StatsResponse> {
    return this.http.get<StatsResponse>(`${this.API_URL}/global`, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir les statistiques globales filtrées
   */
  getStats(filters?: {
    lot?: PrizeType | string;
    boutique?: string;
    periode?: string;
  }): Observable<StatsResponse> {
    let params = new HttpParams();
    if (filters?.lot) params = params.set('lot', filters.lot);
    if (filters?.boutique) params = params.set('boutique', filters.boutique);
    if (filters?.periode) params = params.set('periode', filters.periode);
 
    return this.http.get<StatsResponse>(this.API_URL, {
      params,
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir les statistiques démographiques
   */
  getDemographics(): Observable<DemographicsStats> {
    return this.http.get<DemographicsStats>(`${this.API_URL}/demographics`, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir les statistiques des gains
   */
  getWinsStats(): Observable<WinsStats> {
    return this.http.get<WinsStats>(`${this.API_URL}/wins`, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir les statistiques des codes
   */
  getCodesStats(): Observable<CodesStats> {
    return this.http.get<CodesStats>(`${this.API_URL}/codes`, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir les statistiques admin
   */
  getAdminStats(filters?: {
    lot?: PrizeType | string;
    boutique?: string;
    periode?: string;
  }): Observable<AdminStatsResponse> {
    let params = new HttpParams();
    if (filters?.lot) params = params.set('lot', filters.lot);
    if (filters?.boutique) params = params.set('boutique', filters.boutique);
    if (filters?.periode) params = params.set('periode', filters.periode);
 
    return this.http.get<AdminStatsResponse>(`${environment.apiUrl}/admin/stats`, {
      params,
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir les codes (délégué au CodeService, mais gardé pour compatibilité)
   * @deprecated Utiliser CodeService.getCodes() à la place
   */
  getCodes(filters?: {
    page?: number;
    size?: number;
    prizeType?: PrizeType;
    used?: boolean;
  }): Observable<PageCode> {
    // Cette méthode devrait être dans CodeService
    // Gardée ici pour compatibilité avec le code existant
    let params = new HttpParams();
    if (filters?.page !== undefined) params = params.set('page', filters.page.toString());
    if (filters?.size !== undefined) params = params.set('size', filters.size.toString());
    if (filters?.prizeType) params = params.set('prizeType', filters.prizeType);
    if (filters?.used !== undefined) params = params.set('used', filters.used.toString());
 
    return this.http.get<PageCode>(`${environment.apiUrl}/codes`, {
      params,
      headers: this.authService.getAuthHeaders()
    });
  }
}