286 lines
8.0 KiB
Python
286 lines
8.0 KiB
Python
# models/schema/vendor.py
|
|
"""
|
|
Pydantic schemas for Vendor-related operations.
|
|
|
|
Schemas include:
|
|
- VendorCreate: For creating vendors with owner accounts
|
|
- VendorUpdate: For updating vendor information (Admin only)
|
|
- VendorResponse: Standard vendor response
|
|
- VendorDetailResponse: Vendor response with owner details
|
|
- VendorCreateResponse: Response after vendor creation (includes credentials)
|
|
- VendorListResponse: Paginated vendor list
|
|
- VendorSummary: Lightweight vendor info
|
|
- VendorTransferOwnership: For transferring vendor ownership
|
|
"""
|
|
|
|
import re
|
|
from datetime import datetime
|
|
from typing import Dict, List, Optional, Any
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
class VendorCreate(BaseModel):
|
|
"""Schema for creating a new vendor with owner account."""
|
|
|
|
# Basic Information
|
|
vendor_code: str = Field(
|
|
...,
|
|
description="Unique vendor identifier (e.g., TECHSTORE)",
|
|
min_length=2,
|
|
max_length=50
|
|
)
|
|
subdomain: str = Field(
|
|
...,
|
|
description="Unique subdomain for the vendor",
|
|
min_length=2,
|
|
max_length=100
|
|
)
|
|
name: str = Field(
|
|
...,
|
|
description="Display name of the vendor",
|
|
min_length=2,
|
|
max_length=255
|
|
)
|
|
description: Optional[str] = Field(None, description="Vendor description")
|
|
|
|
# Owner Information (Creates User Account)
|
|
owner_email: str = Field(
|
|
...,
|
|
description="Email for the vendor owner (used for login and authentication)"
|
|
)
|
|
|
|
# Business Contact Information (Vendor Fields)
|
|
contact_email: Optional[str] = Field(
|
|
None,
|
|
description="Public business contact email (defaults to owner_email if not provided)"
|
|
)
|
|
contact_phone: Optional[str] = Field(None, description="Contact phone number")
|
|
website: Optional[str] = Field(None, description="Website URL")
|
|
|
|
# Business Details
|
|
business_address: Optional[str] = Field(None, description="Business address")
|
|
tax_number: Optional[str] = Field(None, description="Tax/VAT number")
|
|
|
|
# Marketplace URLs (multi-language support)
|
|
letzshop_csv_url_fr: Optional[str] = Field(None, description="French CSV URL")
|
|
letzshop_csv_url_en: Optional[str] = Field(None, description="English CSV URL")
|
|
letzshop_csv_url_de: Optional[str] = Field(None, description="German CSV URL")
|
|
|
|
@field_validator("owner_email", "contact_email")
|
|
@classmethod
|
|
def validate_emails(cls, v):
|
|
"""Validate email format and normalize to lowercase."""
|
|
if v and ("@" not in v or "." not in v):
|
|
raise ValueError("Invalid email format")
|
|
return v.lower() if v else v
|
|
|
|
@field_validator("subdomain")
|
|
@classmethod
|
|
def validate_subdomain(cls, v):
|
|
"""Validate subdomain format: lowercase alphanumeric with hyphens."""
|
|
if v and not re.match(r'^[a-z0-9][a-z0-9-]*[a-z0-9]$', v):
|
|
raise ValueError("Subdomain must contain only lowercase letters, numbers, and hyphens")
|
|
return v.lower() if v else v
|
|
|
|
@field_validator("vendor_code")
|
|
@classmethod
|
|
def validate_vendor_code(cls, v):
|
|
"""Ensure vendor code is uppercase for consistency."""
|
|
return v.upper() if v else v
|
|
|
|
|
|
class VendorUpdate(BaseModel):
|
|
"""
|
|
Schema for updating vendor information (Admin only).
|
|
|
|
Note: owner_email is NOT included here. To change the owner,
|
|
use the transfer-ownership endpoint instead.
|
|
"""
|
|
|
|
# Basic Information
|
|
name: Optional[str] = Field(None, min_length=2, max_length=255)
|
|
description: Optional[str] = None
|
|
subdomain: Optional[str] = Field(None, min_length=2, max_length=100)
|
|
|
|
# Business Contact Information (Vendor Fields)
|
|
contact_email: Optional[str] = Field(
|
|
None,
|
|
description="Public business contact email"
|
|
)
|
|
contact_phone: Optional[str] = None
|
|
website: Optional[str] = None
|
|
|
|
# Business Details
|
|
business_address: Optional[str] = None
|
|
tax_number: Optional[str] = None
|
|
|
|
# Marketplace URLs
|
|
letzshop_csv_url_fr: Optional[str] = None
|
|
letzshop_csv_url_en: Optional[str] = None
|
|
letzshop_csv_url_de: Optional[str] = None
|
|
|
|
# Status (Admin only)
|
|
is_active: Optional[bool] = None
|
|
is_verified: Optional[bool] = None
|
|
|
|
@field_validator("subdomain")
|
|
@classmethod
|
|
def subdomain_lowercase(cls, v):
|
|
"""Normalize subdomain to lowercase."""
|
|
return v.lower().strip() if v else v
|
|
|
|
@field_validator("contact_email")
|
|
@classmethod
|
|
def validate_contact_email(cls, v):
|
|
"""Validate contact email format."""
|
|
if v and ("@" not in v or "." not in v):
|
|
raise ValueError("Invalid email format")
|
|
return v.lower() if v else v
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
class VendorResponse(BaseModel):
|
|
"""Standard schema for vendor response data."""
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
vendor_code: str
|
|
subdomain: str
|
|
name: str
|
|
description: Optional[str]
|
|
owner_user_id: int
|
|
|
|
# Contact Information (Business)
|
|
contact_email: Optional[str]
|
|
contact_phone: Optional[str]
|
|
website: Optional[str]
|
|
|
|
# Business Information
|
|
business_address: Optional[str]
|
|
tax_number: Optional[str]
|
|
|
|
# Marketplace URLs
|
|
letzshop_csv_url_fr: Optional[str]
|
|
letzshop_csv_url_en: Optional[str]
|
|
letzshop_csv_url_de: Optional[str]
|
|
|
|
# Status Flags
|
|
is_active: bool
|
|
is_verified: bool
|
|
|
|
# Timestamps
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
|
|
class VendorDetailResponse(VendorResponse):
|
|
"""
|
|
Extended vendor response including owner information.
|
|
|
|
Includes both:
|
|
- contact_email (business contact)
|
|
- owner_email (owner's authentication email)
|
|
"""
|
|
|
|
owner_email: str = Field(
|
|
...,
|
|
description="Email of the vendor owner (for login/authentication)"
|
|
)
|
|
owner_username: str = Field(
|
|
...,
|
|
description="Username of the vendor owner"
|
|
)
|
|
|
|
|
|
class VendorCreateResponse(VendorDetailResponse):
|
|
"""
|
|
Response after creating vendor - includes generated credentials.
|
|
|
|
IMPORTANT: temporary_password is shown only once!
|
|
"""
|
|
|
|
temporary_password: str = Field(
|
|
...,
|
|
description="Temporary password for owner (SHOWN ONLY ONCE)"
|
|
)
|
|
login_url: Optional[str] = Field(
|
|
None,
|
|
description="URL for vendor owner to login"
|
|
)
|
|
|
|
|
|
class VendorListResponse(BaseModel):
|
|
"""Schema for paginated vendor list."""
|
|
vendors: List[VendorResponse]
|
|
total: int
|
|
skip: int
|
|
limit: int
|
|
|
|
|
|
class VendorSummary(BaseModel):
|
|
"""Lightweight vendor summary for dropdowns and quick references."""
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
vendor_code: str
|
|
subdomain: str
|
|
name: str
|
|
is_active: bool
|
|
|
|
|
|
class VendorTransferOwnership(BaseModel):
|
|
"""
|
|
Schema for transferring vendor ownership to another user.
|
|
|
|
This is a critical operation that requires:
|
|
- Confirmation flag
|
|
- Reason for audit trail (optional)
|
|
"""
|
|
|
|
new_owner_user_id: int = Field(
|
|
...,
|
|
description="ID of the user who will become the new owner",
|
|
gt=0
|
|
)
|
|
|
|
confirm_transfer: bool = Field(
|
|
...,
|
|
description="Must be true to confirm ownership transfer"
|
|
)
|
|
|
|
transfer_reason: Optional[str] = Field(
|
|
None,
|
|
max_length=500,
|
|
description="Reason for ownership transfer (for audit logs)"
|
|
)
|
|
|
|
@field_validator("confirm_transfer")
|
|
@classmethod
|
|
def validate_confirmation(cls, v):
|
|
"""Ensure confirmation is explicitly true."""
|
|
if not v:
|
|
raise ValueError("Ownership transfer requires explicit confirmation")
|
|
return v
|
|
|
|
|
|
class VendorTransferOwnershipResponse(BaseModel):
|
|
"""Response after successful ownership transfer."""
|
|
|
|
message: str
|
|
vendor_id: int
|
|
vendor_code: str
|
|
vendor_name: str
|
|
|
|
old_owner: Dict[str, Any] = Field(
|
|
...,
|
|
description="Information about the previous owner"
|
|
)
|
|
new_owner: Dict[str, Any] = Field(
|
|
...,
|
|
description="Information about the new owner"
|
|
)
|
|
|
|
transferred_at: datetime
|
|
transfer_reason: Optional[str]
|