diff --git a/mail/icms/tasks.py b/mail/icms/tasks.py index 971f5609..f5175da2 100644 --- a/mail/icms/tasks.py +++ b/mail/icms/tasks.py @@ -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.""" @@ -51,7 +47,6 @@ 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) @@ -59,10 +54,6 @@ def process_licence_reply_and_usage_emails(): _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}") @@ -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}") diff --git a/mail/libraries/data_processors.py b/mail/libraries/data_processors.py index 267db126..234082f1 100644 --- a/mail/libraries/data_processors.py +++ b/mail/libraries/data_processors.py @@ -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: @@ -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