from collections.abc import Iterable, 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 summaries(self, customer_ids: Iterable[int]) -> dict[int, CustomerSummary]: ids = set(customer_ids) if not ids: return {} qs = CustomerModel.objects.filter(pk__in=ids) return { m.pk: _summary(m) for m in qs.only("id", "name", "type", "business_id", "is_internal") } 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)