Files
clube67_newwhats.local/base-scoreodonto/medico/services/medicoBackend.ts
T
VPS 4 Deploy Agent 5ec6bd6354
continuous-integration/webhook Falha no deploy de clube67_newwhats.local (VPS 4)
chore(ops): migrate clube67 to newwhats.clube67.com directory
2026-05-18 03:26:41 +02:00

155 lines
5.6 KiB
TypeScript

import {
MedPaciente,
MedProfissional,
MedAgendamento,
MedProntuario,
MedFinanceiro,
MedNotificacao
} from '../types.ts';
const API_BASE_URL = '/api/med'; // New namespace for medical API
export class MedicoBackend {
// --- STORAGE KEYS ---
private static KEYS = {
PACIENTES: 'med_pacientes',
PROFISSIONAIS: 'med_profissionais',
AGENDAMENTOS: 'med_agendamentos',
PRONTUARIOS: 'med_prontuarios',
FINANCEIRO: 'med_financeiro',
NOTIFICACOES: 'med_notificacoes',
AUTH: 'med_auth_token'
};
// --- HELPERS ---
private static async request<T>(path: string, options: RequestInit = {}): Promise<T> {
const token = localStorage.getItem(this.KEYS.AUTH);
const headers = {
'Content-Type': 'application/json',
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
...options.headers,
};
try {
const response = await fetch(`${API_BASE_URL}${path}`, { ...options, headers });
if (!response.ok) throw new Error(`API Error: ${response.statusText}`);
return await response.json();
} catch (err) {
console.warn(`API unavailable at ${path}, falling back to local storage.`, err);
throw err; // Let the caller decide how to handle fallback
}
}
// --- PACIENTES ---
static async getPacientes(): Promise<MedPaciente[]> {
try {
return await this.request<MedPaciente[]>('/pacientes');
} catch {
const stored = localStorage.getItem(this.KEYS.PACIENTES);
return stored ? JSON.parse(stored) : [];
}
}
static async savePaciente(paciente: MedPaciente): Promise<void> {
try {
await this.request('/pacientes', {
method: 'POST',
body: JSON.stringify(paciente)
});
} catch {
const pacientes = await this.getPacientes();
const index = pacientes.findIndex(p => p.id === paciente.id);
if (index >= 0) pacientes[index] = paciente;
else pacientes.push(paciente);
localStorage.setItem(this.KEYS.PACIENTES, JSON.stringify(pacientes));
}
}
// --- AGENDA / PROFISSIONAIS ---
static async getProfissionais(): Promise<MedProfissional[]> {
try {
return await this.request<MedProfissional[]>('/profissionais');
} catch {
const stored = localStorage.getItem(this.KEYS.PROFISSIONAIS);
if (stored) return JSON.parse(stored);
// Mock initial data if empty
const mock: MedProfissional[] = [
{ id: '1', nome: 'Dr. Ricardo Santos', crm: '12345-MS', especialidade: 'Cardiologia', email: 'ricardo@med.com', telefone: '67 9999-9999', ativo: true, corAgenda: '#22d3ee' },
{ id: '2', nome: 'Dra. Ana Oliveira', crm: '67890-MS', especialidade: 'Pediatria', email: 'ana@med.com', telefone: '67 8888-8888', ativo: true, corAgenda: '#f43f5e' }
];
localStorage.setItem(this.KEYS.PROFISSIONAIS, JSON.stringify(mock));
return mock;
}
}
static async getAgendamentos(): Promise<MedAgendamento[]> {
try {
return await this.request<MedAgendamento[]>('/agendamentos');
} catch {
const stored = localStorage.getItem(this.KEYS.AGENDAMENTOS);
return stored ? JSON.parse(stored) : [];
}
}
static async saveAgendamento(agenda: MedAgendamento): Promise<void> {
try {
await this.request('/agendamentos', {
method: 'POST',
body: JSON.stringify(agenda)
});
} catch {
const data = await this.getAgendamentos();
const index = data.findIndex(a => a.id === agenda.id);
if (index >= 0) data[index] = agenda;
else data.push(agenda);
localStorage.setItem(this.KEYS.AGENDAMENTOS, JSON.stringify(data));
}
}
// --- FINANCEIRO ---
static async getFinanceiro(): Promise<MedFinanceiro[]> {
try {
return await this.request<MedFinanceiro[]>('/financeiro');
} catch {
const stored = localStorage.getItem(this.KEYS.FINANCEIRO);
return stored ? JSON.parse(stored) : [];
}
}
// --- NOTIFICACOES ---
static async getNotificacoes(): Promise<MedNotificacao[]> {
try {
return await this.request<MedNotificacao[]>('/notificacoes');
} catch {
const stored = localStorage.getItem(this.KEYS.NOTIFICACOES);
return stored ? JSON.parse(stored) : [];
}
}
static async markNotificationRead(id: string): Promise<void> {
try {
await this.request(`/notificacoes/${id}/read`, { method: 'PUT' });
} catch {
const notifs = await this.getNotificacoes();
const index = notifs.findIndex(n => n.id === id);
if (index >= 0) {
notifs[index].lida = true;
localStorage.setItem(this.KEYS.NOTIFICACOES, JSON.stringify(notifs));
}
}
}
// --- AUTH ---
static isAuthenticated(): boolean {
return !!localStorage.getItem(this.KEYS.AUTH);
}
static login(token: string) {
localStorage.setItem(this.KEYS.AUTH, token);
}
static logout() {
localStorage.removeItem(this.KEYS.AUTH);
}
}