From f4b4ca7b343ac308af508444b0e0db29fd8c5229 Mon Sep 17 00:00:00 2001 From: thijmengg <146195501+thijmengg@users.noreply.github.com> Date: Thu, 25 Jan 2024 12:10:42 +0100 Subject: [PATCH] Fix moneybird send invoices to email #3537 (#3574) * Fix moneybird send_invoices_to_email #3537 * Document it --------- Co-authored-by: Dirk Doesburg --- website/moneybirdsynchronization/services.py | 27 +++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/website/moneybirdsynchronization/services.py b/website/moneybirdsynchronization/services.py index b8434cfd4..af68cad49 100644 --- a/website/moneybirdsynchronization/services.py +++ b/website/moneybirdsynchronization/services.py @@ -37,14 +37,29 @@ def create_or_update_contact(member: Member): moneybird = get_moneybird_api_service() if moneybird_contact.moneybird_id is None: - # Push the contact to Moneybird. - response = moneybird.create_contact(moneybird_contact.to_moneybird()) + # Push contact to moneybird. This may fail with 422 when moneybird rejects an + # email address. In that case, we try once more leaving out the email address, + # as moneybird does not require that we set one at all. + try: + response = moneybird.create_contact(moneybird_contact.to_moneybird()) + except Administration.InvalidData: + logger.info("Retrying to create contact without email...") + contact = moneybird_contact.to_moneybird() + del contact["send_invoices_to_email"] + response = moneybird.create_contact(contact) + moneybird_contact.moneybird_id = response["id"] else: - # Update the contact data (right now we always do this, but we could use the version to check if it's needed) - response = moneybird.update_contact( - moneybird_contact.moneybird_id, moneybird_contact.to_moneybird() - ) + # Update the contact data (right now we always do this, but we could use the version to check if it's needed). + try: + response = moneybird.update_contact( + moneybird_contact.moneybird_id, moneybird_contact.to_moneybird() + ) + except Administration.InvalidData: + logger.info("Retrying to update contact without email...") + contact = moneybird_contact.to_moneybird() + del contact["send_invoices_to_email"] + response = moneybird.update_contact(moneybird_contact.moneybird_id, contact) moneybird_contact.moneybird_sepa_mandate_id = response["sepa_mandate_id"] or None moneybird_contact.needs_synchronization = False