294 lines
9.3 KiB
Markdown
294 lines
9.3 KiB
Markdown
# đŻ ARQUITETURA DEFINITIVA: SUPABASE EXTERNO vs NOSSO SUPABASE
|
|
|
|
## đ REGRA DE OURO
|
|
|
|
**Supabase Externo (Fechado da Empresa):** CRUD bĂĄsico de appointments, doctors, patients, reports
|
|
**Nosso Supabase:** Features EXTRAS, KPIs, tracking, gamificação, auditoria, preferĂȘncias
|
|
|
|
---
|
|
|
|
## đ” SUPABASE EXTERNO (FONTE DA VERDADE)
|
|
|
|
### Tabelas que JĂ EXISTEM no Supabase Externo:
|
|
|
|
- â
`appointments` - CRUD completo de agendamentos
|
|
- â
`doctors` - Cadastro de médicos
|
|
- â
`patients` - Cadastro de pacientes
|
|
- â
`reports` - Relatórios médicos båsicos
|
|
- â
`availability` (provavelmente) - Disponibilidade dos médicos
|
|
- â
Dados de autenticação båsica
|
|
|
|
### Endpoints que PUXAM DO EXTERNO:
|
|
|
|
**MĂDULO 2.1 - Appointments (EXTERNO):**
|
|
|
|
- `/appointments/list` â **Puxa de lĂĄ + mescla com nossos logs**
|
|
- `/appointments/create` â **Cria LĂ + grava log aqui**
|
|
- `/appointments/update` â **Atualiza LĂ + grava log aqui**
|
|
- `/appointments/cancel` â **Cancela LĂ + notifica waitlist aqui**
|
|
- `/appointments/confirm` â **Confirma LĂ + grava log aqui**
|
|
- `/appointments/checkin` â **Atualiza LĂ + cria registro de checkin aqui**
|
|
- `/appointments/no-show` â **Marca LĂ + atualiza KPIs aqui**
|
|
|
|
**MĂDULO 2.2 - Availability (DEPENDE):**
|
|
|
|
- `/availability/list` â **SE existir LĂ, puxa de lĂĄ. SENĂO, cria tabela aqui**
|
|
- `/availability/create` â **Cria onde for o source of truth**
|
|
- `/availability/update` â **Atualiza onde for o source of truth**
|
|
- `/availability/delete` â **Deleta onde for o source of truth**
|
|
|
|
**MĂDULO 6 - Reports (EXTERNO):**
|
|
|
|
- `/reports/list-extended` â **Puxa LĂ + adiciona filtros extras**
|
|
- `/reports/export/pdf` â **Puxa dados LĂ + gera PDF aqui**
|
|
- `/reports/export/csv` â **Puxa dados LĂ + gera CSV aqui**
|
|
|
|
**MĂDULO 8 - Patients (EXTERNO):**
|
|
|
|
- `/patients/history` â **Puxa appointments LĂ + histĂłrico estendido aqui**
|
|
- `/patients/portal` â **Mescla dados LĂ + teleconsulta aqui**
|
|
|
|
---
|
|
|
|
## đą NOSSO SUPABASE (FEATURES EXTRAS)
|
|
|
|
### Tabelas que criamos para COMPLEMENTAR:
|
|
|
|
**â
Tracking & Auditoria:**
|
|
|
|
- `user_sync` - Mapear external_user_id â local_user_id
|
|
- `user_actions` - Log de todas as açÔes dos usuårios
|
|
- `user_sessions` - SessÔes de login/logout
|
|
- `audit_actions` - Auditoria detalhada (MĂDULO 13)
|
|
- `access_log` - Quem acessou o quĂȘ (LGPD)
|
|
- `patient_journey` - Jornada do paciente
|
|
|
|
**â
PreferĂȘncias & UI:**
|
|
|
|
- `user_preferences` - Modo escuro, fonte dislexia, acessibilidade (MĂDULO 1 + 11)
|
|
- `patient_preferences` - HorĂĄrios favoritos, comunicação (MĂDULO 8)
|
|
|
|
**â
Agenda Extras:**
|
|
|
|
- `availability_exceptions` - Feriados, exceçÔes (MĂDULO 2.3)
|
|
- `doctor_availability` - SE nĂŁo existir no externo (MĂDULO 2.2)
|
|
|
|
**â
Fila & Waitlist:**
|
|
|
|
- `waitlist` - Lista de espera (MĂDULO 3)
|
|
- `virtual_queue` - Fila virtual da recepção (MĂDULO 4)
|
|
|
|
**â
NotificaçÔes:**
|
|
|
|
- `notifications_queue` - Fila de SMS/Email/WhatsApp (MĂDULO 5)
|
|
- `notification_subscriptions` - Opt-in/opt-out (MĂDULO 5)
|
|
|
|
**â
Analytics & KPIs:**
|
|
|
|
- `kpi_cache` / `analytics_cache` - Cache de mĂ©tricas (MĂDULO 10)
|
|
- `doctor_stats` - Ocupação, no-show %, atraso (MĂDULO 7)
|
|
|
|
**â
Gamificação:**
|
|
|
|
- `doctor_badges` - Conquistas dos mĂ©dicos (MĂDULO 7)
|
|
- `patient_points` - Pontos dos pacientes (gamificação)
|
|
- `patient_streaks` - SequĂȘncias de consultas
|
|
|
|
**â
Teleconsulta:**
|
|
|
|
- `teleconsult_sessions` - Salas Jitsi/WebRTC (MĂDULO 9)
|
|
|
|
**â
Integridade:**
|
|
|
|
- `report_integrity` - Hashes SHA256 anti-fraude (MĂDULO 6)
|
|
|
|
**â
Sistema:**
|
|
|
|
- `feature_flags` - Ativar/desativar features (MĂDULO 14)
|
|
- `patient_extended_history` - HistĂłrico detalhado (MĂDULO 8)
|
|
|
|
### Endpoints 100% NOSSOS:
|
|
|
|
**MĂDULO 1 - User Preferences:**
|
|
|
|
- `/user/info` â **Busca role e permissĂ”es AQUI**
|
|
- `/user/update-preferences` â **Salva AQUI (user_preferences)**
|
|
|
|
**MĂDULO 2.3 - Exceptions:**
|
|
|
|
- `/exceptions/list` â **Lista DAQUI (availability_exceptions)**
|
|
- `/exceptions/create` â **Cria AQUI**
|
|
- `/exceptions/delete` â **Deleta AQUI**
|
|
|
|
**MĂDULO 2.2 - Availability Slots:**
|
|
|
|
- `/availability/slots` â **Gera slots baseado em disponibilidade + exceçÔes DAQUI**
|
|
|
|
**MĂDULO 3 - Waitlist:**
|
|
|
|
- `/waitlist/add` â **Adiciona AQUI**
|
|
- `/waitlist/list` â **Lista DAQUI**
|
|
- `/waitlist/match` â **Busca match AQUI**
|
|
- `/waitlist/remove` â **Remove DAQUI**
|
|
|
|
**MĂDULO 4 - Virtual Queue:**
|
|
|
|
- `/queue/list` â **Lista DAQUI (virtual_queue)**
|
|
- `/queue/checkin` â **Cria registro AQUI**
|
|
- `/queue/advance` â **Avança fila AQUI**
|
|
|
|
**MĂDULO 5 - Notifications:**
|
|
|
|
- `/notifications/enqueue` â **Enfileira AQUI (notifications_queue)**
|
|
- `/notifications/process` â **Worker processa fila DAQUI**
|
|
- `/notifications/confirm` â **Confirma AQUI**
|
|
- `/notifications/subscription` â **Gerencia AQUI (notification_subscriptions)**
|
|
|
|
**MĂDULO 6 - Report Integrity:**
|
|
|
|
- `/reports/integrity-check` â **Verifica hash AQUI (report_integrity)**
|
|
|
|
**MĂDULO 7 - Doctor Stats:**
|
|
|
|
- `/doctor/summary` â **Puxa stats DAQUI (doctor_stats) + appointments LĂ**
|
|
- `/doctor/occupancy` â **Calcula AQUI (doctor_stats)**
|
|
- `/doctor/delay-suggestion` â **Algoritmo AQUI (doctor_stats)**
|
|
- `/doctor/badges` â **Lista DAQUI (doctor_badges)**
|
|
|
|
**MĂDULO 8 - Patient Preferences:**
|
|
|
|
- `/patients/preferences` â **Salva/busca AQUI (patient_preferences)**
|
|
|
|
**MĂDULO 9 - Teleconsulta:**
|
|
|
|
- `/teleconsult/start` â **Cria sessĂŁo AQUI (teleconsult_sessions)**
|
|
- `/teleconsult/status` â **Consulta AQUI**
|
|
- `/teleconsult/end` â **Finaliza AQUI**
|
|
|
|
**MĂDULO 10 - Analytics:**
|
|
|
|
- `/analytics/summary` â **Puxa appointments LĂ + calcula KPIs AQUI**
|
|
- `/analytics/heatmap` â **Processa appointments LĂ + cache AQUI**
|
|
- `/analytics/demand-curve` â **Processa LĂ + cache AQUI**
|
|
- `/analytics/ranking-reasons` â **Agrega LĂ + cache AQUI**
|
|
- `/analytics/monthly-no-show` â **Filtra LĂ + cache AQUI**
|
|
- `/analytics/specialty-heatmap` â **Usa doctor_stats DAQUI**
|
|
- `/analytics/custom-report` â **Query builder sobre dados LĂ + AQUI**
|
|
|
|
**MĂDULO 11 - Accessibility:**
|
|
|
|
- `/accessibility/preferences` â **Salva AQUI (user_preferences)**
|
|
|
|
**MĂDULO 12 - LGPD:**
|
|
|
|
- `/privacy/request-export` â **Exporta dados LĂ + AQUI**
|
|
- `/privacy/request-delete` â **Anonimiza LĂ + deleta AQUI**
|
|
- `/privacy/access-log` â **Consulta AQUI (access_log)**
|
|
|
|
**MĂDULO 13 - Auditoria:**
|
|
|
|
- `/audit/log` â **Grava AQUI (audit_actions)**
|
|
- `/audit/list` â **Lista DAQUI (audit_actions)**
|
|
|
|
**MĂDULO 14 - Feature Flags:**
|
|
|
|
- `/flags/list` â **Lista DAQUI (feature_flags)**
|
|
- `/flags/update` â **Atualiza AQUI**
|
|
|
|
**MĂDULO 15 - System:**
|
|
|
|
- `/system/health-check` â **Verifica saĂșde LĂ + AQUI**
|
|
- `/system/cache-rebuild` â **Recalcula cache AQUI**
|
|
- `/system/cron-runner` â **Executa jobs AQUI**
|
|
|
|
---
|
|
|
|
## đ FLUXO DE DADOS CORRETO
|
|
|
|
### Exemplo 1: Criar Appointment
|
|
|
|
```
|
|
1. Frontend â Edge Function /appointments/create
|
|
2. Edge Function â Supabase EXTERNO (cria appointment)
|
|
3. Edge Function â Nosso Supabase (grava user_actions log)
|
|
4. Edge Function â Nosso Supabase (enfileira notificação)
|
|
5. Retorna sucesso
|
|
```
|
|
|
|
### Exemplo 2: Listar Appointments
|
|
|
|
```
|
|
1. Frontend â Edge Function /appointments/list
|
|
2. Edge Function â Supabase EXTERNO (busca appointments)
|
|
3. Edge Function â Nosso Supabase (busca logs de checkin/no-show)
|
|
4. Edge Function â Mescla dados
|
|
5. Retorna lista completa
|
|
```
|
|
|
|
### Exemplo 3: Dashboard do Médico
|
|
|
|
```
|
|
1. Frontend â Edge Function /doctor/summary
|
|
2. Edge Function â Nosso Supabase (busca doctor_stats)
|
|
3. Edge Function â Supabase EXTERNO (busca appointments de hoje)
|
|
4. Edge Function â Nosso Supabase (busca badges)
|
|
5. Retorna dashboard completo
|
|
```
|
|
|
|
### Exemplo 4: PreferĂȘncias do UsuĂĄrio
|
|
|
|
```
|
|
1. Frontend â Edge Function /user/update-preferences
|
|
2. Edge Function â Nosso Supabase APENAS (salva user_preferences)
|
|
3. Retorna sucesso
|
|
```
|
|
|
|
---
|
|
|
|
## â
CHECKLIST DE IMPLEMENTAĂĂO
|
|
|
|
### O que DEVE usar externalRest():
|
|
|
|
- â
Criar/listar/atualizar/deletar appointments
|
|
- â
Buscar dados de doctors/patients/reports
|
|
- â
Atualizar status de appointments
|
|
- â
Buscar availability (se existir lĂĄ)
|
|
|
|
### O que DEVE usar supabase (nosso):
|
|
|
|
- â
user_preferences, patient_preferences
|
|
- â
user_actions, audit_actions, access_log
|
|
- â
user_sync, user_sessions, patient_journey
|
|
- â
availability_exceptions, doctor_availability (se for nossa tabela)
|
|
- â
waitlist, virtual_queue
|
|
- â
notifications_queue, notification_subscriptions
|
|
- â
kpi_cache, analytics_cache, doctor_stats
|
|
- â
doctor_badges, patient_points, patient_streaks
|
|
- â
teleconsult_sessions
|
|
- â
report_integrity
|
|
- â
feature_flags
|
|
- â
patient_extended_history
|
|
|
|
### O que DEVE mesclar (LĂ + AQUI):
|
|
|
|
- â
/appointments/list (appointments LĂ + logs AQUI)
|
|
- â
/doctor/summary (appointments LĂ + stats AQUI)
|
|
- â
/patients/history (appointments LĂ + extended_history AQUI)
|
|
- â
/patients/portal (dados LĂ + teleconsult AQUI)
|
|
- â
/analytics/\* (dados LĂ + cache/KPIs AQUI)
|
|
|
|
---
|
|
|
|
## đŻ CONCLUSĂO
|
|
|
|
**SUPABASE EXTERNO = CRUD BĂSICO (appointments, doctors, patients, reports)**
|
|
**NOSSO SUPABASE = FEATURES EXTRAS (KPIs, tracking, gamificação, preferĂȘncias, auditoria)**
|
|
|
|
**Todos os endpoints seguem esse padrĂŁo:**
|
|
|
|
1. LĂȘ/Escreve no Supabase Externo quando for dado base
|
|
2. Complementa com nossa DB para features extras
|
|
3. SEMPRE grava logs de auditoria em user_actions
|
|
|
|
â
**Arquitetura 100% alinhada com a especificação!**
|