diff --git a/temba/channels/types/whatsapp/type.py b/temba/channels/types/whatsapp/type.py index 822dd24c192..d8995e9e7e5 100644 --- a/temba/channels/types/whatsapp/type.py +++ b/temba/channels/types/whatsapp/type.py @@ -160,14 +160,13 @@ 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() @@ -175,7 +174,7 @@ def get_api_products(self, channel, catalog): # 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( diff --git a/temba/channels/types/whatsapp_cloud/type.py b/temba/channels/types/whatsapp_cloud/type.py index b48674448f8..45f2026c95f 100644 --- a/temba/channels/types/whatsapp_cloud/type.py +++ b/temba/channels/types/whatsapp_cloud/type.py @@ -153,18 +153,17 @@ 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 not settings.WHATSAPP_ADMIN_SYSTEM_USER_TOKEN: # 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: product_data = [] - url = f"https://graph.facebook.com/v16.0/{catalog_id}/products" + url = f"https://graph.facebook.com/v16.0/{facebook_catalog_id}/products" headers = {"Authorization": f"Bearer {settings.WHATSAPP_ADMIN_SYSTEM_USER_TOKEN}"} while url: diff --git a/temba/utils/whatsapp/tasks.py b/temba/utils/whatsapp/tasks.py index f6471c61622..e1896621e1e 100644 --- a/temba/utils/whatsapp/tasks.py +++ b/temba/utils/whatsapp/tasks.py @@ -194,18 +194,41 @@ def refresh_whatsapp_templates(): update_api_version(channel) -def update_channel_catalogs_status(channel, facebook_catalog_id, is_active): +def update_channel_catalogs_status(channel, facebook_catalog_id, is_active, catalog_name): channel.config["catalog_id"] = facebook_catalog_id if is_active else None channel.save(update_fields=["config"]) + filter_catalog = Catalog.objects.filter(channel=channel, facebook_catalog_id=facebook_catalog_id) Catalog.objects.filter(channel=channel, is_active=True).update(is_active=False) if is_active: - Catalog.objects.filter(channel=channel, facebook_catalog_id=facebook_catalog_id).update(is_active=True) + if filter_catalog: + filter_catalog.update(is_active=True) + else: + Catalog.objects.create( + facebook_catalog_id=facebook_catalog_id, + name=catalog_name, + channel=channel, + org=channel.org, + is_active=is_active, + ) + + 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).first() + + 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"] = "" channel.save(update_fields=["config"]) @@ -327,7 +350,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 diff --git a/temba/wpp_products/serializers.py b/temba/wpp_products/serializers.py index ee9a9a222d8..80fb2143b47 100644 --- a/temba/wpp_products/serializers.py +++ b/temba/wpp_products/serializers.py @@ -2,5 +2,6 @@ class UpdateCatalogSerializer(serializers.Serializer): + name = serializers.CharField(required=True) facebook_catalog_id = serializers.CharField(required=True) is_active = serializers.BooleanField() diff --git a/temba/wpp_products/tests/test_views.py b/temba/wpp_products/tests/test_views.py index 328fe3e2af5..540ecf6e892 100644 --- a/temba/wpp_products/tests/test_views.py +++ b/temba/wpp_products/tests/test_views.py @@ -55,7 +55,7 @@ def test_update_active_catalog(self): url = reverse("catalog-update-status-catalog", args=[self.new_channel.uuid]) self.client.force_login(self.user) - data = {"facebook_catalog_id": "112233445", "is_active": True} + data = {"facebook_catalog_id": "112233445", "is_active": True, "name": "catalog_name"} serializer = UpdateCatalogSerializer(data=data) self.assertTrue(serializer.is_valid(), "Serializer is not valid.") diff --git a/temba/wpp_products/views.py b/temba/wpp_products/views.py index e84c77a4b72..ec549c59f87 100644 --- a/temba/wpp_products/views.py +++ b/temba/wpp_products/views.py @@ -19,7 +19,10 @@ def update_status_catalog(self, request, pk, *args, **kwargs): channel = get_object_or_404(Channel, uuid=pk, is_active=True) update_channel_catalogs_status( - channel, validated_data.get("facebook_catalog_id"), validated_data.get("is_active") + channel, + validated_data.get("facebook_catalog_id"), + validated_data.get("is_active"), + validated_data.get("name"), ) return Response(status=status.HTTP_200_OK)