a↗agentbay.Tải AgentBay
Tài khoản của tôi ▾
Menu
← Quay lại hướng dẫn
ĐẦU RA HƯỚNG DẪN

Kiến trúc

Tải bản gốc ↓

Kiến trúc CRM-142

Ranh giới hiện có (không đổi)

src/contacts.js xuất initialContacts, normalEmail(value), validContact(row), addContact(rows, row), searchContacts(rows, query=''). Tái dùng chuẩn hóa/xác minh và addContact để tạo ID cuối và cắt khoảng trắng. src/storage.js xuất readContacts() và async saveContacts(rows, {fail=false}={}); chỉ thay agentbay-crm-142-contacts sau 180ms chờ. Giữ cả hai mô-đun không đổi. src/app.js giữ kết xuất danh sách, tìm kiếm và tích hợp Add contact.

Mô-đun nhập thuần

Tạo src/import.js với đúng các export công khai được yêu cầu bởi docs/import-contract.md:

  • previewCSV(text, existing) → {rows, counts: {ready, invalid, duplicate}}. Mỗi hàng: {record, name, email, company, status, reason}; record là số bản ghi dữ liệu không trống bắt đầu từ 1, status là ready|invalid|duplicate, reason rỗng với Ready. Không truy cập DOM/lưu trữ hay lưu bền.
  • mergeReady(preview, existing) → {contacts, added, skipped}. Trả array mới, giữ mọi đối tượng/dữ liệu hiện có và không thay đầu vào. Xác minh lại trường Ready và email chuẩn hóa với liên hệ hiện tại được truyền cùng hàng đã chấp nhận trước; dùng addContact cho ID mới. skipped bằng preview.rows.length trừ added, kể cả Ready mới xung đột.

Dùng bộ quét ký tự với trạng thái đầu trường, không ngoặc, trong ngoặc và sau ngoặc; xử lý cặp ngoặc kép và chỉ nhận dấu phân cách/trình tách bản ghi ngoài ngoặc. Từ chối ngoặc lạc, chữ sau ngoặc đóng, ngoặc chưa đóng và quá nhiều trường trước khi tạo xem trước. Bỏ BOM đầu; hỗ trợ LF/CRLF, dấu phẩy trong ngoặc và xuống dòng nội bộ. Chuẩn hóa tiêu đề bằng trim/chữ thường, từ chối trùng và thiếu name/email. Đệm hàng ngắn, bỏ trường tiêu đề lạ khác nhau. Bỏ bản ghi chỉ khoảng trắng. Áp dụng TextEncoder UTF-8 byteLength <=100000 và <=500 bản ghi không trống. Xác minh trước khi giữ chỗ email: hàng lỗi không làm hàng hợp lệ sau bị trùng. Khởi tạo tập email chuẩn hóa từ liên hệ hiện có; lần hợp lệ đầu trong tệp được giữ.

Trạng thái UI và thứ tự ghi

Trong src/app.js, giữ rows là liên hệ đã xác nhận. Thêm giai đoạn bảng (closed|select|reading|review|saving), File đã chọn, preview, token thế hệ đọc, cờ đồng bộ toàn cục writeBusy và boolean failNextSave. renderImport() tạo nút văn bản/thẻ, số lượng và trạng thái tắt. Tăng thế hệ khi chọn/hủy; bỏ kết quả file.text() muộn. Kiểm tra File.size trước đọc và áp dụng lại giới hạn byte trong previewCSV.

Mọi lần ghi Add và Import dùng một bộ phận do ứng dụng sở hữu persistContacts(makeNext) điều phối bất đồng bộ: đặt writeBusy đồng bộ trước bất kỳ await; từ chối lần thứ hai, tắt điều khiển sửa Add/Import, rồi gọi makeNext với hàng xác nhận mới nhất. Factory Import gọi mergeReady(preview, rows); factory Add bọc addContact(rows, input) thành {contacts, added:{0} skipped:0}, đúng dạng kết quả bộ điều phối. Tuần tự hóa ghi trong trang này, không khóa giữa các tab. Chỉ dùng failNextSave lúc gọi saveContacts(next, {fail}); chỉ gán next vào rows và kết xuất sau hoàn tất. Khi bị từ chối giữ hàng, đầu vào/xem trước và điều khiển thử lại; giải phóng busy trong finally. Không xóa/làm trống khóa. Nhập thành công đóng xem trước, nên nhấp đúp/đang đợi không dùng lại được. Không có thêm sau xác minh lại thì quay về danh sách với tổng kết chính xác không thêm/bỏ hết mà không lưu thừa.

Xem trước là tạm thời. Xác minh lại dùng hàng mới nhất đã lưu thành công trong trang ngay trước khi lưu, không phải ảnh chụp lúc tải tệp. Bài tập chỉ trên trình duyệt không cung cấp giao dịch nhiều tab, đồng thời cơ sở dữ liệu từ xa hay xác minh giao thư. localStorage riêng theo nguồn/profile, có thể lỗi và xóa dữ liệu trình duyệt sẽ xóa liên hệ nhập. Không tải lên qua mạng hay khóa lưu trữ mới.

