Vaihe 1/2: asiakkaat, yhteyshenkilöt ja laskutusyksiköt
- customers-moduuli Clean Architecture -kerroksin: asiakas (yritys/kuluttaja), Y-tunnuksen tarkiste ja yksilöllisyys, yhteyshenkilöt, laskutusyksiköt (verkkolaskuosoite vaatii välittäjätunnuksen) - henkilötunnus salattuna (Fernet) ja HMAC-tiiviste päällekkäisyyksiin; pääkäyttäjä ja johto näkevät ja poistavat, muut näkevät peitettynä - sisäinen asiakas data-migraatiolla, ei poistettavissa - yleinen poistonesto käänteisten relaatioiden perusteella (tulevat PROTECT-viiteavaimet estävät poiston automaattisesti) - asiakkaan sivu: lisäys ja muokkaus dialogissa HTMX:llä, fokuksen hallinta ja ruudunlukijailmoitukset; HTMX näyttää nyt 422-validointivirheet - testit: domain, application, integraatio ja selaintestit (axe); yhteiset fixturet apps/conftest.py:hin, FakeUnitOfWork shared-moduuliin - import-linter: moduulien riippumattomuussäännöt - dokumentit päivitetty Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date
|
||||
|
||||
from apps.customers.domain.exceptions import (
|
||||
InvalidBusinessId,
|
||||
InvalidCustomerName,
|
||||
InvalidEInvoiceAddress,
|
||||
InvalidPersonalIdentityCode,
|
||||
)
|
||||
|
||||
|
||||
def _normalize_spaces(value: str) -> str:
|
||||
return " ".join(value.split())
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CustomerName:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
normalized = _normalize_spaces(self.value)
|
||||
if not normalized or len(normalized) > 200:
|
||||
raise InvalidCustomerName()
|
||||
object.__setattr__(self, "value", normalized)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
_BUSINESS_ID_RE = re.compile(r"^(\d{6,7})-?(\d)$")
|
||||
_BUSINESS_ID_WEIGHTS = (7, 9, 10, 5, 8, 4, 2)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class BusinessId:
|
||||
"""Finnish business ID (Y-tunnus), stored as ``1234567-8``."""
|
||||
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
match = _BUSINESS_ID_RE.match(self.value.strip())
|
||||
if not match:
|
||||
raise InvalidBusinessId()
|
||||
body = match.group(1).zfill(7)
|
||||
check = int(match.group(2))
|
||||
remainder = sum(int(d) * w for d, w in zip(body, _BUSINESS_ID_WEIGHTS, strict=True)) % 11
|
||||
if remainder == 1:
|
||||
raise InvalidBusinessId()
|
||||
expected = 0 if remainder == 0 else 11 - remainder
|
||||
if check != expected:
|
||||
raise InvalidBusinessId()
|
||||
object.__setattr__(self, "value", f"{body}-{check}")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
_PIC_RE = re.compile(r"^(\d{2})(\d{2})(\d{2})([+\-A-FU-Y])(\d{3})([0-9A-Y])$")
|
||||
_PIC_CHECK_CHARS = "0123456789ABCDEFHJKLMNPRSTUVWXY"
|
||||
_CENTURY = {"+": 1800, "-": 1900, "U": 1900, "V": 1900, "W": 1900, "X": 1900, "Y": 1900}
|
||||
_CENTURY.update({c: 2000 for c in "ABCDEF"})
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PersonalIdentityCode:
|
||||
"""Finnish personal identity code (henkilötunnus).
|
||||
|
||||
Specially protected data (tietosuojalaki 29 §): ``repr`` never shows it; use ``masked``
|
||||
for display and only pass ``value`` to the encryption vault.
|
||||
"""
|
||||
|
||||
value: str = field(repr=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
normalized = self.value.strip().upper()
|
||||
match = _PIC_RE.match(normalized)
|
||||
if not match:
|
||||
raise InvalidPersonalIdentityCode()
|
||||
day, month, year, sign, individual, check = match.groups()
|
||||
try:
|
||||
date(_CENTURY[sign] + int(year), int(month), int(day))
|
||||
except ValueError:
|
||||
raise InvalidPersonalIdentityCode() from None
|
||||
if (
|
||||
int(individual) < 2
|
||||
or _PIC_CHECK_CHARS[int(day + month + year + individual) % 31] != check
|
||||
):
|
||||
raise InvalidPersonalIdentityCode()
|
||||
object.__setattr__(self, "value", normalized)
|
||||
|
||||
@property
|
||||
def masked(self) -> str:
|
||||
return mask_personal_id(self.value)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.masked
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"PersonalIdentityCode({self.masked!r})"
|
||||
|
||||
|
||||
def mask_personal_id(code: str) -> str:
|
||||
"""``010190-123A`` → ``••••••-123A`` (birth date hidden)."""
|
||||
return "••••••" + code[6:]
|
||||
|
||||
|
||||
_E_INVOICE_RE = re.compile(r"^[A-Za-z0-9]{12,35}$")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class EInvoiceAddress:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
normalized = "".join(self.value.split()).upper()
|
||||
if not _E_INVOICE_RE.match(normalized):
|
||||
raise InvalidEInvoiceAddress()
|
||||
object.__setattr__(self, "value", normalized)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
Reference in New Issue
Block a user