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:
2026-11-29 13:47:09 +02:00
co-authored by Claude Opus 5.5
parent e7b2745916
commit a81c073d6a
68 changed files with 3470 additions and 144 deletions
+82
View File
@@ -0,0 +1,82 @@
from dataclasses import dataclass
from apps.customers.domain.enums import CustomerType
@dataclass(frozen=True, slots=True)
class CustomerDetailsData:
"""Customer form input.
``personal_identity_code``: empty keeps the stored code, a value replaces it.
``remove_personal_id``: remove the stored code (admin and management only).
"""
name: str
type: CustomerType
business_id: str = ""
personal_identity_code: str = ""
remove_personal_id: bool = False
@dataclass(frozen=True, slots=True)
class ContactData:
name: str
title: str = ""
phone: str = ""
email: str = ""
note: str = ""
@dataclass(frozen=True, slots=True)
class BillingUnitData:
name: str
billing_address: str = ""
e_invoice_address: str = ""
e_invoice_operator: str = ""
reference: str = ""
@dataclass(frozen=True, slots=True)
class CustomerSummary:
"""What other modules (jobs, sales) see of a customer."""
id: int
name: str
type: CustomerType
business_id: str | None
is_internal: bool
@dataclass(frozen=True, slots=True)
class CustomerDetails:
id: int
name: str
type: CustomerType
business_id: str | None
is_internal: bool
personal_id_masked: str | None
personal_id_full: str | None # only when explicitly requested by an authorized caller
@dataclass(frozen=True, slots=True)
class ContactView:
id: int
customer_id: int
name: str
title: str
phone: str
email: str
note: str
in_use: bool
@dataclass(frozen=True, slots=True)
class BillingUnitView:
id: int
customer_id: int
name: str
billing_address: str
e_invoice_address: str
e_invoice_operator: str
reference: str
in_use: bool
+38
View File
@@ -0,0 +1,38 @@
from collections.abc import Sequence
from typing import Protocol
from apps.customers.application.dto import (
BillingUnitView,
ContactView,
CustomerDetails,
CustomerSummary,
)
from apps.customers.domain.enums import CustomerType
class CustomerQueries(Protocol):
"""Public read interface of the customers module."""
def list_customers(
self, *, search: str = "", type: CustomerType | None = None
) -> Sequence[CustomerSummary]:
"""Search by name or business ID (never by personal identity code); internal first."""
...
def search(self, term: str, *, limit: int = 20) -> Sequence[CustomerSummary]:
"""Autocomplete for other modules (e.g. job intake)."""
...
def get_summary(self, customer_id: int) -> CustomerSummary | None: ...
def get_details(
self, customer_id: int, *, include_full_personal_id: bool = False
) -> CustomerDetails | None: ...
def contacts(self, customer_id: int) -> Sequence[ContactView]: ...
def billing_units(self, customer_id: int) -> Sequence[BillingUnitView]: ...
def is_deletable(self, customer_id: int) -> bool:
"""Not internal and nothing (contacts, billing units, jobs, sales …) refers to it."""
...
+213
View File
@@ -0,0 +1,213 @@
from apps.customers.application.dto import BillingUnitData, ContactData, CustomerDetailsData
from apps.customers.domain.entities import BillingUnit, Contact, Customer
from apps.customers.domain.exceptions import (
BillingUnitNotFound,
BusinessIdTaken,
CompanyCannotHavePersonalId,
ContactNotFound,
CustomerNotFound,
InternalCustomerCannotBeDeleted,
PersonalIdRemovalNotAllowed,
PersonalIdTaken,
)
from apps.customers.domain.repositories import (
BillingUnitRepository,
ContactRepository,
CustomerRepository,
)
from apps.customers.domain.value_objects import (
BusinessId,
CustomerName,
EInvoiceAddress,
PersonalIdentityCode,
)
from apps.shared.application.ports import UnitOfWork
from apps.shared.domain.value_objects import EmailAddress
def _business_id(value: str) -> BusinessId | None:
return BusinessId(value) if value.strip() else None
def _personal_id(value: str) -> PersonalIdentityCode | None:
return PersonalIdentityCode(value) if value.strip() else None
class RegisterCustomer:
def __init__(self, repo: CustomerRepository, uow: UnitOfWork) -> None:
self.repo = repo
self.uow = uow
def execute(self, data: CustomerDetailsData) -> int:
name = CustomerName(data.name)
business_id = _business_id(data.business_id)
pic = _personal_id(data.personal_identity_code)
customer = Customer.register(name=name, type=data.type, business_id=business_id)
if pic is not None and not customer.can_have_personal_id:
raise CompanyCannotHavePersonalId()
with self.uow:
if business_id is not None and self.repo.is_business_id_taken(business_id):
raise BusinessIdTaken()
if pic is not None and self.repo.is_personal_id_taken(pic):
raise PersonalIdTaken()
saved = self.repo.save(customer)
assert saved.id is not None
if pic is not None:
self.repo.set_personal_id(saved.id, pic)
return saved.id
class UpdateCustomer:
def __init__(self, repo: CustomerRepository, uow: UnitOfWork) -> None:
self.repo = repo
self.uow = uow
def execute(
self, customer_id: int, data: CustomerDetailsData, *, can_manage_personal_id: bool
) -> None:
name = CustomerName(data.name)
business_id = _business_id(data.business_id)
pic = _personal_id(data.personal_identity_code)
if data.remove_personal_id and not can_manage_personal_id:
raise PersonalIdRemovalNotAllowed()
with self.uow:
current = self.repo.get(customer_id)
if current is None:
raise CustomerNotFound()
updated = current.change_details(name=name, type=data.type, business_id=business_id)
if pic is not None and not updated.can_have_personal_id:
raise CompanyCannotHavePersonalId()
if business_id is not None and self.repo.is_business_id_taken(
business_id, exclude_id=customer_id
):
raise BusinessIdTaken()
if pic is not None and self.repo.is_personal_id_taken(pic, exclude_id=customer_id):
raise PersonalIdTaken()
if data.remove_personal_id and pic is None:
updated = updated.with_personal_id(False)
self.repo.save(updated)
if pic is not None:
self.repo.set_personal_id(customer_id, pic)
class DeleteCustomer:
def __init__(self, repo: CustomerRepository, uow: UnitOfWork) -> None:
self.repo = repo
self.uow = uow
def execute(self, customer_id: int) -> None:
with self.uow:
customer = self.repo.get(customer_id)
if customer is None:
raise CustomerNotFound()
if customer.is_internal:
raise InternalCustomerCannotBeDeleted()
self.repo.delete(customer_id)
def _contact(customer_id: int, contact_id: int | None, data: ContactData) -> Contact:
return Contact(
id=contact_id,
customer_id=customer_id,
name=data.name,
title=data.title.strip(),
phone=data.phone.strip(),
email=EmailAddress(data.email) if data.email.strip() else None,
note=data.note.strip(),
)
def _billing_unit(customer_id: int, unit_id: int | None, data: BillingUnitData) -> BillingUnit:
return BillingUnit(
id=unit_id,
customer_id=customer_id,
name=data.name,
billing_address=data.billing_address.strip(),
e_invoice_address=(
EInvoiceAddress(data.e_invoice_address) if data.e_invoice_address.strip() else None
),
e_invoice_operator=data.e_invoice_operator,
reference=data.reference.strip(),
)
class _ContactUseCase:
def __init__(
self, customers: CustomerRepository, contacts: ContactRepository, uow: UnitOfWork
) -> None:
self.customers = customers
self.contacts = contacts
self.uow = uow
def _owned(self, customer_id: int, contact_id: int) -> Contact:
contact = self.contacts.get(contact_id)
if contact is None or contact.customer_id != customer_id:
raise ContactNotFound()
return contact
class AddContact(_ContactUseCase):
def execute(self, customer_id: int, data: ContactData) -> int:
contact = _contact(customer_id, None, data)
with self.uow:
if self.customers.get(customer_id) is None:
raise CustomerNotFound()
saved = self.contacts.save(contact)
assert saved.id is not None
return saved.id
class UpdateContact(_ContactUseCase):
def execute(self, customer_id: int, contact_id: int, data: ContactData) -> None:
contact = _contact(customer_id, contact_id, data)
with self.uow:
self._owned(customer_id, contact_id)
self.contacts.save(contact)
class DeleteContact(_ContactUseCase):
def execute(self, customer_id: int, contact_id: int) -> None:
with self.uow:
self._owned(customer_id, contact_id)
self.contacts.delete(contact_id)
class _BillingUnitUseCase:
def __init__(
self, customers: CustomerRepository, units: BillingUnitRepository, uow: UnitOfWork
) -> None:
self.customers = customers
self.units = units
self.uow = uow
def _owned(self, customer_id: int, unit_id: int) -> BillingUnit:
unit = self.units.get(unit_id)
if unit is None or unit.customer_id != customer_id:
raise BillingUnitNotFound()
return unit
class AddBillingUnit(_BillingUnitUseCase):
def execute(self, customer_id: int, data: BillingUnitData) -> int:
unit = _billing_unit(customer_id, None, data)
with self.uow:
if self.customers.get(customer_id) is None:
raise CustomerNotFound()
saved = self.units.save(unit)
assert saved.id is not None
return saved.id
class UpdateBillingUnit(_BillingUnitUseCase):
def execute(self, customer_id: int, unit_id: int, data: BillingUnitData) -> None:
unit = _billing_unit(customer_id, unit_id, data)
with self.uow:
self._owned(customer_id, unit_id)
self.units.save(unit)
class DeleteBillingUnit(_BillingUnitUseCase):
def execute(self, customer_id: int, unit_id: int) -> None:
with self.uow:
self._owned(customer_id, unit_id)
self.units.delete(unit_id)