Skip to content

Commit

Permalink
feat(notification): add task send_notifs_on_following_replies
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jun 22, 2023
1 parent d3b9fd7 commit 76786a4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
24 changes: 23 additions & 1 deletion lacommunaute/notification/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.notification.emails import bulk_send_user_to_list, collect_users_from_list, send_email
from lacommunaute.notification.enums import EmailSentTrackKind
from lacommunaute.notification.utils import collect_first_replies, collect_new_users_for_onboarding
from lacommunaute.notification.utils import (
collect_first_replies,
collect_following_replies,
collect_new_users_for_onboarding,
)


def send_notifs_when_first_reply():
Expand All @@ -25,6 +29,24 @@ def send_notifs_when_first_reply():
)


def send_notifs_on_following_replies():
replies = collect_following_replies()

for url, subject, to, count_txt in replies:
contacts = [{"email": to}]
params = {
"url": f"{url}?mtm_campaign=followingreplies&mtm_medium=email",
"topic_subject": subject,
"count_txt": count_txt,
}
send_email(
to=contacts,
params=params,
template_id=settings.SIB_FOLLOWING_REPLIES_TEMPLATE,
kind=EmailSentTrackKind.FOLLOWING_REPLIES,
)


def add_user_to_list_when_register():
new_users = collect_new_users_for_onboarding()
if new_users:
Expand Down
37 changes: 34 additions & 3 deletions lacommunaute/notification/tests/tests_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from lacommunaute.notification.models import EmailSentTrack
from lacommunaute.notification.tasks import (
add_user_to_list_when_register,
send_notifs_on_following_replies,
send_notifs_on_unanswered_topics,
send_notifs_when_first_reply,
)
Expand Down Expand Up @@ -55,11 +56,41 @@ def test_send_notifs_when_first_reply(self):

send_notifs_when_first_reply()

self.assertEqual(EmailSentTrack.objects.count(), 1)
email_sent_track = EmailSentTrack.objects.first()
email_sent_track = EmailSentTrack.objects.get()
self.assertEqual(email_sent_track.status_code, 200)
self.assertEqual(email_sent_track.response, json.dumps({"message": "OK"}))
self.assertJSONEqual(email_sent_track.response, {"message": "OK"})
self.assertEqual(email_sent_track.datas, payload)
self.assertEqual(email_sent_track.kind, "first_reply")


class SendNotifsOnFollowingReplyTestCase(TestCase):
def setUp(self):
super().setUp()
respx.post(SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))

@respx.mock
def test_send_notifs_on_following_reply(self):
topic = TopicFactory(with_post=True)
PostFactory.create_batch(2, topic=topic)

url = f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{topic.get_absolute_url()}"
url += "?mtm_campaign=followingreplies&mtm_medium=email"

params = {"url": url, "topic_subject": topic.subject, "count_txt": "2 nouvelles réponses"}
payload = {
"sender": {"name": "La Communauté", "email": DEFAULT_FROM_EMAIL},
"to": [{"email": topic.poster_email}],
"params": params,
"templateId": 13,
}

send_notifs_on_following_replies()

email_sent_track = EmailSentTrack.objects.get()
self.assertEqual(email_sent_track.status_code, 200)
self.assertJSONEqual(email_sent_track.response, {"message": "OK"})
self.assertEqual(email_sent_track.datas, payload)
self.assertEqual(email_sent_track.kind, "following_replies")


class AddUserToListWhenRegister(TestCase):
Expand Down

0 comments on commit 76786a4

Please sign in to comment.