From 10071d7f18420104f1c939736714460c1cc84b65 Mon Sep 17 00:00:00 2001 From: Chaim Date: Sat, 4 Apr 2026 11:21:06 +0000 Subject: [PATCH] Prevent duplicate Paperclip projects: check existing before creating Co-Authored-By: Claude Opus 4.6 (1M context) --- web/paperclip_client.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/web/paperclip_client.py b/web/paperclip_client.py index df868dd..ddec65e 100644 --- a/web/paperclip_client.py +++ b/web/paperclip_client.py @@ -42,23 +42,39 @@ async def create_project( appeal_type: str = "רישוי", color: str = "#6366f1", ) -> dict: - """Create a project in the Paperclip embedded DB.""" + """Create a project in the Paperclip embedded DB, or return existing one.""" company_id = _get_company_id(appeal_type) - project_id = str(uuid.uuid4()) + prefix = "CMP" if _get_company_id(appeal_type) == COMPANIES["licensing"] else "CMPA" conn = await asyncpg.connect(PAPERCLIP_DB_URL) try: + # Check for existing project with this case number + existing = await conn.fetchrow( + "SELECT id, name FROM projects WHERE name LIKE $1 AND company_id = $2::uuid", + f"%{case_number}%", company_id, + ) + if existing: + return { + "id": str(existing["id"]), + "company_id": company_id, + "name": existing["name"], + "url": f"https://pc.nautilus.marcusgroup.org/{prefix}/projects/{existing['id']}/issues", + "existing": True, + } + + project_id = str(uuid.uuid4()) + project_name = f"ערר {case_number} — {title}"[:200] await conn.execute( """INSERT INTO projects (id, company_id, name, description, status, color) - VALUES ($1, $2::uuid, $3, $4, 'backlog', $5) - ON CONFLICT DO NOTHING""", - project_id, company_id, f"ערר {case_number} — {title}"[:200], description[:500] if description else "", color, + VALUES ($1, $2::uuid, $3, $4, 'backlog', $5)""", + project_id, company_id, project_name, description[:500] if description else "", color, ) return { "id": project_id, "company_id": company_id, - "name": f"ערר {case_number} — {title}", - "url": f"https://pc.nautilus.marcusgroup.org/{'CMP' if 'licensing' in appeal_type or appeal_type == 'רישוי' else 'CMPA'}/projects/{project_id}/issues", + "name": project_name, + "url": f"https://pc.nautilus.marcusgroup.org/{prefix}/projects/{project_id}/issues", + "existing": False, } finally: await conn.close()