Features: 1) Add STATUS_MAP dictionary to map status codes in AmoCRM; 2) Update update_order_status method to use STATUS_MAP for status mapping.

Fixes: 1) Simplify retrieval of `OrderCrmLink` to use `get()` instead of `filter().first()`; 2) Eliminate redundant `Order` lookup by utilizing `link.order`.

Extra: Refactored code to enhance readability and efficiency in status update logic.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-09-07 03:06:06 +03:00
parent 0ca0756e50
commit 2c67af969e

View file

@ -14,6 +14,8 @@ logger = logging.getLogger("django")
class AmoCRM: class AmoCRM:
STATUS_MAP: dict[str, str] = {}
def __init__(self): def __init__(self):
try: try:
self.instance = CustomerRelationshipManagementProvider.objects.get(name="amo") self.instance = CustomerRelationshipManagementProvider.objects.get(name="amo")
@ -170,14 +172,10 @@ class AmoCRM:
OrderCrmLink.objects.create(order=order, crm_lead_id=lead_id, crm=self.instance) OrderCrmLink.objects.create(order=order, crm_lead_id=lead_id, crm=self.instance)
return lead_id return lead_id
def update_order_status(self, crm_lead_id: str, new_status_code: str) -> None: def update_order_status(self, crm_lead_id: str, new_status: str) -> None:
link = OrderCrmLink.objects.filter(crm_lead_id=crm_lead_id).first() link = OrderCrmLink.objects.get(crm_lead_id=crm_lead_id)
if not link:
return
from core.models import Order
order = Order.objects.get(uuid=link.order_uuid) if link.order.status == new_status:
if order.status == new_status_code:
return return
order.status = new_status_code link.order.status = self.STATUS_MAP.get(new_status)
order.save(update_fields=["status"]) link.order.save(update_fields=["status"])