apply company info
This commit is contained in:
Binary file not shown.
@@ -1,6 +1,7 @@
|
|||||||
from flask import jsonify, request, current_app, Blueprint
|
from flask import jsonify, request, current_app, Blueprint
|
||||||
from models import (
|
from models import (
|
||||||
KeyValueSettings
|
KeyValueSettings,
|
||||||
|
Instance
|
||||||
)
|
)
|
||||||
from extensions import db, csrf
|
from extensions import db, csrf
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -881,3 +882,95 @@ def save_instance():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
current_app.logger.error(f"Error saving instance data: {str(e)}")
|
current_app.logger.error(f"Error saving instance data: {str(e)}")
|
||||||
return jsonify({'error': str(e)}), 500
|
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
|
||||||
@@ -135,6 +135,18 @@ function initializeSteps() {
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
stepsContainer.appendChild(authStep);
|
stepsContainer.appendChild(authStep);
|
||||||
|
|
||||||
|
// Add Apply Company Information step
|
||||||
|
const companyStep = document.createElement('div');
|
||||||
|
companyStep.className = 'step-item';
|
||||||
|
companyStep.innerHTML = `
|
||||||
|
<div class="step-icon"><i class="fas fa-building"></i></div>
|
||||||
|
<div class="step-content">
|
||||||
|
<h5>Apply Company Information</h5>
|
||||||
|
<p class="step-status">Configuring company details...</p>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
stepsContainer.appendChild(companyStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startLaunch(data) {
|
async function startLaunch(data) {
|
||||||
@@ -478,9 +490,67 @@ async function startLaunch(data) {
|
|||||||
`;
|
`;
|
||||||
authStep.querySelector('.step-content').appendChild(authDetails);
|
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 = `
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body">
|
||||||
|
<h6 class="card-title mb-3">Company Information Applied</h6>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-sm">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Property</th>
|
||||||
|
<th>Value</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Company Name</td>
|
||||||
|
<td>${data.company.name || 'Not set'}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Industry</td>
|
||||||
|
<td>${data.company.industry || 'Not set'}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Email</td>
|
||||||
|
<td>${data.company.email || 'Not set'}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Website</td>
|
||||||
|
<td>${data.company.website || 'Not set'}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Address</td>
|
||||||
|
<td>${data.company.streetAddress || 'Not set'}, ${data.company.city || ''}, ${data.company.state || ''} ${data.company.zipCode || ''}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
companyStep.querySelector('.step-content').appendChild(companyDetails);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Launch failed:', 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);
|
showError(error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1086,8 +1156,8 @@ function updateStep(stepNumber, title, description) {
|
|||||||
document.getElementById('currentStep').textContent = title;
|
document.getElementById('currentStep').textContent = title;
|
||||||
document.getElementById('stepDescription').textContent = description;
|
document.getElementById('stepDescription').textContent = description;
|
||||||
|
|
||||||
// Calculate progress based on total number of steps (10 steps total)
|
// Calculate progress based on total number of steps (11 steps total)
|
||||||
const totalSteps = 10;
|
const totalSteps = 11;
|
||||||
const progress = ((stepNumber - 1) / (totalSteps - 1)) * 100;
|
const progress = ((stepNumber - 1) / (totalSteps - 1)) * 100;
|
||||||
const progressBar = document.getElementById('launchProgress');
|
const progressBar = document.getElementById('launchProgress');
|
||||||
progressBar.style.width = `${progress}%`;
|
progressBar.style.width = `${progress}%`;
|
||||||
@@ -1492,3 +1562,41 @@ async function authenticateInstance(instanceUrl, instanceId) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user