feat(organizations): migrate source storage to polymorphic records

This commit is contained in:
2026-05-19 10:23:53 +02:00
parent 19a7d5a91c
commit 4ca2fa25d5
44 changed files with 7129 additions and 1551 deletions

View File

@@ -0,0 +1,52 @@
"""Identity normalization helpers for organization source ingestion."""
from __future__ import annotations
import re
VALID_INN_LENGTHS = frozenset({10, 12})
VALID_KPP_LENGTH = 9
VALID_OGRN_LENGTH = 13
VALID_OGRIP_LENGTH = 15
def digits(value: str | None) -> str:
"""Return only decimal digits from a source identity value."""
return re.sub(r"\D+", "", str(value or ""))
def normalize_identity_fields(
*,
inn: str | None = "",
kpp: str | None = "",
ogrn: str | None = "",
ogrip: str | None = "",
) -> tuple[str, str, str, str]:
"""Normalize parser identity values to canonical Organization constraints."""
inn_digits = digits(inn)
kpp_digits = digits(kpp)
ogrn_digits = digits(ogrn)
ogrip_digits = digits(ogrip)
normalized_ogrip = ""
if len(ogrip_digits) == VALID_OGRIP_LENGTH:
normalized_ogrip = ogrip_digits
elif len(ogrn_digits) == VALID_OGRIP_LENGTH:
normalized_ogrip = ogrn_digits
normalized_inn = inn_digits if len(inn_digits) in VALID_INN_LENGTHS else ""
normalized_kpp = (
""
if normalized_ogrip
else kpp_digits
if len(kpp_digits) == VALID_KPP_LENGTH
else ""
)
normalized_ogrn = (
""
if normalized_ogrip
else ogrn_digits
if len(ogrn_digits) == VALID_OGRN_LENGTH
else ""
)
return normalized_inn, normalized_kpp, normalized_ogrn, normalized_ogrip