| 
							 | 
						from flask import Blueprint, session, redirect, url_for, flash, render_template, request, jsonify | 
					
					
						
						| 
							 | 
						from salesforce import get_salesforce_connection | 
					
					
						
						| 
							 | 
						sf = get_salesforce_connection()  | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						user_details_blueprint = Blueprint('user_details', __name__) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						@user_details_blueprint.route("/customer_details", methods=["GET"]) | 
					
					
						
						| 
							 | 
						def customer_details(): | 
					
					
						
						| 
							 | 
						    email = session.get('user_email')   | 
					
					
						
						| 
							 | 
						    if not email: | 
					
					
						
						| 
							 | 
						        return redirect(url_for("login")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						@user_details_blueprint.route("/update_profile", methods=["POST"]) | 
					
					
						
						| 
							 | 
						def update_profile(): | 
					
					
						
						| 
							 | 
						    email = session.get('user_email')   | 
					
					
						
						| 
							 | 
						    if not email: | 
					
					
						
						| 
							 | 
						        return jsonify({'status': 'error', 'message': 'User not logged in'}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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') | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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 | 
					
					
						
						| 
							 | 
						        } | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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)}) | 
					
					
						
						| 
							 | 
						
 |