Create user_details.py
Browse files- user_details.py +91 -0
user_details.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Blueprint, session, redirect, url_for, flash, render_template, request, jsonify
|
| 2 |
+
from your_salesforce_module import sf # Import Salesforce connection
|
| 3 |
+
|
| 4 |
+
# Create blueprint for user details
|
| 5 |
+
user_details_blueprint = Blueprint('user_details', __name__)
|
| 6 |
+
|
| 7 |
+
# Route for customer details page
|
| 8 |
+
@user_details_blueprint.route("/customer_details", methods=["GET"])
|
| 9 |
+
def customer_details():
|
| 10 |
+
email = session.get('user_email') # Get logged-in user's email
|
| 11 |
+
if not email:
|
| 12 |
+
return redirect(url_for("login"))
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
# Fetch customer details from Salesforce based on the email
|
| 16 |
+
customer_record = sf.query(f"""
|
| 17 |
+
SELECT Id, Name, Email__c, Phone_Number__c, Referral__c, Reward_Points__c
|
| 18 |
+
FROM Customer_Login__c
|
| 19 |
+
WHERE Email__c = '{email}'
|
| 20 |
+
LIMIT 1
|
| 21 |
+
""")
|
| 22 |
+
|
| 23 |
+
if not customer_record.get("records"):
|
| 24 |
+
flash("Customer not found", "danger")
|
| 25 |
+
return redirect(url_for("login"))
|
| 26 |
+
|
| 27 |
+
customer = customer_record["records"][0]
|
| 28 |
+
|
| 29 |
+
# Prepare the data to return to the frontend
|
| 30 |
+
customer_data = {
|
| 31 |
+
"name": customer.get("Name", ""),
|
| 32 |
+
"email": customer.get("Email__c", ""),
|
| 33 |
+
"phone": customer.get("Phone_Number__c", ""),
|
| 34 |
+
"referral_code": customer.get("Referral__c", ""),
|
| 35 |
+
"reward_points": customer.get("Reward_Points__c", 0)
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return render_template("customer_details.html", customer=customer_data)
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
flash(f"Error fetching customer details: {str(e)}", "danger")
|
| 42 |
+
return redirect(url_for("login"))
|
| 43 |
+
|
| 44 |
+
# Route for updating the user profile
|
| 45 |
+
@user_details_blueprint.route("/update_profile", methods=["POST"])
|
| 46 |
+
def update_profile():
|
| 47 |
+
email = session.get('user_email') # Get logged-in user's email
|
| 48 |
+
if not email:
|
| 49 |
+
return jsonify({'status': 'error', 'message': 'User not logged in'})
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
# Fetch user details from Salesforce
|
| 53 |
+
result = sf.query(f"""
|
| 54 |
+
SELECT Id, Name, Email__c, Phone_Number__c, Referral__c, Reward_Points__c
|
| 55 |
+
FROM Customer_Login__c
|
| 56 |
+
WHERE Email__c = '{email}'
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
if not result['records']:
|
| 60 |
+
return jsonify({'status': 'error', 'message': 'User not found'})
|
| 61 |
+
|
| 62 |
+
user = result['records'][0]
|
| 63 |
+
user_id = user.get("Id")
|
| 64 |
+
|
| 65 |
+
# Get updated profile data from the form
|
| 66 |
+
new_name = request.form.get('customerName')
|
| 67 |
+
new_email = request.form.get('email')
|
| 68 |
+
new_phone = request.form.get('phone')
|
| 69 |
+
new_referral_code = request.form.get('referralCode')
|
| 70 |
+
new_reward_points = request.form.get('rewardPoints')
|
| 71 |
+
|
| 72 |
+
# Prepare data for Salesforce update
|
| 73 |
+
update_data = {
|
| 74 |
+
'Name': new_name,
|
| 75 |
+
'Email__c': new_email,
|
| 76 |
+
'Phone_Number__c': new_phone,
|
| 77 |
+
'Referral__c': new_referral_code,
|
| 78 |
+
'Reward_Points__c': new_reward_points
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
# Update Salesforce record
|
| 82 |
+
sf.Customer_Login__c.update(user_id, update_data)
|
| 83 |
+
|
| 84 |
+
return jsonify({
|
| 85 |
+
'status': 'success',
|
| 86 |
+
'message': 'Profile updated successfully!',
|
| 87 |
+
'data': update_data
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return jsonify({'status': 'error', 'message': str(e)})
|