From f4c2c6861a5adf380f3d0d6ddf40ecaf77d05eb9 Mon Sep 17 00:00:00 2001 From: VPS 4 Builder Date: Mon, 6 Jul 2026 06:31:33 +0200 Subject: [PATCH] =?UTF-8?q?feat(agenda):=20GET=20/dentista-contato=20?= =?UTF-8?q?=E2=80=94=20resolve=20WhatsApp=20do=20dentista=20(Parte=20A)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Endpoint para a Secretária IA acionar o dentista responsável num encaixe sub-hora. Co-Authored-By: Claude Opus 4.8 --- backend/newwhats/agenda-bridge.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/backend/newwhats/agenda-bridge.js b/backend/newwhats/agenda-bridge.js index 5a15968..a7102dc 100644 --- a/backend/newwhats/agenda-bridge.js +++ b/backend/newwhats/agenda-bridge.js @@ -344,6 +344,25 @@ export function createAgendaBridge(pool) { } catch (e) { res.status(500).json({ error: e.message }); } }); + // ── GET /dentista-contato ───────────────────────────────────────────────────── + // Resolve o WhatsApp de um dentista da clínica (Parte A: a IA aciona o Dr). + // Query: clinica_id (obrig.), nome? OU dentista_id? + router.get('/dentista-contato', async (req, res) => { + const clinicaId = String(req.query.clinica_id || ''); + if (!clinicaId) return res.status(400).json({ error: 'clinica_id obrigatório' }); + try { + const params = [clinicaId]; let where = 'clinica_id = $1'; + if (req.query.dentista_id) { params.push(String(req.query.dentista_id)); where += ` AND id = $${params.length}`; } + else if (req.query.nome) { params.push(String(req.query.nome)); where += ` AND lower(nome) LIKE '%'||lower($${params.length})||'%'`; } + const { rows } = await pool.query( + `SELECT id, nome, telefone FROM dentistas WHERE ${where} AND ativo IS DISTINCT FROM 0 ORDER BY nome LIMIT 1`, params); + if (!rows.length) return res.json({ encontrado: false }); + const d = rows[0]; + const tel = String(d.telefone || '').replace(/\D/g, ''); + res.json({ encontrado: true, dentista_id: d.id, nome: d.nome, telefone: tel || null }); + } catch (e) { res.status(500).json({ error: e.message }); } + }); + // ── POST /book ──────────────────────────────────────────────────────────────── // Body: { clinica_id, dentista_id, start, end?, paciente_nome, paciente_celular, procedimento? } router.post('/book', async (req, res) => {