fix: preserve Chatwoot message visibility and identity

This commit is contained in:
plx
2026-08-14 14:08:23 +00:00
parent 475ff15b9b
commit c4d55d37ed
10 changed files with 531 additions and 71 deletions

View File

@@ -18,6 +18,9 @@ from app.db import engine
OUT_CSV = Path("/tmp/clientflow_chatwoot_ingestion_gap.csv")
OUT_MD = Path("/tmp/clientflow_chatwoot_ingestion_gap.md")
# Historical report label retained for downstream parsers; pending inbound is
# now grouped under the required PROCESSING_ERROR state.
LEGACY_PENDING_STATE = "PENDING_INCOMING_RAW_EVENT"
def clean(value: Any, limit: int = 240) -> str:
@@ -45,13 +48,15 @@ def main() -> int:
re.payload #>> '{sender,email}' AS sender_email,
re.payload #>> '{conversation,meta,sender,email}' AS meta_sender_email,
left(coalesce(re.payload #>> '{content}', ''), 600) AS content,
m.id IS NOT NULL AS has_message,
EXISTS (
SELECT 1 FROM messages m
WHERE (m.source_system = 'chatwoot' AND m.source_event_id = re.source_event_id)
OR m.id = re.message_id
OR m.raw_event_id = re.id
) AS has_message,
c.id IS NOT NULL AS has_communication,
o.id IS NOT NULL AS has_opportunity
FROM raw_events re
LEFT JOIN messages m
ON m.source_system = 'chatwoot'
AND m.source_event_id = re.source_event_id
LEFT JOIN communications c
ON c.source_system = 'chatwoot'
AND c.source_message_id = re.source_event_id
@@ -76,16 +81,16 @@ def main() -> int:
status = "NON_INCOMING_OR_UNKNOWN"
if not args.include_ok:
continue
elif not row.get("processed") and not row.get("ignored") and not row.get("processing_error"):
status = "PENDING_INCOMING_RAW_EVENT"
elif row.get("processing_error"):
status = "INCOMING_PROCESSING_ERROR"
status = "PROCESSING_ERROR"
elif row.get("ignored"):
status = "INCOMING_IGNORED"
elif not row.get("has_message") and not row.get("has_communication"):
status = "PROCESSED_BUT_NOT_VISIBLE"
elif not row.get("has_opportunity"):
status = "VISIBLE_BUT_UNLINKED_CONVERSATION"
status = "IGNORED"
elif row.get("has_message") and not row.get("has_communication"):
status = "MESSAGE_PRESENT_NO_COMMUNICATION"
elif row.get("processed") and not row.get("has_message"):
status = "TRUE_PROCESSED_WITHOUT_MESSAGE"
elif not row.get("processed"):
status = "PROCESSING_ERROR"
else:
status = "OK"
if not args.include_ok: