Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Починил пересборку тегов при оплате заказа #2137

Merged
merged 2 commits into from
Dec 6, 2023
Merged
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
43 changes: 41 additions & 2 deletions src/apps/orders/services/order_paid_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

from celery import chain

from django.conf import settings
from django.utils import timezone

from apps.amocrm.tasks import amocrm_enabled
from apps.amocrm.tasks import push_order
from apps.amocrm.tasks import push_user
from apps.dashamail.tasks import update_subscription as update_dashamail_subscription
from apps.orders import human_readable
from apps.orders.models import Order
from apps.users.tasks import rebuild_tags
from core.services import BaseService
from core.tasks import send_telegram_message


@dataclass
Expand All @@ -27,8 +31,12 @@ def __post_init__(self) -> None:
def act(self) -> None:
self.mark_order_as_paid()
self.ship()

self.send_happiness_message()

self.rebuild_user_tags()
self.update_amocrm()
self.update_dashamail()

def mark_order_as_paid(self) -> None:
self.order.paid = timezone.now()
Expand All @@ -47,8 +55,39 @@ def rebuild_user_tags(self) -> None:
def update_amocrm(self) -> None:
if amocrm_enabled():
chain(
push_user.si(user_id=self.order.user.id),
push_user.si(user_id=self.order.user_id),
push_order.si(order_id=self.order.id),
).apply_async(
countdown=30
) # hope rebuild tags are finished
) # hope tags are rebuilt by this time

def update_dashamail(self) -> None:
update_dashamail_subscription.apply_async(
kwargs=dict(student_id=self.order.user_id),
countdown=30, # hope tags are built by this time
)

def send_happiness_message(self) -> None:
if not settings.HAPPINESS_MESSAGES_CHAT_ID:
return

if self.is_already_paid or self.silent or self.order.price <= 0:
return

send_telegram_message.delay(
chat_id=settings.HAPPINESS_MESSAGES_CHAT_ID,
text=self._get_happiness_message_text(self.order),
)

@staticmethod
def _get_happiness_message_text(order: Order) -> str:
sum = str(order.price).replace(".00", "")
reason = str(order.item)
payment_method = human_readable.get_order_payment_method_name(order)

payment_info = f"💰+{sum} ₽, {payment_method}"

if order.promocode:
payment_info += f", промокод {order.promocode}"

return f"{payment_info}\n{reason}\n{order.user}"
28 changes: 0 additions & 28 deletions src/apps/orders/services/order_shipper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from dataclasses import dataclass

from django.conf import settings
from django.utils import timezone

from apps.orders import human_readable
from apps.orders.models import Order
from core.services import BaseService
from core.tasks import send_telegram_message


@dataclass
Expand All @@ -21,35 +18,10 @@ def act(self) -> None:
self.ship()
self.mark_order_as_shipped()

if not self.silent and self.order.price > 0:
self.send_happiness_message()

def ship(self) -> None:
"""Ship the order"""
self.order.item.ship(to=self.order.user, order=self.order)

def mark_order_as_shipped(self) -> None:
self.order.shipped = timezone.now()
self.order.save(update_fields=["shipped", "modified"])

def send_happiness_message(self) -> None:
if not settings.HAPPINESS_MESSAGES_CHAT_ID:
return

send_telegram_message.delay(
chat_id=settings.HAPPINESS_MESSAGES_CHAT_ID,
text=self.get_order_happiness_message(self.order),
)

@staticmethod
def get_order_happiness_message(order: Order) -> str:
sum = str(order.price).replace(".00", "")
reason = str(order.item)
payment_method = human_readable.get_order_payment_method_name(order)

payment_info = f"💰+{sum} ₽, {payment_method}"

if order.promocode:
payment_info += f", промокод {order.promocode}"

return f"{payment_info}\n{reason}\n{order.user}"
5 changes: 0 additions & 5 deletions tests/apps/orders/order_shipping/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@



@pytest.fixture(autouse=True)
def rebuild_tags(mocker):
return mocker.patch("apps.users.tasks.rebuild_tags.delay")


@pytest.fixture
def user(mixer):
return mixer.blend("users.User", first_name="Kamaz", last_name="Otkhodov", email="[email protected]")
Expand Down
54 changes: 54 additions & 0 deletions tests/apps/orders/order_shipping/tests_marketing_integrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest


pytestmark = [
pytest.mark.django_db,

]

