Skip to content

Commit

Permalink
Revert "feat: Optimize Contacts Sync (#271)"
Browse files Browse the repository at this point in the history
This reverts commit bcaa7ef.
  • Loading branch information
Shwetabhk committed Nov 16, 2023
1 parent bcaa7ef commit d086c03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 45 deletions.
96 changes: 53 additions & 43 deletions apps/xero/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,12 @@ def get_suppliers(self):
tenant_mapping = TenantMapping.objects.get(workspace_id=self.workspace_id)
self.connection.set_tenant_id(tenant_mapping.tenant_id)

params = {
'where': 'IsSupplier=true',
'includeArchived': 'false',
}

suppliers_generator = self.connection.contacts.list_all_generator(**params)
suppliers_generator = self.connection.contacts.list_all_generator()

Check warning on line 71 in apps/xero/utils.py

View check run for this annotation

Codecov / codecov/patch

apps/xero/utils.py#L71

Added line #L71 was not covered by tests
merchant_names: List[str] = []

for suppliers in suppliers_generator:
for supplier in suppliers["Contacts"]:
merchant_names.append(supplier["Name"])

sleep(1)

if supplier["IsSupplier"] and supplier["ContactStatus"] == "ACTIVE":
merchant_names.append(supplier["Name"])

Check warning on line 76 in apps/xero/utils.py

View check run for this annotation

Codecov / codecov/patch

apps/xero/utils.py#L75-L76

Added lines #L75 - L76 were not covered by tests
return merchant_names

def get_or_create_contact(
Expand Down Expand Up @@ -276,13 +268,8 @@ def sync_contacts(self):

updated_at = get_last_synced_at(self.workspace_id, "CONTACT")

params = {
'where': 'IsCustomer=false',
'includeArchived': 'true'
}

contacts_generator = self.connection.contacts.list_all_generator(
modified_after=updated_at, **params
modified_after=updated_at
)

for contacts in contacts_generator:
Expand All @@ -301,14 +288,12 @@ def sync_contacts(self):
"value": contact["Name"],
"destination_id": contact["ContactID"],
"detail": detail,
"active": True if contact["ContactStatus"] == "ACTIVE" else False,
}
)

DestinationAttribute.bulk_create_or_update_destination_attributes(
contact_attributes, "CONTACT", self.workspace_id, True
)
sleep(1)

return []

Expand All @@ -320,36 +305,61 @@ def sync_customers(self):

self.connection.set_tenant_id(tenant_mapping.tenant_id)

params = {
'where': 'IsCustomer=true',
'includeArchived': 'true',
}
customers_generator = self.connection.contacts.list_all_generator()

customers_generator = self.connection.contacts.list_all_generator(**params)
customer_attributes = []

for customers in customers_generator:
customer_attributes = []
destination_attributes = DestinationAttribute.objects.filter(
workspace_id=self.workspace_id,
attribute_type="CUSTOMER",
display_name="Customer",
).values("destination_id", "value", "detail")

disabled_fields_map = {}

for destination_attribute in destination_attributes:
disabled_fields_map[destination_attribute["destination_id"]] = {
"value": destination_attribute["value"],
"detail": destination_attribute["detail"],
}

for customers in customers_generator:
for customer in customers["Contacts"]:
customer_attributes.append(
{
"attribute_type": "CUSTOMER",
"display_name": "Customer",
"value": customer["Name"],
"destination_id": customer["ContactID"],
"detail": {
"email": customer["EmailAddress"]
if "EmailAddress" in customer
else None
},
"active": True if customer["ContactStatus"] == "ACTIVE" else False,
}
)
if customer["IsCustomer"]:
customer_attributes.append(
{
"attribute_type": "CUSTOMER",
"display_name": "Customer",
"value": customer["Name"],
"destination_id": customer["ContactID"],
"detail": {
"email": customer["EmailAddress"]
if "EmailAddress" in customer
else None
},
"active": True if customer["ContactStatus"] == "ACTIVE" else False,
}
)

DestinationAttribute.bulk_create_or_update_destination_attributes(
customer_attributes, "CUSTOMER", self.workspace_id, True
if (customer["ContactStatus"] == "ACTIVE" and customer["ContactID"] in disabled_fields_map):
disabled_fields_map.pop(customer["ContactID"])
sleep(0.5)

for destination_id in disabled_fields_map:
customer_attributes.append(
{
"attribute_type": "CUSTOMER",
"display_name": "customer",
"value": disabled_fields_map[destination_id]["value"],
"destination_id": destination_id,
"active": False,
"detail": disabled_fields_map[destination_id]["detail"],
}
)
sleep(1)

DestinationAttribute.bulk_create_or_update_destination_attributes(
customer_attributes, "CUSTOMER", self.workspace_id, True
)

return []

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Unidecode==1.1.2
urllib3==1.26.11
wcwidth==0.1.8
wrapt==1.12.1
xerosdk==0.14.0
xerosdk==0.13.0
pytest==7.1.2
pytest-cov==3.0.0
pytest-django==4.5.2
Expand Down
2 changes: 1 addition & 1 deletion tests/test_xero/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_sync_customers(mocker, db):
new_customers_count = DestinationAttribute.objects.filter(
workspace_id=workspace_id, attribute_type="CUSTOMER"
).count()
assert new_customers_count == 62
assert new_customers_count == 14


def test_sync_tracking_categories(mocker, db):
Expand Down

0 comments on commit d086c03

Please sign in to comment.