Skip to content

Commit

Permalink
feat: viewsets and models for wpp-cloud catalog and product
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Sep 25, 2023
1 parent 9d5cf80 commit a0d2db2
Show file tree
Hide file tree
Showing 20 changed files with 1,430 additions and 5 deletions.
27 changes: 27 additions & 0 deletions marketplace/clients/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests

from marketplace.clients.exceptions import CustomAPIException


class RequestClient:
def make_request(
self, url: str, method: str, headers=None, data=None, params=None, files=None
):
response = requests.request(

Check warning on line 10 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L10

Added line #L10 was not covered by tests
method=method,
url=url,
headers=headers,
json=data,
timeout=60,
params=params,
files=files,
)
if response.status_code >= 500:
raise CustomAPIException(status_code=response.status_code)
elif response.status_code >= 400:
raise CustomAPIException(

Check warning on line 22 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L19-L22

Added lines #L19 - L22 were not covered by tests
detail=response.json() if response.text else response.text,
status_code=response.status_code,
)

return response

Check warning on line 27 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L27

Added line #L27 was not covered by tests
7 changes: 7 additions & 0 deletions marketplace/clients/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework.exceptions import APIException


class CustomAPIException(APIException):
def __init__(self, detail=None, code=None, status_code=None):
super().__init__(detail, code)
self.status_code = status_code or self.status_code

Check warning on line 7 in marketplace/clients/exceptions.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/exceptions.py#L6-L7

Added lines #L6 - L7 were not covered by tests
217 changes: 217 additions & 0 deletions marketplace/clients/facebook/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import time
import json

from django.conf import settings

from marketplace.clients.base import RequestClient

WHATSAPP_VERSION = settings.WHATSAPP_VERSION
ACCESS_TOKEN = settings.WHATSAPP_SYSTEM_USER_ACCESS_TOKEN


class FacebookAuthorization:
BASE_URL = f"https://graph.facebook.com/{WHATSAPP_VERSION}/"

def __init__(self):
self.access_token = ACCESS_TOKEN

Check warning on line 16 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L16

Added line #L16 was not covered by tests

def _get_headers(self):
headers = {"Authorization": f"Bearer {self.access_token}"}
return headers

Check warning on line 20 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L19-L20

Added lines #L19 - L20 were not covered by tests

@property
def get_url(self):
return self.BASE_URL

Check warning on line 24 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L24

Added line #L24 was not covered by tests


class FacebookClient(FacebookAuthorization, RequestClient):
def create_catalog(self, business_id, name, category=None):
url = self.get_url + f"{business_id}/owned_product_catalogs"
data = {"name": name}
if category:
data["vertical"] = category

Check warning on line 32 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L29-L32

Added lines #L29 - L32 were not covered by tests

headers = self._get_headers()
response = self.make_request(url, method="POST", headers=headers, data=data)

Check warning on line 35 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L34-L35

Added lines #L34 - L35 were not covered by tests

return response.json()

Check warning on line 37 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L37

Added line #L37 was not covered by tests

def destroy_catalog(self, catalog_id):
url = self.get_url + f"{catalog_id}"

Check warning on line 40 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L40

Added line #L40 was not covered by tests

headers = self._get_headers()
response = self.make_request(url, method="DELETE", headers=headers)

Check warning on line 43 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L42-L43

Added lines #L42 - L43 were not covered by tests

return response.json().get("success")

Check warning on line 45 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L45

Added line #L45 was not covered by tests

def create_product_feed(self, product_catalog_id, name):
url = self.get_url + f"{product_catalog_id}/product_feeds"

Check warning on line 48 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L48

Added line #L48 was not covered by tests

data = {"name": name}
headers = self._get_headers()
response = self.make_request(url, method="POST", headers=headers, data=data)

Check warning on line 52 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L50-L52

Added lines #L50 - L52 were not covered by tests

return response.json()

Check warning on line 54 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L54

Added line #L54 was not covered by tests

def upload_product_feed(self, feed_id, file):
url = self.get_url + f"{feed_id}/uploads"

Check warning on line 57 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L57

Added line #L57 was not covered by tests

headers = self._get_headers()
files = {

Check warning on line 60 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L59-L60

Added lines #L59 - L60 were not covered by tests
"file": (
file.name,
file,
file.content_type,
)
}
response = self.make_request(url, method="POST", headers=headers, files=files)
return response.json()

Check warning on line 68 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L67-L68

Added lines #L67 - L68 were not covered by tests

