Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -203,61 +203,43 @@ def login():
|
|
| 203 |
print(f"Login attempt with email: {email}") # Debug log
|
| 204 |
|
| 205 |
try:
|
| 206 |
-
# Fetch user details
|
| 207 |
query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
| 208 |
result = sf.query(query)
|
| 209 |
|
| 210 |
if result["records"]:
|
| 211 |
user = result["records"][0]
|
| 212 |
session['user_id'] = user['Id']
|
| 213 |
-
session['user_email'] = email
|
| 214 |
-
print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
|
| 215 |
-
|
| 216 |
-
user_name = user.get("Name", "") # Get the user's name
|
| 217 |
-
reward_points = user.get("Reward_Points__c") or 0 # Ensures reward_points is always an integer
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
|
|
|
| 220 |
|
|
|
|
| 221 |
if reward_points >= 500:
|
| 222 |
-
print(f"User {email} has {reward_points} reward points. Generating coupon...")
|
| 223 |
-
|
| 224 |
-
# Generate a new coupon code
|
| 225 |
new_coupon_code = generate_coupon_code()
|
| 226 |
-
|
| 227 |
-
# Check if user already has a record in Referral_Coupon__c
|
| 228 |
-
coupon_query = sf.query(f"""
|
| 229 |
-
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 230 |
-
""")
|
| 231 |
|
| 232 |
if coupon_query["records"]:
|
| 233 |
-
# If record exists, append the new coupon
|
| 234 |
coupon_record = coupon_query["records"][0]
|
| 235 |
referral_coupon_id = coupon_record["Id"]
|
| 236 |
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
| 237 |
|
| 238 |
-
# Append the new coupon on the next line
|
| 239 |
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
| 240 |
-
|
| 241 |
-
# Update the Referral_Coupon__c record
|
| 242 |
-
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 243 |
-
"Coupon_Code__c": updated_coupons
|
| 244 |
-
})
|
| 245 |
-
print(f"Updated existing coupon record for {email}. New Coupon: {new_coupon_code}")
|
| 246 |
else:
|
| 247 |
-
# If no record exists, create a new one with Referral_Name__c
|
| 248 |
sf.Referral_Coupon__c.create({
|
| 249 |
"Referral_Email__c": email,
|
| 250 |
-
"Name":
|
| 251 |
"Coupon_Code__c": new_coupon_code
|
| 252 |
})
|
| 253 |
-
print(f"Created new coupon record for {email} with name {user_name}. Coupon: {new_coupon_code}")
|
| 254 |
|
| 255 |
-
# Subtract 500 reward points from user's account
|
| 256 |
new_reward_points = reward_points - 500
|
| 257 |
-
sf.Customer_Login__c.update(user['Id'], {
|
| 258 |
-
"Reward_Points__c": new_reward_points
|
| 259 |
-
})
|
| 260 |
-
print(f"Coupon {new_coupon_code} generated and 500 points deducted. New balance: {new_reward_points}")
|
| 261 |
|
| 262 |
return redirect(url_for("menu"))
|
| 263 |
|
|
|
|
| 203 |
print(f"Login attempt with email: {email}") # Debug log
|
| 204 |
|
| 205 |
try:
|
| 206 |
+
# Fetch user details from Salesforce
|
| 207 |
query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
| 208 |
result = sf.query(query)
|
| 209 |
|
| 210 |
if result["records"]:
|
| 211 |
user = result["records"][0]
|
| 212 |
session['user_id'] = user['Id']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
# ✅ Always store or update session email
|
| 215 |
+
if 'user_email' not in session or session['user_email'] != email:
|
| 216 |
+
session['user_email'] = email
|
| 217 |
+
session['user_name'] = user.get("Name", "")
|
| 218 |
+
print(f"✅ Session email updated: {session['user_email']}")
|
| 219 |
|
| 220 |
+
reward_points = user.get("Reward_Points__c") or 0
|
| 221 |
|
| 222 |
+
# Coupon generation logic (if reward points >= 500)
|
| 223 |
if reward_points >= 500:
|
|
|
|
|
|
|
|
|
|
| 224 |
new_coupon_code = generate_coupon_code()
|
| 225 |
+
coupon_query = sf.query(f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
if coupon_query["records"]:
|
|
|
|
| 228 |
coupon_record = coupon_query["records"][0]
|
| 229 |
referral_coupon_id = coupon_record["Id"]
|
| 230 |
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
| 231 |
|
|
|
|
| 232 |
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
| 233 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {"Coupon_Code__c": updated_coupons})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
else:
|
|
|
|
| 235 |
sf.Referral_Coupon__c.create({
|
| 236 |
"Referral_Email__c": email,
|
| 237 |
+
"Name": user.get("Name", ""),
|
| 238 |
"Coupon_Code__c": new_coupon_code
|
| 239 |
})
|
|
|
|
| 240 |
|
|
|
|
| 241 |
new_reward_points = reward_points - 500
|
| 242 |
+
sf.Customer_Login__c.update(user['Id'], {"Reward_Points__c": new_reward_points})
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
return redirect(url_for("menu"))
|
| 245 |
|