Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions marketplace/clients/facebook/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,14 @@ def register_phone_number(
response = self.make_request(url, method="POST", headers=headers, data=data)
return response.json()

def sync_coexistence_contacts(self, phone_number_id: str) -> dict:
url = f"{self.get_url}/{phone_number_id}/smb_app_data"
data = {"messaging_product": "whatsapp", "sync_type": "smb_app_state_sync"}
response = self.make_request(
url, method="POST", headers=self._get_headers(), data=data
)
return response.json()


class FacebookClient(
CatalogsRequests,
Expand Down
8 changes: 8 additions & 0 deletions marketplace/core/types/channels/whatsapp_cloud/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class Meta:


class WhatsAppCloudConfigureSerializer(serializers.Serializer):
INTEGRATION_TYPE_CHOICES = (
("default", "Default"),
("coexistence", "Coexistence"),
)

waba_id = serializers.CharField(required=True)
phone_number_id = serializers.CharField(required=True)
auth_code = serializers.CharField(required=True)
integration_type = serializers.ChoiceField(
choices=INTEGRATION_TYPE_CHOICES, default="default"
)
38 changes: 38 additions & 0 deletions marketplace/core/types/channels/whatsapp_cloud/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ def configure_whatsapp_cloud(
def register_phone_number(self, phone_number_id, user_access_token, data):
pass

def sync_coexistence_contacts(self, phone_number_id):
return {"success": True}


class MockPhoneNumbersService:
def get_phone_number(self, phone_number_id):
Expand Down Expand Up @@ -489,6 +492,41 @@ def test_create_whatsapp_cloud_success(self):
self.assertEqual(len(app.config["wa_pin"]), 6)
self.assertEqual(app.config["wa_user_token"], "mock_user_access_token")

def test_create_whatsapp_cloud_coexistence_success(self):
coexistence_payload = {
**self.payload,
"integration_type": "coexistence",
}
response = self.request.post(self.url, body=coexistence_payload)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertTrue(
App.objects.filter(
uuid=response.json["app_uuid"],
project_uuid=self.payload["project_uuid"],
).exists()
)
coexistence_app = App.objects.get(
uuid=response.json["app_uuid"], project_uuid=self.payload["project_uuid"]
)
self.assertEqual(coexistence_app.config["integration_type"], "coexistence")
self.assertEqual(
coexistence_app.config["wa_number"], "mock_display_phone_number"
)
self.assertEqual(
coexistence_app.config["wa_verified_name"], "mock_verified_name"
)
self.assertEqual(coexistence_app.config["wa_waba_id"], self.payload["waba_id"])
self.assertEqual(coexistence_app.config["wa_currency"], "USD")
self.assertEqual(coexistence_app.config["wa_business_id"], "mock_business_id")
self.assertEqual(
coexistence_app.config["wa_message_template_namespace"],
"mock_message_template_namespace",
)
self.assertEqual(
coexistence_app.config["wa_user_token"], "mock_user_access_token"
)

def test_create_whatsapp_cloud_failure_on_exchange_auth_code(self):
self.mock_business_meta_service.configure_whatsapp_cloud = Mock(
side_effect=ValidationError("Invalid auth code")
Expand Down
20 changes: 14 additions & 6 deletions marketplace/core/types/channels/whatsapp_cloud/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def create(self, request, *args, **kwargs):
waba_id = serializer.validated_data.get("waba_id")
phone_number_id = serializer.validated_data.get("phone_number_id")
auth_code = serializer.validated_data.get("auth_code")
integration_type = serializer.validated_data.get("integration_type", "default")
waba_currency = "USD"

whatsapp_system_user_access_token = settings.WHATSAPP_SYSTEM_USER_ACCESS_TOKEN
Expand All @@ -116,22 +117,26 @@ def create(self, request, *args, **kwargs):
)
phone_number = phone_number_request.get_phone_number(phone_number_id)

# Register phone number
pin = get_random_string(6, string.digits)
data = dict(messaging_product="whatsapp", pin=pin)
business_service.register_phone_number(phone_number_id, user_access_token, data)

config = dict(
wa_number=phone_number.get("display_phone_number"),
wa_verified_name=phone_number.get("verified_name"),
wa_waba_id=waba_id,
wa_currency=waba_currency,
wa_business_id=business_id,
wa_message_template_namespace=message_template_namespace,
wa_pin=pin,
wa_user_token=user_access_token,
integration_type=integration_type,
)

# Register phone number if it is a default integration
if integration_type == "default":
pin = get_random_string(6, string.digits)
data = dict(messaging_product="whatsapp", pin=pin)
business_service.register_phone_number(
phone_number_id, user_access_token, data
)
config["wa_pin"] = pin

flows_service = FlowsService(client=FlowsClient())
channel = flows_service.create_wac_channel(
request.user.email, project_uuid, phone_number_id, config
Expand All @@ -155,6 +160,9 @@ def create(self, request, *args, **kwargs):
celery_app.send_task(name="sync_whatsapp_cloud_wabas")
celery_app.send_task(name="sync_whatsapp_cloud_phone_numbers")

if integration_type == "coexistence":
business_service.sync_coexistence_contacts(phone_number_id)

response_data = {
**serializer.validated_data,
"app_uuid": str(app.uuid),
Expand Down
9 changes: 6 additions & 3 deletions marketplace/services/facebook/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ def save_template_in_db(self, app: App, template_data: dict, response_data: dict
translation=returned_translation,
button_type=button["type"],
url=button["url"]["base_url"] if "url" in button else None,
example=button["url"]["url_suffix_example"]
if "url" in button
else None,
example=(
button["url"]["url_suffix_example"] if "url" in button else None
),
text=button.get("text", ""),
phone_number=None,
)
Expand Down Expand Up @@ -453,3 +453,6 @@ def configure_whatsapp_cloud(
"message_template_namespace": message_template_namespace,
"allocation_config_id": allocation_config_id,
}

def sync_coexistence_contacts(self, phone_number_id: str) -> Dict[str, Any]:
return self.client.sync_coexistence_contacts(phone_number_id)
7 changes: 7 additions & 0 deletions marketplace/services/facebook/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def create_library_template_message(self, waba_id, template_data):
"category": template_data.get("category", "UTILITY"),
}

def sync_coexistence_contacts(self, phone_number_id):
return {"success": True}


class TestFacebookService(TestCase):
def generate_unique_facebook_catalog_id(self):
Expand Down Expand Up @@ -682,3 +685,7 @@ def test_configure_whatsapp_cloud(self):
"allocation_config_id": "mock_allocation_id",
},
)

def test_sync_coexistence_contacts(self):
response = self.service.sync_coexistence_contacts("phone_number_id")
self.assertEqual(response, {"success": True})