def create_product_feed_via_url(
self, product_catalog_id, name, feed_url, file_type, interval, hour
): # TODO: adjust this method
url = self.get_url + f"{product_catalog_id}/product_feeds"

Check warning on line 73 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L73

Added line #L73 was not covered by tests

schedule = {"interval": interval, "url": feed_url, "hour": str(hour)}

Check warning on line 75 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L75

Added line #L75 was not covered by tests

data = {"name": name, "schedule": json.dumps(schedule), "file_type": file_type}

Check warning on line 77 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L77

Added line #L77 was not covered by tests

headers = self._get_headers()
response = self.make_request(url, method="POST", headers=headers, data=data)
return response.json()

Check warning on line 81 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L79-L81

Added lines #L79 - L81 were not covered by tests

def get_upload_status(self, feed_id, max_attempts=10, wait_time=30):
"""
Checks the upload status using long polling.
Args:
upload_id (str): The ID of the upload.
max_attempts (int): Maximum number of polling attempts. Default is 10.
wait_time (int): Wait time in seconds between polling attempts. Default is 30 seconds.
Returns:
bool or str: True if 'end_time' is found, otherwise a formatted error message.
"""
url = self.get_url + f"{feed_id}/uploads"
headers = self._get_headers()

Check warning on line 96 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L95-L96

Added lines #L95 - L96 were not covered by tests

attempts = 0
while attempts < max_attempts:
response = self.make_request(url, method="GET", headers=headers)
data = response.json()

Check warning on line 101 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L98-L101

Added lines #L98 - L101 were not covered by tests

if data.get("data") and data["data"][0].get("end_time"):
return True

Check warning on line 104 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L103-L104

Added lines #L103 - L104 were not covered by tests

time.sleep(wait_time)
attempts += 1

Check warning on line 107 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L106-L107

Added lines #L106 - L107 were not covered by tests

total_wait_time = wait_time * max_attempts
return (

Check warning on line 110 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L109-L110

Added lines #L109 - L110 were not covered by tests
f"Unable to retrieve the upload completion status for feed {feed_id}. "
f"Waited for a total of {total_wait_time} seconds."
)

def list_products_by_feed(self, feed_id):
url = self.get_url + f"{feed_id}/products"

Check warning on line 116 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L116

Added line #L116 was not covered by tests

headers = self._get_headers()
response = self.make_request(url, method="GET", headers=headers)

Check warning on line 119 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L118-L119

Added lines #L118 - L119 were not covered by tests

return response.json()

Check warning on line 121 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L121

Added line #L121 was not covered by tests

def list_all_products_by_feed(self, feed_id):
url = self.get_url + f"{feed_id}/products"
headers = self._get_headers()
all_products = []

Check warning on line 126 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L124-L126

Added lines #L124 - L126 were not covered by tests

while url:
response = self.make_request(url, method="GET", headers=headers).json()
all_products.extend(response.get("data", []))
url = response.get("paging", {}).get("next")

Check warning on line 131 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L128-L131

Added lines #L128 - L131 were not covered by tests

return all_products

Check warning on line 133 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L133

Added line #L133 was not covered by tests

def list_all_catalogs(self, wa_business_id):
url = self.get_url + f"{wa_business_id}/owned_product_catalogs"
headers = self._get_headers()
all_catalog_ids = []

Check warning on line 138 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L136-L138

Added lines #L136 - L138 were not covered by tests

while url:
response = self.make_request(url, method="GET", headers=headers).json()
catalog_data = response.get("data", [])
all_catalog_ids.extend([item["id"] for item in catalog_data])
url = response.get("paging", {}).get("next")

Check warning on line 144 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L140-L144

Added lines #L140 - L144 were not covered by tests

return all_catalog_ids

Check warning on line 146 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L146

Added line #L146 was not covered by tests

def destroy_feed(self, feed_id):
url = self.get_url + f"{feed_id}"

Check warning on line 149 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L149

Added line #L149 was not covered by tests

headers = self._get_headers()
response = self.make_request(url, method="DELETE", headers=headers)

Check warning on line 152 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L151-L152

Added lines #L151 - L152 were not covered by tests

return response.json().get("success")

Check warning on line 154 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L154

Added line #L154 was not covered by tests

def get_connected_catalog(self, waba_id):
url = self.get_url + f"{waba_id}/product_catalogs"
headers = self._get_headers()
response = self.make_request(url, method="GET", headers=headers)
return response.json()

