feat: enhance integration setup process with error handling and validation
This commit is contained in:
@@ -36,12 +36,18 @@ class TenantIntegrationController extends Controller
|
|||||||
{
|
{
|
||||||
$integration = Integration::where('integration_code', $integrationCode)->firstOrFail();
|
$integration = Integration::where('integration_code', $integrationCode)->firstOrFail();
|
||||||
|
|
||||||
$tenantIntegration = $this->tenantIntegrationService->updateOrCreateIntegration(
|
try {
|
||||||
$tenantCode,
|
$tenantIntegration = $this->tenantIntegrationService->updateOrCreateIntegration(
|
||||||
$integration,
|
$tenantCode,
|
||||||
$request->input('integration_data', [])
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,4 +152,16 @@ abstract class BaseIntegrationService
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
abstract public function getHeaders(): 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -213,4 +213,16 @@ class TelepagosIntegrationService extends BaseIntegrationService
|
|||||||
$cacheKey = "integration_token:{$this->tenantCode}:{$this->integrationCode}";
|
$cacheKey = "integration_token:{$this->tenantCode}:{$this->integrationCode}";
|
||||||
Cache::forget($cacheKey);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Domains\Integration\Services;
|
|||||||
|
|
||||||
use App\Domains\Integration\Models\Integration;
|
use App\Domains\Integration\Models\Integration;
|
||||||
use App\Domains\Integration\Models\TenantIntegration;
|
use App\Domains\Integration\Models\TenantIntegration;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class TenantIntegrationService
|
class TenantIntegrationService
|
||||||
{
|
{
|
||||||
@@ -23,14 +24,34 @@ class TenantIntegrationService
|
|||||||
|
|
||||||
public function updateOrCreateIntegration(string $tenantCode, Integration $integration, array $data): TenantIntegration
|
public function updateOrCreateIntegration(string $tenantCode, Integration $integration, array $data): TenantIntegration
|
||||||
{
|
{
|
||||||
return TenantIntegration::updateOrCreate(
|
return DB::transaction(function () use ($tenantCode, $integration, $data) {
|
||||||
[
|
$tenantIntegration = TenantIntegration::updateOrCreate(
|
||||||
'tenant_code' => $tenantCode,
|
[
|
||||||
'integration_code' => $integration->integration_code,
|
'tenant_code' => $tenantCode,
|
||||||
],
|
'integration_code' => $integration->integration_code,
|
||||||
[
|
],
|
||||||
'integration_data' => $data,
|
[
|
||||||
]
|
'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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user