Spaces:
Sleeping
Sleeping
Ajit Panday
commited on
Commit
·
974ed50
1
Parent(s):
c83ffdd
Fix customer list endpoint and add database health check
Browse files- app/auth.py +13 -2
- main.py +66 -15
app/auth.py
CHANGED
|
@@ -145,7 +145,7 @@ async def create_customer(
|
|
| 145 |
detail=f"Error creating customer: {str(e)}"
|
| 146 |
)
|
| 147 |
|
| 148 |
-
@router.get("/customers/", response_model=List[
|
| 149 |
async def list_customers(
|
| 150 |
db: Session = Depends(get_db),
|
| 151 |
current_admin: str = Depends(get_current_admin)
|
|
@@ -153,7 +153,18 @@ async def list_customers(
|
|
| 153 |
"""List all customers"""
|
| 154 |
try:
|
| 155 |
customers = db.query(Customer).all()
|
| 156 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
print(f"Error listing customers: {str(e)}")
|
| 159 |
raise HTTPException(
|
|
|
|
| 145 |
detail=f"Error creating customer: {str(e)}"
|
| 146 |
)
|
| 147 |
|
| 148 |
+
@router.get("/customers/", response_model=List[Customer])
|
| 149 |
async def list_customers(
|
| 150 |
db: Session = Depends(get_db),
|
| 151 |
current_admin: str = Depends(get_current_admin)
|
|
|
|
| 153 |
"""List all customers"""
|
| 154 |
try:
|
| 155 |
customers = db.query(Customer).all()
|
| 156 |
+
return [
|
| 157 |
+
Customer(
|
| 158 |
+
id=customer.id,
|
| 159 |
+
name=customer.name,
|
| 160 |
+
company_name=customer.company_name,
|
| 161 |
+
email=customer.email,
|
| 162 |
+
api_key=customer.api_key,
|
| 163 |
+
is_active=customer.is_active,
|
| 164 |
+
created_at=customer.created_at,
|
| 165 |
+
updated_at=customer.updated_at
|
| 166 |
+
) for customer in customers
|
| 167 |
+
]
|
| 168 |
except Exception as e:
|
| 169 |
print(f"Error listing customers: {str(e)}")
|
| 170 |
raise HTTPException(
|
main.py
CHANGED
|
@@ -264,21 +264,52 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
|
| 264 |
|
| 265 |
@app.post("/api/v1/customers/", response_model=Customer)
|
| 266 |
async def create_customer(
|
| 267 |
-
customer:
|
| 268 |
db: Session = Depends(get_db),
|
| 269 |
current_admin: str = Depends(get_current_admin)
|
| 270 |
):
|
| 271 |
"""Create new customer"""
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
@app.get("/api/v1/calls", response_model=List[CallRecord])
|
| 284 |
async def get_calls(
|
|
@@ -306,9 +337,20 @@ async def search_calls(
|
|
| 306 |
return customer.search_calls(query)
|
| 307 |
|
| 308 |
@app.get("/api/v1/health")
|
| 309 |
-
async def health_check():
|
| 310 |
-
"""Health check endpoint"""
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
@app.get("/api/v1/customers", response_model=List[Customer])
|
| 314 |
async def list_customers(
|
|
@@ -318,7 +360,16 @@ async def list_customers(
|
|
| 318 |
"""List all customers"""
|
| 319 |
try:
|
| 320 |
customers = db.query(CustomerModel).all()
|
| 321 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
except Exception as e:
|
| 323 |
raise HTTPException(
|
| 324 |
status_code=500,
|
|
|
|
| 264 |
|
| 265 |
@app.post("/api/v1/customers/", response_model=Customer)
|
| 266 |
async def create_customer(
|
| 267 |
+
customer: CustomerCreate,
|
| 268 |
db: Session = Depends(get_db),
|
| 269 |
current_admin: str = Depends(get_current_admin)
|
| 270 |
):
|
| 271 |
"""Create new customer"""
|
| 272 |
+
try:
|
| 273 |
+
# Check if email already exists
|
| 274 |
+
existing_customer = db.query(CustomerModel).filter(CustomerModel.email == customer.email).first()
|
| 275 |
+
if existing_customer:
|
| 276 |
+
raise HTTPException(
|
| 277 |
+
status_code=400,
|
| 278 |
+
detail="Email already registered"
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
# Create new customer
|
| 282 |
+
db_customer = CustomerModel(
|
| 283 |
+
name=customer.name,
|
| 284 |
+
company_name=customer.company_name,
|
| 285 |
+
email=customer.email,
|
| 286 |
+
api_key=str(uuid.uuid4()),
|
| 287 |
+
is_active=True,
|
| 288 |
+
created_at=datetime.utcnow(),
|
| 289 |
+
updated_at=datetime.utcnow()
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
db.add(db_customer)
|
| 293 |
+
db.commit()
|
| 294 |
+
db.refresh(db_customer)
|
| 295 |
+
|
| 296 |
+
return Customer(
|
| 297 |
+
id=db_customer.id,
|
| 298 |
+
name=db_customer.name,
|
| 299 |
+
company_name=db_customer.company_name,
|
| 300 |
+
email=db_customer.email,
|
| 301 |
+
api_key=db_customer.api_key,
|
| 302 |
+
is_active=db_customer.is_active,
|
| 303 |
+
created_at=db_customer.created_at,
|
| 304 |
+
updated_at=db_customer.updated_at
|
| 305 |
+
)
|
| 306 |
+
except HTTPException as he:
|
| 307 |
+
raise he
|
| 308 |
+
except Exception as e:
|
| 309 |
+
raise HTTPException(
|
| 310 |
+
status_code=500,
|
| 311 |
+
detail=f"Failed to create customer: {str(e)}"
|
| 312 |
+
)
|
| 313 |
|
| 314 |
@app.get("/api/v1/calls", response_model=List[CallRecord])
|
| 315 |
async def get_calls(
|
|
|
|
| 337 |
return customer.search_calls(query)
|
| 338 |
|
| 339 |
@app.get("/api/v1/health")
|
| 340 |
+
async def health_check(db: Session = Depends(get_db)):
|
| 341 |
+
"""Health check endpoint to verify database connection"""
|
| 342 |
+
try:
|
| 343 |
+
# Test database connection
|
| 344 |
+
db.execute("SELECT 1")
|
| 345 |
+
return {
|
| 346 |
+
"status": "healthy",
|
| 347 |
+
"database": "connected"
|
| 348 |
+
}
|
| 349 |
+
except Exception as e:
|
| 350 |
+
raise HTTPException(
|
| 351 |
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
| 352 |
+
detail=f"Database connection error: {str(e)}"
|
| 353 |
+
)
|
| 354 |
|
| 355 |
@app.get("/api/v1/customers", response_model=List[Customer])
|
| 356 |
async def list_customers(
|
|
|
|
| 360 |
"""List all customers"""
|
| 361 |
try:
|
| 362 |
customers = db.query(CustomerModel).all()
|
| 363 |
+
return [Customer(
|
| 364 |
+
id=c.id,
|
| 365 |
+
name=c.name,
|
| 366 |
+
company_name=c.company_name,
|
| 367 |
+
email=c.email,
|
| 368 |
+
api_key=c.api_key,
|
| 369 |
+
is_active=c.is_active,
|
| 370 |
+
created_at=c.created_at,
|
| 371 |
+
updated_at=c.updated_at
|
| 372 |
+
) for c in customers]
|
| 373 |
except Exception as e:
|
| 374 |
raise HTTPException(
|
| 375 |
status_code=500,
|