Repository: uselotus/lotus

Issue:
Error creating new customer when address specified
**Describe the bug**
The server returns a 500 internal server error while creating new customer when address specified. 

Request sample (JSON payload):

```json
{
   "customer_id": "test_c1707035604_detailed",
   "customer_name": "test_c1707035604_detailed",
   "default_currency_code": "EUR",
   "email": "test_c1707035604_detailed@sample.com",
   "properties": {
      "remark": "Remark content"
   },
   "tax_rate": "0.15",
   "billing_address": {
      "city": "Paris",
      "country": "FR",
      "line1": "10 Rue de la Paix",
      "line2": "Apartment 5B",
      "postal_code": "75002",
      "state": "Île-de-France"
   },
   "shipping_address": {
      "city": "Berlin",
      "country": "DE",
      "line1": "Friedrichstraße 176-179",
      "line2": "Floor 3, Office 301",
      "postal_code": "10117",
      "state": "Berlin"
   }
}
```

Response sample:

```
ValueError: Cannot assign "(<Address: Address: 10 Rue de la Paix, Paris, Île-de-France, 75002, FR>, False)": "Customer.billing_address" must be a "Address" instance
```

**To Reproduce**
Use the request body described above to create a customer, and observe server output.

**Expected behavior**
A customer should be created




Discussion:

Resolving patch:
diff --git a/backend/api/serializers/model_serializers.py b/backend/api/serializers/model_serializers.py
--- a/backend/api/serializers/model_serializers.py
+++ b/backend/api/serializers/model_serializers.py
@@ -888,17 +888,17 @@ def create(self, validated_data):
         shipping_address = validated_data.pop("shipping_address", None)
         customer = Customer.objects.create(**validated_data)
         if address:
-            address = Address.objects.get_or_create(
+            address, _ = Address.objects.get_or_create(
                 **address, organization=self.context["organization"]
             )
             customer.billing_address = address
         if billing_address:
-            billing_address = Address.objects.get_or_create(
+            billing_address, _ = Address.objects.get_or_create(
                 **billing_address, organization=self.context["organization"]
             )
             customer.billing_address = billing_address
         if shipping_address:
-            shipping_address = Address.objects.get_or_create(
+            shipping_address, _ = Address.objects.get_or_create(
                 **shipping_address, organization=self.context["organization"]
             )
             customer.shipping_address = shipping_address
diff --git a/backend/metering_billing/views/crm_views.py b/backend/metering_billing/views/crm_views.py
--- a/backend/metering_billing/views/crm_views.py
+++ b/backend/metering_billing/views/crm_views.py
@@ -362,7 +362,7 @@ def sync_customers_with_salesforce(organization):
                     fuzzy_country = None
 
                 try:
-                    billing_address = Address.objects.get_or_create(
+                    billing_address, _ = Address.objects.get_or_create(
                         line1=billing_address["street"],
                         city=billing_address["city"],
                         state=billing_address["state"],
@@ -372,7 +372,7 @@ def sync_customers_with_salesforce(organization):
                 except Exception:
                     try:
                         normalized = normalize_address_record(billing_address["street"])
-                        billing_address = Address.objects.get_or_create(
+                        billing_address, _ = Address.objects.get_or_create(
                             line1=normalized["address_line_1"],
                             line2=normalized["address_line_2"],
                             city=normalized["city"],
@@ -391,7 +391,7 @@ def sync_customers_with_salesforce(organization):
                     fuzzy_country = None
 
                 try:
-                    shipping_address = Address.objects.get_or_create(
+                    shipping_address, _ = Address.objects.get_or_create(
                         line1=shipping_address["street"],
                         city=shipping_address["city"],
                         state=shipping_address["state"],
@@ -403,7 +403,7 @@ def sync_customers_with_salesforce(organization):
                         normalized = normalize_address_record(
                             shipping_address["street"]
                         )
-                        shipping_address = Address.objects.get_or_create(
+                        shipping_address, _ = Address.objects.get_or_create(
                             line1=normalized["address_line_1"],
                             line2=normalized["address_line_2"],
                             city=normalized["city"],


Test patch: