Spaces:
Sleeping
Sleeping
changing to merchant_name
Browse files
app/controllers/merchant_controller.py
CHANGED
|
@@ -1,97 +1,97 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
from typing import Optional
|
| 3 |
-
from datetime import datetime, timedelta
|
| 4 |
-
from fastapi import APIRouter, HTTPException, Query, Depends, status, Body
|
| 5 |
-
from app.models.merchant import MerchantRegister
|
| 6 |
-
from app.services.merchant_services import get_otp_from_cache, verify_otp_and_save_merchant, validate_merchant
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Initialize router and logger
|
| 10 |
-
router = APIRouter(prefix="/merchant")
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@router.post("/register")
|
| 16 |
-
async def register_customer(user: MerchantRegister = Body(...)):
|
| 17 |
-
"""
|
| 18 |
-
API endpoint to register a new Merchant.
|
| 19 |
-
|
| 20 |
-
Args:
|
| 21 |
-
user (MerchantRegister): The details of the Merchant to register.
|
| 22 |
-
|
| 23 |
-
Returns:
|
| 24 |
-
dict: Confirmation message indicating email and SMS verification pending.
|
| 25 |
-
"""
|
| 26 |
-
try:
|
| 27 |
-
logger.info("Registering a new Merchant with Merchant name : %s, email: %s and mobile: %s", user.
|
| 28 |
-
|
| 29 |
-
# Validate email and mobile format
|
| 30 |
-
validation_result = await validate_merchant(user)
|
| 31 |
-
if validation_result["status"] == "error":
|
| 32 |
-
logger.error("Validation failed: %s", validation_result["errors"])
|
| 33 |
-
raise HTTPException(status_code=400, detail=validation_result["errors"])
|
| 34 |
-
|
| 35 |
-
return user.dict()
|
| 36 |
-
|
| 37 |
-
except HTTPException as e:
|
| 38 |
-
logger.error("Failed to register merchant: %s", e.detail)
|
| 39 |
-
raise e
|
| 40 |
-
except Exception as e:
|
| 41 |
-
logger.error("Unexpected error while registering merchant: %s", e)
|
| 42 |
-
raise HTTPException(status_code=500, detail="Failed to register Merchant") from e
|
| 43 |
-
|
| 44 |
-
@router.post("/otp/verify")
|
| 45 |
-
async def customer_otp_verification(user: MerchantRegister = Body(...)):
|
| 46 |
-
"""
|
| 47 |
-
API endpoint to validate OTPs and save the complete Merchant object.
|
| 48 |
-
|
| 49 |
-
Args:
|
| 50 |
-
user (MerchantRegister): The complete Merchant object including email, mobile, and OTPs.
|
| 51 |
-
|
| 52 |
-
Returns:
|
| 53 |
-
dict: Confirmation message indicating success or failure.
|
| 54 |
-
"""
|
| 55 |
-
try:
|
| 56 |
-
logger.info("Verifying OTPs and registering merchant with email: %s and mobile: %s", user.email, user.mobile)
|
| 57 |
-
# Verify OTPs and save customer
|
| 58 |
-
result = await verify_otp_and_save_merchant(user)
|
| 59 |
-
if result["status"] == "error":
|
| 60 |
-
raise HTTPException(status_code=400, detail=result["errors"])
|
| 61 |
-
|
| 62 |
-
return {"message": "Merchant successfully registered and verified."}
|
| 63 |
-
|
| 64 |
-
except HTTPException as e:
|
| 65 |
-
logger.error("Failed to verify and register merchant: %s", e.detail)
|
| 66 |
-
raise e
|
| 67 |
-
except Exception as e:
|
| 68 |
-
logger.error("Unexpected error while verifying and registering merchant: %s", e)
|
| 69 |
-
raise HTTPException(status_code=500, detail="Failed to verify and register merchant") from e
|
| 70 |
-
|
| 71 |
-
@router.get("/otp/get")
|
| 72 |
-
async def get_otps(key: str = Query(..., description="The email or mobile number to retrieve the OTP for")):
|
| 73 |
-
"""
|
| 74 |
-
API endpoint to retrieve the email or SMS OTP from Redis cache.
|
| 75 |
-
|
| 76 |
-
Args:
|
| 77 |
-
key (str): The email or mobile number to retrieve the OTP for.
|
| 78 |
-
|
| 79 |
-
Returns:
|
| 80 |
-
dict: The OTP data if found, or an error message if not found.
|
| 81 |
-
"""
|
| 82 |
-
try:
|
| 83 |
-
logger.info("Retrieving OTP for key: %s", key)
|
| 84 |
-
|
| 85 |
-
# Retrieve OTP from Redis
|
| 86 |
-
otp_data = await get_otp_from_cache(key)
|
| 87 |
-
if not otp_data:
|
| 88 |
-
raise HTTPException(status_code=404, detail="OTP not found or expired")
|
| 89 |
-
|
| 90 |
-
return {"key": key, "otp_data": otp_data}
|
| 91 |
-
|
| 92 |
-
except HTTPException as e:
|
| 93 |
-
logger.error("Failed to retrieve OTP for key %s: %s", key, e.detail)
|
| 94 |
-
raise e
|
| 95 |
-
except Exception as e:
|
| 96 |
-
logger.error("Unexpected error while retrieving OTP for key %s: %s", key, e)
|
| 97 |
raise HTTPException(status_code=500, detail="Failed to retrieve OTP") from e
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
from fastapi import APIRouter, HTTPException, Query, Depends, status, Body
|
| 5 |
+
from app.models.merchant import MerchantRegister
|
| 6 |
+
from app.services.merchant_services import get_otp_from_cache, verify_otp_and_save_merchant, validate_merchant
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Initialize router and logger
|
| 10 |
+
router = APIRouter(prefix="/merchant")
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@router.post("/register")
|
| 16 |
+
async def register_customer(user: MerchantRegister = Body(...)):
|
| 17 |
+
"""
|
| 18 |
+
API endpoint to register a new Merchant.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
user (MerchantRegister): The details of the Merchant to register.
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
dict: Confirmation message indicating email and SMS verification pending.
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
logger.info("Registering a new Merchant with Merchant name : %s, email: %s and mobile: %s", user.merchant_name, user.email, user.mobile)
|
| 28 |
+
|
| 29 |
+
# Validate email and mobile format
|
| 30 |
+
validation_result = await validate_merchant(user)
|
| 31 |
+
if validation_result["status"] == "error":
|
| 32 |
+
logger.error("Validation failed: %s", validation_result["errors"])
|
| 33 |
+
raise HTTPException(status_code=400, detail=validation_result["errors"])
|
| 34 |
+
|
| 35 |
+
return user.dict()
|
| 36 |
+
|
| 37 |
+
except HTTPException as e:
|
| 38 |
+
logger.error("Failed to register merchant: %s", e.detail)
|
| 39 |
+
raise e
|
| 40 |
+
except Exception as e:
|
| 41 |
+
logger.error("Unexpected error while registering merchant: %s", e)
|
| 42 |
+
raise HTTPException(status_code=500, detail="Failed to register Merchant") from e
|
| 43 |
+
|
| 44 |
+
@router.post("/otp/verify")
|
| 45 |
+
async def customer_otp_verification(user: MerchantRegister = Body(...)):
|
| 46 |
+
"""
|
| 47 |
+
API endpoint to validate OTPs and save the complete Merchant object.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
user (MerchantRegister): The complete Merchant object including email, mobile, and OTPs.
|
| 51 |
+
|
| 52 |
+
Returns:
|
| 53 |
+
dict: Confirmation message indicating success or failure.
|
| 54 |
+
"""
|
| 55 |
+
try:
|
| 56 |
+
logger.info("Verifying OTPs and registering merchant with email: %s and mobile: %s", user.email, user.mobile)
|
| 57 |
+
# Verify OTPs and save customer
|
| 58 |
+
result = await verify_otp_and_save_merchant(user)
|
| 59 |
+
if result["status"] == "error":
|
| 60 |
+
raise HTTPException(status_code=400, detail=result["errors"])
|
| 61 |
+
|
| 62 |
+
return {"message": "Merchant successfully registered and verified."}
|
| 63 |
+
|
| 64 |
+
except HTTPException as e:
|
| 65 |
+
logger.error("Failed to verify and register merchant: %s", e.detail)
|
| 66 |
+
raise e
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logger.error("Unexpected error while verifying and registering merchant: %s", e)
|
| 69 |
+
raise HTTPException(status_code=500, detail="Failed to verify and register merchant") from e
|
| 70 |
+
|
| 71 |
+
@router.get("/otp/get")
|
| 72 |
+
async def get_otps(key: str = Query(..., description="The email or mobile number to retrieve the OTP for")):
|
| 73 |
+
"""
|
| 74 |
+
API endpoint to retrieve the email or SMS OTP from Redis cache.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
key (str): The email or mobile number to retrieve the OTP for.
|
| 78 |
+
|
| 79 |
+
Returns:
|
| 80 |
+
dict: The OTP data if found, or an error message if not found.
|
| 81 |
+
"""
|
| 82 |
+
try:
|
| 83 |
+
logger.info("Retrieving OTP for key: %s", key)
|
| 84 |
+
|
| 85 |
+
# Retrieve OTP from Redis
|
| 86 |
+
otp_data = await get_otp_from_cache(key)
|
| 87 |
+
if not otp_data:
|
| 88 |
+
raise HTTPException(status_code=404, detail="OTP not found or expired")
|
| 89 |
+
|
| 90 |
+
return {"key": key, "otp_data": otp_data}
|
| 91 |
+
|
| 92 |
+
except HTTPException as e:
|
| 93 |
+
logger.error("Failed to retrieve OTP for key %s: %s", key, e.detail)
|
| 94 |
+
raise e
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.error("Unexpected error while retrieving OTP for key %s: %s", key, e)
|
| 97 |
raise HTTPException(status_code=500, detail="Failed to retrieve OTP") from e
|