refactor(prospecting): migrate SVC-006 transaction control to endpoint level
Some checks failed
CI / validate (push) Has been cancelled
CI / ruff (push) Successful in 10s
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has started running

Move db.commit() from services to API endpoints and Celery tasks.
Services now use db.flush() only; endpoints own the transaction.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 16:40:09 +01:00
parent 78ee05f50e
commit 22ae63b414
12 changed files with 52 additions and 24 deletions

View File

@@ -48,6 +48,7 @@ def create_template(
):
"""Create a new campaign template."""
template = campaign_service.create_template(db, data.model_dump())
db.commit()
return CampaignTemplateResponse.model_validate(template)
@@ -60,6 +61,7 @@ def update_template(
):
"""Update a campaign template."""
template = campaign_service.update_template(db, template_id, data.model_dump(exclude_none=True))
db.commit()
return CampaignTemplateResponse.model_validate(template)
@@ -71,6 +73,7 @@ def delete_template(
):
"""Delete a campaign template."""
campaign_service.delete_template(db, template_id)
db.commit()
return CampaignTemplateDeleteResponse(message="Template deleted")
@@ -98,6 +101,7 @@ def send_campaign(
prospect_ids=data.prospect_ids,
sent_by_user_id=current_admin.user_id,
)
db.commit()
return [CampaignSendResponse.model_validate(s) for s in sends]

View File

@@ -38,6 +38,7 @@ def http_check_single(
"""Run HTTP connectivity check for a single prospect."""
prospect = prospect_service.get_by_id(db, prospect_id)
result = enrichment_service.check_http(db, prospect)
db.commit()
return HttpCheckResult(**result)
@@ -53,6 +54,7 @@ def http_check_batch(
for prospect in prospects:
result = enrichment_service.check_http(db, prospect)
results.append(HttpCheckBatchItem(domain=prospect.domain_name, **result))
db.commit()
return HttpCheckBatchResponse(processed=len(results), results=results)
@@ -65,6 +67,7 @@ def tech_scan_single(
"""Run technology scan for a single prospect."""
prospect = prospect_service.get_by_id(db, prospect_id)
profile = enrichment_service.scan_tech_stack(db, prospect)
db.commit()
return ScanSingleResponse(domain=prospect.domain_name, profile=profile is not None)
@@ -81,6 +84,7 @@ def tech_scan_batch(
result = enrichment_service.scan_tech_stack(db, prospect)
if result:
count += 1
db.commit()
return ScanBatchResponse(processed=len(prospects), successful=count)
@@ -93,6 +97,7 @@ def performance_scan_single(
"""Run PageSpeed audit for a single prospect."""
prospect = prospect_service.get_by_id(db, prospect_id)
profile = enrichment_service.scan_performance(db, prospect)
db.commit()
return ScanSingleResponse(domain=prospect.domain_name, profile=profile is not None)
@@ -109,6 +114,7 @@ def performance_scan_batch(
result = enrichment_service.scan_performance(db, prospect)
if result:
count += 1
db.commit()
return ScanBatchResponse(processed=len(prospects), successful=count)
@@ -121,6 +127,7 @@ def scrape_contacts_single(
"""Scrape contacts for a single prospect."""
prospect = prospect_service.get_by_id(db, prospect_id)
contacts = enrichment_service.scrape_contacts(db, prospect)
db.commit()
return ContactScrapeResponse(domain=prospect.domain_name, contacts_found=len(contacts))
@@ -154,6 +161,7 @@ def full_enrichment(
# Step 5: Compute score
db.refresh(prospect)
score = scoring_service.compute_score(db, prospect)
db.commit()
return FullEnrichmentResponse(
domain=prospect.domain_name,
@@ -174,4 +182,5 @@ def compute_scores_batch(
):
"""Compute or recompute scores for all prospects."""
count = scoring_service.compute_all(db, limit=limit)
db.commit()
return ScoreComputeBatchResponse(scored=count)

View File

@@ -51,6 +51,7 @@ def create_interaction(
user_id=current_admin.user_id,
data=data.model_dump(exclude_none=True),
)
db.commit()
return InteractionResponse.model_validate(interaction)

View File

@@ -82,6 +82,7 @@ def create_prospect(
data.model_dump(exclude_none=True),
captured_by_user_id=current_admin.user_id,
)
db.commit()
return _to_response(prospect)
@@ -94,6 +95,7 @@ def update_prospect(
):
"""Update a prospect."""
prospect = prospect_service.update(db, prospect_id, data.model_dump(exclude_none=True))
db.commit()
return _to_response(prospect)
@@ -105,6 +107,7 @@ def delete_prospect(
):
"""Delete a prospect."""
prospect_service.delete(db, prospect_id)
db.commit()
return ProspectDeleteResponse(message="Prospect deleted")
@@ -125,6 +128,7 @@ async def import_domains(
domains.append(domain)
created, skipped = prospect_service.create_bulk(db, domains, source="csv_import")
db.commit()
return ProspectImportResponse(created=created, skipped=skipped, total=len(domains))