from django.db.models import ProtectedError from apps.customers.domain.entities import BillingUnit, Contact, Customer from apps.customers.domain.enums import CustomerType from apps.customers.domain.exceptions import BillingUnitInUse, ContactInUse, CustomerInUse from apps.customers.domain.value_objects import ( BusinessId, CustomerName, EInvoiceAddress, PersonalIdentityCode, ) from apps.customers.infrastructure import vault from apps.customers.infrastructure.orm import BillingUnitModel, ContactModel, CustomerModel from apps.customers.infrastructure.references import is_referenced from apps.shared.domain.value_objects import EmailAddress def customer_entity(model: CustomerModel) -> Customer: return Customer.reconstitute( id=model.pk, name=CustomerName(model.name), type=CustomerType(model.type), business_id=BusinessId(model.business_id) if model.business_id else None, is_internal=model.is_internal, has_personal_id=bool(model.personal_identity_code), ) class DjangoCustomerRepository: def get(self, customer_id: int) -> Customer | None: model = CustomerModel.objects.filter(pk=customer_id).first() return customer_entity(model) if model else None def save(self, customer: Customer) -> Customer: model = ( CustomerModel() if customer.id is None else CustomerModel.objects.get(pk=customer.id) ) model.name = customer.name.value model.type = customer.type.value model.business_id = customer.business_id.value if customer.business_id else None if not customer.has_personal_id: model.personal_identity_code = None model.personal_identity_code_hash = None model.save() return customer.with_id(model.pk) def set_personal_id(self, customer_id: int, code: PersonalIdentityCode) -> None: CustomerModel.objects.filter(pk=customer_id).update( personal_identity_code=vault.encrypt(code.value), personal_identity_code_hash=vault.fingerprint(code.value), ) def is_business_id_taken( self, business_id: BusinessId, *, exclude_id: int | None = None ) -> bool: qs = CustomerModel.objects.filter(business_id=business_id.value) if exclude_id is not None: qs = qs.exclude(pk=exclude_id) return qs.exists() def is_personal_id_taken( self, code: PersonalIdentityCode, *, exclude_id: int | None = None ) -> bool: qs = CustomerModel.objects.filter(personal_identity_code_hash=vault.fingerprint(code.value)) if exclude_id is not None: qs = qs.exclude(pk=exclude_id) return qs.exists() def delete(self, customer_id: int) -> None: model = CustomerModel.objects.get(pk=customer_id) if is_referenced(model): raise CustomerInUse() try: model.delete() except ProtectedError: raise CustomerInUse() from None def contact_entity(model: ContactModel) -> Contact: return Contact( id=model.pk, customer_id=model.customer_id, name=model.name, title=model.title, phone=model.phone, email=EmailAddress(model.email) if model.email else None, note=model.note, ) class DjangoContactRepository: def get(self, contact_id: int) -> Contact | None: model = ContactModel.objects.filter(pk=contact_id).first() return contact_entity(model) if model else None def save(self, contact: Contact) -> Contact: model = ContactModel() if contact.id is None else ContactModel.objects.get(pk=contact.id) model.customer_id = contact.customer_id model.name = contact.name model.title = contact.title model.phone = contact.phone model.email = contact.email.value if contact.email else "" model.note = contact.note model.save() return contact.with_id(model.pk) def delete(self, contact_id: int) -> None: model = ContactModel.objects.get(pk=contact_id) if is_referenced(model): raise ContactInUse() try: model.delete() except ProtectedError: raise ContactInUse() from None def billing_unit_entity(model: BillingUnitModel) -> BillingUnit: return BillingUnit( id=model.pk, customer_id=model.customer_id, name=model.name, billing_address=model.billing_address, e_invoice_address=EInvoiceAddress(model.e_invoice_address) if model.e_invoice_address else None, e_invoice_operator=model.e_invoice_operator, reference=model.reference, ) class DjangoBillingUnitRepository: def get(self, billing_unit_id: int) -> BillingUnit | None: model = BillingUnitModel.objects.filter(pk=billing_unit_id).first() return billing_unit_entity(model) if model else None def save(self, billing_unit: BillingUnit) -> BillingUnit: model = ( BillingUnitModel() if billing_unit.id is None else BillingUnitModel.objects.get(pk=billing_unit.id) ) model.customer_id = billing_unit.customer_id model.name = billing_unit.name model.billing_address = billing_unit.billing_address model.e_invoice_address = ( billing_unit.e_invoice_address.value if billing_unit.e_invoice_address else "" ) model.e_invoice_operator = billing_unit.e_invoice_operator model.reference = billing_unit.reference model.save() return billing_unit.with_id(model.pk) def delete(self, billing_unit_id: int) -> None: model = BillingUnitModel.objects.get(pk=billing_unit_id) if is_referenced(model): raise BillingUnitInUse() try: model.delete() except ProtectedError: raise BillingUnitInUse() from None