Vaibhav84 commited on
Commit
3d9c6c2
·
1 Parent(s): 2154961
Files changed (1) hide show
  1. app.py +37 -24
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
- # Check if customer exists in the purchase history
 
 
 
160
  if customer_id in purchase_history['Customer_Id'].unique():
161
- # Get customer's basic information
162
- customer_data = purchase_history[purchase_history['Customer_Id'] == customer_id]
163
- total_purchases = len(customer_data)
164
- total_spent = customer_data['Amount (In Dollars)'].sum()
165
-
166
- # Convert last purchase date to datetime if it's not already
167
- last_purchase = pd.to_datetime(customer_data['Purchase_Date'].max())
168
- last_purchase_str = last_purchase.strftime('%Y-%m-%d')
169
-
170
- return JSONResponse(
171
- status_code=status.HTTP_200_OK,
172
- content={
173
- "status": "success",
174
- "message": "Login successful",
175
- "customer_id": customer_id,
176
- "customer_stats": {
177
- "total_purchases": total_purchases,
178
- "total_spent": float(total_spent),
179
- "last_purchase_date": last_purchase_str
 
 
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,