349 lines
8.2 KiB
Markdown
349 lines
8.2 KiB
Markdown
# Configuração das APIs - MediConnect
|
|
|
|
## ✅ APIs Testadas e Funcionando
|
|
|
|
### 1. Autenticação (Auth API)
|
|
|
|
**Base URL:** `https://yuanqfswhberkoevtmfr.supabase.co/auth/v1`
|
|
|
|
#### Endpoints Funcionais:
|
|
|
|
- **Login** ✅
|
|
|
|
- `POST /token?grant_type=password`
|
|
- Body: `{ email, password }`
|
|
- Retorna: `{ access_token, refresh_token, user }`
|
|
|
|
- **Recuperação de Senha** ✅
|
|
|
|
- `POST /recover`
|
|
- Body: `{ email, options: { redirectTo: url } }`
|
|
- Envia email com link de recuperação
|
|
|
|
- **Atualizar Senha** ✅
|
|
- `PUT /user`
|
|
- Headers: `Authorization: Bearer <access_token>`
|
|
- Body: `{ password: "nova_senha" }`
|
|
- **IMPORTANTE:** Nova senha deve ser diferente da anterior (erro 422 se for igual)
|
|
|
|
### 2. REST API
|
|
|
|
**Base URL:** `https://yuanqfswhberkoevtmfr.supabase.co/rest/v1`
|
|
|
|
#### Tabelas e Campos Corretos:
|
|
|
|
##### **appointments** ✅
|
|
|
|
```typescript
|
|
{
|
|
id: string (UUID)
|
|
order_number: string (auto-gerado: APT-YYYY-NNNN)
|
|
patient_id: string (UUID)
|
|
doctor_id: string (UUID)
|
|
scheduled_at: string (ISO 8601 DateTime)
|
|
duration_minutes: number
|
|
appointment_type: "presencial" | "telemedicina"
|
|
status: "requested" | "confirmed" | "checked_in" | "in_progress" | "completed" | "cancelled" | "no_show"
|
|
chief_complaint: string | null
|
|
patient_notes: string | null
|
|
notes: string | null
|
|
insurance_provider: string | null
|
|
checked_in_at: string | null
|
|
completed_at: string | null
|
|
cancelled_at: string | null
|
|
cancellation_reason: string | null
|
|
created_at: string
|
|
updated_at: string
|
|
created_by: string (UUID)
|
|
updated_by: string | null
|
|
}
|
|
```
|
|
|
|
**Criar Consulta:**
|
|
|
|
```bash
|
|
POST /rest/v1/appointments
|
|
Headers:
|
|
- apikey: <SUPABASE_ANON_KEY>
|
|
- Authorization: Bearer <user_access_token>
|
|
- Content-Type: application/json
|
|
- Prefer: return=representation
|
|
|
|
Body:
|
|
{
|
|
"patient_id": "uuid",
|
|
"doctor_id": "uuid",
|
|
"scheduled_at": "2025-11-03T10:00:00.000Z",
|
|
"duration_minutes": 30,
|
|
"appointment_type": "presencial",
|
|
"chief_complaint": "Motivo da consulta"
|
|
}
|
|
```
|
|
|
|
##### **doctor_availability** ✅
|
|
|
|
```typescript
|
|
{
|
|
id: string (UUID)
|
|
doctor_id: string (UUID)
|
|
weekday: "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday"
|
|
start_time: string (HH:MM:SS, ex: "07:00:00")
|
|
end_time: string (HH:MM:SS, ex: "19:00:00")
|
|
slot_duration_minutes: number (ex: 30)
|
|
appointment_type: "presencial" | "telemedicina"
|
|
is_active: boolean
|
|
created_at: string
|
|
updated_at: string
|
|
created_by: string (UUID)
|
|
updated_by: string | null
|
|
}
|
|
```
|
|
|
|
**Criar Disponibilidade:**
|
|
|
|
```bash
|
|
POST /rest/v1/doctor_availability
|
|
Headers:
|
|
- apikey: <SUPABASE_ANON_KEY>
|
|
- Authorization: Bearer <admin_access_token>
|
|
- Content-Type: application/json
|
|
- Prefer: return=representation
|
|
|
|
Body:
|
|
{
|
|
"doctor_id": "uuid",
|
|
"weekday": "monday", // ⚠️ Texto, não número!
|
|
"start_time": "07:00:00",
|
|
"end_time": "19:00:00",
|
|
"slot_duration_minutes": 30,
|
|
"appointment_type": "presencial",
|
|
"is_active": true,
|
|
"created_by": "admin_user_id"
|
|
}
|
|
```
|
|
|
|
##### **patients** ✅
|
|
|
|
```typescript
|
|
{
|
|
id: string(UUID);
|
|
user_id: string(UUID); // ⚠️ Deve estar vinculado ao auth.users
|
|
full_name: string;
|
|
email: string;
|
|
cpf: string;
|
|
phone_mobile: string;
|
|
// ... outros campos
|
|
}
|
|
```
|
|
|
|
**Atualizar Patient:**
|
|
|
|
```bash
|
|
PATCH /rest/v1/patients?id=eq.<patient_id>
|
|
Headers:
|
|
- apikey: <SUPABASE_ANON_KEY>
|
|
- Authorization: Bearer <admin_access_token>
|
|
- Content-Type: application/json
|
|
|
|
Body:
|
|
{
|
|
"user_id": "auth_user_id"
|
|
}
|
|
```
|
|
|
|
##### **doctors** ✅
|
|
|
|
```typescript
|
|
{
|
|
id: string(UUID);
|
|
user_id: string(UUID);
|
|
full_name: string;
|
|
email: string;
|
|
crm: string;
|
|
crm_uf: string;
|
|
specialty: string;
|
|
// ... outros campos
|
|
}
|
|
```
|
|
|
|
### 3. Edge Functions
|
|
|
|
**Base URL:** `https://yuanqfswhberkoevtmfr.supabase.co/functions/v1`
|
|
|
|
#### Funcionais:
|
|
|
|
- **create-user-with-password** ✅
|
|
- `POST /functions/v1/create-user-with-password`
|
|
- Cria usuário com senha e perfil completo
|
|
- Body:
|
|
```json
|
|
{
|
|
"email": "email@example.com",
|
|
"password": "senha123",
|
|
"full_name": "Nome Completo",
|
|
"phone_mobile": "(11) 99999-9999",
|
|
"cpf": "12345678900",
|
|
"create_patient_record": true,
|
|
"role": "paciente"
|
|
}
|
|
```
|
|
|
|
#### Com Problemas:
|
|
|
|
- **request-password-reset** ❌
|
|
- CORS blocking - não usar
|
|
- Usar diretamente `/auth/v1/recover` em vez disso
|
|
|
|
## 🔑 Chaves de API
|
|
|
|
```typescript
|
|
SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
|
SUPABASE_ANON_KEY =
|
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
|
```
|
|
|
|
## 👥 Usuários de Teste
|
|
|
|
### Admin
|
|
|
|
- Email: `riseup@popcode.com.br`
|
|
- Senha: `riseup`
|
|
|
|
### Dr. Fernando Pirichowski
|
|
|
|
- Email: `fernando.pirichowski@souunit.com.br`
|
|
- Senha: `fernando123`
|
|
- User ID: `38aca60d-7418-4c35-95b6-cb206bb18a0a`
|
|
- Doctor ID: `6dad001d-229b-40b5-80f3-310243c4599c`
|
|
- CRM: `24245`
|
|
- Disponibilidade: Segunda a Domingo, 07:00-19:00
|
|
|
|
### Aurora Sabrina Clara Nascimento (Paciente)
|
|
|
|
- Email: `aurora-nascimento94@gmx.com`
|
|
- Senha: `auroranasc94`
|
|
- User ID: `6dc15cc5-7dae-4b30-924a-a4b4fa142f24`
|
|
- Patient ID: `b85486f7-9135-4b67-9aa7-b884d9603d12`
|
|
- CPF: `66864784231`
|
|
- Telefone: `(21) 99856-3014`
|
|
|
|
## ⚠️ Pontos de Atenção
|
|
|
|
### 1. Weekday no doctor_availability
|
|
|
|
- ❌ **NÃO** usar números (0-6)
|
|
- ✅ **USAR** strings em inglês: `"sunday"`, `"monday"`, `"tuesday"`, `"wednesday"`, `"thursday"`, `"friday"`, `"saturday"`
|
|
|
|
### 2. scheduled_at em appointments
|
|
|
|
- ❌ **NÃO** usar campos separados `appointment_date` e `appointment_time`
|
|
- ✅ **USAR** campo único `scheduled_at` com ISO 8601 DateTime
|
|
- Exemplo: `"2025-11-03T10:00:00.000Z"`
|
|
|
|
### 3. user_id nas tabelas patients e doctors
|
|
|
|
- ⚠️ Sempre vincular ao `auth.users.id`
|
|
- Sem esse vínculo, queries por `user_id` não funcionam
|
|
|
|
### 4. Senha na recuperação
|
|
|
|
- ⚠️ Nova senha DEVE ser diferente da anterior
|
|
- Erro 422 com `error_code: "same_password"` se tentar usar a mesma
|
|
|
|
### 5. redirectTo no password recovery
|
|
|
|
- ⚠️ Supabase pode ignorar o parâmetro `redirectTo`
|
|
- ✅ Implementar detecção de token no lado do cliente
|
|
- Verificar tanto query string `?token=` quanto hash `#access_token=`
|
|
|
|
## 📦 Estrutura de Serviços no Frontend
|
|
|
|
```typescript
|
|
// Tudo configurado em:
|
|
src / services / api / config.ts; // URLs e chaves
|
|
src / services / api / client.ts; // Cliente axios
|
|
src /
|
|
services /
|
|
appointments / // Serviço de consultas
|
|
src /
|
|
services /
|
|
availability / // Disponibilidade médicos
|
|
src /
|
|
services /
|
|
auth / // Autenticação
|
|
src /
|
|
services /
|
|
doctors / // Médicos
|
|
src /
|
|
services /
|
|
patients / // Pacientes
|
|
src /
|
|
services /
|
|
index.ts; // Exportações centralizadas
|
|
```
|
|
|
|
## ✅ Status Atual
|
|
|
|
- [x] Autenticação funcionando
|
|
- [x] Recuperação de senha funcionando
|
|
- [x] Criação de usuários funcionando
|
|
- [x] Criação de pacientes funcionando
|
|
- [x] Criação de disponibilidade médica funcionando
|
|
- [x] Criação de consultas funcionando
|
|
- [x] Vinculação user_id ↔ patient_id corrigida
|
|
- [x] Todos os serviços usando campos corretos
|
|
|
|
## 🚀 Próximos Passos
|
|
|
|
1. Testar agendamento completo no frontend
|
|
2. Verificar listagem de consultas
|
|
3. Testar cancelamento e atualização de consultas
|
|
4. Verificar notificações SMS
|
|
5. Testar fluxo completo de check-in e prontuário
|
|
|
|
## 📝 Exemplos de Uso
|
|
|
|
### Criar Consulta
|
|
|
|
```typescript
|
|
import { appointmentService } from "@/services";
|
|
|
|
const appointment = await appointmentService.create({
|
|
patient_id: "patient-uuid",
|
|
doctor_id: "doctor-uuid",
|
|
scheduled_at: "2025-11-03T10:00:00.000Z",
|
|
duration_minutes: 30,
|
|
appointment_type: "presencial",
|
|
chief_complaint: "Consulta de rotina",
|
|
});
|
|
```
|
|
|
|
### Criar Disponibilidade
|
|
|
|
```typescript
|
|
import { availabilityService } from "@/services";
|
|
|
|
const availability = await availabilityService.create({
|
|
doctor_id: "doctor-uuid",
|
|
weekday: "monday",
|
|
start_time: "07:00:00",
|
|
end_time: "19:00:00",
|
|
slot_duration_minutes: 30,
|
|
appointment_type: "presencial",
|
|
});
|
|
```
|
|
|
|
### Login
|
|
|
|
```typescript
|
|
import { authService } from "@/services";
|
|
|
|
const response = await authService.login({
|
|
email: "user@example.com",
|
|
password: "senha123",
|
|
});
|
|
|
|
// response.access_token - JWT token
|
|
// response.user - dados do usuário
|
|
```
|