Skip to content

Commit

Permalink
[IMP] api: Simplify email search logic in create_contact
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JAntonioSalas committed May 22, 2024
1 parent 41c1a4e commit 9eaa429
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions hantec_api_ecommerce/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9eaa429

Please sign in to comment.