Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
d37b213
1
Parent(s): 5e04bce
feat: Threshold crossing for CRM - prevent spam
Browse files
backend/app/models/lead.py
CHANGED
|
@@ -83,6 +83,11 @@ class Lead(SQLModel, table=True):
|
|
| 83 |
last_message_at: datetime = Field(default_factory=datetime.utcnow)
|
| 84 |
last_activity_at: datetime = Field(default_factory=datetime.utcnow)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Assignment
|
| 87 |
assigned_to_manager_id: Optional[UUID] = None
|
| 88 |
assigned_at: Optional[datetime] = None
|
|
|
|
| 83 |
last_message_at: datetime = Field(default_factory=datetime.utcnow)
|
| 84 |
last_activity_at: datetime = Field(default_factory=datetime.utcnow)
|
| 85 |
|
| 86 |
+
# Threshold Crossing (for CRM sync optimization)
|
| 87 |
+
ever_qualified: bool = Field(default=False) # Has ever crossed hot threshold (70+)
|
| 88 |
+
qualified_at: Optional[datetime] = None # When first became qualified
|
| 89 |
+
peak_heat_score: int = Field(default=0) # Highest heat score ever reached
|
| 90 |
+
|
| 91 |
# Assignment
|
| 92 |
assigned_to_manager_id: Optional[UUID] = None
|
| 93 |
assigned_at: Optional[datetime] = None
|
backend/app/services/auto_sync_service.py
CHANGED
|
@@ -137,21 +137,33 @@ class AutoSyncService:
|
|
| 137 |
}
|
| 138 |
|
| 139 |
def _should_attempt_sync(self, lead: Lead) -> bool:
|
| 140 |
-
"""
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
# Must have contact information
|
| 147 |
if not lead.email and not lead.phone:
|
| 148 |
return False
|
| 149 |
-
|
| 150 |
-
# Don't sync junk leads
|
| 151 |
if lead.heat_level == LeadHeat.JUNK:
|
| 152 |
return False
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
async def _get_active_integrations(
|
| 157 |
self,
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
def _should_attempt_sync(self, lead: Lead) -> bool:
|
| 140 |
+
"""
|
| 141 |
+
Threshold Crossing Logic - prevents CRM spam.
|
| 142 |
+
Syncs only when: 1) First time qualified, 2) New peak score
|
| 143 |
+
"""
|
| 144 |
+
# Must have contact
|
|
|
|
|
|
|
| 145 |
if not lead.email and not lead.phone:
|
| 146 |
return False
|
|
|
|
|
|
|
| 147 |
if lead.heat_level == LeadHeat.JUNK:
|
| 148 |
return False
|
| 149 |
|
| 150 |
+
current = lead.heat_score
|
| 151 |
+
|
| 152 |
+
# First time HOT
|
| 153 |
+
if current >= 70 and not lead.ever_qualified:
|
| 154 |
+
lead.ever_qualified = True
|
| 155 |
+
lead.qualified_at = datetime.utcnow()
|
| 156 |
+
lead.peak_heat_score = current
|
| 157 |
+
logger.info(f"🔥 Lead {lead.id}: First HOT ({current})")
|
| 158 |
+
return True
|
| 159 |
+
|
| 160 |
+
# New peak
|
| 161 |
+
if lead.ever_qualified and current > lead.peak_heat_score:
|
| 162 |
+
lead.peak_heat_score = current
|
| 163 |
+
logger.info(f"📈 Lead {lead.id}: New peak {current}")
|
| 164 |
+
return True
|
| 165 |
+
|
| 166 |
+
return False
|
| 167 |
|
| 168 |
async def _get_active_integrations(
|
| 169 |
self,
|