@pytest.fixture
def _enable_amocrm(settings):
settings.AMOCRM_BASE_URL = "https://test.amocrm.ru"
settings.AMOCRM_REDIRECT_URL = "https://test-education.ru"
settings.AMOCRM_INTEGRATION_ID = "4815162342"
settings.AMOCRM_CLIENT_SECRET = "top-secret-007"
settings.AMOCRM_AUTHORIZATION_CODE = "1337yep"


@pytest.fixture
def update_amocrm_order(mocker):
return mocker.patch("apps.amocrm.tasks.AmoCRMOrderPusher.act")

@pytest.fixture
def update_amocrm_user(mocker):
return mocker.patch("apps.amocrm.tasks.AmoCRMUserPusher.act")


@pytest.fixture
def update_dashamail(mocker):
return mocker.patch("apps.dashamail.tasks.DashamailListsClient.subscribe_or_update")


@pytest.mark.usefixtures('_enable_amocrm')
def test_amocrm_is_updated(order, update_amocrm_order, update_amocrm_user):
order.set_paid()

update_amocrm_order.assert_called_once()
update_amocrm_user.assert_called_once()


@pytest.mark.user_tags_rebuild
def test_tags_are_rebuilt(order):
assert 'any-purchase' not in order.user.tags

order.set_paid()
order.user.refresh_from_db()

assert 'any-purchase' in order.user.tags


@pytest.mark.dashamail
def test_dashamail_is_updated(order, update_dashamail):
order.set_paid()

update_dashamail.assert_called_once()
8 changes: 0 additions & 8 deletions tests/apps/orders/order_shipping/tests_order_set_paid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ def test_ships(order, course, user, ship):
ship.assert_called_once_with(course, to=user, order=order)


def test_update_user_tags(order, rebuild_tags):
order.user.update(email="")

order.set_paid()

rebuild_tags.assert_called_once_with(student_id=order.user.id)


def test_not_ships_if_order_is_already_paid(order, ship):
order.update(paid=datetime(2032, 12, 1, 15, 30, tzinfo=timezone.utc))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from apps.banking.selector import BANK_KEYS
from apps.banking.selector import BANKS
from apps.orders.services import OrderShipper
from apps.orders.services import OrderPaidSetter

pytestmark = [pytest.mark.django_db]

Expand All @@ -20,7 +20,7 @@ def tg_message(mocker):

@pytest.fixture
def mock_get_happiness_message(mocker):
return mocker.patch("apps.orders.services.order_shipper.OrderShipper.get_order_happiness_message", return_value="happiness_message")
return mocker.patch("apps.orders.services.order_paid_setter.OrderPaidSetter._get_happiness_message_text", return_value="happiness_message")


def test(tg_message, order, mock_get_happiness_message):
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_notification_message_include_payment_method(order, bank_id):
order.update(bank_id=bank_id)
order.set_paid()

message = OrderShipper.get_order_happiness_message(order)
message = OrderPaidSetter._get_happiness_message_text(order)

assert message == f"💰+1500 ₽, {BANKS[bank_id].name}\nЗапись курсов катанья и мытья\nKamaz Otkhodov"

Expand All @@ -79,7 +79,7 @@ def test_include_promocode_if_set(order, mixer):
order.update(promocode=mixer.blend("orders.PromoCode", name="YARR!", discount_percent=1))
order.set_paid()

message = OrderShipper.get_order_happiness_message(order)
message = OrderPaidSetter._get_happiness_message_text(order)

assert message == "💰+1500 ₽, Tinkoff, промокод YARR!\nЗапись курсов катанья и мытья\nKamaz Otkhodov"

Expand All @@ -88,6 +88,6 @@ def test_include_group_if_set(order, factory):
order.item.update(group=factory.group(name="Эффективная прокрастинация поток 2"))
order.set_paid()

message = OrderShipper.get_order_happiness_message(order)
message = OrderPaidSetter._get_happiness_message_text(order)

assert message == "💰+1500 ₽, Tinkoff\nЗапись курсов катанья и мытья - Эффективная прокрастинация поток 2\nKamaz Otkhodov"
2 changes: 1 addition & 1 deletion tests/test_order_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@


def test(factory, django_assert_max_num_queries):
with django_assert_max_num_queries(10):
with django_assert_max_num_queries(11):
factory.order(is_paid=True)