Files
petriandClaude Opus 5.5 a81c073d6a 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]>
2026-11-29 13:47:09 +02:00

214 lines
7.4 KiB
Python

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)