feat: duplicate product detection — STEP conflict warnings on Excel import and CAD upload
- Excel preview detects when a product already has a different STEP file linked - Excel preview detects intra-Excel conflicts (same product, different CAD model names) - Product STEP upload warns when replacing an existing file and shows render count - All warnings are non-blocking (amber badges, toast warnings) - LEARNINGS.md: all open items resolved Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -470,6 +470,11 @@ async def upload_product_cad(
|
||||
if not product:
|
||||
raise HTTPException(404, detail="Product not found")
|
||||
|
||||
# Check for STEP replacement warnings before proceeding
|
||||
warnings: list[str] = []
|
||||
existing_render_count = 0
|
||||
old_cad_file_id = product.cad_file_id
|
||||
|
||||
content = await file.read()
|
||||
file_hash = hashlib.sha256(content).hexdigest()
|
||||
|
||||
@@ -477,6 +482,24 @@ async def upload_product_cad(
|
||||
existing_cad = await db.execute(select(CadFile).where(CadFile.file_hash == file_hash))
|
||||
cad_file = existing_cad.scalar_one_or_none()
|
||||
|
||||
# Detect replacement: product already has a different CAD file
|
||||
if old_cad_file_id and (cad_file is None or cad_file.id != old_cad_file_id):
|
||||
old_name = product.cad_file.original_name if product.cad_file else "unknown"
|
||||
warnings.append(
|
||||
f"Replacing existing STEP file '{old_name}' with '{file.filename}'."
|
||||
)
|
||||
# Count existing renders (MediaAssets) for this product
|
||||
from app.domains.media.models import MediaAsset
|
||||
render_count_result = await db.execute(
|
||||
select(func.count(MediaAsset.id)).where(MediaAsset.product_id == product_id)
|
||||
)
|
||||
existing_render_count = render_count_result.scalar() or 0
|
||||
if existing_render_count > 0:
|
||||
warnings.append(
|
||||
f"This product has {existing_render_count} existing render(s) that were "
|
||||
"generated from the previous STEP file. They may no longer match."
|
||||
)
|
||||
|
||||
if cad_file is None:
|
||||
step_dir = Path(settings.upload_dir) / "step_files"
|
||||
step_dir.mkdir(parents=True, exist_ok=True)
|
||||
@@ -511,6 +534,8 @@ async def upload_product_cad(
|
||||
"file_hash": file_hash,
|
||||
"status": "uploaded" if cad_file.processing_status == ProcessingStatus.pending else "already_exists",
|
||||
"product_id": str(product_id),
|
||||
"warnings": warnings,
|
||||
"existing_render_count": existing_render_count,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -39,6 +39,14 @@ class ExcelPreviewRow(BaseModel):
|
||||
has_step: bool = False
|
||||
is_duplicate: bool = False
|
||||
duplicate_of_row: int | None = None
|
||||
# STEP conflict: existing product has a different STEP file than Excel row's name_cad_modell
|
||||
step_conflict: bool = False
|
||||
step_conflict_existing_name: str | None = None
|
||||
step_conflict_excel_name: str | None = None
|
||||
# Intra-Excel conflict: same product key appears with different name_cad_modell
|
||||
cad_name_conflict: bool = False
|
||||
cad_name_conflict_other_name: str | None = None
|
||||
cad_name_conflict_row: int | None = None
|
||||
|
||||
|
||||
class ExcelPreviewResponse(BaseModel):
|
||||
@@ -52,6 +60,8 @@ class ExcelPreviewResponse(BaseModel):
|
||||
has_step_count: int = 0
|
||||
no_step_count: int = 0
|
||||
duplicate_count: int = 0
|
||||
step_conflict_count: int = 0
|
||||
cad_name_conflict_count: int = 0
|
||||
warnings: list[str]
|
||||
rows: list[ExcelPreviewRow]
|
||||
column_headers: list[str] = []
|
||||
@@ -145,6 +155,12 @@ async def upload_excel(
|
||||
has_step=r.get("has_step", False),
|
||||
is_duplicate=r.get("is_duplicate", False),
|
||||
duplicate_of_row=r.get("duplicate_of_row"),
|
||||
step_conflict=r.get("step_conflict", False),
|
||||
step_conflict_existing_name=r.get("step_conflict_existing_name"),
|
||||
step_conflict_excel_name=r.get("step_conflict_excel_name"),
|
||||
cad_name_conflict=r.get("cad_name_conflict", False),
|
||||
cad_name_conflict_other_name=r.get("cad_name_conflict_other_name"),
|
||||
cad_name_conflict_row=r.get("cad_name_conflict_row"),
|
||||
)
|
||||
for r in preview.rows
|
||||
]
|
||||
@@ -195,6 +211,8 @@ async def upload_excel(
|
||||
has_step_count=preview.has_step_count,
|
||||
no_step_count=preview.no_step_count,
|
||||
duplicate_count=preview.duplicate_count,
|
||||
step_conflict_count=preview.step_conflict_count,
|
||||
cad_name_conflict_count=preview.cad_name_conflict_count,
|
||||
warnings=all_warnings,
|
||||
rows=annotated_rows,
|
||||
column_headers=parsed_dict.get("column_headers", []),
|
||||
|
||||
Reference in New Issue
Block a user