66 lines
2.6 KiB
TypeScript
66 lines
2.6 KiB
TypeScript
import { MedNotificacao, MedAgendamento, MedFinanceiro } from '../types.ts';
|
|
import { MedicoBackend } from './medicoBackend.ts';
|
|
|
|
/**
|
|
* Intelligent Notification Engine for Medical Module
|
|
*/
|
|
export class MedicoNotif {
|
|
static async generateIntelligentAlerts(): Promise<void> {
|
|
const agendamentos = await MedicoBackend.getAgendamentos();
|
|
const financeiro = await MedicoBackend.getFinanceiro();
|
|
const existingNotifs = await MedicoBackend.getNotificacoes();
|
|
|
|
const newNotifs: MedNotificacao[] = [];
|
|
const now = new Date();
|
|
|
|
// 1. Check for upcoming appointments (next 24h)
|
|
agendamentos.forEach(ag => {
|
|
const agDate = new Date(ag.start);
|
|
const diffHours = (agDate.getTime() - now.getTime()) / (1000 * 60 * 60);
|
|
|
|
if (diffHours > 0 && diffHours < 24 && ag.status === 'Agendado') {
|
|
const title = 'Confirmação de Agenda';
|
|
const msg = `Paciente ${ag.pacienteNome} tem agendamento amanhã às ${agDate.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`;
|
|
|
|
if (!this.exists(existingNotifs, title, msg)) {
|
|
newNotifs.push(this.create(title, msg, 'agenda', 'medium'));
|
|
}
|
|
}
|
|
});
|
|
|
|
// 2. Check for overdue payments
|
|
financeiro.forEach(fin => {
|
|
const venc = new Date(fin.dataVencimento);
|
|
if (fin.status === 'Pendente' && venc < now) {
|
|
const title = 'Pagamento em Atraso';
|
|
const msg = `Mensalidade do paciente com ID ${fin.pacienteId} está atrasada desde ${venc.toLocaleDateString()}.`;
|
|
|
|
if (!this.exists(existingNotifs, title, msg)) {
|
|
newNotifs.push(this.create(title, msg, 'financeiro', 'high'));
|
|
}
|
|
}
|
|
});
|
|
|
|
if (newNotifs.length > 0) {
|
|
const allNotifs = [...newNotifs, ...existingNotifs];
|
|
localStorage.setItem('med_notificacoes', JSON.stringify(allNotifs));
|
|
}
|
|
}
|
|
|
|
private static create(titulo: string, mensagem: string, tipo: MedNotificacao['tipo'], priority: MedNotificacao['priority']): MedNotificacao {
|
|
return {
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
titulo,
|
|
mensagem,
|
|
tipo,
|
|
priority,
|
|
data: new Date().toISOString(),
|
|
lida: false
|
|
};
|
|
}
|
|
|
|
private static exists(list: MedNotificacao[], title: string, msg: string): boolean {
|
|
return list.some(n => n.titulo === title && n.mensagem === msg);
|
|
}
|
|
}
|