Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -120,76 +120,69 @@ def signup():
|
|
120 |
|
121 |
|
122 |
|
123 |
-
@app.route("/
|
124 |
-
def
|
125 |
-
|
126 |
-
email = request.form.get("email")
|
127 |
-
password = request.form.get("password")
|
128 |
-
print(f"Login attempt with email: {email}") # Debug log
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
result = sf.query(query)
|
133 |
-
|
134 |
-
if result["records"]:
|
135 |
-
user = result["records"][0]
|
136 |
-
session['user_id'] = user['Id']
|
137 |
-
session['user_email'] = email
|
138 |
-
print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
|
139 |
-
|
140 |
-
reward_points = user.get("Reward_Points__c", 0)
|
141 |
-
|
142 |
-
if reward_points >= 500:
|
143 |
-
print(f"User {email} has {reward_points} reward points. Generating coupon...")
|
144 |
-
|
145 |
-
# Generate new coupon code
|
146 |
-
new_coupon_code = generate_coupon_code()
|
147 |
-
|
148 |
-
# Check if user already has a record in Referral_Coupon__c
|
149 |
-
coupon_query = sf.query(f"""
|
150 |
-
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
151 |
-
""")
|
152 |
-
|
153 |
-
if coupon_query["records"]:
|
154 |
-
# Append the new coupon to existing ones
|
155 |
-
coupon_record = coupon_query["records"][0]
|
156 |
-
referral_coupon_id = coupon_record["Id"]
|
157 |
-
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
158 |
-
|
159 |
-
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
160 |
-
|
161 |
-
# Update the Referral_Coupon__c record
|
162 |
-
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
163 |
-
"Coupon_Code__c": updated_coupons
|
164 |
-
})
|
165 |
-
else:
|
166 |
-
# Create a new record for the user
|
167 |
-
sf.Referral_Coupon__c.create({
|
168 |
-
"Referral_Email__c": email,
|
169 |
-
"Coupon_Code__c": new_coupon_code
|
170 |
-
})
|
171 |
-
|
172 |
-
# Subtract 500 reward points from user's account
|
173 |
-
new_reward_points = reward_points - 500
|
174 |
-
sf.Customer_Login__c.update(user['Id'], {
|
175 |
-
"Reward_Points__c": new_reward_points
|
176 |
-
})
|
177 |
-
print(f"Coupon {new_coupon_code} generated and 500 points deducted. New balance: {new_reward_points}")
|
178 |
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
return render_template("login.html", error="Invalid credentials!")
|
184 |
|
185 |
-
|
186 |
-
print(f"Error during login: {str(e)}")
|
187 |
-
return render_template("login.html", error=f"Error: {str(e)}")
|
188 |
|
189 |
-
|
|
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
-
return render_template("login.html")
|
193 |
@app.route("/menu", methods=["GET", "POST"])
|
194 |
def menu():
|
195 |
selected_category = request.args.get("category", "All")
|
|
|
120 |
|
121 |
|
122 |
|
123 |
+
@app.route("/redeem_coupon", methods=["POST"])
|
124 |
+
def redeem_coupon():
|
125 |
+
email = session.get('user_email')
|
|
|
|
|
|
|
126 |
|
127 |
+
if not email:
|
128 |
+
return jsonify({"success": False, "message": "User not logged in"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
+
try:
|
131 |
+
# Fetch reward points
|
132 |
+
customer_record = sf.query(f"""
|
133 |
+
SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'
|
134 |
+
""")
|
135 |
+
customer = customer_record.get("records", [])[0] if customer_record else None
|
136 |
|
137 |
+
if not customer:
|
138 |
+
return jsonify({"success": False, "message": "User not found"})
|
|
|
139 |
|
140 |
+
reward_points = customer.get("Reward_Points__c", 0)
|
|
|
|
|
141 |
|
142 |
+
if reward_points < 500:
|
143 |
+
return jsonify({"success": False, "message": "Not enough reward points to redeem a coupon"})
|
144 |
|
145 |
+
# Generate a new coupon code
|
146 |
+
new_coupon_code = generate_coupon_code()
|
147 |
+
|
148 |
+
# Check if user already has a record in Referral_Coupon__c
|
149 |
+
coupon_query = sf.query(f"""
|
150 |
+
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
151 |
+
""")
|
152 |
+
|
153 |
+
if coupon_query["records"]:
|
154 |
+
# β
Case 1: User already has a referral coupon record β Append new coupon
|
155 |
+
coupon_record = coupon_query["records"][0]
|
156 |
+
referral_coupon_id = coupon_record["Id"]
|
157 |
+
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
158 |
+
|
159 |
+
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
160 |
+
|
161 |
+
# β
Update the Referral_Coupon__c record
|
162 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
163 |
+
"Coupon_Code__c": updated_coupons
|
164 |
+
})
|
165 |
+
print(f"β
Coupon appended for existing user: {email}")
|
166 |
+
else:
|
167 |
+
# β Case 2: No record found β Create a new record in Referral_Coupon__c
|
168 |
+
sf.Referral_Coupon__c.create({
|
169 |
+
"Referral_Email__c": email,
|
170 |
+
"Coupon_Code__c": new_coupon_code
|
171 |
+
})
|
172 |
+
print(f"β
New Referral_Coupon__c record created for {email}")
|
173 |
+
|
174 |
+
# β
Deduct 500 reward points
|
175 |
+
new_reward_points = reward_points - 500
|
176 |
+
sf.Customer_Login__c.update(customer['Id'], {
|
177 |
+
"Reward_Points__c": new_reward_points
|
178 |
+
})
|
179 |
+
print(f"β
Reward points updated: New balance = {new_reward_points}")
|
180 |
+
|
181 |
+
return jsonify({"success": True, "message": f"Coupon {new_coupon_code} generated!", "new_reward_points": new_reward_points})
|
182 |
+
|
183 |
+
except Exception as e:
|
184 |
+
return jsonify({"success": False, "error": str(e)})
|
185 |
|
|
|
186 |
@app.route("/menu", methods=["GET", "POST"])
|
187 |
def menu():
|
188 |
selected_category = request.args.get("category", "All")
|