Skip to content

Commit

Permalink
change of approach
Browse files Browse the repository at this point in the history
  • Loading branch information
markj0hnst0n committed Dec 16, 2024
1 parent 6d1deee commit c7c5d39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
17 changes: 0 additions & 17 deletions mail/icms/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
logger = logging.getLogger(__name__)


class EdifactFileError(Exception):
pass


def process_licence_reply_and_usage_emails():
"""Downloads licenceReply and usageData emails from HMRC mailbox and stores in Mail model."""

Expand All @@ -51,18 +47,13 @@ def process_licence_reply_and_usage_emails():
logger.debug("Subject of email being processed: %s", subject)

if "licenceReply" in subject:
# _check_for_file_errors(mail)
_save_licence_reply_email(mail)
con.dele(msg_id)

elif "usageData" in subject:
_save_usage_data_email(mail)
con.dele(msg_id)

elif "licenceData" in subject:
_check_for_file_errors(mail)
con.dele(msg_id)

else:
raise ValueError(f"Unable to process email with subject: {subject}")

Expand Down Expand Up @@ -247,11 +238,3 @@ def _save_usage_data_email(usage_email: email.message.EmailMessage) -> None:
# extract_type=ExtractTypeEnum.USAGE_DATA
# )
raise NotImplementedError


def _check_for_file_errors(reply_email: email.message.EmailMessage) -> None:
processor = LicenceReplyProcessor.load_from_mail(reply_email)
error = None
for error in processor._current_rejected.errors:
if "Duplicate transaction reference" in error:
raise EdifactFileError(f"Unable to process file due to the following error: {error}")
16 changes: 16 additions & 0 deletions mail/libraries/data_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
from mail.libraries.mailbox_service import find_mail_of
from mail.models import LicenceData, Mail, UsageData
from mail.serializers import LicenceDataMailSerializer, UpdateResponseSerializer, UsageDataMailSerializer
from mail.chief.licence_reply import LicenceReplyProcessor


class EdifactFileError(Exception):
pass


def serialize_email_message(dto: EmailMessageDto) -> Mail or None:
Expand Down Expand Up @@ -53,6 +58,17 @@ def serialize_email_message(dto: EmailMessageDto) -> Mail or None:

logging.info("Successfully serialized email (subject: %s)", dto.subject)

processor = LicenceReplyProcessor.load_from_mail(_mail)

rejected_transaction_errors = processor._current_rejected.errors

for error in rejected_transaction_errors:
if "Duplicate transaction reference" in error.text:
run_number = LicenceData.objects.get(mail=_mail).hmrc_run_number
raise EdifactFileError(
f"Unable to process file due to the following error: {error.text}. It was sent in run number {run_number}"
)

return _mail


Expand Down

0 comments on commit c7c5d39

Please sign in to comment.