diff --git a/backend/routers/items.py b/backend/routers/items.py index 86a906c0..20815d37 100644 --- a/backend/routers/items.py +++ b/backend/routers/items.py @@ -97,13 +97,22 @@ def create_item( current_user: auth.TokenData = Depends(auth.get_current_user) ): """[C-01] Create item — only for authenticated users. [M-02] user_id from token.""" + # [DUPLICATE CHECK] Prevent duplicate barcodes (part numbers) + if item.barcode: + existing = db.query(models.Item).filter(models.Item.barcode == item.barcode).first() + if existing: + raise HTTPException( + status_code=409, + detail=f"Item with barcode '{item.barcode}' already exists (ID: {existing.id}). Update it instead or use a different barcode." + ) + # [AUTO-PERSIST] Create Category/Color if not exists if item.category: cat = db.query(models.Category).filter(models.Category.name == item.category).first() if not cat: db.add(models.Category(name=item.category)) db.commit() - + if item.color: col = db.query(models.Color).filter(models.Color.name == item.color).first() if not col: diff --git a/frontend/app/page.tsx b/frontend/app/page.tsx index dd50f739..1ee39f23 100644 --- a/frontend/app/page.tsx +++ b/frontend/app/page.tsx @@ -170,8 +170,15 @@ export default function Home() { await inventoryApi.createItem(currentUser.id, itemData); toast.success("Item saved to cloud catalog!"); } catch (err: any) { - console.error("Cloud sync failed:", err.message); - toast.error("Item saved locally. Cloud sync failed."); + const status = err.response?.status; + const detail = err.response?.data?.detail || err.message; + console.error("Cloud sync failed:", detail); + + if (status === 409) { + toast.error(`Item already exists. ${detail}`); + } else { + toast.error("Item saved locally. Cloud sync failed."); + } } } else if (!isOnline) { toast.success("Item saved locally. Will sync when online.");