feat: Implement asynchronous checkout status check with improved error handling and logging
This commit is contained in:
parent
2a9926d934
commit
e91a2445f3
1 changed files with 75 additions and 24 deletions
|
|
@ -522,15 +522,21 @@ class MainWindow(QMainWindow):
|
|||
|
||||
asset_id = match.group(1)
|
||||
|
||||
# Check checkout status first
|
||||
# Store callback ID for this check
|
||||
callback_id = f"checkout_check_{id(self)}"
|
||||
|
||||
# Check checkout status - use callback approach since Qt doesn't handle Promise returns well
|
||||
js_code = f"""
|
||||
(async function() {{
|
||||
(async () => {{
|
||||
try {{
|
||||
const authToken = window.capturedAuthToken;
|
||||
if (!authToken) {{
|
||||
return {{ error: 'No auth token' }};
|
||||
console.log('[Checkout Check] No auth token available');
|
||||
window['{callback_id}'] = JSON.stringify({{ error: 'No auth token' }});
|
||||
return;
|
||||
}}
|
||||
|
||||
console.log('[Checkout Check] Fetching asset data for {asset_id}');
|
||||
const response = await fetch(
|
||||
'https://devagravityprivate.azurewebsites.net/api/assets/{asset_id}?fields=checkout',
|
||||
{{
|
||||
|
|
@ -542,38 +548,83 @@ class MainWindow(QMainWindow):
|
|||
}}
|
||||
);
|
||||
|
||||
console.log('[Checkout Check] Response status:', response.status);
|
||||
|
||||
if (response.ok) {{
|
||||
const data = await response.json();
|
||||
return {{ checkout: data.checkout }};
|
||||
console.log('[Checkout Check] Full data:', JSON.stringify(data));
|
||||
console.log('[Checkout Check] Checkout field:', data.checkout);
|
||||
const hasCheckout = data.checkout && Object.keys(data.checkout).length > 0;
|
||||
console.log('[Checkout Check] Has checkout:', hasCheckout);
|
||||
window['{callback_id}'] = JSON.stringify({{ checkout: data.checkout, hasCheckout: hasCheckout }});
|
||||
}} else {{
|
||||
return {{ error: 'Failed to fetch asset' }};
|
||||
console.log('[Checkout Check] Failed to fetch, status:', response.status);
|
||||
window['{callback_id}'] = JSON.stringify({{ error: 'Failed to fetch asset', status: response.status }});
|
||||
}}
|
||||
}} catch (error) {{
|
||||
return {{ error: error.toString() }};
|
||||
console.error('[Checkout Check] Error:', error);
|
||||
window['{callback_id}'] = JSON.stringify({{ error: error.toString() }});
|
||||
}}
|
||||
}})();
|
||||
"""
|
||||
|
||||
def on_checkout_status(result):
|
||||
"""Callback when checkout status is received."""
|
||||
if not result or isinstance(result, dict) and result.get('error'):
|
||||
logger.warning(f"Could not check checkout status: {result}")
|
||||
# Show dialog anyway as fallback
|
||||
# Execute the async fetch
|
||||
self.web_view.page().runJavaScript(js_code)
|
||||
|
||||
# After a short delay, read the result from window variable
|
||||
def check_result():
|
||||
read_code = f"window['{callback_id}']"
|
||||
self.web_view.page().runJavaScript(read_code, lambda result: self._handle_checkout_status(result, azure_url, filename, callback_id))
|
||||
|
||||
# Wait 500ms for async fetch to complete
|
||||
from PySide6.QtCore import QTimer
|
||||
QTimer.singleShot(500, check_result)
|
||||
|
||||
def _handle_checkout_status(self, result, azure_url: str, filename: str, callback_id: str) -> None:
|
||||
"""Handle the result of checkout status check.
|
||||
|
||||
Args:
|
||||
result: Result from JavaScript (JSON string)
|
||||
azure_url: Azure URL
|
||||
filename: Asset filename
|
||||
callback_id: Callback ID to clean up
|
||||
"""
|
||||
# Clean up window variable
|
||||
cleanup_code = f"delete window['{callback_id}']"
|
||||
self.web_view.page().runJavaScript(cleanup_code)
|
||||
|
||||
logger.debug(f"Checkout status result type: {type(result)}, value: {result}")
|
||||
|
||||
if not result or not isinstance(result, str):
|
||||
logger.warning(f"Checkout status check returned invalid result: {result}")
|
||||
self._show_checkout_dialog(azure_url, filename)
|
||||
return
|
||||
|
||||
# Parse JSON string
|
||||
try:
|
||||
import json
|
||||
parsed_result = json.loads(result)
|
||||
except (json.JSONDecodeError, ValueError) as e:
|
||||
logger.warning(f"Failed to parse checkout status result: {e}")
|
||||
self._show_checkout_dialog(azure_url, filename)
|
||||
return
|
||||
|
||||
if parsed_result.get('error'):
|
||||
logger.warning(f"Could not check checkout status: {parsed_result}")
|
||||
self._show_checkout_dialog(azure_url, filename)
|
||||
return
|
||||
|
||||
# Check if already checked out
|
||||
checkout_info = result.get('checkout')
|
||||
if checkout_info:
|
||||
logger.info(f"Asset {filename} is already checked out, skipping dialog")
|
||||
has_checkout = parsed_result.get('hasCheckout', False)
|
||||
if has_checkout:
|
||||
checkout_info = parsed_result.get('checkout', {})
|
||||
logger.info(f"Asset {filename} is already checked out: {checkout_info}, skipping dialog")
|
||||
return
|
||||
|
||||
# Not checked out, show confirmation dialog
|
||||
logger.debug(f"Asset {filename} is not checked out, showing dialog")
|
||||
self._show_checkout_dialog(azure_url, filename)
|
||||
|
||||
# Execute JavaScript to check status
|
||||
self.web_view.page().runJavaScript(js_code, on_checkout_status)
|
||||
|
||||
def _show_checkout_dialog(self, azure_url: str, filename: str) -> None:
|
||||
"""Show the checkout confirmation dialog.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue