This commit is contained in:
Lizandro Guarnizo
2026-05-15 21:01:30 -05:00
parent 5555514929
commit 5ec5595196
13 changed files with 788 additions and 9 deletions
+169
View File
@@ -0,0 +1,169 @@
<div x-data="notifConfig()" x-init="init()" class="p-6">
<div class="mb-6">
<h1 class="text-xl font-bold text-slate-800">Configuración de notificaciones</h1>
<p class="text-sm text-slate-500 mt-0.5">
Define por qué canales se envían las alertas a cada destinatario. Más eventos se agregarán conforme el sistema crezca.
</p>
</div>
<!-- Tabla de configuración -->
<div class="bg-white border border-slate-200 rounded-xl shadow-sm overflow-hidden">
<table class="w-full text-sm">
<thead>
<tr class="bg-slate-50 border-b border-slate-200">
<th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider w-48">Evento</th>
<th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider w-40">Destinatario</th>
<th class="px-4 py-3 text-center text-xs font-semibold text-slate-500 uppercase tracking-wider">
📧 Email
</th>
<th class="px-4 py-3 text-center text-xs font-semibold text-slate-500 uppercase tracking-wider">
✈️ Telegram
</th>
<th class="px-4 py-3 text-center text-xs font-semibold text-slate-500 uppercase tracking-wider">
🔔 Sistema
</th>
<th class="px-4 py-3 text-center text-xs font-semibold text-slate-500 uppercase tracking-wider w-24">Guardar</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100">
<template x-for="row in rows" :key="row.key">
<tr class="hover:bg-slate-50 transition-colors">
<td class="px-4 py-3">
<span class="font-medium text-slate-700" x-text="row.eventoLabel"></span>
</td>
<td class="px-4 py-3">
<span class="inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-xs font-medium"
:class="row.destinatario==='admin' ? 'bg-slate-100 text-slate-700' : 'bg-emerald-50 text-emerald-700'">
<span x-text="row.destinatario==='admin' ? '👤 Admin' : '🧑‍💼 Cliente portal'"></span>
</span>
</td>
<!-- Email -->
<td class="px-4 py-3 text-center">
<label class="inline-flex items-center justify-center cursor-pointer">
<input type="checkbox" x-model="row.canal_email"
class="w-4 h-4 rounded accent-[#8eb02f]">
</label>
</td>
<!-- Telegram -->
<td class="px-4 py-3 text-center">
<div class="flex flex-col items-center gap-1">
<label class="inline-flex items-center justify-center cursor-pointer">
<input type="checkbox" x-model="row.canal_telegram"
:disabled="row.destinatario==='portal_user' ? false : !hasTelegramConfig"
class="w-4 h-4 rounded accent-[#8eb02f] disabled:opacity-40">
</label>
<span x-show="row.destinatario==='admin' && !hasTelegramConfig"
class="text-xs text-amber-500">Sin bot config.</span>
<span x-show="row.destinatario==='portal_user'"
class="text-xs text-slate-400">Requiere Chat ID en usuario</span>
</div>
</td>
<!-- Sistema -->
<td class="px-4 py-3 text-center">
<label class="inline-flex items-center justify-center cursor-pointer">
<input type="checkbox" x-model="row.canal_sistema"
class="w-4 h-4 rounded accent-[#8eb02f]">
</label>
</td>
<!-- Guardar -->
<td class="px-4 py-3 text-center">
<button @click="save(row)"
class="px-3 py-1.5 rounded-lg text-white text-xs font-medium transition-colors"
style="background:#8eb02f"
onmouseover="this.style.background='#6d8c24'" onmouseout="this.style.background='#8eb02f'">
Guardar
</button>
</td>
</tr>
</template>
</tbody>
</table>
</div>
<!-- Ayuda: Telegram para portal users -->
<div class="mt-6 bg-blue-50 border border-blue-200 rounded-xl p-4 text-sm text-blue-800">
<p class="font-semibold mb-1">📱 Telegram para clientes del portal</p>
<p class="text-blue-700 text-xs leading-relaxed">
Para notificar a un cliente por Telegram, el cliente debe abrir una conversación con el bot y compartirte su <strong>Chat ID</strong>.
Luego ve a <a href="/app/portal-usuarios" class="underline font-medium">Portal Usuarios</a> y agrega el Chat ID en su perfil.
El bot a usar es el mismo configurado en <a href="/app/telegram" class="underline font-medium">Configuración Telegram</a>.
</p>
</div>
<!-- Toast -->
<div x-show="toast.visible" x-transition
class="fixed bottom-6 right-6 z-50 px-4 py-3 rounded-xl shadow-lg text-white text-sm font-medium"
:class="toast.ok ? 'bg-green-600' : 'bg-red-500'"
x-text="toast.msg">
</div>
</div>
<script>
// Definición de todos los eventos configurables (extensible)
const EVENTOS = [
{ evento: 'ticket_nuevo', destinatario: 'admin', label: 'Ticket nuevo', icon: '🎫' },
{ evento: 'ticket_respuesta_cliente', destinatario: 'admin', label: 'Respuesta del cliente', icon: '💬' },
{ evento: 'ticket_respuesta_admin', destinatario: 'portal_user', label: 'Respuesta del admin', icon: '💬' },
// Próximos eventos (deshabilitados por ahora):
// { evento: 'factura_emitida', destinatario: 'portal_user', label: 'Factura emitida', icon: '🧾' },
// { evento: 'avance_publicado', destinatario: 'portal_user', label: 'Avance publicado', icon: '📦' },
];
function notifConfig() {
return {
rows: [],
hasTelegramConfig: false,
toast: { visible: false, ok: true, msg: '' },
async init() {
// Verificar si hay telegram configurado
try {
const t = await axios.get('/app/loadtelegram');
this.hasTelegramConfig = (t.data?.items?.length ?? 0) > 0;
} catch {}
// Cargar configuración existente
const r = await axios.get('/app/notif-config/data');
const existing = {};
(r.data || []).forEach(cfg => { existing[`${cfg.evento}__${cfg.destinatario}`] = cfg; });
// Construir filas combinando EVENTOS con config guardada
this.rows = EVENTOS.map(ev => {
const key = `${ev.evento}__${ev.destinatario}`;
const saved = existing[key] || {};
return {
key,
evento: ev.evento,
destinatario: ev.destinatario,
eventoLabel: `${ev.icon} ${ev.label}`,
canal_email: saved.canal_email ?? (ev.destinatario === 'admin'),
canal_telegram: saved.canal_telegram ?? false,
canal_sistema: saved.canal_sistema ?? true,
};
});
},
async save(row) {
try {
await axios.post('/app/notif-config', {
evento: row.evento,
destinatario: row.destinatario,
canal_email: row.canal_email,
canal_telegram: row.canal_telegram,
canal_sistema: row.canal_sistema,
});
this.showToast('Configuración guardada', true);
} catch (e) {
this.showToast('Error al guardar', false);
}
},
showToast(msg, ok) {
this.toast = { visible: true, ok, msg };
setTimeout(() => this.toast.visible = false, 3000);
},
};
}
</script>
+152 -2
View File
@@ -3,8 +3,158 @@
open: false,
initial: '{{ .user.NombreUsuario }}'.charAt(0).toUpperCase() || 'U',
userName: '{{ .user.NombreUsuario }}',
roleName: '{{ .user.Role.Name }}'
}">
roleName: '{{ .user.Role.Name }}',
bellOpen: false,
notifs: [],
unread: 0,
async loadNotifs() {
try {
const r = await axios.get('/app/mis-notifs?no_leidas=0');
this.notifs = r.data.items || [];
this.unread = r.data.unread || 0;
} catch {}
},
async markRead(id) {
await axios.put('/app/mis-notifs/' + id + '/leida');
const n = this.notifs.find(x => x.ID === id);
if (n) { n.leida = true; this.unread = Math.max(0, this.unread - 1); }
},
async markAll() {
await axios.post('/app/mis-notifs/marcar-todas');
this.notifs.forEach(n => n.leida = true);
this.unread = 0;
}
}"
x-init="loadNotifs(); setInterval(() => loadNotifs(), 30000)">
<!-- Left: page context -->
<div class="flex items-center gap-3">
<div class="md:hidden w-8"></div>
<div class="hidden sm:flex items-center gap-2 text-sm text-slate-500">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"/>
</svg>
<span class="text-slate-400">Panel de control</span>
</div>
</div>
<!-- Right: actions + user menu -->
<div class="flex items-center gap-3">
<!-- 🔔 Bell de notificaciones -->
<div class="relative">
<button @click="bellOpen = !bellOpen; if(bellOpen) loadNotifs()"
class="relative p-2 rounded-xl hover:bg-slate-100 transition-colors focus:outline-none">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-slate-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0"/>
</svg>
<!-- Badge -->
<span x-show="unread > 0"
class="absolute -top-0.5 -right-0.5 h-4 w-4 flex items-center justify-center rounded-full text-white text-[10px] font-bold"
style="background:#8eb02f"
x-text="unread > 9 ? '9+' : unread">
</span>
</button>
<!-- Dropdown notificaciones -->
<div x-show="bellOpen"
@click.outside="bellOpen = false"
x-transition:enter="transition ease-out duration-150"
x-transition:enter-start="opacity-0 scale-95 -translate-y-1"
x-transition:enter-end="opacity-100 scale-100 translate-y-0"
x-transition:leave="transition ease-in duration-100"
x-transition:leave-start="opacity-100 scale-100 translate-y-0"
x-transition:leave-end="opacity-0 scale-95 -translate-y-1"
class="absolute right-0 top-12 w-80 bg-white rounded-xl shadow-lg border border-slate-200 overflow-hidden z-50">
<!-- Header -->
<div class="px-4 py-3 border-b border-slate-100 flex items-center justify-between">
<span class="text-sm font-semibold text-slate-800">Notificaciones</span>
<button x-show="unread > 0" @click="markAll()"
class="text-xs text-slate-400 hover:text-slate-600">Marcar todas leídas</button>
</div>
<!-- Lista -->
<div class="max-h-72 overflow-y-auto divide-y divide-slate-100">
<template x-if="notifs.length === 0">
<div class="px-4 py-6 text-center text-slate-400 text-sm">Sin notificaciones</div>
</template>
<template x-for="n in notifs" :key="n.ID">
<a :href="n.url || '#'"
@click="if(!n.leida) markRead(n.ID)"
class="flex items-start gap-3 px-4 py-3 hover:bg-slate-50 transition-colors"
:class="!n.leida ? 'bg-emerald-50/50' : ''">
<span class="text-lg leading-none mt-0.5" x-text="n.icono || '🔔'"></span>
<div class="flex-1 min-w-0">
<p class="text-sm font-medium text-slate-800 truncate" x-text="n.titulo"></p>
<p class="text-xs text-slate-500 truncate mt-0.5" x-text="n.cuerpo"></p>
</div>
<div x-show="!n.leida" class="w-2 h-2 rounded-full flex-shrink-0 mt-1.5" style="background:#8eb02f"></div>
</a>
</template>
</div>
<!-- Footer -->
<div class="border-t border-slate-100 px-4 py-2">
<a href="/app/tickets" class="text-xs text-slate-500 hover:text-slate-700">Ver todos los tickets →</a>
</div>
</div>
</div>
<!-- User info (desktop) -->
<div class="hidden md:flex flex-col items-end mr-1">
<span class="text-xs font-semibold text-slate-700 leading-tight" x-text="userName"></span>
<span class="text-xs text-slate-400 leading-tight" x-text="roleName"></span>
</div>
<!-- Avatar dropdown -->
<div class="relative">
<button @click="open = !open"
class="flex items-center gap-2 p-1 rounded-xl hover:bg-slate-100 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-1"
style="--tw-ring-color:#8eb02f">
<div class="ui-avatar ui-avatar-lg" x-text="initial"></div>
<svg xmlns="http://www.w3.org/2000/svg"
class="h-3.5 w-3.5 text-slate-400 transition-transform duration-150"
:class="open ? 'rotate-180' : ''"
fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<!-- Dropdown -->
<div x-show="open"
@click.outside="open = false"
x-transition:enter="transition ease-out duration-150"
x-transition:enter-start="opacity-0 scale-95 -translate-y-1"
x-transition:enter-end="opacity-100 scale-100 translate-y-0"
x-transition:leave="transition ease-in duration-100"
x-transition:leave-start="opacity-100 scale-100 translate-y-0"
x-transition:leave-end="opacity-0 scale-95 -translate-y-1"
class="absolute right-0 top-12 w-52 bg-white rounded-xl shadow-lg border border-slate-200 overflow-hidden z-50">
<div class="px-4 py-3 border-b border-slate-100">
<p class="text-sm font-semibold text-slate-800 truncate" x-text="userName"></p>
<p class="text-xs text-slate-500 truncate" x-text="roleName"></p>
</div>
<div class="py-1">
<a href="/app/profile"
class="flex items-center gap-3 px-4 py-2.5 text-sm text-slate-700 hover:bg-slate-50 transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M17.982 18.725A7.488 7.488 0 0012 15.75a7.488 7.488 0 00-5.982 2.975m11.963 0a9 9 0 10-11.963 0m11.963 0A8.966 8.966 0 0112 21a8.966 8.966 0 01-5.982-2.275M15 9.75a3 3 0 11-6 0 3 3 0 016 0z"/>
</svg>
Mi cuenta
</a>
<div class="border-t border-slate-100 my-1"></div>
<a href="/do/logout"
class="flex items-center gap-3 px-4 py-2.5 text-sm text-red-600 hover:bg-red-50 transition-colors"
onclick="event.preventDefault(); document.getElementById('header-logout').submit();">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"/>
</svg>
Cerrar sesión
</a>
<form id="header-logout" action="/do/logout" method="POST" class="hidden"></form>
</div>
</div>
</div>
</div>
</header>
<!-- Left: page context -->
<div class="flex items-center gap-3">