vendor features for admin and vendor admin area
This commit is contained in:
128
app/api/v1/public/vendors/vendors.py
vendored
Normal file
128
app/api/v1/public/vendors/vendors.py
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
# app/api/v1/public/vendors/vendors.py
|
||||
"""
|
||||
Public vendor information endpoints.
|
||||
|
||||
Provides public-facing vendor lookup and information retrieval.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from models.database.vendor import Vendor
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("/by-code/{vendor_code}")
|
||||
def get_vendor_by_code(
|
||||
vendor_code: str = Path(..., description="Vendor code (e.g., TECHSTORE233)"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Get public vendor information by vendor code.
|
||||
|
||||
This endpoint is used by:
|
||||
- Frontend vendor login page to validate vendor existence
|
||||
- Customer shop to display vendor information
|
||||
|
||||
Returns basic vendor information (no sensitive data).
|
||||
"""
|
||||
vendor = db.query(Vendor).filter(
|
||||
Vendor.vendor_code == vendor_code.upper(),
|
||||
Vendor.is_active == True
|
||||
).first()
|
||||
|
||||
if not vendor:
|
||||
logger.warning(f"Vendor lookup failed for code: {vendor_code}")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Vendor '{vendor_code}' not found or inactive"
|
||||
)
|
||||
|
||||
logger.info(f"Vendor lookup successful: {vendor.vendor_code}")
|
||||
|
||||
# Return public vendor information (no sensitive data)
|
||||
return {
|
||||
"id": vendor.id,
|
||||
"vendor_code": vendor.vendor_code,
|
||||
"subdomain": vendor.subdomain,
|
||||
"name": vendor.name,
|
||||
"description": vendor.description,
|
||||
"website": vendor.website,
|
||||
"is_active": vendor.is_active,
|
||||
"is_verified": vendor.is_verified
|
||||
}
|
||||
|
||||
|
||||
@router.get("/by-subdomain/{subdomain}")
|
||||
def get_vendor_by_subdomain(
|
||||
subdomain: str = Path(..., description="Vendor subdomain (e.g., techstore233)"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Get public vendor information by subdomain.
|
||||
|
||||
Used for subdomain-based vendor detection in production environments.
|
||||
Example: techstore233.platform.com -> subdomain is "techstore233"
|
||||
"""
|
||||
vendor = db.query(Vendor).filter(
|
||||
Vendor.subdomain == subdomain.lower(),
|
||||
Vendor.is_active == True
|
||||
).first()
|
||||
|
||||
if not vendor:
|
||||
logger.warning(f"Vendor lookup failed for subdomain: {subdomain}")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Vendor with subdomain '{subdomain}' not found or inactive"
|
||||
)
|
||||
|
||||
logger.info(f"Vendor lookup by subdomain successful: {vendor.vendor_code}")
|
||||
|
||||
return {
|
||||
"id": vendor.id,
|
||||
"vendor_code": vendor.vendor_code,
|
||||
"subdomain": vendor.subdomain,
|
||||
"name": vendor.name,
|
||||
"description": vendor.description,
|
||||
"website": vendor.website,
|
||||
"is_active": vendor.is_active,
|
||||
"is_verified": vendor.is_verified
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{vendor_id}/info")
|
||||
def get_vendor_info(
|
||||
vendor_id: int = Path(..., description="Vendor ID"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Get public vendor information by ID.
|
||||
|
||||
Used when vendor_id is already known (e.g., from URL parameters).
|
||||
"""
|
||||
vendor = db.query(Vendor).filter(
|
||||
Vendor.id == vendor_id,
|
||||
Vendor.is_active == True
|
||||
).first()
|
||||
|
||||
if not vendor:
|
||||
logger.warning(f"Vendor lookup failed for ID: {vendor_id}")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Vendor with ID {vendor_id} not found or inactive"
|
||||
)
|
||||
|
||||
return {
|
||||
"id": vendor.id,
|
||||
"vendor_code": vendor.vendor_code,
|
||||
"subdomain": vendor.subdomain,
|
||||
"name": vendor.name,
|
||||
"description": vendor.description,
|
||||
"website": vendor.website,
|
||||
"is_active": vendor.is_active,
|
||||
"is_verified": vendor.is_verified
|
||||
}
|
||||
Reference in New Issue
Block a user