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

@@ -63,8 +63,7 @@ class CampaignService:
is_active=data.get("is_active", True),
)
db.add(template)
db.commit()
db.refresh(template)
db.flush()
return template
def update_template(self, db: Session, template_id: int, data: dict) -> CampaignTemplate:
@@ -72,14 +71,13 @@ class CampaignService:
for field in ["name", "lead_type", "channel", "language", "subject_template", "body_template", "is_active"]:
if field in data and data[field] is not None:
setattr(template, field, data[field])
db.commit()
db.refresh(template)
db.flush()
return template
def delete_template(self, db: Session, template_id: int) -> bool:
template = self.get_template_by_id(db, template_id)
db.delete(template)
db.commit()
db.flush()
return True
# --- Rendering ---
@@ -143,7 +141,7 @@ class CampaignService:
db.add(send)
sends.append(send)
db.commit()
db.flush()
logger.info("Sent campaign %d to %d prospects", template_id, len(prospect_ids))
return sends

View File

@@ -110,7 +110,7 @@ class EnrichmentService:
if result["has_website"]:
prospect.status = "active"
db.commit()
db.flush()
return result
def scan_tech_stack(self, db: Session, prospect: Prospect) -> ProspectTechProfile | None:
@@ -177,7 +177,7 @@ class EnrichmentService:
profile.scan_source = "basic_http"
prospect.last_tech_scan_at = datetime.now(UTC)
db.commit()
db.flush()
return profile
except Exception as e:
@@ -185,7 +185,7 @@ class EnrichmentService:
if prospect.tech_profile:
prospect.tech_profile.scan_error = str(e)
prospect.last_tech_scan_at = datetime.now(UTC)
db.commit()
db.flush()
return None
def scan_performance(self, db: Session, prospect: Prospect) -> ProspectPerformanceProfile | None:
@@ -251,13 +251,13 @@ class EnrichmentService:
profile.scan_strategy = "mobile"
prospect.last_perf_scan_at = datetime.now(UTC)
db.commit()
db.flush()
return profile
except Exception as e:
logger.error("Performance scan failed for %s: %s", domain, e)
prospect.last_perf_scan_at = datetime.now(UTC)
db.commit()
db.flush()
return None
def scrape_contacts(self, db: Session, prospect: Prospect) -> list[ProspectContact]:
@@ -339,7 +339,7 @@ class EnrichmentService:
break
prospect.last_contact_scrape_at = datetime.now(UTC)
db.commit()
db.flush()
return contacts
def _detect_cms(self, html: str) -> str | None:

View File

@@ -38,8 +38,7 @@ class InteractionService:
created_by_user_id=user_id,
)
db.add(interaction)
db.commit()
db.refresh(interaction)
db.flush()
logger.info("Interaction logged for prospect %d: %s", prospect_id, data["interaction_type"])
return interaction

View File

@@ -137,8 +137,7 @@ class ProspectService:
)
db.add(contact)
db.commit()
db.refresh(prospect)
db.flush()
logger.info("Created prospect: %s (channel=%s)", prospect.display_name, channel)
return prospect
@@ -161,7 +160,7 @@ class ProspectService:
db.add(prospect)
created += 1
db.commit()
db.flush()
logger.info("Bulk import: %d created, %d skipped", created, skipped)
return created, skipped
@@ -178,14 +177,13 @@ class ProspectService:
tags = json.dumps(tags)
prospect.tags = tags
db.commit()
db.refresh(prospect)
db.flush()
return prospect
def delete(self, db: Session, prospect_id: int) -> bool:
prospect = self.get_by_id(db, prospect_id)
db.delete(prospect)
db.commit()
db.flush()
logger.info("Deleted prospect: %d", prospect_id)
return True

View File

@@ -79,7 +79,7 @@ class ScoringService:
score.score_breakdown = json.dumps(breakdown)
score.lead_tier = lead_tier
db.commit()
db.flush()
logger.info("Scored prospect %d: %d (%s)", prospect.id, total, lead_tier)
return score