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,99 @@
|
||||
from collections.abc import Sequence
|
||||
|
||||
from django.db.models import Q
|
||||
|
||||
from apps.customers.application.dto import (
|
||||
BillingUnitView,
|
||||
ContactView,
|
||||
CustomerDetails,
|
||||
CustomerSummary,
|
||||
)
|
||||
from apps.customers.domain.enums import CustomerType
|
||||
from apps.customers.domain.value_objects import mask_personal_id
|
||||
from apps.customers.infrastructure import vault
|
||||
from apps.customers.infrastructure.orm import BillingUnitModel, ContactModel, CustomerModel
|
||||
from apps.customers.infrastructure.references import is_referenced
|
||||
|
||||
|
||||
def _summary(model: CustomerModel) -> CustomerSummary:
|
||||
return CustomerSummary(
|
||||
id=model.pk,
|
||||
name=model.name,
|
||||
type=CustomerType(model.type),
|
||||
business_id=model.business_id,
|
||||
is_internal=model.is_internal,
|
||||
)
|
||||
|
||||
|
||||
class DjangoCustomerQueries:
|
||||
def list_customers(
|
||||
self, *, search: str = "", type: CustomerType | None = None
|
||||
) -> Sequence[CustomerSummary]:
|
||||
qs = CustomerModel.objects.order_by("-is_internal", "name", "pk")
|
||||
if search:
|
||||
qs = qs.filter(Q(name__icontains=search) | Q(business_id__icontains=search))
|
||||
if type is not None:
|
||||
qs = qs.filter(type=type.value)
|
||||
return [_summary(m) for m in qs.only("id", "name", "type", "business_id", "is_internal")]
|
||||
|
||||
def search(self, term: str, *, limit: int = 20) -> Sequence[CustomerSummary]:
|
||||
return list(self.list_customers(search=term.strip()))[:limit]
|
||||
|
||||
def get_summary(self, customer_id: int) -> CustomerSummary | None:
|
||||
model = CustomerModel.objects.filter(pk=customer_id).first()
|
||||
return _summary(model) if model else None
|
||||
|
||||
def get_details(
|
||||
self, customer_id: int, *, include_full_personal_id: bool = False
|
||||
) -> CustomerDetails | None:
|
||||
model = CustomerModel.objects.filter(pk=customer_id).first()
|
||||
if model is None:
|
||||
return None
|
||||
masked = full = None
|
||||
if model.personal_identity_code:
|
||||
plain = vault.decrypt(model.personal_identity_code)
|
||||
masked = mask_personal_id(plain)
|
||||
full = plain if include_full_personal_id else None
|
||||
return CustomerDetails(
|
||||
id=model.pk,
|
||||
name=model.name,
|
||||
type=CustomerType(model.type),
|
||||
business_id=model.business_id,
|
||||
is_internal=model.is_internal,
|
||||
personal_id_masked=masked,
|
||||
personal_id_full=full,
|
||||
)
|
||||
|
||||
def contacts(self, customer_id: int) -> Sequence[ContactView]:
|
||||
return [
|
||||
ContactView(
|
||||
id=m.pk,
|
||||
customer_id=m.customer_id,
|
||||
name=m.name,
|
||||
title=m.title,
|
||||
phone=m.phone,
|
||||
email=m.email,
|
||||
note=m.note,
|
||||
in_use=is_referenced(m),
|
||||
)
|
||||
for m in ContactModel.objects.filter(customer_id=customer_id)
|
||||
]
|
||||
|
||||
def billing_units(self, customer_id: int) -> Sequence[BillingUnitView]:
|
||||
return [
|
||||
BillingUnitView(
|
||||
id=m.pk,
|
||||
customer_id=m.customer_id,
|
||||
name=m.name,
|
||||
billing_address=m.billing_address,
|
||||
e_invoice_address=m.e_invoice_address,
|
||||
e_invoice_operator=m.e_invoice_operator,
|
||||
reference=m.reference,
|
||||
in_use=is_referenced(m),
|
||||
)
|
||||
for m in BillingUnitModel.objects.filter(customer_id=customer_id)
|
||||
]
|
||||
|
||||
def is_deletable(self, customer_id: int) -> bool:
|
||||
model = CustomerModel.objects.filter(pk=customer_id).first()
|
||||
return model is not None and not model.is_internal and not is_referenced(model)
|
||||
Reference in New Issue
Block a user