def send_payments_report(payments: List[Payment], recipients: List[str]) -> None: if not payments: logger.info( "[BATCH][PAYMENTS] No payments to report to the pass Culture team") return groups = group_payments_by_status(payments) payments_error_details = create_all_payments_details( groups["ERROR"]) if "ERROR" in groups else [] error_csv = generate_payment_details_csv(payments_error_details) payments_not_processable_details = (create_all_payments_details( groups["NOT_PROCESSABLE"]) if "NOT_PROCESSABLE" in groups else []) not_processable_csv = generate_payment_details_csv( payments_not_processable_details) logger.info( "[BATCH][PAYMENTS] Sending report on %s payment in ERROR and %s payment NOT_PROCESSABLE", len(payments_error_details), len(payments_not_processable_details), ) logger.info("[BATCH][PAYMENTS] Recipients of email : %s", recipients) try: send_payments_report_emails(not_processable_csv, error_csv, groups, recipients) except MailServiceException as exception: logger.exception( "[BATCH][PAYMENTS] Error while sending payments reports to MailJet: %s", exception)
def test_send_payments_report_emails_email_has_pass_culture_dev_as_recipient_when_send_email_disabled( app): # Given not_processable_csv = '"header A","header B","header C","header D"\n"part A","part B","part C","part D"\n' error_csv = '"header 1","header 2","header 3","header 4"\n"part 1","part 2","part 3","part 4"\n' grouped_payments = { "ERROR": [Mock(), Mock()], "SENT": [Mock()], "PENDING": [Mock(), Mock(), Mock()] } mocked_send_email = Mock() return_value = Mock() return_value.status_code = 200 mocked_send_email.return_value = return_value # When with patch("pcapi.utils.mailing.feature_send_mail_to_users_enabled", return_value=False): send_payments_report_emails(not_processable_csv, error_csv, grouped_payments, ["*****@*****.**"], mocked_send_email) # Then mocked_send_email.assert_called_once() args = mocked_send_email.call_args_list[0] email = args[1]["data"] assert email["To"] == "*****@*****.**"
def test_send_payments_report_email_sends_email_to_recipients(app): # Given not_processable_csv = '"header A","header B","header C","header D"\n"part A","part B","part C","part D"\n' n_payments_by_status = {"ERROR": 1, "PENDING": 2} # When send_payments_report_emails(not_processable_csv, n_payments_by_status, ["*****@*****.**"]) # Then assert len(mails_testing.outbox) == 1 assert mails_testing.outbox[0].sent_data["To"] == "*****@*****.**"
def send_payments_report(batch_date: datetime, recipients: list[str]) -> None: not_processable_payments = payment_queries.join_for_payment_details( payment_queries.get_payments_by_status( [TransactionStatus.NOT_PROCESSABLE], batch_date)) logger.info( "[BATCH][PAYMENTS] Sending report on %d payments NOT_PROCESSABLE", not_processable_payments.count(), ) logger.info("[BATCH][PAYMENTS] Recipients of email: %s", recipients) not_processable_csv = generate_payment_details_csv( not_processable_payments) n_payments_by_status = payment_queries.get_payment_count_by_status( batch_date) path = _save_file_on_disk("payments_not_processable", not_processable_csv, "csv") if not send_payments_report_emails(not_processable_csv, n_payments_by_status, recipients): # FIXME (dbaty, 2021-06-16): we are likely to end up here # because the attachment is now over Mailjet's 15Mb limit. # This is an ugly quick fix. logger.info( "[BATCH][PAYMENTS] Could not send payment reports email. CSV file has been stored at %s", path)
def test_send_payments_report_email_ssends_email_to_recipients(app): # Given not_processable_csv = '"header A","header B","header C","header D"\n"part A","part B","part C","part D"\n' error_csv = '"header 1","header 2","header 3","header 4"\n"part 1","part 2","part 3","part 4"\n' grouped_payments = { "ERROR": [Mock(), Mock()], "SENT": [Mock()], "PENDING": [Mock(), Mock(), Mock()] } # When send_payments_report_emails(not_processable_csv, error_csv, grouped_payments, ["*****@*****.**"]) # Then assert len(mails_testing.outbox) == 1 assert mails_testing.outbox[0].sent_data["To"] == "*****@*****.**"