// src/app/modules/users/profile/personal-profile.ts import { Component, inject, OnInit, Output, EventEmitter, ChangeDetectorRef, OnDestroy } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { NgIcon } from '@ng-icons/core'; import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap'; import { Subject, takeUntil } from 'rxjs'; import { HubUsersService, UserRole, UpdateHubUserDto } from '../users/services/users.service'; import { RoleManagementService } from '@core/services/role-management.service'; import { AuthService } from '@core/services/auth.service'; @Component({ selector: 'app-my-profile', standalone: true, imports: [CommonModule, FormsModule, NgIcon, NgbAlertModule], templateUrl: './profile.html', styles: [` .avatar-lg { width: 80px; height: 80px; } .fs-24 { font-size: 24px; } `] }) export class MyProfile implements OnInit, OnDestroy { private usersService = inject(HubUsersService); private roleService = inject(RoleManagementService); private authService = inject(AuthService); private cdRef = inject(ChangeDetectorRef); private destroy$ = new Subject(); @Output() back = new EventEmitter(); @Output() openResetPasswordModal = new EventEmitter(); user: any | null = null; loading = false; saving = false; error = ''; success = ''; // Gestion des permissions (toujours true pour le profil personnel) currentUserRole: UserRole | null = null; canEditUsers = true; // Toujours vrai pour son propre profil canManageRoles = false; // Jamais vrai pour le profil personnel canDeleteUsers = false; // Jamais vrai pour le profil personnel // Édition isEditing = false; editedUser: UpdateHubUserDto = {}; // Gestion des rôles (simplifiée pour profil personnel) availableRoles: { value: UserRole; label: string; description: string }[] = []; updatingRoles = false; ngOnInit() { this.initializeUserPermissions(); this.loadAvailableRoles(); this.loadUserProfile(); } ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); } /** * Initialise les permissions de l'utilisateur courant */ private initializeUserPermissions(): void { this.authService.loadUserProfile() .pipe(takeUntil(this.destroy$)) .subscribe({ next: (profile) => { this.currentUserRole = profile?.roles?.[0] as UserRole || null; // Pour le profil personnel, on peut toujours éditer son propre profil this.canEditUsers = true; this.canManageRoles = false; // On ne peut pas gérer les rôles de son propre profil this.canDeleteUsers = false; // On ne peut pas se supprimer soi-même }, error: (error) => { console.error('Error loading user permissions:', error); } }); } /** * Charge les rôles disponibles (lecture seule pour profil personnel) */ private loadAvailableRoles(): void { this.roleService.getAvailableRolesSimple() .pipe(takeUntil(this.destroy$)) .subscribe({ next: (roles) => { this.availableRoles = roles; }, error: (error) => { console.error('Error loading available roles:', error); // Fallback this.availableRoles = [ { value: UserRole.DCB_ADMIN, label: 'DCB Admin', description: 'Administrateur système' }, { value: UserRole.DCB_SUPPORT, label: 'DCB Support', description: 'Support technique' }, { value: UserRole.DCB_PARTNER, label: 'DCB Partner', description: 'Partenaire commercial' } ]; } }); } loadUserProfile() { this.loading = true; this.error = ''; this.usersService.getUserById(this.user.id) .pipe(takeUntil(this.destroy$)) .subscribe({ next: (user) => { this.user = user; this.loading = false; this.cdRef.detectChanges(); }, error: (error) => { this.error = 'Erreur lors du chargement de votre profil'; this.loading = false; this.cdRef.detectChanges(); console.error('Error loading user profile:', error); } }); } startEditing() { // Pas de vérification de permission pour le profil personnel this.isEditing = true; this.editedUser = { firstName: this.user?.firstName, lastName: this.user?.lastName, email: this.user?.email // On ne permet pas de modifier 'enabled' sur son propre profil }; this.cdRef.detectChanges(); } cancelEditing() { this.isEditing = false; this.editedUser = {}; this.error = ''; this.success = ''; this.cdRef.detectChanges(); } saveProfile() { if (!this.user) return; this.saving = true; this.error = ''; this.success = ''; this.usersService.updateUser(this.user.id, this.editedUser) .pipe(takeUntil(this.destroy$)) .subscribe({ next: (updatedUser) => { this.user = updatedUser; this.isEditing = false; this.saving = false; this.success = 'Profil mis à jour avec succès'; this.editedUser = {}; this.cdRef.detectChanges(); }, error: (error) => { this.error = this.getErrorMessage(error); this.saving = false; this.cdRef.detectChanges(); } }); } // Gestion des rôles - désactivée pour profil personnel updateUserRole(newRole: UserRole) { // Non autorisé pour le profil personnel this.error = 'Vous ne pouvez pas modifier votre propre rôle'; this.cdRef.detectChanges(); } // Gestion du statut - désactivée pour profil personnel enableUser() { // Non autorisé pour le profil personnel this.error = 'Vous ne pouvez pas vous activer/désactiver vous-même'; this.cdRef.detectChanges(); } disableUser() { // Non autorisé pour le profil personnel this.error = 'Vous ne pouvez pas vous activer/désactiver vous-même'; this.cdRef.detectChanges(); } // Réinitialisation du mot de passe resetPassword() { if (this.user) { this.openResetPasswordModal.emit(this.user.id); } } // Gestion des erreurs - même méthode que le premier composant private getErrorMessage(error: any): string { if (error.error?.message) { return error.error.message; } if (error.status === 403) { return 'Vous n\'avez pas les permissions nécessaires pour cette action'; } if (error.status === 404) { return 'Utilisateur non trouvé'; } if (error.status === 400) { return 'Données invalides'; } return 'Une erreur est survenue. Veuillez réessayer.'; } // Utilitaires d'affichage - mêmes méthodes que le premier composant getStatusBadgeClass(): string { if (!this.user) return 'badge bg-secondary'; if (!this.user.enabled) return 'badge bg-danger'; if (!this.user.emailVerified) return 'badge bg-warning'; return 'badge bg-success'; } getStatusText(): string { if (!this.user) return 'Inconnu'; if (!this.user.enabled) return 'Désactivé'; if (!this.user.emailVerified) return 'Email non vérifié'; return 'Actif'; } formatTimestamp(timestamp: number): string { if (!timestamp) return 'Non disponible'; return new Date(timestamp).toLocaleDateString('fr-FR', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' }); } getUserInitials(): string { if (!this.user) return 'U'; return (this.user.firstName?.charAt(0) || '') + (this.user.lastName?.charAt(0) || '') || 'U'; } getUserDisplayName(): string { if (!this.user) return 'Utilisateur'; if (this.user.firstName && this.user.lastName) { return `${this.user.firstName} ${this.user.lastName}`; } return this.user.username; } getRoleBadgeClass(role: UserRole): string { return this.roleService.getRoleBadgeClass(role); } getRoleLabel(role: UserRole): string { return this.roleService.getRoleLabel(role); } getRoleIcon(role: UserRole): string { return this.roleService.getRoleIcon(role); } getRoleDescription(role: UserRole): string { const roleInfo = this.availableRoles.find(r => r.value === role); return roleInfo?.description || 'Description non disponible'; } // Vérification des permissions pour les actions - toujours false pour les actions sensibles canAssignRole(targetRole: UserRole): boolean { return false; // Jamais autorisé pour le profil personnel } // Vérifie si c'est le profil de l'utilisateur courant - toujours true isCurrentUserProfile(): boolean { return true; // Toujours vrai pour le profil personnel } }