Skip to content

Commit

Permalink
add loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Barbosa committed Jan 14, 2025
1 parent a4339ad commit 5b2c474
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion retail/clients/flows/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def send_whatsapp_broadcast(self, payload: dict) -> dict:
"""

url = f"{self.base_url}/api/v2/internals/whatsapp_broadcasts"

print(f"whatsapp broadcast url: {url}")
response = self.make_request(
url,
method="POST",
json=payload,
headers=self.authentication_instance.headers,
)
print(f"response: {response}")
return response.json()
1 change: 1 addition & 0 deletions retail/vtex/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ def mark_cart_as_abandoned(cart_uuid: str):
cart_uuid (str): The UUID of the cart to process.
"""
use_case = CartAbandonmentUseCase()
print("process abandoned cart")
use_case.process_abandoned_cart(cart_uuid)
12 changes: 9 additions & 3 deletions retail/vtex/usecases/cart_abandonment.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ def process_abandoned_cart(self, cart_uuid: str):
try:
# Fetch the cart
cart = self._get_cart(cart_uuid)

print(f"cart: {cart.__dict__}")
# Fetch order form details from VTEX IO
order_form = self._fetch_order_form(cart)

print(f"order_form: {order_form}")
# Process and update cart information
client_profile = self._extract_client_profile(cart, order_form)
print(f"client_profile: ", client_profile)

if not order_form.get("items", []):
# Mark cart as empty if no items are found
Expand All @@ -60,6 +61,7 @@ def process_abandoned_cart(self, cart_uuid: str):

# Check orders by email
orders = self._fetch_orders_by_email(cart, client_profile["email"])
print(f"orders: {orders}")
self._evaluate_orders(cart, orders, order_form)

except Cart.DoesNotExist:
Expand Down Expand Up @@ -156,6 +158,7 @@ def _evaluate_orders(self, cart: Cart, orders: dict, order_form: dict):
orders (dict): List of orders retrieved.
"""
if not orders.get("list"):
print("cart abandoned")
self._mark_cart_as_abandoned(cart, order_form)
return

Expand All @@ -164,7 +167,7 @@ def _evaluate_orders(self, cart: Cart, orders: dict, order_form: dict):
if order.get("orderFormId") == cart.cart_id:
self._update_cart_status(cart, "purchased")
return

print("not find any recent_order, mark abandoned")
self._mark_cart_as_abandoned(cart, order_form)

def _mark_cart_as_abandoned(self, cart: Cart, order_form: dict):
Expand All @@ -175,10 +178,13 @@ def _mark_cart_as_abandoned(self, cart: Cart, order_form: dict):
cart (Cart): The cart to process.
"""
self._update_cart_status(cart, "abandoned")
print(f"update cart: {cart.__dict__}")

# Prepare and send the notification
payload = self.message_builder.build_abandonment_message(cart, order_form)
print(f"payload: {payload}")
response = self.flows_service.send_whatsapp_broadcast(payload=payload)
print(f"response: {response}")
self._update_cart_status(cart, "delivered_success", response)

def _update_cart_status(self, cart: Cart, status: str, response=None):
Expand Down
7 changes: 5 additions & 2 deletions retail/webhooks/vtex/usecases/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ def process_cart_notification(self, cart_id: str, phone: str) -> Cart:
phone_number=phone,
status="created",
)
print(f"get cart: {cart}")
# Renew abandonment task
self._schedule_abandonment_task(str(cart.uuid))
print("call schedule_abandonment_task")
return cart
except Cart.DoesNotExist:
# Create new cart if it doesn't exist
Expand All @@ -124,9 +126,10 @@ def _create_cart(self, cart_id: str, phone: str) -> Cart:
integrated_feature=self._get_feature(),
phone_number=phone,
)

print(f"not exist cart, created: {cart.__dict__}")
# Schedule abandonment task
self._schedule_abandonment_task(str(cart.uuid))
print("call schedule_abandonment_task")
return cart

def _schedule_abandonment_task(self, cart_uuid: str):
Expand All @@ -137,7 +140,7 @@ def _schedule_abandonment_task(self, cart_uuid: str):
cart_uuid (str): The UUID of the cart.
"""
task_key = generate_task_key(cart_uuid)

print(f"task_key: {task_key}")
mark_cart_as_abandoned.apply_async(
(cart_uuid,), countdown=25 * 60, task_id=task_key
)
6 changes: 4 additions & 2 deletions retail/webhooks/vtex/views/abandoned_cart_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ class AbandonedCartNotification(APIView):
def post(self, request):
serializer = CartSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
print(f"serializer is valid: {serializer.validated_data}")

validated_data = serializer.validated_data
account = validated_data["account"]
cart_id = validated_data["cart_id"]
phone = PhoneNumberNormalizer.normalize(validated_data["phone"])

print(f"account: {account}\ncart_id: {cart_id}\nphone: {phone}")
cart_use_case = CartUseCase(account=account)
print(f"cart_use_case: {cart_use_case}")
result = cart_use_case.process_cart_notification(cart_id, phone)

print(f"result: {result}")
return Response(
{
"message": "Cart processed successfully.",
Expand Down

0 comments on commit 5b2c474

Please sign in to comment.