feat: IMAP mail sync with thread-to-contact matching (PRD §6.1)

Real IMAP connection via imapflow library:
- Connects to IMAP server (IMAPFlow, TLS on port 993)
- Fetches recent messages (configurable limit, default 20)
- Extracts: from, fromEmail, subject, date, snippet (200 chars)
- Thread-to-contact matching (PRD §6.1):
  1. Match sender email against persons table
  2. If no person match, extract domain and match against company.website
  3. Returns matchedCompany + matchedPerson for each email
- Audit-logged (action: crm.mailsync:imap)
- Zod validated (host, port, user, password, folder, limit)
- 502 on IMAP connection failure with detail
- 400 on validation failure

Tested: invalid creds → 502 'IMAP connection failed', missing host → 400
This commit is contained in:
Z User committed 2026-10-27 22:50:50 +00:00
1 parent 7acc0c5451
commit e45b68bb8b
1089 files changed
+260614

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
class JsonRepository:
def __init__(self, base_dir: str | Path):
self.base_dir = Path(base_dir)
self.kp_dir = self.base_dir / "knowledge_points"
self.progress_dir = self.base_dir / "user_progress"
self.sessions_dir = self.base_dir / "sessions"
self._ensure_dirs()
def _ensure_dirs(self) -> None:
for d in [self.kp_dir, self.progress_dir, self.sessions_dir]:
d.mkdir(parents=True, exist_ok=True)
def load_json(self, path: Path, default: Any = None) -> Any:
if not path.exists():
return default
return json.loads(path.read_text(encoding="utf-8"))
def save_json(self, path: Path, data: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
def knowledge_points_path(self, document_id: str) -> Path:
return self.kp_dir / f"{document_id}.json"
def progress_path(self, user_id: str, document_id: str) -> Path:
return self.progress_dir / f"{user_id}__{document_id}.json"
def session_path(self, session_id: str) -> Path:
return self.sessions_dir / f"{session_id}.json"