Improve public pages
This commit is contained in:
@@ -85,20 +85,77 @@ def get_payment_service():
|
||||
async def register_bakery(
|
||||
bakery_data: BakeryRegistration,
|
||||
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
||||
tenant_service: EnhancedTenantService = Depends(get_enhanced_tenant_service)
|
||||
tenant_service: EnhancedTenantService = Depends(get_enhanced_tenant_service),
|
||||
payment_service: PaymentService = Depends(get_payment_service)
|
||||
):
|
||||
"""Register a new bakery/tenant with enhanced validation and features"""
|
||||
|
||||
try:
|
||||
# Validate coupon if provided
|
||||
coupon_validation = None
|
||||
if bakery_data.coupon_code:
|
||||
from app.core.config import settings
|
||||
database_manager = create_database_manager(settings.DATABASE_URL, "tenant-service")
|
||||
|
||||
async with database_manager.get_session() as session:
|
||||
# Temp tenant ID for validation (will be replaced with actual after creation)
|
||||
temp_tenant_id = f"temp_{current_user['user_id']}"
|
||||
|
||||
coupon_validation = payment_service.validate_coupon_code(
|
||||
bakery_data.coupon_code,
|
||||
temp_tenant_id,
|
||||
session
|
||||
)
|
||||
|
||||
if not coupon_validation["valid"]:
|
||||
logger.warning(
|
||||
"Invalid coupon code provided during registration",
|
||||
coupon_code=bakery_data.coupon_code,
|
||||
error=coupon_validation["error_message"]
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=coupon_validation["error_message"]
|
||||
)
|
||||
|
||||
# Create bakery/tenant
|
||||
result = await tenant_service.create_bakery(
|
||||
bakery_data,
|
||||
current_user["user_id"]
|
||||
)
|
||||
|
||||
# If coupon was validated, redeem it now with actual tenant_id
|
||||
if coupon_validation and coupon_validation["valid"]:
|
||||
from app.core.config import settings
|
||||
database_manager = create_database_manager(settings.DATABASE_URL, "tenant-service")
|
||||
|
||||
async with database_manager.get_session() as session:
|
||||
success, discount, error = payment_service.redeem_coupon(
|
||||
bakery_data.coupon_code,
|
||||
result.id,
|
||||
session
|
||||
)
|
||||
|
||||
if success:
|
||||
logger.info(
|
||||
"Coupon redeemed successfully",
|
||||
tenant_id=result.id,
|
||||
coupon_code=bakery_data.coupon_code,
|
||||
discount=discount
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
"Failed to redeem coupon after registration",
|
||||
tenant_id=result.id,
|
||||
coupon_code=bakery_data.coupon_code,
|
||||
error=error
|
||||
)
|
||||
|
||||
logger.info("Bakery registered successfully",
|
||||
name=bakery_data.name,
|
||||
owner_email=current_user.get('email'),
|
||||
tenant_id=result.id)
|
||||
tenant_id=result.id,
|
||||
coupon_applied=bakery_data.coupon_code is not None)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user