Check warning on line 160 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L157-L160

Added lines #L157 - L160 were not covered by tests

def enable_catalog(self, waba_id, catalog_id):
url = self.get_url + f"{waba_id}/product_catalogs"
data = {"catalog_id": catalog_id}
headers = self._get_headers()
response = self.make_request(url, method="POST", headers=headers, data=data)
return response.json()

Check warning on line 167 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L163-L167

Added lines #L163 - L167 were not covered by tests

def disable_catalog(self, waba_id, catalog_id):
url = self.get_url + f"{waba_id}/product_catalogs"
data = {"catalog_id": catalog_id, "method": "delete"}
headers = self._get_headers()
response = self.make_request(url, method="POST", headers=headers, data=data)
return response.json()

Check warning on line 174 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L170-L174

Added lines #L170 - L174 were not covered by tests

def get_catalog_details(self, catalog_id):
url = self.get_url + f"{catalog_id}"
params = {"fields": "name,vertical"}
headers = self._get_headers()
response = self.make_request(url, method="GET", headers=headers, params=params)

Check warning on line 180 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L177-L180

Added lines #L177 - L180 were not covered by tests

return response.json()

Check warning on line 182 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L182

Added line #L182 was not covered by tests

def _update_commerce_settings(self, wa_phone_number_id, **settings):
url = self.BASE_URL + f"{wa_phone_number_id}/whatsapp_commerce_settings"
headers = self._get_headers()
response = self.make_request(url, method="POST", headers=headers, data=settings)
return response.json()

Check warning on line 188 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L185-L188

Added lines #L185 - L188 were not covered by tests

def toggle_cart(self, wa_phone_number_id, enable=True):
return self._update_commerce_settings(

Check warning on line 191 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L191

Added line #L191 was not covered by tests
wa_phone_number_id, is_cart_enabled=enable
)

def toggle_catalog_visibility(self, wa_phone_number_id, make_visible=True):
return self._update_commerce_settings(

Check warning on line 196 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L196

Added line #L196 was not covered by tests
wa_phone_number_id, is_catalog_visible=make_visible
)

def get_wpp_commerce_settings(self, wa_phone_number_id):
"""
Returns:
"data": [
{
"is_cart_enabled": true,
"is_catalog_visible": true,
"id": "270925148880242"
}
]
Or:
"data": []
"""
url = self.BASE_URL + f"{wa_phone_number_id}/whatsapp_commerce_settings"

Check warning on line 213 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L213

Added line #L213 was not covered by tests

headers = self._get_headers()
response = self.make_request(url, method="GET", headers=headers)
return response.json()

Check warning on line 217 in marketplace/clients/facebook/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/facebook/client.py#L215-L217

Added lines #L215 - L217 were not covered by tests
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from marketplace.clients.facebook.client import FacebookClient
from marketplace.wpp_products.models import Catalog


class FacebookService:
def __init__(self):
self.client = FacebookClient()

Check warning on line 7 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L7

Added line #L7 was not covered by tests

def get_app_facebook_credentials(self, app):
wa_business_id = app.config.get("wa_business_id")
wa_waba_id = app.config.get("wa_waba_id")
wa_phone_number_id = app.config.get("wa_phone_number_id")

Check warning on line 12 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L10-L12

Added lines #L10 - L12 were not covered by tests

if not wa_business_id or not wa_waba_id or not wa_phone_number_id:
raise ValueError(

Check warning on line 15 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L14-L15

Added lines #L14 - L15 were not covered by tests
"Not found 'wa_waba_id', 'wa_business_id' or wa_phone_number_id in app.config "
)
return {

Check warning on line 18 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L18

Added line #L18 was not covered by tests
"wa_business_id": wa_business_id,
"wa_waba_id": wa_waba_id,
"wa_phone_number_id": wa_phone_number_id,
}

def catalog_creation(self, validated_data, app, user):
business_id = self.get_app_facebook_credentials(app=app).get("wa_business_id")
response = self.client.create_catalog(

Check warning on line 26 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L25-L26

Added lines #L25 - L26 were not covered by tests
business_id, validated_data["name"], validated_data["category"]
)

if response and response.get("id"):
catalog = Catalog.objects.create(

Check warning on line 31 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L30-L31

Added lines #L30 - L31 were not covered by tests
app=app,
facebook_catalog_id=response.get("id"),
name=validated_data["name"],
created_by=user,
)
return catalog, response.get("id")

Check warning on line 37 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L37

