All files / app/services code.service.ts

100% Statements 23/23
100% Branches 4/4
100% Functions 8/8
100% Lines 23/23

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                                    1x 9x     9x 9x             1x 1x 1x         1x                       2x   2x 1x   2x 1x   2x 1x   2x 1x     2x                   1x                 1x                 1x                 1x                 1x          
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../config/environment';
import { AuthService } from './auth.service';
import {
  Code,
  CreateCodeRequest,
  UpdateCodeRequest,
  CheckCodeRequest,
  CheckCodeResponse,
  PageCode,
  PrizeType
} from '../models';
 
@Injectable({
  providedIn: 'root'
})
export class CodeService {
  private readonly API_URL = `${environment.apiUrl}/codes`;
 
  constructor(
    private http: HttpClient,
    private authService: AuthService
  ) {}
 
  /**
   * Vérifier la validité d'un code
   */
  checkCode(codeValue: string): Observable<CheckCodeResponse> {
    const request: CheckCodeRequest = { valeur: codeValue };
    const authHeaders = this.authService.getAuthHeaders();
    const headers = new HttpHeaders({
      ...authHeaders,
      'Content-Type': 'application/json'
    });
    
    return this.http.post<CheckCodeResponse>(`${this.API_URL}/check`, request, { headers });
  }
 
  /**
   * Lister les codes (paginés et filtrés)
   */
  getCodes(filters?: {
    page?: number;
    size?: number;
    prizeType?: PrizeType;
    used?: boolean;
  }): Observable<PageCode> {
    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>(this.API_URL, {
      params,
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Obtenir un code spécifique par sa valeur
   */
  getCode(codeValue: string): Observable<Code> {
    return this.http.get<Code>(`${this.API_URL}/${codeValue}`, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Créer un nouveau code (admin)
   */
  createCode(request: CreateCodeRequest): Observable<Code> {
    return this.http.post<Code>(this.API_URL, request, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Mettre à jour un code existant (admin)
   */
  updateCode(codeId: number, request: UpdateCodeRequest): Observable<Code> {
    return this.http.put<Code>(`${this.API_URL}/${codeId}`, request, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Supprimer un code (admin)
   */
  deleteCode(codeId: number): Observable<any> {
    return this.http.delete(`${this.API_URL}/${codeId}`, {
      headers: this.authService.getAuthHeaders()
    });
  }
 
  /**
   * Vérifier l'état du service Codes
   */
  ping(): Observable<string> {
    return this.http.get<string>(`${this.API_URL}/health`, {
      responseType: 'text' as 'json'
    });
  }
}