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)})