Delete duplicate on get_or_create

This commit is contained in:
zandercymatics 2024-05-02 09:46:07 -06:00
parent 059585d3e1
commit 7ba71f4f2c
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -1966,10 +1966,11 @@ class Domain(TimeStampedModel, DomainHelper):
domain=self, domain=self,
) )
# Raise an error if we find duplicates. # If we find duplicates, log it and delete the newest one.
# This should not occur
if db_contact.count() > 1: 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. # Save to DB if it doesn't exist already.
if db_contact.count() == 0: if db_contact.count() == 0:
@ -1981,16 +1982,14 @@ class Domain(TimeStampedModel, DomainHelper):
existing_contact = db_contact.get() existing_contact = db_contact.get()
# Does the item we're grabbing match # Does the item we're grabbing match what we have in our DB?
# what we have in our DB?
if existing_contact.email != public_contact.email or existing_contact.registry_id != public_contact.registry_id: if existing_contact.email != public_contact.email or existing_contact.registry_id != public_contact.registry_id:
existing_contact.delete() existing_contact.delete()
public_contact.save() public_contact.save()
logger.warning("Requested PublicContact is out of sync " "with DB.") logger.warning("Requested PublicContact is out of sync " "with DB.")
return public_contact return public_contact
# If it already exists, we can
# assume that the DB instance was updated # If it already exists, we can assume that the DB instance was updated during set, so we should just use that.
# during set, so we should just use that.
return existing_contact return existing_contact
def _registrant_to_public_contact(self, registry_id: str): def _registrant_to_public_contact(self, registry_id: str):