File size: 2,857 Bytes
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
from sqlalchemy.orm import Session
from typing import List
import logging

from app.db.session import get_db
from app.models.models import User, PartnerLead
from app.schemas import schemas
from app.core.dependencies import get_current_user
from app.services.email_service import send_email
from app.core import config

logger = logging.getLogger(__name__)

router = APIRouter()

@router.post("/brief", response_model=schemas.PartnerLead)
async def submit_partner_brief(
    payload: schemas.PartnerLeadCreate,
    background_tasks: BackgroundTasks,
    db: Session = Depends(get_db)
):
    """
    Public endpoint for brands to submit campaign briefs.
    Saves to DB and sends an email notification to the admin.
    """
    lead = PartnerLead(
        brand_name=payload.brand_name,
        contact_email=payload.contact_email,
        campaign_type=payload.campaign_type,
        details=payload.details
    )
    db.add(lead)
    db.commit()
    db.refresh(lead)

    # Notify Admin via Email
    message = (
        f"New Partnership Brief from {payload.brand_name}.\n\n"
        f"Contact: {payload.contact_email}\n"
        f"Campaign Type: {payload.campaign_type}\n"
        f"Details: {payload.details}\n\n"
        "You can view and manage this lead in the Admin Dashboard."
    )
    
    background_tasks.add_task(
        send_email,
        to_email=config.SMTP_USER, # Send to the configured admin email
        subject=f"New Partner Lead: {payload.brand_name}",
        title="New Campaign Brief Received",
        message=message,
        action_text="View in Dashboard",
        action_url="/admin/partners" # Hypothetical admin path
    )

    return lead

@router.get("/admin/leads", response_model=List[schemas.PartnerLead])
def get_partner_leads(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Admin-only endpoint to list all partnership leads.
    """
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")
    
    return db.query(PartnerLead).order_by(PartnerLead.created_at.desc()).all()

@router.put("/admin/leads/{lead_id}", response_model=schemas.PartnerLead)
def update_lead_status(
    lead_id: int,
    status: str,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Admin-only endpoint to update the status of a lead.
    """
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")
    
    lead = db.query(PartnerLead).filter(PartnerLead.id == lead_id).first()
    if not lead:
        raise HTTPException(status_code=404, detail="Lead not found")
    
    lead.status = status
    db.commit()
    db.refresh(lead)
    return lead