- New models: WarrantyModel, WalletModel, SystemConfigModel - New schemas: WarrantySchema, RefundSchema, SystemConfig, CrmTaskCalendar, NotificationTemplate, NotificationGateway, RepairErp, ReviewModeration, InventoryPos, MultiDashboard, BillingFinance - New services: WarrantyService, RefundService, CrmService, CrmTaskCalendarService, DeliveryService, EmailTemplateService, InventoryPosService, MultiDashboardService, NotificationGatewayService, RepairErpService, ReviewModerationService, SystemConfigService, TemplateService, CreditService - Updated routers: AdminOrder, BillingFinance, Cart, Catalog, Checkout, Crm, InventoryPos, NotificationGateway, RolePermission, ServiceJob, Settings, Storefront, UserCreation - Updated models: Inventory, Notification, Order, Product, Service, Setting, SystemConfig, User - Updated core: database init, invoice generator, main app - Added WarrantyRouter with full CRUD and claim flow
144 lines
5 KiB
Python
144 lines
5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query, status, Request
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional, Any
|
|
|
|
from app.core.database.db_session import get_db
|
|
from app.core.permissions.RoleChecker import get_current_user
|
|
from app.models.UserModel import User
|
|
from app.services.CrmService import CrmService
|
|
from app.schemas.Crm import (
|
|
CrmCustomerCreate, CrmCustomerUpdate, CrmCustomerResponse,
|
|
CrmLeadCreate, CrmLeadUpdate, CrmLeadConvert, CrmLeadResponse, CrmLeadConvertResponse,
|
|
CrmDealCreate, CrmDealUpdate, CrmDealResponse,
|
|
Customer360Response
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/v1/crm", tags=["CRM Core Engine"])
|
|
|
|
|
|
def get_crm_service(request: Request, db: Session = Depends(get_db)) -> CrmService:
|
|
tenant_id = getattr(request.state, "tenant_id", "tenant_default")
|
|
return CrmService(db=db, tenant_id=tenant_id)
|
|
|
|
|
|
# --- CUSTOMER ENDPOINTS ---
|
|
@router.post("/customers", response_model=CrmCustomerResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_customer(
|
|
payload: CrmCustomerCreate,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Creates a new CRM customer profile."""
|
|
return svc.create_customer(payload)
|
|
|
|
|
|
@router.get("/customers", response_model=List[CrmCustomerResponse])
|
|
def list_customers(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
search: Optional[str] = None,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Lists CRM customers with search and pagination."""
|
|
return svc.list_customers(skip=skip, limit=limit, search=search)
|
|
|
|
|
|
@router.get("/customers/{customer_id}", response_model=CrmCustomerResponse)
|
|
def get_customer_by_id(
|
|
customer_id: str,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Retrieves a single CRM customer profile."""
|
|
customer = svc.get_customer(customer_id)
|
|
if not customer:
|
|
raise HTTPException(status_code=404, detail="Customer not found")
|
|
return customer
|
|
|
|
|
|
@router.get("/customers/{customer_id}/360", response_model=Customer360Response)
|
|
def get_customer_360_profile(
|
|
customer_id: str,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Retrieves consolidated Customer 360 profile (Orders, Repairs, Leads, Deals)."""
|
|
profile = svc.get_customer_360(customer_id)
|
|
if not profile:
|
|
raise HTTPException(status_code=404, detail="Customer not found")
|
|
return profile
|
|
|
|
|
|
# --- LEADS ENDPOINTS ---
|
|
@router.post("/leads", response_model=CrmLeadResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_lead(
|
|
payload: CrmLeadCreate,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Creates a new inbound CRM lead."""
|
|
return svc.create_lead(payload)
|
|
|
|
|
|
@router.get("/leads", response_model=List[CrmLeadResponse])
|
|
def list_leads(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
lead_status: Optional[str] = Query(None, alias="status"),
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Lists inbound CRM leads."""
|
|
return svc.list_leads(skip=skip, limit=limit, status=lead_status)
|
|
|
|
|
|
@router.patch("/leads/{lead_id}", response_model=CrmLeadResponse)
|
|
def update_lead(
|
|
lead_id: str,
|
|
payload: CrmLeadUpdate,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Updates status or details of a CRM lead."""
|
|
updated = svc.update_lead(lead_id, payload)
|
|
if not updated:
|
|
raise HTTPException(status_code=404, detail="Lead not found")
|
|
return updated
|
|
|
|
|
|
@router.post("/leads/{lead_id}/convert", response_model=CrmLeadConvertResponse)
|
|
def convert_lead(
|
|
lead_id: str,
|
|
payload: CrmLeadConvert,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Converts a lead to a Customer + Deal, tagging as E-Commerce / Service / Both and evaluating Gold Star status."""
|
|
try:
|
|
return svc.convert_lead(lead_id, payload)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
|
|
|
|
# --- DEALS KANBAN ENDPOINTS ---
|
|
@router.post("/deals", response_model=CrmDealResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_deal(
|
|
payload: CrmDealCreate,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Creates a new CRM sales/wholesale deal."""
|
|
return svc.create_deal(payload)
|
|
|
|
|
|
@router.get("/deals", response_model=List[CrmDealResponse])
|
|
def list_deals(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
stage: Optional[str] = None,
|
|
svc: CrmService = Depends(get_crm_service),
|
|
current_user: User = Depends(get_current_user)
|
|
) -> Any:
|
|
"""Lists deals for Kanban pipeline view."""
|
|
return svc.list_deals(skip=skip, limit=limit, stage=stage)
|