From 843af814fd5aae3a6f8d478edd8d124c3d1a1635 Mon Sep 17 00:00:00 2001 From: Kobe Date: Fri, 20 Jun 2025 12:41:06 +0200 Subject: [PATCH] apply company info --- routes/__pycache__/main.cpython-313.pyc | Bin 92250 -> 92250 bytes routes/launch_api.py | 95 +++++++++++++++++++- static/js/launch_progress.js | 114 +++++++++++++++++++++++- 3 files changed, 205 insertions(+), 4 deletions(-) diff --git a/routes/__pycache__/main.cpython-313.pyc b/routes/__pycache__/main.cpython-313.pyc index 5da7b342c539ee0a40e1cac9bf2f53d17cb93269..db517854bf82cd9940c0515f8bde91c1e40fffa8 100644 GIT binary patch delta 23 dcmcb0f%Vn}R?g47yj%=GaI2AXD<@-M1^{Q%2U`FD delta 23 dcmcb0f%Vn}R?g47yj%=Gu)C3SD<@-M1^{N;2Rr}( diff --git a/routes/launch_api.py b/routes/launch_api.py index 7b8d612..6b76f3d 100644 --- a/routes/launch_api.py +++ b/routes/launch_api.py @@ -1,6 +1,7 @@ from flask import jsonify, request, current_app, Blueprint from models import ( - KeyValueSettings + KeyValueSettings, + Instance ) from extensions import db, csrf from datetime import datetime @@ -880,4 +881,96 @@ def save_instance(): except Exception as e: current_app.logger.error(f"Error saving instance data: {str(e)}") + return jsonify({'error': str(e)}), 500 + +@launch_api.route('/apply-company-information', methods=['POST']) +@csrf.exempt +def apply_company_information(): + """Apply company information to a launched instance""" + try: + if not request.is_json: + return jsonify({'error': 'Request must be JSON'}), 400 + + data = request.get_json() + required_fields = ['instance_url', 'company_data'] + + if not all(field in data for field in required_fields): + missing_fields = [field for field in required_fields if field not in data] + return jsonify({'error': f'Missing required fields: {", ".join(missing_fields)}'}), 400 + + instance_url = data['instance_url'] + company_data = data['company_data'] + + # Get the instance from database to get the connection token + instance = Instance.query.filter_by(main_url=instance_url).first() + + if not instance: + return jsonify({'error': 'Instance not found in database'}), 404 + + if not instance.connection_token: + return jsonify({'error': 'Instance not authenticated'}), 400 + + # First get JWT token from the instance + jwt_response = requests.post( + f"{instance_url.rstrip('/')}/api/admin/management-token", + headers={ + 'X-API-Key': instance.connection_token, + 'Accept': 'application/json' + }, + timeout=10 + ) + + if jwt_response.status_code != 200: + return jsonify({'error': f'Failed to get JWT token: {jwt_response.text}'}), 400 + + jwt_data = jwt_response.json() + jwt_token = jwt_data.get('token') + + if not jwt_token: + return jsonify({'error': 'No JWT token received'}), 400 + + # Prepare company data for the API + api_company_data = { + 'company_name': company_data.get('name'), + 'company_industry': company_data.get('industry'), + 'company_email': company_data.get('email'), + 'company_website': company_data.get('website'), + 'company_address': company_data.get('streetAddress'), + 'company_city': company_data.get('city'), + 'company_state': company_data.get('state'), + 'company_zip': company_data.get('zipCode'), + 'company_country': company_data.get('country'), + 'company_description': company_data.get('description'), + 'company_phone': company_data.get('phone') + } + + # Apply company information to the instance + company_response = requests.put( + f"{instance_url.rstrip('/')}/api/admin/settings", + headers={ + 'Authorization': f'Bearer {jwt_token}', + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + json=api_company_data, + timeout=10 + ) + + if company_response.status_code != 200: + return jsonify({'error': f'Failed to apply company information: {company_response.text}'}), 400 + + # Update the instance company name in our database + instance.company = company_data.get('name', 'Unknown') + db.session.commit() + + return jsonify({ + 'message': 'Company information applied successfully', + 'data': { + 'company_name': company_data.get('name'), + 'instance_url': instance_url + } + }) + + except Exception as e: + current_app.logger.error(f"Error applying company information: {str(e)}") return jsonify({'error': str(e)}), 500 \ No newline at end of file diff --git a/static/js/launch_progress.js b/static/js/launch_progress.js index ce373b5..93b0995 100644 --- a/static/js/launch_progress.js +++ b/static/js/launch_progress.js @@ -135,6 +135,18 @@ function initializeSteps() { `; stepsContainer.appendChild(authStep); + + // Add Apply Company Information step + const companyStep = document.createElement('div'); + companyStep.className = 'step-item'; + companyStep.innerHTML = ` +
+
+
Apply Company Information
+

Configuring company details...

+
+ `; + stepsContainer.appendChild(companyStep); } async function startLaunch(data) { @@ -478,9 +490,67 @@ async function startLaunch(data) { `; authStep.querySelector('.step-content').appendChild(authDetails); + // Step 11: Apply Company Information + await updateStep(11, 'Apply Company Information', 'Configuring company details...'); + const companyResult = await applyCompanyInformation(`https://${data.webAddresses[0]}`, data.company); + + if (!companyResult.success) { + throw new Error(`Company information application failed: ${companyResult.error}`); + } + + // Update the company step to show success + const companyStep = document.querySelectorAll('.step-item')[10]; + companyStep.classList.remove('active'); + companyStep.classList.add('completed'); + companyStep.querySelector('.step-status').textContent = 'Successfully applied company information'; + + // Add company details + const companyDetails = document.createElement('div'); + companyDetails.className = 'mt-3'; + companyDetails.innerHTML = ` +
+
+
Company Information Applied
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyValue
Company Name${data.company.name || 'Not set'}
Industry${data.company.industry || 'Not set'}
Email${data.company.email || 'Not set'}
Website${data.company.website || 'Not set'}
Address${data.company.streetAddress || 'Not set'}, ${data.company.city || ''}, ${data.company.state || ''} ${data.company.zipCode || ''}
+
+
+
+ `; + companyStep.querySelector('.step-content').appendChild(companyDetails); + } catch (error) { console.error('Launch failed:', error); - await updateStep(10, 'Instance Authentication', `Error: ${error.message}`); + await updateStep(11, 'Apply Company Information', `Error: ${error.message}`); showError(error.message); } } @@ -1086,8 +1156,8 @@ function updateStep(stepNumber, title, description) { document.getElementById('currentStep').textContent = title; document.getElementById('stepDescription').textContent = description; - // Calculate progress based on total number of steps (10 steps total) - const totalSteps = 10; + // Calculate progress based on total number of steps (11 steps total) + const totalSteps = 11; const progress = ((stepNumber - 1) / (totalSteps - 1)) * 100; const progressBar = document.getElementById('launchProgress'); progressBar.style.width = `${progress}%`; @@ -1491,4 +1561,42 @@ async function authenticateInstance(instanceUrl, instanceId) { error: error.message }; } +} + +async function applyCompanyInformation(instanceUrl, company) { + try { + console.log('Applying company information to:', instanceUrl); + + const response = await fetch('/api/admin/apply-company-information', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content + }, + body: JSON.stringify({ + instance_url: instanceUrl, + company_data: company + }) + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error(`Failed to apply company information: ${errorText}`); + } + + const result = await response.json(); + console.log('Company information applied successfully:', result); + + return { + success: true, + message: result.message, + data: result.data + }; + } catch (error) { + console.error('Error applying company information:', error); + return { + success: false, + error: error.message + }; + } } \ No newline at end of file