Skip to content

Commit

Permalink
Update products for a catalog when updated in integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslinhares committed Feb 1, 2024
1 parent b64f924 commit 2cefc19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 3 additions & 4 deletions temba/channels/types/whatsapp/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,21 @@ def get_api_catalogs(self, channel):
HTTPLog.create_from_exception(HTTPLog.WHATSAPP_CATALOGS_SYNCED, url, e, start, channel=channel)
return [], False

def get_api_products(self, channel, catalog):
def get_api_products(self, channel, facebook_catalog_id):
if (
CONFIG_FB_BUSINESS_ID not in channel.config or CONFIG_FB_ACCESS_TOKEN not in channel.config
): # pragma: no cover
return [], False

catalog_id = catalog.facebook_catalog_id
if not catalog_id: # pragma: no cover
if not facebook_catalog_id: # pragma: no cover
return [], False

start = timezone.now()
try:
# Retrieve the template domain, fallback to the default for channels
# that have been setup earlier for backwards compatibility
facebook_product_domain = "graph.facebook.com"
url = PRODUCT_LIST_URL % (facebook_product_domain, catalog_id)
url = PRODUCT_LIST_URL % (facebook_product_domain, facebook_catalog_id)
product_data = []
while url:
response = requests.get(
Expand Down
14 changes: 13 additions & 1 deletion temba/utils/whatsapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,20 @@ def update_channel_catalogs_status(channel, facebook_catalog_id, is_active):
if is_active:
Catalog.objects.filter(channel=channel, facebook_catalog_id=facebook_catalog_id).update(is_active=True)

sync_products_catalogs(channel, facebook_catalog_id)

return True

def sync_products_catalogs(channel, facebook_catalog_id):
try:
products_data, valid = channel.get_type().get_api_products(channel, facebook_catalog_id)

catalog = Catalog.objects.filter(channel=channel, facebook_catalog_id=facebook_catalog_id)
update_local_products(catalog, products_data, channel)

except Exception as e:
logger.error(f"Error refreshing WhatsApp catalog and products: {str(e)}", exc_info=True)


def set_false_is_active_catalog(channel, catalogs_data):
channel.config["catalog_id"] = ""
Expand Down Expand Up @@ -327,7 +339,7 @@ def refresh_whatsapp_catalog_and_products():
for channel in Channel.objects.filter(is_active=True, channel_type="WAC"):
for catalog in Catalog.objects.filter(channel=channel, is_active=True):
# Fetch products for each catalog
products_data, valid = channel.get_type().get_api_products(channel, catalog)
products_data, valid = channel.get_type().get_api_products(channel, catalog.facebook_catalog_id)
if not valid:
continue

Expand Down

0 comments on commit 2cefc19

Please sign in to comment.