This commit is contained in:
alorig
2025-11-22 20:29:26 +05:00
parent 6e25c5e307
commit 6f50b3c88f
2 changed files with 33 additions and 12 deletions

View File

@@ -161,13 +161,16 @@ class IntegrationService:
def test_connection( def test_connection(
self, self,
integration: SiteIntegration integration: SiteIntegration,
is_initial_connection: bool = False
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Test connection to the integrated platform. Test connection to the integrated platform.
Args: Args:
integration: SiteIntegration instance integration: SiteIntegration instance
is_initial_connection: If True, allows connection test to pass even if WordPress
plugin hasn't stored API key yet (for initial setup)
Returns: Returns:
dict: { dict: {
@@ -181,7 +184,7 @@ class IntegrationService:
""" """
try: try:
if integration.platform == 'wordpress': if integration.platform == 'wordpress':
return self._test_wordpress_connection(integration) return self._test_wordpress_connection(integration, is_initial_connection=is_initial_connection)
elif integration.platform == 'shopify': elif integration.platform == 'shopify':
return self._test_shopify_connection(integration) return self._test_shopify_connection(integration)
else: else:
@@ -201,7 +204,8 @@ class IntegrationService:
def _test_wordpress_connection( def _test_wordpress_connection(
self, self,
integration: SiteIntegration integration: SiteIntegration,
is_initial_connection: bool = False
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Test WordPress connection using API key only. Test WordPress connection using API key only.
@@ -301,13 +305,23 @@ class IntegrationService:
except Exception as e: except Exception as e:
issues.append(f"Cannot detect IGNY8 plugin: {str(e)}") issues.append(f"Cannot detect IGNY8 plugin: {str(e)}")
# Success determination - only based on API key and plugin # Success determination
is_healthy = ( # For initial connection, we don't require plugin_has_api_key yet
health_checks['api_key_configured'] and # because WordPress hasn't stored it yet - that's what we're testing!
health_checks['wp_rest_api_reachable'] and # For subsequent health checks, we require all checks to pass
health_checks['plugin_installed'] and if is_initial_connection:
health_checks['plugin_has_api_key'] is_healthy = (
) health_checks['api_key_configured'] and
health_checks['wp_rest_api_reachable'] and
health_checks['plugin_installed']
)
else:
is_healthy = (
health_checks['api_key_configured'] and
health_checks['wp_rest_api_reachable'] and
health_checks['plugin_installed'] and
health_checks['plugin_has_api_key']
)
# Save site_url to config if successful and not already set # Save site_url to config if successful and not already set
if is_healthy and not config.get('site_url'): if is_healthy and not config.get('site_url'):
@@ -324,7 +338,11 @@ class IntegrationService:
elif not health_checks['plugin_installed']: elif not health_checks['plugin_installed']:
message = "⚠️ WordPress is reachable but IGNY8 plugin not installed" message = "⚠️ WordPress is reachable but IGNY8 plugin not installed"
elif not health_checks['plugin_has_api_key']: elif not health_checks['plugin_has_api_key']:
message = "⚠️ Plugin is installed but API key not configured in WordPress" if is_initial_connection:
# During initial connection, this is expected - WordPress will store the key after successful test
message = "✅ WordPress integration ready - API key will be stored after connection"
else:
message = "⚠️ Plugin is installed but API key not configured in WordPress"
else: else:
message = "❌ WordPress connection failed" message = "❌ WordPress connection failed"

View File

@@ -169,7 +169,10 @@ class IntegrationViewSet(SiteSectorModelViewSet):
) )
service = IntegrationService() service = IntegrationService()
result = service.test_connection(integration) # Mark this as initial connection test since API key was provided in request body
# This allows the test to pass even if WordPress plugin hasn't stored the key yet
is_initial_connection = bool(api_key and request.data.get('api_key'))
result = service._test_wordpress_connection(integration, is_initial_connection=is_initial_connection)
if result.get('success'): if result.get('success'):
return success_response(result, request=request) return success_response(result, request=request)