Spaces:
Sleeping
Sleeping
API
Browse files
app.py
CHANGED
@@ -142,12 +142,13 @@ async def health_check():
|
|
142 |
}
|
143 |
|
144 |
@app.post("/login")
|
145 |
-
async def login(customer_id: str):
|
146 |
"""
|
147 |
-
Login endpoint to validate customer ID
|
148 |
|
149 |
Parameters:
|
150 |
- customer_id: The ID of the customer to validate
|
|
|
151 |
|
152 |
Returns:
|
153 |
- JSON object containing login status and message
|
@@ -156,30 +157,42 @@ async def login(customer_id: str):
|
|
156 |
# Convert customer_id to string to match the format in purchase_history
|
157 |
customer_id = str(customer_id)
|
158 |
|
159 |
-
#
|
|
|
|
|
|
|
160 |
if customer_id in purchase_history['Customer_Id'].unique():
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
"
|
178 |
-
|
179 |
-
|
|
|
|
|
180 |
}
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
else:
|
184 |
return JSONResponse(
|
185 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
142 |
}
|
143 |
|
144 |
@app.post("/login")
|
145 |
+
async def login(customer_id: str, password: str):
|
146 |
"""
|
147 |
+
Login endpoint to validate customer ID and password
|
148 |
|
149 |
Parameters:
|
150 |
- customer_id: The ID of the customer to validate
|
151 |
+
- password: Password (first three chars of customer_id + "123")
|
152 |
|
153 |
Returns:
|
154 |
- JSON object containing login status and message
|
|
|
157 |
# Convert customer_id to string to match the format in purchase_history
|
158 |
customer_id = str(customer_id)
|
159 |
|
160 |
+
# Generate expected password (first three chars + "123")
|
161 |
+
expected_password = f"{customer_id[:3]}123"
|
162 |
+
|
163 |
+
# Check if customer exists and password matches
|
164 |
if customer_id in purchase_history['Customer_Id'].unique():
|
165 |
+
if password == expected_password:
|
166 |
+
# Get customer's basic information
|
167 |
+
customer_data = purchase_history[purchase_history['Customer_Id'] == customer_id]
|
168 |
+
total_purchases = len(customer_data)
|
169 |
+
total_spent = customer_data['Amount (In Dollars)'].sum()
|
170 |
+
|
171 |
+
# Convert last purchase date to datetime if it's not already
|
172 |
+
last_purchase = pd.to_datetime(customer_data['Purchase_Date'].max())
|
173 |
+
last_purchase_str = last_purchase.strftime('%Y-%m-%d')
|
174 |
+
|
175 |
+
return JSONResponse(
|
176 |
+
status_code=status.HTTP_200_OK,
|
177 |
+
content={
|
178 |
+
"status": "success",
|
179 |
+
"message": "Login successful",
|
180 |
+
"customer_id": customer_id,
|
181 |
+
"customer_stats": {
|
182 |
+
"total_purchases": total_purchases,
|
183 |
+
"total_spent": float(total_spent),
|
184 |
+
"last_purchase_date": last_purchase_str
|
185 |
+
}
|
186 |
}
|
187 |
+
)
|
188 |
+
else:
|
189 |
+
return JSONResponse(
|
190 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
191 |
+
content={
|
192 |
+
"status": "error",
|
193 |
+
"message": "Invalid password"
|
194 |
+
}
|
195 |
+
)
|
196 |
else:
|
197 |
return JSONResponse(
|
198 |
status_code=status.HTTP_401_UNAUTHORIZED,
|