apply company info
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user