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