From f23029e785f5d19f0e130ff5cecbcef932ba615e Mon Sep 17 00:00:00 2001 From: ncoronel Date: Wed, 8 Jul 2026 09:00:42 -0300 Subject: [PATCH] feat: enhance integration setup process with error handling and validation --- .../TenantIntegrationController.php | 18 ++++++--- .../Services/BaseIntegrationService.php | 12 ++++++ .../Services/TelepagosIntegrationService.php | 12 ++++++ .../Services/TenantIntegrationService.php | 39 ++++++++++++++----- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/app/Domains/Integration/Controllers/TenantIntegrationController.php b/app/Domains/Integration/Controllers/TenantIntegrationController.php index a4ced05..308e590 100644 --- a/app/Domains/Integration/Controllers/TenantIntegrationController.php +++ b/app/Domains/Integration/Controllers/TenantIntegrationController.php @@ -36,12 +36,18 @@ class TenantIntegrationController extends Controller { $integration = Integration::where('integration_code', $integrationCode)->firstOrFail(); - $tenantIntegration = $this->tenantIntegrationService->updateOrCreateIntegration( - $tenantCode, - $integration, - $request->input('integration_data', []) - ); + try { + $tenantIntegration = $this->tenantIntegrationService->updateOrCreateIntegration( + $tenantCode, + $integration, + $request->input('integration_data', []) + ); - return response()->json($tenantIntegration); + return response()->json($tenantIntegration); + } catch (\Exception $e) { + return response()->json([ + 'message' => 'Error validando la configuración: ' . $e->getMessage() + ], 400); + } } } diff --git a/app/Domains/Integration/Services/BaseIntegrationService.php b/app/Domains/Integration/Services/BaseIntegrationService.php index d7b8583..ba797e4 100644 --- a/app/Domains/Integration/Services/BaseIntegrationService.php +++ b/app/Domains/Integration/Services/BaseIntegrationService.php @@ -152,4 +152,16 @@ abstract class BaseIntegrationService * @return array */ abstract public function getHeaders(): array; + + /** + * Hook called after the integration is configured for the tenant. + * Can be used to validate credentials or perform initial setups. + * Throw an Exception on failure. + * + * @return void + */ + public function onSetup(): void + { + // Override in child classes if needed + } } diff --git a/app/Domains/Integration/Services/TelepagosIntegrationService.php b/app/Domains/Integration/Services/TelepagosIntegrationService.php index cb7dc2b..cde949c 100644 --- a/app/Domains/Integration/Services/TelepagosIntegrationService.php +++ b/app/Domains/Integration/Services/TelepagosIntegrationService.php @@ -213,4 +213,16 @@ class TelepagosIntegrationService extends BaseIntegrationService $cacheKey = "integration_token:{$this->tenantCode}:{$this->integrationCode}"; Cache::forget($cacheKey); } + + /** + * Perform initial setup validation for Telepagos. + * + * @return void + * @throws Exception + */ + public function onSetup(): void + { + // Realiza un login de prueba para validar que las credenciales son correctas. + $this->login(); + } } diff --git a/app/Domains/Integration/Services/TenantIntegrationService.php b/app/Domains/Integration/Services/TenantIntegrationService.php index c400259..2baa478 100644 --- a/app/Domains/Integration/Services/TenantIntegrationService.php +++ b/app/Domains/Integration/Services/TenantIntegrationService.php @@ -4,6 +4,7 @@ namespace App\Domains\Integration\Services; use App\Domains\Integration\Models\Integration; use App\Domains\Integration\Models\TenantIntegration; +use Illuminate\Support\Facades\DB; class TenantIntegrationService { @@ -23,14 +24,34 @@ class TenantIntegrationService public function updateOrCreateIntegration(string $tenantCode, Integration $integration, array $data): TenantIntegration { - return TenantIntegration::updateOrCreate( - [ - 'tenant_code' => $tenantCode, - 'integration_code' => $integration->integration_code, - ], - [ - 'integration_data' => $data, - ] - ); + return DB::transaction(function () use ($tenantCode, $integration, $data) { + $tenantIntegration = TenantIntegration::updateOrCreate( + [ + 'tenant_code' => $tenantCode, + 'integration_code' => $integration->integration_code, + ], + [ + 'integration_data' => $data, + ] + ); + + $service = $this->resolveService($integration->integration_code); + if ($service) { + $service->forTenant($tenantCode)->onSetup(); + } + + return $tenantIntegration; + }); + } + + protected function resolveService(string $integrationCode): ?BaseIntegrationService + { + switch ($integrationCode) { + case 'telepagos': + case 'telepagos_homo': + return new TelepagosIntegrationService($integrationCode); + default: + return null; + } } }