All files / app/services role.service.ts

100% Statements 14/14
100% Branches 5/5
100% Functions 9/9
100% Lines 13/13

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                                1x 26x 26x           2x             2x             2x             2x             2x             2x             2x             6x                 6x         6x        
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../config/environment';
 
export interface Role {
  id: number;
  name: string;
  description?: string;
  active?: boolean;
  userCount?: number;
}
 
@Injectable({
  providedIn: 'root'
})
export class RoleService {
  private http = inject(HttpClient);
  private apiUrl = `${environment.apiUrl}/admin/roles`;
 
  /**
   * Récupère tous les rôles
   */
  getAllRoles(): Observable<Role[]> {
    return this.http.get<Role[]>(this.apiUrl);
  }
 
  /**
   * Récupère un rôle par ID
   */
  getRoleById(id: number): Observable<Role> {
    return this.http.get<Role>(`${this.apiUrl}/${id}`);
  }
 
  /**
   * Récupère un rôle par nom
   */
  getRoleByName(name: string): Observable<Role> {
    return this.http.get<Role>(`${this.apiUrl}/name/${name}`);
  }
 
  /**
   * Crée un nouveau rôle
   */
  createRole(role: Partial<Role>): Observable<Role> {
    return this.http.post<Role>(this.apiUrl, role);
  }
 
  /**
   * Met à jour un rôle
   */
  updateRole(id: number, role: Partial<Role>): Observable<Role> {
    return this.http.put<Role>(`${this.apiUrl}/${id}`, role);
  }
 
  /**
   * Active/Désactive un rôle
   */
  toggleRole(id: number): Observable<Role> {
    return this.http.patch<Role>(`${this.apiUrl}/${id}/toggle`, {});
  }
 
  /**
   * Supprime un rôle
   */
  deleteRole(id: number): Observable<void> {
    return this.http.delete<void>(`${this.apiUrl}/${id}`);
  }
 
  /**
   * Vérifie si un rôle est un rôle système (ne peut pas être modifié/supprimé)
   */
  isSystemRole(roleName: string): boolean {
    return roleName === 'ROLE_USER' || 
           roleName === 'ROLE_ADMIN' || 
           roleName === 'ROLE_EMPLOYEE';
  }
 
  /**
   * Retourne le nom d'affichage d'un rôle
   */
  getRoleDisplayName(roleName: string): string {
    const roleNames: { [key: string]: string } = {
      'ROLE_USER': 'Utilisateur',
      'ROLE_ADMIN': 'Administrateur',
      'ROLE_EMPLOYEE': 'Employé boutique'
    };
    return roleNames[roleName] || roleName;
  }
}