2026-08-13 22:43:33 +02:00
|
|
|
"""Import/export service — CSV/JSON/XLSX import with dry-run preview, partial-failure, CSV/XLSX export.
|
|
|
|
|
|
2026-08-16 01:17:18 +02:00
|
|
|
This service is Contact-specific — it handles import/export for Contacts and
|
|
|
|
|
Companies (both use the Contact model). It is NOT a generic import/export
|
|
|
|
|
service. If other entity types need import/export in the future, a separate
|
|
|
|
|
service or a plugin-based interface should be created.
|
|
|
|
|
|
|
|
|
|
Declared as Contact-specific (P1-23 fix): no pretense of being generic.
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
Uses shared helpers from import_export_helpers.py for parsing, writing, mapping,
|
|
|
|
|
and validation. Implements partial-failure semantics: valid rows are committed
|
|
|
|
|
while invalid rows are collected into a structured error report.
|
2026-07-23 08:42:26 +02:00
|
|
|
|
2026-07-26 02:42:07 +02:00
|
|
|
Uses unified Contact model fields: firstname, surname, email_1, phone_1, phone_2, function.
|
2026-07-23 08:42:26 +02:00
|
|
|
Company import creates Contact with type='company'.
|
|
|
|
|
"""
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
import logging
|
2026-06-29 00:44:34 +02:00
|
|
|
import uuid
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.audit import log_audit
|
2026-07-29 02:11:29 +02:00
|
|
|
from app.core.visibility import apply_visibility_filter
|
2026-07-23 08:42:26 +02:00
|
|
|
from app.models.contact import Contact
|
2026-08-13 22:43:33 +02:00
|
|
|
from app.services import import_export_helpers as helpers
|
2026-07-19 21:12:49 +02:00
|
|
|
from app.services.contact_service import _serialize_contact as _contact_to_dict
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-06-29 00:44:34 +02:00
|
|
|
# Expected CSV columns for each entity type
|
2026-07-23 08:42:26 +02:00
|
|
|
# Company import creates Contact with type='company' using name field
|
2026-08-12 20:47:43 +02:00
|
|
|
COMPANY_COLUMNS = ["name", "industry", "phone", "email", "website"]
|
2026-07-23 08:42:26 +02:00
|
|
|
# Contact import uses unified Contact fields
|
|
|
|
|
CONTACT_COLUMNS = ["firstname", "surname", "email", "phone", "mobile", "function", "department"]
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
# Target fields for mapping suggestions
|
|
|
|
|
CONTACT_TARGET_FIELDS = ["firstname", "surname", "email", "phone", "mobile", "function", "department"]
|
|
|
|
|
COMPANY_TARGET_FIELDS = ["name", "industry", "phone", "email", "website"]
|
|
|
|
|
|
|
|
|
|
# Validators for each entity type
|
|
|
|
|
CONTACT_VALIDATORS: dict[str, dict] = {
|
|
|
|
|
"email": {"type": "email"},
|
|
|
|
|
}
|
|
|
|
|
COMPANY_VALIDATORS: dict[str, dict] = {
|
|
|
|
|
"email": {"type": "email"},
|
|
|
|
|
"website": {"type": "url"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Threshold for background job processing (rows above this go to ARQ)
|
|
|
|
|
BACKGROUND_JOB_THRESHOLD = 1000
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
|
|
|
|
|
def _parse_csv(content: str | bytes) -> list[dict[str, str]]:
|
|
|
|
|
"""Parse file content into list of dicts (delegates to helpers with format detection).
|
|
|
|
|
|
|
|
|
|
Despite the name, this handles CSV, JSON, and XLSX formats by detecting
|
|
|
|
|
from content. This maintains backward compatibility with existing callers.
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(content, str):
|
|
|
|
|
content = content.encode("utf-8")
|
|
|
|
|
# Detect format from content magic bytes
|
|
|
|
|
fmt = helpers.detect_format("upload", content)
|
|
|
|
|
if fmt == "json":
|
|
|
|
|
return helpers.parse_json(content)
|
|
|
|
|
elif fmt == "xlsx":
|
|
|
|
|
return helpers.parse_xlsx(content)
|
|
|
|
|
return helpers.parse_csv(content)
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _validate_row(row: dict[str, str], required: list[str]) -> list[str]:
|
2026-08-13 22:43:33 +02:00
|
|
|
"""Validate a single row (delegates to helpers for backward compat)."""
|
|
|
|
|
return helpers.validate_row(row, required)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _normalize_contact_row(row: dict[str, str]) -> dict[str, str]:
|
|
|
|
|
"""Normalize contact row to unified field names."""
|
|
|
|
|
firstname = (row.get("firstname") or row.get("first_name") or "").strip()
|
|
|
|
|
surname = (row.get("surname") or row.get("last_name") or "").strip()
|
|
|
|
|
row["firstname"] = firstname
|
|
|
|
|
row["surname"] = surname
|
|
|
|
|
# Map old column names to unified ones
|
|
|
|
|
if "email" not in row and "email_address" in row:
|
|
|
|
|
row["email"] = row["email_address"]
|
|
|
|
|
if "mobile" not in row and "phone_2" in row:
|
|
|
|
|
row["mobile"] = row["phone_2"]
|
|
|
|
|
if "function" not in row and "position" in row:
|
|
|
|
|
row["function"] = row["position"]
|
|
|
|
|
return row
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _normalize_company_row(row: dict[str, str]) -> dict[str, str]:
|
|
|
|
|
"""Normalize company row to unified field names."""
|
|
|
|
|
name = (row.get("name") or row.get("company") or row.get("company_name") or "").strip()
|
|
|
|
|
row["name"] = name
|
|
|
|
|
if "email" not in row and "email_address" in row:
|
|
|
|
|
row["email"] = row["email_address"]
|
|
|
|
|
if "website" not in row and "url" in row:
|
|
|
|
|
row["website"] = row["url"]
|
|
|
|
|
if "website" not in row and "homepage" in row:
|
|
|
|
|
row["website"] = row["homepage"]
|
|
|
|
|
return row
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def import_companies(
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
tenant_id: uuid.UUID,
|
|
|
|
|
user_id: uuid.UUID,
|
2026-08-13 22:43:33 +02:00
|
|
|
csv_content: str | bytes,
|
2026-06-29 00:44:34 +02:00
|
|
|
dry_run: bool = False,
|
2026-08-13 22:43:33 +02:00
|
|
|
field_mapping: dict[str, str] | None = None,
|
2026-06-29 00:44:34 +02:00
|
|
|
) -> dict[str, Any]:
|
2026-07-23 08:42:26 +02:00
|
|
|
"""Import companies from CSV as Contact with type='company'.
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-07-23 08:42:26 +02:00
|
|
|
Uses unified Contact model: name field for company name, email_1/phone_1 for contact info.
|
2026-08-13 22:43:33 +02:00
|
|
|
Implements partial-failure: valid rows committed, invalid rows collected in error report.
|
2026-06-29 00:44:34 +02:00
|
|
|
"""
|
|
|
|
|
rows = _parse_csv(csv_content)
|
|
|
|
|
total = len(rows)
|
2026-08-13 22:43:33 +02:00
|
|
|
errors: list[dict] = []
|
|
|
|
|
valid_rows: list[dict] = []
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
for idx, row in enumerate(rows, start=1):
|
2026-08-13 22:43:33 +02:00
|
|
|
# Apply field mapping if provided
|
|
|
|
|
if field_mapping:
|
|
|
|
|
row = helpers.map_fields(row, field_mapping)
|
|
|
|
|
row = _normalize_company_row(row)
|
|
|
|
|
row_errors = helpers.validate_row(row, ["name"], COMPANY_VALIDATORS)
|
2026-06-29 00:44:34 +02:00
|
|
|
if row_errors:
|
|
|
|
|
for e in row_errors:
|
2026-08-13 22:43:33 +02:00
|
|
|
errors.append({"row": idx, "field": "", "message": e})
|
2026-06-29 00:44:34 +02:00
|
|
|
else:
|
|
|
|
|
valid_rows.append(row)
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
# Count unique failed rows (a row may have multiple errors)
|
|
|
|
|
failed_row_count = len({e["row"] for e in errors})
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
if dry_run:
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=len(valid_rows),
|
|
|
|
|
failed=failed_row_count,
|
|
|
|
|
errors=errors,
|
|
|
|
|
dry_run=True,
|
2026-06-29 00:44:34 +02:00
|
|
|
)
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
created: list[dict] = []
|
|
|
|
|
failed: list[dict] = []
|
|
|
|
|
succeeded = 0
|
|
|
|
|
|
|
|
|
|
for idx, row in enumerate(valid_rows, start=1):
|
|
|
|
|
try:
|
|
|
|
|
contact = Contact(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
type="company",
|
|
|
|
|
name=row["name"].strip(),
|
|
|
|
|
displayname=row["name"].strip(),
|
|
|
|
|
email_1=row.get("email", "").strip() or None,
|
|
|
|
|
phone_1=row.get("phone", "").strip() or None,
|
|
|
|
|
website=row.get("website", "").strip() or None,
|
|
|
|
|
owner_id=user_id,
|
|
|
|
|
created_by=user_id,
|
|
|
|
|
updated_by=user_id,
|
|
|
|
|
)
|
|
|
|
|
db.add(contact)
|
|
|
|
|
await db.flush()
|
|
|
|
|
await log_audit(
|
|
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
user_id,
|
|
|
|
|
"import",
|
|
|
|
|
"contact",
|
|
|
|
|
contact.id,
|
|
|
|
|
changes={"name": contact.name, "type": "company"},
|
|
|
|
|
)
|
|
|
|
|
created.append(_contact_to_dict(contact))
|
|
|
|
|
succeeded += 1
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.warning("Import companies: row %d failed: %s", idx, exc)
|
|
|
|
|
await db.rollback()
|
|
|
|
|
failed.append({"row": idx, "field": "", "message": str(exc)})
|
|
|
|
|
|
|
|
|
|
if succeeded > 0:
|
|
|
|
|
try:
|
|
|
|
|
await db.commit()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.error("Import companies: commit failed: %s", exc)
|
|
|
|
|
await db.rollback()
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=0,
|
|
|
|
|
failed=len(valid_rows),
|
|
|
|
|
errors=errors + [{"row": 0, "field": "", "message": f"Commit failed: {exc}"}],
|
|
|
|
|
created=[],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Count unique failed rows from validation errors + runtime failures
|
|
|
|
|
all_failed_rows = {e["row"] for e in errors} | {f["row"] for f in failed}
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=succeeded,
|
|
|
|
|
failed=len(all_failed_rows),
|
|
|
|
|
errors=errors + failed,
|
|
|
|
|
created=created,
|
|
|
|
|
)
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def import_contacts(
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
tenant_id: uuid.UUID,
|
|
|
|
|
user_id: uuid.UUID,
|
2026-08-13 22:43:33 +02:00
|
|
|
csv_content: str | bytes,
|
2026-06-29 00:44:34 +02:00
|
|
|
dry_run: bool = False,
|
2026-08-13 22:43:33 +02:00
|
|
|
field_mapping: dict[str, str] | None = None,
|
2026-06-29 00:44:34 +02:00
|
|
|
) -> dict[str, Any]:
|
2026-07-23 08:42:26 +02:00
|
|
|
"""Import contacts from CSV using unified Contact model fields.
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-07-23 08:42:26 +02:00
|
|
|
CSV columns: firstname, surname, email, phone, mobile, function, department.
|
2026-07-26 02:42:07 +02:00
|
|
|
Maps to Contact fields: firstname, surname, email_1, phone_1, phone_2, function.
|
2026-08-13 22:43:33 +02:00
|
|
|
Implements partial-failure: valid rows committed, invalid rows collected in error report.
|
2026-06-29 00:44:34 +02:00
|
|
|
"""
|
|
|
|
|
rows = _parse_csv(csv_content)
|
|
|
|
|
total = len(rows)
|
2026-08-13 22:43:33 +02:00
|
|
|
errors: list[dict] = []
|
|
|
|
|
valid_rows: list[dict] = []
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
for idx, row in enumerate(rows, start=1):
|
2026-08-13 22:43:33 +02:00
|
|
|
# Apply field mapping if provided
|
|
|
|
|
if field_mapping:
|
|
|
|
|
row = helpers.map_fields(row, field_mapping)
|
|
|
|
|
row = _normalize_contact_row(row)
|
|
|
|
|
# Validate: at least one of firstname or surname required
|
|
|
|
|
if not row["firstname"] and not row["surname"]:
|
|
|
|
|
errors.append({"row": idx, "field": "", "message": "Missing required field: firstname or surname"})
|
|
|
|
|
continue
|
|
|
|
|
row_errors = helpers.validate_row(row, [], CONTACT_VALIDATORS)
|
|
|
|
|
if row_errors:
|
|
|
|
|
for e in row_errors:
|
|
|
|
|
errors.append({"row": idx, "field": "", "message": e})
|
2026-06-29 00:44:34 +02:00
|
|
|
else:
|
|
|
|
|
valid_rows.append(row)
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
# Count unique failed rows (a row may have multiple errors)
|
|
|
|
|
failed_row_count = len({e["row"] for e in errors})
|
2026-06-29 00:44:34 +02:00
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
if dry_run:
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=len(valid_rows),
|
|
|
|
|
failed=failed_row_count,
|
|
|
|
|
errors=errors,
|
|
|
|
|
dry_run=True,
|
2026-06-29 00:44:34 +02:00
|
|
|
)
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
created: list[dict] = []
|
|
|
|
|
failed: list[dict] = []
|
|
|
|
|
succeeded = 0
|
|
|
|
|
|
|
|
|
|
for idx, row in enumerate(valid_rows, start=1):
|
|
|
|
|
try:
|
|
|
|
|
contact = Contact(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
type="person",
|
|
|
|
|
firstname=row["firstname"].strip() or None,
|
|
|
|
|
surname=row["surname"].strip() or None,
|
|
|
|
|
displayname=f"{row['firstname']} {row['surname']}".strip(),
|
|
|
|
|
email_1=row.get("email", "").strip() or None,
|
|
|
|
|
phone_1=row.get("phone", "").strip() or None,
|
|
|
|
|
phone_2=row.get("mobile", "").strip() or None,
|
|
|
|
|
owner_id=user_id,
|
|
|
|
|
created_by=user_id,
|
|
|
|
|
updated_by=user_id,
|
|
|
|
|
)
|
|
|
|
|
db.add(contact)
|
|
|
|
|
await db.flush()
|
|
|
|
|
await log_audit(
|
|
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
user_id,
|
|
|
|
|
"import",
|
|
|
|
|
"contact",
|
|
|
|
|
contact.id,
|
|
|
|
|
changes={"firstname": contact.firstname, "surname": contact.surname},
|
|
|
|
|
)
|
|
|
|
|
created.append(_contact_to_dict(contact))
|
|
|
|
|
succeeded += 1
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.warning("Import contacts: row %d failed: %s", idx, exc)
|
|
|
|
|
await db.rollback()
|
|
|
|
|
failed.append({"row": idx, "field": "", "message": str(exc)})
|
|
|
|
|
|
|
|
|
|
if succeeded > 0:
|
|
|
|
|
try:
|
|
|
|
|
await db.commit()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.error("Import contacts: commit failed: %s", exc)
|
|
|
|
|
await db.rollback()
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=0,
|
|
|
|
|
failed=len(valid_rows),
|
|
|
|
|
errors=errors + [{"row": 0, "field": "", "message": f"Commit failed: {exc}"}],
|
|
|
|
|
created=[],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Count unique failed rows from validation errors + runtime failures
|
|
|
|
|
all_failed_rows = {e["row"] for e in errors} | {f["row"] for f in failed}
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=succeeded,
|
|
|
|
|
failed=len(all_failed_rows),
|
|
|
|
|
errors=errors + failed,
|
|
|
|
|
created=created,
|
|
|
|
|
)
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def import_csv(
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
tenant_id: uuid.UUID,
|
|
|
|
|
user_id: uuid.UUID,
|
2026-08-13 22:43:33 +02:00
|
|
|
csv_content: str | bytes,
|
2026-06-29 00:44:34 +02:00
|
|
|
entity_type: str,
|
|
|
|
|
dry_run: bool = False,
|
2026-08-13 22:43:33 +02:00
|
|
|
field_mapping: dict[str, str] | None = None,
|
2026-06-29 00:44:34 +02:00
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Generic CSV import dispatcher based on entity_type ('companies' or 'contacts')."""
|
|
|
|
|
if entity_type == "companies":
|
2026-08-13 22:43:33 +02:00
|
|
|
return await import_companies(db, tenant_id, user_id, csv_content, dry_run=dry_run, field_mapping=field_mapping)
|
2026-06-29 00:44:34 +02:00
|
|
|
elif entity_type == "contacts":
|
2026-08-13 22:43:33 +02:00
|
|
|
return await import_contacts(db, tenant_id, user_id, csv_content, dry_run=dry_run, field_mapping=field_mapping)
|
2026-06-29 00:44:34 +02:00
|
|
|
else:
|
2026-08-13 22:43:33 +02:00
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=0,
|
|
|
|
|
succeeded=0,
|
|
|
|
|
failed=1,
|
|
|
|
|
errors=[{"row": 0, "field": "", "message": f"Unknown entity_type: {entity_type}"}],
|
|
|
|
|
)
|
2026-06-29 00:44:34 +02:00
|
|
|
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
# ─── Export functions ────────────────────────────────────────────────────────
|
|
|
|
|
|
2026-06-29 00:44:34 +02:00
|
|
|
async def export_contacts_csv(
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
tenant_id: uuid.UUID,
|
2026-07-29 02:11:29 +02:00
|
|
|
user_id: uuid.UUID | None = None,
|
|
|
|
|
is_system_admin: bool = False,
|
2026-06-29 00:44:34 +02:00
|
|
|
) -> str:
|
2026-07-23 08:42:26 +02:00
|
|
|
"""Export contacts as CSV string using unified Contact model fields."""
|
2026-06-29 17:43:56 +02:00
|
|
|
q = (
|
|
|
|
|
select(Contact)
|
|
|
|
|
.where(
|
|
|
|
|
Contact.tenant_id == tenant_id,
|
|
|
|
|
Contact.deleted_at.is_(None),
|
|
|
|
|
)
|
2026-07-23 08:42:26 +02:00
|
|
|
.order_by(Contact.surname, Contact.firstname)
|
2026-06-29 17:43:56 +02:00
|
|
|
)
|
2026-07-29 02:11:29 +02:00
|
|
|
if user_id:
|
|
|
|
|
q = await apply_visibility_filter(
|
|
|
|
|
db, q, "contact", Contact, user_id, tenant_id, is_system_admin
|
|
|
|
|
)
|
2026-06-29 00:44:34 +02:00
|
|
|
result = await db.execute(q)
|
|
|
|
|
contacts = result.scalars().all()
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
headers = ["id", "type", "firstname", "surname", "name", "email", "phone", "mobile", "city", "postalcode", "country"]
|
|
|
|
|
rows = [
|
|
|
|
|
{
|
|
|
|
|
"id": str(c.id),
|
|
|
|
|
"type": c.type or "person",
|
|
|
|
|
"firstname": c.firstname or "",
|
|
|
|
|
"surname": c.surname or "",
|
|
|
|
|
"name": c.name or "",
|
|
|
|
|
"email": c.email_1 or "",
|
|
|
|
|
"phone": c.phone_1 or "",
|
|
|
|
|
"mobile": c.phone_2 or "",
|
|
|
|
|
"city": c.mailing_city or "",
|
|
|
|
|
"postalcode": c.mailing_postalcode or "",
|
|
|
|
|
"country": c.mailing_country or "",
|
|
|
|
|
}
|
|
|
|
|
for c in contacts
|
|
|
|
|
]
|
|
|
|
|
return helpers.write_csv(rows, headers).decode("utf-8")
|
2026-07-26 02:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def export_companies_csv(
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
tenant_id: uuid.UUID,
|
2026-07-29 02:11:29 +02:00
|
|
|
user_id: uuid.UUID | None = None,
|
|
|
|
|
is_system_admin: bool = False,
|
2026-07-26 02:35:44 +02:00
|
|
|
) -> str:
|
|
|
|
|
"""Export companies (Contact.type == 'company') as CSV string."""
|
|
|
|
|
q = (
|
|
|
|
|
select(Contact)
|
|
|
|
|
.where(
|
|
|
|
|
Contact.tenant_id == tenant_id,
|
|
|
|
|
Contact.deleted_at.is_(None),
|
|
|
|
|
Contact.type == "company",
|
|
|
|
|
)
|
|
|
|
|
.order_by(Contact.name)
|
|
|
|
|
)
|
2026-07-29 02:11:29 +02:00
|
|
|
if user_id:
|
|
|
|
|
q = await apply_visibility_filter(
|
|
|
|
|
db, q, "contact", Contact, user_id, tenant_id, is_system_admin
|
|
|
|
|
)
|
2026-07-26 02:35:44 +02:00
|
|
|
result = await db.execute(q)
|
|
|
|
|
companies = result.scalars().all()
|
|
|
|
|
|
2026-08-13 22:43:33 +02:00
|
|
|
headers = ["id", "type", "name", "email", "phone", "website", "city", "postalcode", "country"]
|
|
|
|
|
rows = [
|
|
|
|
|
{
|
|
|
|
|
"id": str(c.id),
|
|
|
|
|
"type": c.type or "company",
|
|
|
|
|
"name": c.name or "",
|
|
|
|
|
"email": c.email_1 or "",
|
|
|
|
|
"phone": c.phone_1 or "",
|
|
|
|
|
"website": c.website or "",
|
|
|
|
|
"city": c.mailing_city or "",
|
|
|
|
|
"postalcode": c.mailing_postalcode or "",
|
|
|
|
|
"country": c.mailing_country or "",
|
|
|
|
|
}
|
|
|
|
|
for c in companies
|
|
|
|
|
]
|
|
|
|
|
return helpers.write_csv(rows, headers).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ─── Preview & Validate ──────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
def preview_import(
|
|
|
|
|
filename: str,
|
|
|
|
|
content: bytes,
|
|
|
|
|
entity_type: str,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Parse a file and return preview data (first 10 rows) + mapping suggestion.
|
|
|
|
|
|
|
|
|
|
Does NOT touch the database.
|
|
|
|
|
"""
|
|
|
|
|
rows = helpers.parse_file(filename, content)
|
|
|
|
|
columns = list(rows[0].keys()) if rows else []
|
|
|
|
|
preview_rows = rows[:10]
|
|
|
|
|
|
|
|
|
|
if entity_type == "contacts":
|
|
|
|
|
target_fields = CONTACT_TARGET_FIELDS
|
|
|
|
|
elif entity_type == "companies":
|
|
|
|
|
target_fields = COMPANY_TARGET_FIELDS
|
|
|
|
|
else:
|
|
|
|
|
target_fields = []
|
|
|
|
|
|
|
|
|
|
mapping_suggestion = helpers.suggest_mapping(columns, target_fields)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"total_rows": len(rows),
|
|
|
|
|
"columns": columns,
|
|
|
|
|
"preview_rows": preview_rows,
|
|
|
|
|
"mapping_suggestion": mapping_suggestion,
|
|
|
|
|
"target_fields": target_fields,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_import(
|
|
|
|
|
filename: str,
|
|
|
|
|
content: bytes,
|
|
|
|
|
entity_type: str,
|
|
|
|
|
field_mapping: dict[str, str] | None = None,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Validate all rows against mapping and return error report without importing."""
|
|
|
|
|
rows = helpers.parse_file(filename, content)
|
|
|
|
|
total = len(rows)
|
|
|
|
|
errors: list[dict] = []
|
|
|
|
|
valid_count = 0
|
|
|
|
|
|
|
|
|
|
for idx, row in enumerate(rows, start=1):
|
|
|
|
|
if field_mapping:
|
|
|
|
|
row = helpers.map_fields(row, field_mapping)
|
|
|
|
|
if entity_type == "contacts":
|
|
|
|
|
row = _normalize_contact_row(row)
|
|
|
|
|
if not row["firstname"] and not row["surname"]:
|
|
|
|
|
errors.append({"row": idx, "field": "", "message": "Missing required field: firstname or surname"})
|
|
|
|
|
continue
|
|
|
|
|
row_errors = helpers.validate_row(row, [], CONTACT_VALIDATORS)
|
|
|
|
|
elif entity_type == "companies":
|
|
|
|
|
row = _normalize_company_row(row)
|
|
|
|
|
row_errors = helpers.validate_row(row, ["name"], COMPANY_VALIDATORS)
|
|
|
|
|
else:
|
|
|
|
|
errors.append({"row": idx, "field": "", "message": f"Unknown entity_type: {entity_type}"})
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if row_errors:
|
|
|
|
|
for e in row_errors:
|
|
|
|
|
errors.append({"row": idx, "field": "", "message": e})
|
|
|
|
|
else:
|
|
|
|
|
valid_count += 1
|
|
|
|
|
|
|
|
|
|
# Count unique failed rows
|
|
|
|
|
failed_row_count = len({e["row"] for e in errors})
|
|
|
|
|
return helpers.build_import_result(
|
|
|
|
|
total=total,
|
|
|
|
|
succeeded=valid_count,
|
|
|
|
|
failed=failed_row_count,
|
|
|
|
|
errors=errors,
|
|
|
|
|
dry_run=True,
|
2026-07-26 02:35:44 +02:00
|
|
|
)
|