Initial Switch to V2. Completely Overhauled Backend, Frontend and General Structure.
This commit is contained in:
@@ -305,6 +305,33 @@ async def get_last_comm_timestamp(customer_id: str) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
async def get_latest_comm_batch(customer_ids: list[str]) -> dict[str, dict]:
|
||||
"""Return a dict of customer_id → {id, type, occurred_at} for the latest comm per customer.
|
||||
Uses a single SQL query — no N+1 regardless of list size.
|
||||
"""
|
||||
if not customer_ids:
|
||||
return {}
|
||||
db = await mqtt_db.get_db()
|
||||
placeholders = ",".join("?" * len(customer_ids))
|
||||
rows = await db.execute_fetchall(
|
||||
f"""
|
||||
SELECT customer_id, id, type, COALESCE(occurred_at, created_at) AS ts
|
||||
FROM crm_comms_log
|
||||
WHERE customer_id IN ({placeholders})
|
||||
AND customer_id IS NOT NULL AND customer_id != ''
|
||||
ORDER BY ts DESC
|
||||
""",
|
||||
customer_ids,
|
||||
)
|
||||
# Keep only the first (latest) row per customer
|
||||
result: dict[str, dict] = {}
|
||||
for row in rows:
|
||||
cid = row[0]
|
||||
if cid not in result:
|
||||
result[cid] = {"id": row[1], "type": row[2], "occurred_at": row[3]}
|
||||
return result
|
||||
|
||||
|
||||
async def list_customers_sorted_by_latest_comm(customers: list[CustomerInDB]) -> list[CustomerInDB]:
|
||||
"""Re-sort a list of customers so those with the most recent comm come first."""
|
||||
timestamps = await asyncio.gather(
|
||||
|
||||
Reference in New Issue
Block a user