diff --git a/routes/launch_api.py b/routes/launch_api.py index 0658ceb..bee33d0 100644 --- a/routes/launch_api.py +++ b/routes/launch_api.py @@ -1510,4 +1510,81 @@ Thank you for choosing DocuPulse! except Exception as e: current_app.logger.error(f"Error sending completion email: {str(e)}") + return jsonify({'error': str(e)}), 500 + +@launch_api.route('/copy-smtp-settings', methods=['POST']) +@csrf.exempt +def copy_smtp_settings(): + """Copy SMTP settings from master instance to launched instance""" + try: + if not request.is_json: + return jsonify({'error': 'Request must be JSON'}), 400 + + data = request.get_json() + if 'instance_url' not in data: + return jsonify({'error': 'Missing instance_url parameter'}), 400 + + instance_url = data['instance_url'] + + # Get SMTP settings from master instance + smtp_settings = KeyValueSettings.get_value('smtp_settings') + if not smtp_settings: + return jsonify({'error': 'SMTP settings not configured on master instance'}), 400 + + # 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 + + # Get JWT token from the launched instance using management API key + 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 + + # Copy SMTP settings to the launched instance + smtp_response = requests.post( + f"{instance_url.rstrip('/')}/api/admin/key-value", + headers={ + 'Authorization': f'Bearer {jwt_token}', + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + json={ + 'key': 'smtp_settings', + 'value': smtp_settings + }, + timeout=10 + ) + + if smtp_response.status_code != 200: + return jsonify({'error': f'Failed to copy SMTP settings: {smtp_response.text}'}), 400 + + # Log the SMTP settings copy + current_app.logger.info(f"SMTP settings copied to instance {instance_url}") + + return jsonify({ + 'message': 'SMTP settings copied successfully', + 'data': smtp_settings + }) + + except Exception as e: + current_app.logger.error(f"Error copying SMTP settings: {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 b3159d8..a45a761 100644 --- a/static/js/launch_progress.js +++ b/static/js/launch_progress.js @@ -172,6 +172,18 @@ function initializeSteps() { `; stepsContainer.appendChild(credentialsStep); + // Add Copy SMTP Settings step + const smtpStep = document.createElement('div'); + smtpStep.className = 'step-item'; + smtpStep.innerHTML = ` +
+Configuring email settings...
+| Property | +Value | +
|---|---|
| SMTP Host | +${smtpResult.data.smtp_host || 'Not set'} | +
| SMTP Port | +${smtpResult.data.smtp_port || 'Not set'} | +
| Security | +${smtpResult.data.smtp_security || 'None'} | +
| Username | +${smtpResult.data.smtp_username || 'Not set'} | +
| From Email | +${smtpResult.data.smtp_from_email || 'Not set'} | +
| From Name | +${smtpResult.data.smtp_from_name || 'Not set'} | +
| Status | +Copied Successfully | +