Salud_UT/saludut-inpec/src/app/components/dashboard/dashboard.ts

208 lines
5.0 KiB
TypeScript

import {
Component,
OnInit,
OnDestroy
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';
import { AuthService } from '../../services/auth';
import { AppHeaderComponent } from '../shared/app-header/app-header';
@Component({
selector: 'app-dashboard',
standalone: true,
imports: [CommonModule, AppHeaderComponent],
templateUrl: './dashboard.html',
styleUrls: ['./dashboard.css']
})
export class DashboardComponent implements OnInit, OnDestroy {
currentUser: any = null;
errorMessage: string | null = null;
private subscriptions: Subscription[] = [];
constructor(
private authService: AuthService,
private router: Router,
private route: ActivatedRoute
) {}
ngOnInit(): void {
// Suscribirse al usuario actual
this.subscriptions.push(
this.authService.currentUser$.subscribe(user => {
this.currentUser = user;
})
);
// Verificar si hay mensajes de error en los query params
this.subscriptions.push(
this.route.queryParamMap.subscribe(params => {
const error = params.get('error');
if (error) {
this.handleErrorMessage(error);
}
})
);
// Si no hay usuario autenticado, redirigir al login
if (!this.authService.isLoggedIn()) {
this.router.navigate(['/login']);
}
}
ngOnDestroy(): void {
// Limpiar suscripciones
this.subscriptions.forEach(sub => sub.unsubscribe());
}
private handleErrorMessage(error: string): void {
switch (error) {
case 'no_admin':
this.errorMessage =
'No tienes permisos de administrador para acceder a esa página.';
break;
case 'no_permission':
this.errorMessage =
'No tienes los permisos necesarios para realizar esa acción.';
break;
default:
this.errorMessage =
'Ha ocurrido un error. Por favor, intenta nuevamente.';
}
// Limpiar el mensaje después de 5 segundos
setTimeout(() => {
this.errorMessage = null;
}, 5000);
}
// =========================
// Navegación
// =========================
irABusquedaPacientes(): void {
this.router.navigate(['/autorizaciones']);
}
irAVerAutorizacionesPorFecha(): void {
this.router.navigate(['/autorizaciones-por-fecha']);
}
irAEstadisticasAutorizaciones(): void {
this.router.navigate(['/estadisticas-autorizaciones']);
}
irACargarCups(): void {
this.router.navigate(['/cargar-cups']);
}
irACargarPacientes(): void {
this.router.navigate(['/cargar-pacientes']);
}
irACargarAutorizacionesMasivas(): void {
this.router.navigate(['/cargar-autorizaciones-masivas']);
}
irACargarAutorizadosMasivos(): void {
this.router.navigate(['/cargar-autorizados-masivos']);
}
irACargarIpsReps(): void {
this.router.navigate(['/cargar-ips-reps']);
}
irAUsuarios(): void {
this.router.navigate(['/usuarios']);
}
// ahora simplemente comprobamos si es admin
puedeGestionarUsuarios(): boolean {
return this.authService.isAdministrador();
}
// =========================
// Logout y helpers de usuario
// =========================
logout(): void {
this.authService.logout();
}
getNombreUsuario(): string {
return this.currentUser?.nombre_completo || 'Usuario';
}
getRolUsuario(): string {
return this.currentUser?.nombre_rol || 'Sin rol';
}
getNombreRolFormateado(): string {
const rol = this.getRolUsuario();
switch (rol) {
case 'administrador':
return 'Administrador';
case 'administrativo_sede':
return 'Administrativo de Sede';
default:
return rol;
}
}
// =========================
// Permisos
// =========================
puedeCargarPacientes(): boolean {
return this.authService.puedeCargarPacientes();
}
puedeCargarCups(): boolean {
return this.authService.isAdministrador();
}
puedeCargarIpsReps(): boolean {
return this.authService.isAdministrador();
}
puedeDescargarPdfs(): boolean {
return this.authService.puedeDescargarPdfs();
}
puedeVerTodasAutorizaciones(): boolean {
return this.authService.puedeVerTodasAutorizaciones();
}
puedeVerEstadisticas(): boolean {
return this.authService.isAdministrador();
}
puedeGenerarAutorizaciones(): boolean {
return this.authService.puedeGenerarAutorizaciones();
}
puedeCargarAutorizacionesMasivas(): boolean {
return this.authService.puedeGenerarAutorizaciones();
}
puedeCargarAutorizadosMasivos(): boolean {
return this.authService.isAdministrador();
}
puedeVerAutorizacionesPorFecha(): boolean {
return this.authService.isLoggedIn();
}
cerrarMensajeError(): void {
this.errorMessage = null;
}
getFechaActual(): string {
const hoy = new Date();
return hoy.toLocaleDateString('es-CO', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
}