From 9eaa429e45606e1746e27f88dd59e82e4671fecc Mon Sep 17 00:00:00 2001 From: "DESKTOP-IQ6LM3K\\Jose Antonio" Date: Wed, 22 May 2024 16:44:04 -0600 Subject: [PATCH] [IMP] api: Simplify email search logic in create_contact This commit refines the email handling in the create_contact endpoint by directly searching for the complete email address instead of using a prefix-based search pattern. Previously, the endpoint extracted a prefix from the provided email address and used it to construct a domain for searching contacts. The change to search for the exact email address improves the accuracy of contact searches by ensuring that only contacts with the exact email are considered. This adjustment reduces the likelihood of incorrect matches and streamlines the logic by removing unnecessary regex operations. --- hantec_api_ecommerce/controllers/main.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/hantec_api_ecommerce/controllers/main.py b/hantec_api_ecommerce/controllers/main.py index 64ec7f1..80cc012 100644 --- a/hantec_api_ecommerce/controllers/main.py +++ b/hantec_api_ecommerce/controllers/main.py @@ -37,16 +37,14 @@ def create_contact(self): contact_data = data.get("contact_data", {}) if email or phone: - # Email pattern to get the value before @ - email_pattern = r"^([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*)" - email_prefix = re.match(email_pattern, email).group() if email else None phone_suffix = phone[len(phone) - 4 :] if phone else None domain = [] - if email_prefix: - domain.append(("email", "=", f"{email_prefix}%")) + if email: + domain.append(("email", "=", f"{email}")) if phone_suffix: - domain.append(("phone", "=", f"%{phone_suffix}")) + # Use "like" operator to use the "%" wildcard + domain.append(("phone", "like", f"%{phone_suffix}")) existing_contact = env["res.partner"].search(domain, limit=1)