From 1310e4cfb792de34d04430ec758f0abee9bf99bc Mon Sep 17 00:00:00 2001 From: jp Date: Thu, 13 Aug 2026 19:39:27 +0100 Subject: [PATCH] Backfill contact_name when creating/updating contacts - create_contact/update_contact now update matching messages - normalize callerid spaces/quotes before LIKE match - match both E.164 and 0-prefix variants --- src/vm_api.py | 53 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/vm_api.py b/src/vm_api.py index b66b80a..5937e47 100644 --- a/src/vm_api.py +++ b/src/vm_api.py @@ -353,12 +353,35 @@ async def create_contact(body: ContactIn, vm_session: str = Cookie(default=None) normalized = vm_contacts.normalize_uk(vm_contacts.digits_of(body.number)) if body.number else "" con = vm_store.connect() try: - con.execute( - "INSERT INTO contacts (number_e164, name, email) VALUES (?, ?, ?)", - (normalized, body.name, body.email), - ) - con.commit() - cid = con.lastrowid + if normalized: + row = con.execute("SELECT id FROM contacts WHERE number_e164 = ?", (normalized,)).fetchone() + if row and row.get("id"): + con.execute( + "UPDATE contacts SET name=?, email=?, updated_at=NOW() WHERE id=?", + (body.name, body.email, row["id"]), + ) + cid = row["id"] + else: + con.execute( + "INSERT INTO contacts (number_e164, name, email) VALUES (?, ?, ?)", + (normalized, body.name, body.email), + ) + cid = con.lastrowid + con.execute( + "UPDATE messages SET contact_name=?, contact_email=? " + "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? " + " OR REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ?) " + "AND (contact_name IS NULL OR contact_name = '')", + (body.name, body.email, "%%%s%%" % normalized, "%%%s%%" % normalized.replace("+44", "0"),), + ) + con.commit() + else: + con.execute( + "INSERT INTO contacts (number_e164, name, email) VALUES (?, ?, ?)", + ("", body.name, body.email), + ) + con.commit() + cid = con.lastrowid finally: con.close() return {"id": cid, "number_e164": normalized, "name": body.name, "email": body.email} @@ -379,8 +402,10 @@ async def update_contact(cid: int, body: ContactIn, vm_session: str = Cookie(def ) con.execute( "UPDATE messages SET contact_name=?, contact_email=? " - "WHERE callerid LIKE ? AND (contact_name IS NULL OR contact_name = '')", - (body.name, body.email, "%%%s%%" % normalized), + "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? " + " OR REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ?) " + "AND (contact_name IS NULL OR contact_name = '')", + ("%%%s%%" % normalized, "%%%s%%" % normalized.replace("+44", "0"),), ) con.commit() finally: @@ -537,8 +562,10 @@ async def add_contact(body: AddContactIn, vm_session: str = Cookie(default=None) ) con.execute( "UPDATE messages SET contact_name=?, contact_email=? " - "WHERE callerid LIKE ? AND (contact_name IS NULL OR contact_name = '')", - (name, email, "%%%s%%" % normalized), + "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? " + " OR REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ?) " + "AND (contact_name IS NULL OR contact_name = '')", + (name, email, "%%%s%%" % normalized, "%%%s%%" % normalized.replace("+44", "0"),), ) con.commit() con.close() @@ -587,8 +614,10 @@ async def add_contact(body: AddContactIn, vm_session: str = Cookie(default=None) ) con.execute( "UPDATE messages SET contact_name=?, contact_email=? " - "WHERE callerid LIKE ? AND (contact_name IS NULL OR contact_name = '')", - (name, email, "%%%s%%" % normalized), + "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? " + " OR REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ?) " + "AND (contact_name IS NULL OR contact_name = '')", + (name, email, "%%%s%%" % normalized, "%%%s%%" % normalized.replace("+44", "0"),), ) con.commit() con.close()