From 7ba71f4f2c5abff8092adfbde7498b8b15d07f0a Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 2 May 2024 09:46:07 -0600 Subject: [PATCH] Delete duplicate on get_or_create --- src/registrar/models/domain.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index c49050d4a..2dbe72560 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -1966,10 +1966,11 @@ class Domain(TimeStampedModel, DomainHelper): domain=self, ) - # Raise an error if we find duplicates. - # This should not occur + # If we find duplicates, log it and delete the newest one. if db_contact.count() > 1: - raise Exception(f"Multiple contacts found for {public_contact.contact_type}") + logger.warning("_get_or_create_public_contact() -> Duplicate contacts found. Deleting duplicate.") + newest_duplicate = db_contact.order_by('-created_at').first() + newest_duplicate.delete() # Save to DB if it doesn't exist already. if db_contact.count() == 0: @@ -1981,16 +1982,14 @@ class Domain(TimeStampedModel, DomainHelper): existing_contact = db_contact.get() - # Does the item we're grabbing match - # what we have in our DB? + # Does the item we're grabbing match what we have in our DB? if existing_contact.email != public_contact.email or existing_contact.registry_id != public_contact.registry_id: existing_contact.delete() public_contact.save() logger.warning("Requested PublicContact is out of sync " "with DB.") return public_contact - # If it already exists, we can - # assume that the DB instance was updated - # during set, so we should just use that. + + # If it already exists, we can assume that the DB instance was updated during set, so we should just use that. return existing_contact def _registrant_to_public_contact(self, registry_id: str):