Skip to content

Commit

Permalink
Creates webhook to update vtex products
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Dec 6, 2023
1 parent 229fcfa commit b843537
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 2 deletions.
3 changes: 3 additions & 0 deletions marketplace/services/flows/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ def update_vtex_integration_status(self, project_uuid, user_email, action):
return self.client.update_vtex_integration_status(

Check warning on line 6 in marketplace/services/flows/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/flows/service.py#L6

Added line #L6 was not covered by tests
project_uuid, user_email, action
)

def update_vtex_products(self, products: list):
pass

Check warning on line 11 in marketplace/services/flows/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/flows/service.py#L11

Added line #L11 was not covered by tests
4 changes: 3 additions & 1 deletion marketplace/services/vtex/private/products/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ def update_product_info(self, domain, webhook_payload):
seller_ids = self.client.list_active_sellers(domain)

Check warning on line 83 in marketplace/services/vtex/private/products/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/vtex/private/products/service.py#L83

Added line #L83 was not covered by tests

if price_modified or stock_modified or other_changes:
updated_products = self.data_processor.process_product_data(
updated_products_dto = self.data_processor.process_product_data(

Check warning on line 86 in marketplace/services/vtex/private/products/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/vtex/private/products/service.py#L85-L86

Added lines #L85 - L86 were not covered by tests
[sku_id], seller_ids, self, domain, update_product=True
)

updated_products = DataProcessor.convert_dtos_to_dicts(updated_products_dto)

Check warning on line 90 in marketplace/services/vtex/private/products/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/vtex/private/products/service.py#L90

Added line #L90 was not covered by tests

return updated_products

Check warning on line 92 in marketplace/services/vtex/private/products/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/vtex/private/products/service.py#L92

Added line #L92 was not covered by tests

# ================================
Expand Down
3 changes: 3 additions & 0 deletions marketplace/services/vtex/utils/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ def generate_csv_file(csv_content: str) -> io.BytesIO:
csv_bytes = csv_content.encode("utf-8")
csv_memory = io.BytesIO(csv_bytes)
return csv_memory

Check warning on line 159 in marketplace/services/vtex/utils/data_processor.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/vtex/utils/data_processor.py#L157-L159

Added lines #L157 - L159 were not covered by tests

def convert_dtos_to_dicts(dtos: List[FacebookProductDTO]) -> List[dict]:
return [dataclasses.asdict(dto) for dto in dtos]

Check warning on line 162 in marketplace/services/vtex/utils/data_processor.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/vtex/utils/data_processor.py#L162

Added line #L162 was not covered by tests
1 change: 1 addition & 0 deletions marketplace/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"marketplace.wpp_templates",
"marketplace.event_driven",
"marketplace.wpp_products",
"marketplace.webhooks",
# installed apps
"rest_framework",
"storages",
Expand Down
7 changes: 6 additions & 1 deletion marketplace/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
from marketplace.swagger import view as swagger_view
from marketplace.applications import urls as applications_urls
from marketplace.interactions import urls as interactions_urls
from marketplace.webhooks import urls as webhooks_urls


admin.site.unregister(Group)


api_urls = [path("", include(applications_urls)), path("", include(interactions_urls))]
api_urls = [
path("", include(applications_urls)),
path("", include(interactions_urls)),
path("", include(webhooks_urls)),
]


urlpatterns = [
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions marketplace/webhooks/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.urls import path, include

urlpatterns = [
path("webhook/", include("marketplace.webhooks.vtex.urls")),
]
Empty file.
60 changes: 60 additions & 0 deletions marketplace/webhooks/vtex/product_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from marketplace.services.vtex.private.products.service import PrivateProductsService
from marketplace.clients.vtex.client import VtexPrivateClient
from marketplace.applications.models import App
from marketplace.services.vtex.exceptions import (
CredentialsValidationError,
NoVTEXAppConfiguredException,
)
from marketplace.clients.flows.client import FlowsClient
from marketplace.services.flows.service import FlowsService


class VtexProductUpdateWebhook(APIView):
flows_client_class = FlowsClient
flows_service_class = FlowsService
vtex_client_class = VtexPrivateClient
vtex_service_class = PrivateProductsService

def post(self, request, app_uuid):
app = self.get_app(app_uuid)
if not self.can_synchronize(app):
return Response(

Check warning on line 25 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L23-L25

Added lines #L23 - L25 were not covered by tests
{"error": "initial sync not completed"},
status=status.HTTP_400_BAD_REQUEST,
)

domain, app_key, app_token = self.get_credentials_or_raise(app)
vtex_service = self.get_vtex_service(app_key, app_token)

Check warning on line 31 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L30-L31

Added lines #L30 - L31 were not covered by tests

products_updated = vtex_service.update_product_info(domain, request.data)
self.send_products_to_flows(products_updated)
return Response(status=status.HTTP_200_OK)

Check warning on line 35 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L33-L35

Added lines #L33 - L35 were not covered by tests

def get_app(self, app_uuid):
try:
return App.objects.get(uuid=app_uuid, configured=True, code="vtex")
except App.DoesNotExist:
raise NoVTEXAppConfiguredException()

Check warning on line 41 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L38-L41

Added lines #L38 - L41 were not covered by tests

def can_synchronize(self, app):
return app.config.get("initial_sync_completed", False)

Check warning on line 44 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L44

Added line #L44 was not covered by tests

def get_credentials_or_raise(self, app):
domain = app.config["api_credentials"]["domain"]
app_key = app.config["api_credentials"]["app_key"]
app_token = app.config["api_credentials"]["app_token"]
if not domain or not app_key or not app_token:
raise CredentialsValidationError()
return domain, app_key, app_token

Check warning on line 52 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L47-L52

Added lines #L47 - L52 were not covered by tests

def get_vtex_service(self, app_key, app_token):
client = self.vtex_client_class(app_key, app_token)
return self.vtex_service_class(client)

Check warning on line 56 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L55-L56

Added lines #L55 - L56 were not covered by tests

def send_products_to_flows(self, products):
flows_service = self.flows_service_class(self.flows_client_class())
flows_service.update_vtex_products(products)

Check warning on line 60 in marketplace/webhooks/vtex/product_updates.py

View check run for this annotation

Codecov / codecov/patch

marketplace/webhooks/vtex/product_updates.py#L59-L60

Added lines #L59 - L60 were not covered by tests
10 changes: 10 additions & 0 deletions marketplace/webhooks/vtex/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from .product_updates import VtexProductUpdateWebhook

urlpatterns = [
path(
"vtex/<uuid:app_uuid>/products-update/api/notification/",
VtexProductUpdateWebhook.as_view(),
name="vtex-product-updates",
),
]

0 comments on commit b843537

Please sign in to comment.