Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize Contacts Sync #271

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 43 additions & 53 deletions apps/xero/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,20 @@
tenant_mapping = TenantMapping.objects.get(workspace_id=self.workspace_id)
self.connection.set_tenant_id(tenant_mapping.tenant_id)

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

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
'where': 'IsSupplier=true',
'includeArchived': 'false',
}

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

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

View check run for this annotation

Codecov / codecov/patch

apps/xero/utils.py#L76

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

for suppliers in suppliers_generator:
for supplier in suppliers["Contacts"]:
if supplier["IsSupplier"] and supplier["ContactStatus"] == "ACTIVE":
merchant_names.append(supplier["Name"])
merchant_names.append(supplier["Name"])

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

View check run for this annotation

Codecov / codecov/patch

apps/xero/utils.py#L81

Added line #L81 was not covered by tests

sleep(1)

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

View check run for this annotation

Codecov / codecov/patch

apps/xero/utils.py#L83

Added line #L83 was not covered by tests

return merchant_names

def get_or_create_contact(
Expand Down Expand Up @@ -268,8 +276,13 @@

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
modified_after=updated_at, **params
)

for contacts in contacts_generator:
Expand All @@ -288,12 +301,14 @@
"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 @@ -305,61 +320,36 @@

self.connection.set_tenant_id(tenant_mapping.tenant_id)

customers_generator = self.connection.contacts.list_all_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 = {}
params = {
'where': 'IsCustomer=true',
'includeArchived': 'true',
}

for destination_attribute in destination_attributes:
disabled_fields_map[destination_attribute["destination_id"]] = {
"value": destination_attribute["value"],
"detail": destination_attribute["detail"],
}
customers_generator = self.connection.contacts.list_all_generator(**params)

for customers in customers_generator:
for customer in customers["Contacts"]:
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,
}
)
customer_attributes = []

if (customer["ContactStatus"] == "ACTIVE" and customer["ContactID"] in disabled_fields_map):
disabled_fields_map.pop(customer["ContactID"])
sleep(0.5)
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't handle the delete case I think - if delete is not possible in Xero this is fine

}
)

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"],
}
DestinationAttribute.bulk_create_or_update_destination_attributes(
customer_attributes, "CUSTOMER", self.workspace_id, True
)

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

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.13.0
xerosdk==0.14.0
pytest==7.1.2
pytest-cov==3.0.0
pytest-django==4.5.2
Expand Down
Loading