From 43933bfe3a7f294de6e473df2210e871e75ff506 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 4 Jul 2026 15:19:46 -0500 Subject: [PATCH] feat: endpoints usan api_base_url como base, rutas relativas en UI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Banner en tab Endpoints muestra la URL base de General - Inputs de URL aceptan rutas relativas (?param=x o /ruta) - Preview live de la URL completa (base+ruta) bajo cada input - Backend prepend api_base_url en test y sync si URL no es absoluta - Compatible hacia atrás: URLs absolutas existentes siguen funcionando Co-Authored-By: Claude Sonnet 4.6 --- admin/DashboardController.php | 77 ++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/admin/DashboardController.php b/admin/DashboardController.php index 61e2f11..ac517e8 100644 --- a/admin/DashboardController.php +++ b/admin/DashboardController.php @@ -1113,12 +1113,23 @@ HTML; $epStmt->execute([$id]); $allEps = $epStmt->fetchAll(\PDO::FETCH_ASSOC); + $baseRaw = rtrim($company['api_base_url'] ?? '', '/'); + $baseHtml = self::h($baseRaw); + $epRows = ''; foreach ($allEps as $ep) { $epKey = self::h($ep['endpoint_key']); $epName = self::h($ep['name'] ?? ''); $epUrl = self::h($ep['url'] ?? ''); $epMeth = $ep['method'] ?? 'GET'; + // Preview full URL + $rawUrl = $ep['url'] ?? ''; + if ($rawUrl === '' || str_starts_with($rawUrl, 'http')) { + $previewUrl = self::h($rawUrl); + } else { + $sep = str_starts_with($rawUrl, '?') ? '' : '/'; + $previewUrl = self::h($baseRaw . $sep . ltrim($rawUrl, '/')); + } $epDir = $ep['direction'] ?? 'download'; $epParams = self::h($ep['params'] ?? ''); $epAct = $ep['is_active'] ? 'checked' : ''; @@ -1215,9 +1226,10 @@ VR; - - -
Usa {'{var}'} para variables. {$lastAt}
+ + +
{$previewUrl}
+
Usa {'{var}'} para variables. {$lastAt}
@@ -1246,15 +1258,21 @@ ROW; $epHtml = <<🔌 Endpoints API +
+ 🔗 URL base: + {$baseHtml} + cambiar en General → + · Los endpoints usan rutas relativas a esta URL +

- Define los endpoints del ERP. Usa {'{variable}'} en la URL para variables que el bot pedirá al usuario.
- Ejemplo: ?peticion=produccion&desde={'{desde}'}&hasta={'{hasta}'} + Escribe solo la ruta o sufijo: ?peticion=produccion o /mi/ruta. Se combina con la URL base.
+ Usa {'{variable}'} para variables que el bot pedirá al usuario. Ejemplo: ?desde={'{desde}'}&hasta={'{hasta}'}

- + {$epRows} @@ -1290,8 +1308,9 @@ ROW;
- - + + +
@@ -1620,8 +1639,33 @@ function collectVarConfigs(epId) { return JSON.stringify(configs); } +const BASE_URL = '{$baseHtml}'; + +function buildFullUrl(path) { + if (!path || path.startsWith('http')) return path; + const base = BASE_URL.replace(/\/$/, ''); + const sep = path.startsWith('?') ? '' : '/'; + return base + sep + path.replace(/^\//, ''); +} + +function updateEpPreview(epId) { + const path = document.getElementById('epu_'+epId)?.value || ''; + const el = document.getElementById('ep-preview-'+epId); + if (!el) return; + const full = buildFullUrl(path); + el.textContent = full || ''; + el.title = full || ''; +} + +function updateNewEpPreview() { + const path = document.getElementById('newEpUrl')?.value || ''; + const el = document.getElementById('newEpPreview'); + if (el) el.textContent = buildFullUrl(path) || ''; +} + // When URL changes, refresh the vars section function onUrlChange(epId) { + updateEpPreview(epId); const url = document.getElementById('epu_'+epId).value; const matches = [...url.matchAll(/\{([^}]+)\}/g)].map(m => m[1]); const vrRows = document.getElementById('vr-rows-'+epId); @@ -1797,9 +1841,15 @@ HTML; exit; } - // Llamar al endpoint + // Llamar al endpoint — prepend base URL si es ruta relativa + $epUrl = $ep['url']; + if (!str_starts_with($epUrl, 'http')) { + $base = rtrim($company['api_base_url'] ?? '', '/'); + $sep = str_starts_with($epUrl, '?') ? '' : '/'; + $epUrl = $base . $sep . ltrim($epUrl, '/'); + } $apiKey = $company['api_key'] ?? ''; - $ch = curl_init($ep['url']); + $ch = curl_init($epUrl); $opts = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, @@ -2050,6 +2100,13 @@ HTML; $company = CompanyRepository::findById($companyId); $apiKey = $company['api_key'] ?? ''; + // Prepend api_base_url for relative paths + if ($url !== '' && !str_starts_with($url, 'http')) { + $base = rtrim($company['api_base_url'] ?? '', '/'); + $sep = str_starts_with($url, '?') ? '' : '/'; + $url = $base . $sep . ltrim($url, '/'); + } + $ch = curl_init($url); $opts = [ CURLOPT_RETURNTRANSFER => true,
KeyNombreDirecciónMétodoURLActivoKeyNombreDirecciónMétodoRuta / sufijoActivo