compliance/api/company_profile_routes.py (640 LOC) -> 154 LOC thin routes.
Unusual for this repo: persistence uses raw SQL via sqlalchemy.text()
because the underlying compliance_company_profiles table has ~45 columns
with complex jsonb coercion and there is no SQLAlchemy model for it.
New files:
compliance/schemas/company_profile.py (127) — 4 request/response models
compliance/services/company_profile_service.py (340) — Service class + row_to_response + log_audit
compliance/services/_company_profile_sql.py (139) — 70-line INSERT/UPDATE statements
separated for readability
Minor behavioral improvement: the handlers now use Depends(get_db) for
session management instead of the bespoke `db = SessionLocal(); try: ...
finally: db.close()` pattern. This makes the routes consistent with
every other refactored service, fixes the broken-ness under test
dependency_overrides, and removes 6 duplicate try/finally blocks.
Legacy exports preserved: CompanyProfileRequest, CompanyProfileResponse,
AuditEntryResponse, AuditListResponse, row_to_response, and log_audit are
re-exported from compliance.api.company_profile_routes so that the two
existing test files
(tests/test_company_profile_routes.py, tests/test_company_profile_extend.py)
keep importing from the same path.
Pre-existing broken tests noted: 6 tests in those files feed a 40-tuple
row into row_to_response, but _BASE_COLUMNS_LIST has 46 columns (has had
since the Phase 2 Stammdaten extension). These tests fail on main too
(verified via `git stash` round-trip). Not fixed in this commit — they
require a rewrite of the test's _make_row helper, which is out of scope
for a pure structural refactor. Flagged for follow-up.
Verified:
- 173/173 pytest compliance/tests/ tests/contracts/ pass
- OpenAPI 360/484 unchanged
- mypy compliance/ -> Success on 127 source files
- company_profile_routes.py 640 -> 154 LOC
- All new files under soft 300 target except service (340, under hard 500)
- Hard-cap violations: 15 -> 14
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
140 lines
6.6 KiB
Python
140 lines
6.6 KiB
Python
"""
|
|
Internal raw-SQL helpers for company_profile_service.
|
|
|
|
Separated from the service class because the INSERT/UPDATE statements are
|
|
~70 lines each; keeping them here lets the service module stay readable.
|
|
"""
|
|
|
|
import json
|
|
from typing import Any, Optional
|
|
|
|
from sqlalchemy import text
|
|
from sqlalchemy.orm import Session
|
|
|
|
from compliance.schemas.company_profile import CompanyProfileRequest
|
|
|
|
|
|
def build_upsert_params(
|
|
tid: str, pid: Optional[str], profile: CompanyProfileRequest
|
|
) -> dict[str, Any]:
|
|
return {
|
|
"tid": tid,
|
|
"pid": pid,
|
|
"company_name": profile.company_name,
|
|
"legal_form": profile.legal_form,
|
|
"industry": profile.industry,
|
|
"founded_year": profile.founded_year,
|
|
"business_model": profile.business_model,
|
|
"offerings": json.dumps(profile.offerings),
|
|
"offering_urls": json.dumps(profile.offering_urls),
|
|
"company_size": profile.company_size,
|
|
"employee_count": profile.employee_count,
|
|
"annual_revenue": profile.annual_revenue,
|
|
"hq_country": profile.headquarters_country,
|
|
"hq_country_other": profile.headquarters_country_other,
|
|
"hq_street": profile.headquarters_street,
|
|
"hq_zip": profile.headquarters_zip,
|
|
"hq_city": profile.headquarters_city,
|
|
"hq_state": profile.headquarters_state,
|
|
"has_intl": profile.has_international_locations,
|
|
"intl_countries": json.dumps(profile.international_countries),
|
|
"target_markets": json.dumps(profile.target_markets),
|
|
"jurisdiction": profile.primary_jurisdiction,
|
|
"is_controller": profile.is_data_controller,
|
|
"is_processor": profile.is_data_processor,
|
|
"uses_ai": profile.uses_ai,
|
|
"ai_use_cases": json.dumps(profile.ai_use_cases),
|
|
"dpo_name": profile.dpo_name,
|
|
"dpo_email": profile.dpo_email,
|
|
"legal_name": profile.legal_contact_name,
|
|
"legal_email": profile.legal_contact_email,
|
|
"machine_builder": json.dumps(profile.machine_builder) if profile.machine_builder else None,
|
|
"is_complete": profile.is_complete,
|
|
"repos": json.dumps(profile.repos),
|
|
"document_sources": json.dumps(profile.document_sources),
|
|
"processing_systems": json.dumps(profile.processing_systems),
|
|
"ai_systems": json.dumps(profile.ai_systems),
|
|
"technical_contacts": json.dumps(profile.technical_contacts),
|
|
"subject_to_nis2": profile.subject_to_nis2,
|
|
"subject_to_ai_act": profile.subject_to_ai_act,
|
|
"subject_to_iso27001": profile.subject_to_iso27001,
|
|
"supervisory_authority": profile.supervisory_authority,
|
|
"review_cycle_months": profile.review_cycle_months,
|
|
}
|
|
|
|
|
|
def execute_update(
|
|
db: Session,
|
|
params: dict[str, Any],
|
|
completed_at_sql: str,
|
|
where_clause: str,
|
|
) -> None:
|
|
db.execute(
|
|
text(f"""UPDATE compliance_company_profiles SET
|
|
company_name = :company_name, legal_form = :legal_form,
|
|
industry = :industry, founded_year = :founded_year,
|
|
business_model = :business_model, offerings = :offerings::jsonb,
|
|
offering_urls = :offering_urls::jsonb,
|
|
company_size = :company_size, employee_count = :employee_count,
|
|
annual_revenue = :annual_revenue,
|
|
headquarters_country = :hq_country, headquarters_country_other = :hq_country_other,
|
|
headquarters_street = :hq_street, headquarters_zip = :hq_zip,
|
|
headquarters_city = :hq_city, headquarters_state = :hq_state,
|
|
has_international_locations = :has_intl,
|
|
international_countries = :intl_countries::jsonb,
|
|
target_markets = :target_markets::jsonb, primary_jurisdiction = :jurisdiction,
|
|
is_data_controller = :is_controller, is_data_processor = :is_processor,
|
|
uses_ai = :uses_ai, ai_use_cases = :ai_use_cases::jsonb,
|
|
dpo_name = :dpo_name, dpo_email = :dpo_email,
|
|
legal_contact_name = :legal_name, legal_contact_email = :legal_email,
|
|
machine_builder = :machine_builder::jsonb, is_complete = :is_complete,
|
|
repos = :repos::jsonb, document_sources = :document_sources::jsonb,
|
|
processing_systems = :processing_systems::jsonb,
|
|
ai_systems = :ai_systems::jsonb, technical_contacts = :technical_contacts::jsonb,
|
|
subject_to_nis2 = :subject_to_nis2, subject_to_ai_act = :subject_to_ai_act,
|
|
subject_to_iso27001 = :subject_to_iso27001,
|
|
supervisory_authority = :supervisory_authority,
|
|
review_cycle_months = :review_cycle_months,
|
|
updated_at = NOW(), completed_at = {completed_at_sql}
|
|
WHERE {where_clause}"""),
|
|
params,
|
|
)
|
|
|
|
|
|
def execute_insert(
|
|
db: Session,
|
|
params: dict[str, Any],
|
|
completed_at_sql: str,
|
|
) -> None:
|
|
db.execute(
|
|
text(f"""INSERT INTO compliance_company_profiles
|
|
(tenant_id, project_id, company_name, legal_form, industry, founded_year,
|
|
business_model, offerings, offering_urls,
|
|
company_size, employee_count, annual_revenue,
|
|
headquarters_country, headquarters_country_other,
|
|
headquarters_street, headquarters_zip, headquarters_city, headquarters_state,
|
|
has_international_locations, international_countries,
|
|
target_markets, primary_jurisdiction,
|
|
is_data_controller, is_data_processor, uses_ai, ai_use_cases,
|
|
dpo_name, dpo_email, legal_contact_name, legal_contact_email,
|
|
machine_builder, is_complete, completed_at,
|
|
repos, document_sources, processing_systems, ai_systems, technical_contacts,
|
|
subject_to_nis2, subject_to_ai_act, subject_to_iso27001,
|
|
supervisory_authority, review_cycle_months)
|
|
VALUES (:tid, :pid, :company_name, :legal_form, :industry, :founded_year,
|
|
:business_model, :offerings::jsonb, :offering_urls::jsonb,
|
|
:company_size, :employee_count, :annual_revenue,
|
|
:hq_country, :hq_country_other,
|
|
:hq_street, :hq_zip, :hq_city, :hq_state,
|
|
:has_intl, :intl_countries::jsonb,
|
|
:target_markets::jsonb, :jurisdiction,
|
|
:is_controller, :is_processor, :uses_ai, :ai_use_cases::jsonb,
|
|
:dpo_name, :dpo_email, :legal_name, :legal_email,
|
|
:machine_builder::jsonb, :is_complete, {completed_at_sql},
|
|
:repos::jsonb, :document_sources::jsonb, :processing_systems::jsonb,
|
|
:ai_systems::jsonb, :technical_contacts::jsonb,
|
|
:subject_to_nis2, :subject_to_ai_act, :subject_to_iso27001,
|
|
:supervisory_authority, :review_cycle_months)"""),
|
|
params,
|
|
)
|