Prevent duplicate Paperclip projects: check existing before creating

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 11:21:06 +00:00
parent 5fc52ce530
commit 10071d7f18

View File

@@ -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()