Added line #L37 was not covered by tests

return None, None

Check warning on line 39 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L39

Added line #L39 was not covered by tests

def catalog_deletion(self, catalog):
return self.client.destroy_catalog(catalog.facebook_catalog_id)

Check warning on line 42 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L42

Added line #L42 was not covered by tests

def enable_catalog(self, catalog):
waba_id = self.get_app_facebook_credentials(app=catalog.app).get("wa_waba_id")
return self.client.enable_catalog(

Check warning on line 46 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L45-L46

Added lines #L45 - L46 were not covered by tests
waba_id=waba_id, catalog_id=catalog.facebook_catalog_id
)

def disable_catalog(self, catalog):
waba_id = self.get_app_facebook_credentials(app=catalog.app).get("wa_waba_id")
return self.client.disable_catalog(

Check warning on line 52 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L51-L52

Added lines #L51 - L52 were not covered by tests
waba_id=waba_id, catalog_id=catalog.facebook_catalog_id
)

def get_connected_catalog(self, app):
waba_id = self.get_app_facebook_credentials(app=app).get("wa_waba_id")
response = self.client.get_connected_catalog(waba_id=waba_id)
return response.get("data")[0].get("id") if response else []

Check warning on line 59 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L57-L59

Added lines #L57 - L59 were not covered by tests

def toggle_cart(self, app, enable=True):
business_phone_number_id = self.get_app_facebook_credentials(app=app).get(

Check warning on line 62 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L62

Added line #L62 was not covered by tests
"wa_phone_number_id"
)
return self.client.toggle_cart(business_phone_number_id, enable)

Check warning on line 65 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L65

Added line #L65 was not covered by tests

def toggle_catalog_visibility(self, app, visible=True):
business_phone_number_id = self.get_app_facebook_credentials(app=app).get(

Check warning on line 68 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L68

Added line #L68 was not covered by tests
"wa_phone_number_id"
)
return self.client.toggle_catalog_visibility(business_phone_number_id, visible)

Check warning on line 71 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L71

Added line #L71 was not covered by tests

def wpp_commerce_settings(self, app):
business_phone_number_id = self.get_app_facebook_credentials(app=app).get(

Check warning on line 74 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L74

Added line #L74 was not covered by tests
"wa_phone_number_id"
)
return self.client.get_wpp_commerce_settings(business_phone_number_id)

Check warning on line 77 in marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook_service.py#L77

Added line #L77 was not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from marketplace.flows.client import FlowsClient


class FlowsService:
def __init__(self):
self.client = FlowsClient()

def _update_flows_config(self, app):
"""
synchronize Flows channel configuration.
"""
detail_channel = self.client.detail_channel(app.flow_object_uuid)
flows_config = detail_channel["config"]
flows_config["catalogs"] = app.config["catalogs"]
self.client.update_config(

Check warning on line 15 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L12-L15

Added lines #L12 - L15 were not covered by tests
data=flows_config, flow_object_uuid=app.flow_object_uuid
)

def update_app_and_flows_with_catalog(self, app, catalog, catalog_id):
if "catalogs" not in app.config:
app.config["catalogs"] = []

Check warning on line 21 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L20-L21

Added lines #L20 - L21 were not covered by tests

app.config["catalogs"].append({"facebook_catalog_id": catalog_id})
app.save()

Check warning on line 24 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L23-L24

Added lines #L23 - L24 were not covered by tests

self._update_flows_config(app)

Check warning on line 26 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L26

Added line #L26 was not covered by tests

return catalog

Check warning on line 28 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L28

Added line #L28 was not covered by tests

def remove_catalog_from_app(self, catalog):
if "catalogs" in catalog.app.config:
catalogs_to_remove = [

Check warning on line 32 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L31-L32

Added lines #L31 - L32 were not covered by tests
idx
for idx, catalog_entry in enumerate(catalog.app.config["catalogs"])
if catalog_entry.get("facebook_catalog_id")
== catalog.facebook_catalog_id
]

for idx in reversed(catalogs_to_remove):
del catalog.app.config["catalogs"][idx]

Check warning on line 40 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L39-L40

Added lines #L39 - L40 were not covered by tests

catalog.app.save()

Check warning on line 42 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L42

Added line #L42 was not covered by tests

self._update_flows_config(catalog.app)

Check warning on line 44 in marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows_service.py#L44

Added line #L44 was not covered by tests
Loading

0 comments on commit a0d2db2

Please sign in to comment.