From a7ce399180c30d47f522146a91ff1d2c09fe9700 Mon Sep 17 00:00:00 2001 From: elitonzky Date: Wed, 5 Feb 2025 18:44:09 -0300 Subject: [PATCH] feature: add route to create library template messages --- marketplace/clients/facebook/client.py | 10 ++++++ marketplace/interfaces/facebook/interfaces.py | 6 ++++ marketplace/services/facebook/service.py | 12 +++++++ marketplace/wpp_templates/views.py | 36 +++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/marketplace/clients/facebook/client.py b/marketplace/clients/facebook/client.py index bc9d3390..304b511a 100644 --- a/marketplace/clients/facebook/client.py +++ b/marketplace/clients/facebook/client.py @@ -367,6 +367,16 @@ def create_template_message( return response.json() + def create_library_template_message( + self, waba_id: str, template_data: dict + ) -> dict: + url = f"{self.get_url}/{waba_id}/message_templates" + response = self.make_request( + url, method="POST", json=template_data, headers=self._get_headers() + ) + + return response.json() + def get_template_analytics(self, waba_id, fields): url = f"{self.get_url}/{waba_id}/template_analytics" headers = self._get_headers() diff --git a/marketplace/interfaces/facebook/interfaces.py b/marketplace/interfaces/facebook/interfaces.py index 4548a0b8..ed278698 100644 --- a/marketplace/interfaces/facebook/interfaces.py +++ b/marketplace/interfaces/facebook/interfaces.py @@ -61,6 +61,12 @@ def update_template_message( def delete_template_message(self, waba_id: str, name: str) -> Dict[str, Any]: pass + @abstractmethod + def create_library_template_message( + self, waba_id: str, template_data: dict + ) -> Dict[str, Any]: + pass + class PhotoAPIRequestsInterface(ABC): @abstractmethod diff --git a/marketplace/services/facebook/service.py b/marketplace/services/facebook/service.py index f9720213..f57cfb6e 100644 --- a/marketplace/services/facebook/service.py +++ b/marketplace/services/facebook/service.py @@ -4,6 +4,7 @@ from typing import List, Dict, Any +from marketplace.applications.models import App from marketplace.wpp_products.models import Catalog from marketplace.interfaces.facebook.interfaces import ( TemplatesRequestsInterface, @@ -251,6 +252,17 @@ def _clean_text(self, text): text = text.replace("\xa0", " ") return text.strip() + def create_library_template_message( + self, + app: App, + template_data: dict, + ) -> Dict[str, Any]: + waba_id = app.config["wa_waba_id"] + return self.client.create_library_template_message( + waba_id=waba_id, + template_data=template_data, + ) + class PhotoAPIService: def __init__(self, client: PhotoAPIRequestsInterface): diff --git a/marketplace/wpp_templates/views.py b/marketplace/wpp_templates/views.py index 4bbeb942..b9f2ac8c 100644 --- a/marketplace/wpp_templates/views.py +++ b/marketplace/wpp_templates/views.py @@ -12,6 +12,7 @@ from rest_framework.response import Response from rest_framework.decorators import action from rest_framework.pagination import PageNumberPagination +from rest_framework.generics import get_object_or_404 from marketplace.applications.models import App from marketplace.core.types import APPTYPES @@ -19,6 +20,7 @@ from marketplace.services.facebook.service import TemplateService, PhotoAPIService from marketplace.clients.facebook.client import FacebookClient from marketplace.accounts.permissions import ProjectManagePermission +from marketplace.clients.exceptions import CustomAPIException from .models import TemplateHeader, TemplateMessage, TemplateTranslation, TemplateButton from .serializers import TemplateMessageSerializer, TemplateTranslationSerializer @@ -256,3 +258,37 @@ def partial_update(self, request, app_uuid=None, uuid=None): ) return Response(response) + + @action(detail=False, methods=["POST"]) + def create_library_templates(self, request, app_uuid=None, uuid=None): + """ + Creates a library template message for the given app. + + Args: + request (Request): The request object containing template data. + app_uuid (str): UUID of the app. + uuid (str, optional): Additional UUID parameter, not used in the function. + + Returns: + Response: API response with the template creation status. + """ + app = get_object_or_404(App, uuid=app_uuid) + + try: + # Instantiate the template service with a Facebook client + template_service = TemplateService( + client=FacebookClient(app.apptype.get_system_access_token(app)) + ) + + # Create the template using the provided request data + response_data = template_service.create_library_template_message( + app=app, template_data=request.data + ) + + return Response(response_data, status=status.HTTP_201_CREATED) + + except KeyError as e: + raise ValidationError(f"Missing configuration: {e}") + + except CustomAPIException as e: + return Response({"detail": str(e)}, status=e.status_code)