Skip to content

Commit

Permalink
Merge pull request #109 from uktrade/LTD-Remove-unnecessary-mail-pend…
Browse files Browse the repository at this point in the history
…ing-error

LTD: Remove unnecessary mail is pending error as it is not an error
  • Loading branch information
saruniitr authored Mar 18, 2022
2 parents 89320ef + e251eaf commit 06cdf40
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions mail/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.utils import timezone
from rest_framework.status import HTTP_207_MULTI_STATUS, HTTP_208_ALREADY_REPORTED

from mail.enums import ReceptionStatusEnum, ReplyStatusEnum
from mail.enums import ReceptionStatusEnum
from mail.libraries.builders import build_licence_data_mail
from mail.libraries.data_processors import build_request_mail_message_dto
from mail.libraries.mailbox_service import send_email
Expand Down Expand Up @@ -205,8 +205,11 @@ def send_licence_data_to_hmrc():

logging.info("Sending LITE licence updates to HMRC")

if not _is_email_slot_free():
logging.info("There is currently an update in progress or an email is in flight")
if Mail.objects.exclude(status=ReceptionStatusEnum.REPLY_SENT).count():
logging.info(
"Currently we are either waiting for a reply or next one is ready to be processed,\n"
"so we cannot send this update now and will be picked up in the next cycle"
)
return

try:
Expand Down Expand Up @@ -244,31 +247,7 @@ def send_licence_data_to_hmrc():
return True


def _is_email_slot_free() -> bool:
pending_mail = _get_pending_mail()
if pending_mail:
logging.error(f"The following Mail is pending: {pending_mail}")
return False

return True


def _get_pending_mail() -> []:
return list(Mail.objects.exclude(status=ReceptionStatusEnum.REPLY_SENT).values_list("id", flat=True))


def _get_rejected_mail() -> []:
return list(
Mail.objects.filter(
status=ReceptionStatusEnum.REPLY_SENT,
response_data__icontains=ReplyStatusEnum.REJECTED,
).values_list("id", flat=True)
)


# Notify Users of Rejected Mail


@background(queue=NOTIFY_USERS_TASK_QUEUE, schedule=0)
def notify_users_of_rejected_mail(mail_id, mail_response_date):
"""If a rejected email is found, this task notifies users of the rejection"""
Expand Down

0 comments on commit 06cdf40

Please sign in to comment.