Ranh giới kết xuất và thất bại

Dùng createElement/textContent/replaceChildren cho nội dung CSV, tên tệp, lý do và tổng kết theo bộ kết xuất danh sách an toàn hiện có. Không nội suy innerHTML. Giữ vùng thông báo trực tiếp hiện có #status . Lỗi cấu trúc/đọc chỉ bỏ xem trước có thể nhập, không bao giờ bỏ liên hệ đã xác nhận. Lưu thất bại giữ xem trước và chỉ bộ điều hợp quyết định lưu bền thành công. Chi tiết UI và tiêu điểm ở docs/screen-design.md.

Tệp gốc và chi tiết nguồn

output/docs/architecture.md.txt · SHA-256 69d6f9009c7611dac555bd970d0b58d9346461bb735a225f6b2aa7d80887b557

# CRM-142 architecture

## Existing boundaries (unchanged)
`src/contacts.js` exports initialContacts, normalEmail(value), validContact(row), addContact(rows, row), searchContacts(rows, query=''). Reuse normalization/validation and addContact for final contact IDs and trimming. `src/storage.js` exports readContacts() and async saveContacts(rows, {fail=false}={}); it replaces only `agentbay-crm-142-contacts` after a 180ms delay. Leave both modules unchanged. `src/app.js` retains list rendering, search and Add contact integration.

## Pure import module
Create `src/import.js` with exactly the public exports required by `docs/import-contract.md`:

- `previewCSV(text, existing)` → `{rows, counts: {ready, invalid, duplicate}}`. Each row: `{record, name, email, company, status, reason}`; record is the 1-based nonblank data-record number, status is `ready|invalid|duplicate`, reason is empty for Ready. No DOM/storage access or persistence.
- `mergeReady(preview, existing)` → `{contacts, added, skipped}`. Return a new array, preserve all existing objects/data, never mutate either input. Revalidate Ready row fields and normalized emails against the passed current contacts and earlier accepted rows; use addContact for new IDs. skipped equals preview.rows.length minus added, including newly conflicting Ready records.

Use a character scanner with field-start, unquoted, quoted and after-quote states; handle doubled quotes, delimiters and record separators only outside quotes. Reject stray quotes, text after a closing quote, unclosed quotes and too many fields before producing a preview. Strip initial BOM; support LF/CRLF, quoted commas and embedded newlines. Normalize header names by trim/lowercase, reject duplicates and missing name/email. Pad shorter rows, ignore distinct unknown header fields. Ignore parsed all-whitespace records. Enforce TextEncoder UTF-8 byteLength <=100000 and <=500 nonblank data records. Validate fields before reserving emails: invalid rows cannot make later valid rows duplicate. Seed normalized email set from existing contacts; first valid file occurrence wins.

## UI state and write ordering
In `src/app.js`, retain `rows` as committed contacts. Add panel phase (`closed|select|reading|review|saving`), selected File, preview, read generation token, a synchronous global writeBusy flag and failNextSave boolean. `renderImport()` creates text nodes/cards, counts and disabled states. Increment generation on selection/cancel; ignore late file.text() results. Check File.size before reading and enforce bytes again in previewCSV.

All Add and Import writes use one app-owned `persistContacts(makeNext)` async coordinator: synchronously acquire writeBusy before any await; reject a second attempt, disable Add/Import mutation controls, then call makeNext with latest committed rows. Import's factory calls mergeReady(preview, rows); Add's factory wraps addContact(rows, input) as {contacts, added:1, skipped:0}, matching the coordinator result shape. This serializes writes in this page; it is not a cross-tab lock. Consume failNextSave only when calling saveContacts(next, {fail}); publish next to rows and render only after resolution. On rejection preserve committed rows, input/preview and retry controls; release busy in finally. Never clear/remove the storage key. Successful import closes preview, so queued/double clicks cannot reuse it. Zero additions after revalidation return to list with accurate zero-added/all-skipped summary without an unnecessary save.

Preview is transient. Revalidation uses latest successful in-page rows immediately before save, not a captured upload snapshot. This browser-only exercise provides no multi-tab transaction, remote database concurrency or delivery verification. localStorage is origin/browser-profile specific, may fail, and clearing browser data removes imported contacts. No network upload or new storage key.

## Rendering and failure boundary
Use createElement/textContent/replaceChildren for CSV content, filenames, reasons and summaries, matching the existing safe list renderer. Do not use innerHTML interpolation. Keep existing `#status` live region. Structural/read errors discard importable preview only, never committed contacts. Save failure retains preview and only the adapter decides persistence success. UI details and focus rules are in `docs/screen-design.md`.