1
0
forked from erp-dev/erp

feat: mission replied calling task

This commit is contained in:
2026-04-17 23:26:33 +08:00
parent e109c00837
commit 8b1afc64c5
22 changed files with 1056 additions and 95 deletions

View File

@@ -1,9 +1,10 @@
from unittest.mock import patch
from django.test import TestCase
from django.utils import timezone
from basic_info.models import Merchant, MerchantTypeEnum
from mission.models import MissionCategory
from basic_info.models import Employee, Merchant, MerchantTypeEnum
from mission.models import Mission, MissionCategory, MissionReply
from notifier.models import NotificationEventKeyEnum, Notifier, NotifierChannelEnum, NotifierRoute
from notifier.services import (
dispatch_notification_event,
@@ -11,6 +12,7 @@ from notifier.services import (
render_notification_content,
send_notification_with_notifier,
)
from notifier.tasks import notify_unreplied_missions_task
class NotifierServiceTestCase(TestCase):
@@ -30,6 +32,7 @@ class NotifierServiceTestCase(TestCase):
notifier=self.notifier,
event_key=NotificationEventKeyEnum.MISSION_CREATED,
)
self.creator = Employee.objects.create(merchant=self.merchant, name="任务创建者")
def test_render_notification_content(self):
content = render_notification_content(
@@ -189,3 +192,63 @@ class NotifierServiceTestCase(TestCase):
self.assertIsNone(task_id)
mock_delay.assert_called_once()
@patch("notifier.tasks.dispatch_notification_event")
def test_notify_unreplied_missions_task_dispatches_due_mission_and_updates_state(self, mock_dispatch):
mock_dispatch.return_value = [{"status": "sent"}]
notifier = Notifier.objects.create(
merchant=self.merchant,
name="未回复提醒通知器",
channel=NotifierChannelEnum.WECOM_WEBHOOK,
template_key="mission_unreplied",
config={"key": "unreplied-key"},
)
NotifierRoute.objects.create(
merchant=self.merchant,
notifier=notifier,
event_key=NotificationEventKeyEnum.MISSION_UNREPLIED,
)
mission = Mission.objects.create(
merchant=self.merchant,
category=self.general_category,
creator=self.creator,
description="超时未回复任务",
notify_if_unreplied=True,
unreplied_notify_interval_minutes=1,
)
mission.created_at = timezone.now() - timezone.timedelta(minutes=3)
mission.save(update_fields=["created_at", "updated_at"])
result = notify_unreplied_missions_task.run(limit=10)
mission.refresh_from_db()
self.assertEqual(result["sent_count"], 1)
self.assertEqual(mission.unreplied_notify_sent_count, 1)
self.assertIsNotNone(mission.unreplied_last_notified_at)
mock_dispatch.assert_called_once()
self.assertEqual(mock_dispatch.call_args.kwargs["event_key"], NotificationEventKeyEnum.MISSION_UNREPLIED)
@patch("notifier.tasks.dispatch_notification_event")
def test_notify_unreplied_missions_task_skips_mission_with_effective_reply(self, mock_dispatch):
mission = Mission.objects.create(
merchant=self.merchant,
category=self.general_category,
creator=self.creator,
description="已有回复任务",
notify_if_unreplied=True,
unreplied_notify_interval_minutes=1,
)
mission.created_at = timezone.now() - timezone.timedelta(minutes=3)
mission.save(update_fields=["created_at", "updated_at"])
responder = Employee.objects.create(merchant=self.merchant, name="回应人")
MissionReply.objects.create(
merchant=self.merchant,
mission=mission,
responder=responder,
content="收到",
)
result = notify_unreplied_missions_task.run(limit=10)
self.assertEqual(result["sent_count"], 0)
mock_dispatch.assert_not_called()