File size: 2,029 Bytes
e23172f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Lead management module"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime

router = APIRouter(prefix="/leads")

class LeadCreate(BaseModel):
    company: str
    name: Optional[str] = None
    email: Optional[str] = None
    job_title: str
    industry: str
    revenue: Optional[str] = None
    employees: Optional[int] = None
    source: Optional[str] = "organic"

class LeadResponse(BaseModel):
    id: int
    company: str
    job_title: str
    score: int
    status: str
    created_at: str

# In-memory store
_leads_db = {}
_next_id = 1

@router.post("/", response_model=LeadResponse)
async def create_lead(lead: LeadCreate):
    global _next_id
    
    # Simple scoring
    score = 50
    if lead.revenue:
        try:
            rev = float(lead.revenue.replace('$', ''))
            if rev > 50000000:
                score += 30
            elif rev > 10000000:
                score += 20
        except:
            pass
    
    _leads_db[_next_id] = {
        "company": lead.company,
        "job_title": lead.job_title,
        "score": score,
        "status": "new",
        "created_at": datetime.utcnow().isoformat()
    }
    
    lead_id = _next_id
    _next_id += 1
    
    return LeadResponse(
        id=lead_id,
        company=lead.company,
        job_title=lead.job_title,
        score=score,
        status="new",
        created_at=datetime.utcnow().isoformat()
    )

@router.get("/", response_model=List[LeadResponse])
async def list_leads(status: Optional[str] = None):
    results = []
    for lid, data in _leads_db.items():
        if status is None or data["status"] == status:
            results.append(LeadResponse(
                id=lid,
                company=data["company"],
                job_title=data["job_title"],
                score=data["score"],
                status=data["status"],
                created_at=data["created_at"]
            ))
    return results