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
This commit is contained in:
jp
2026-08-13 19:39:27 +01:00
parent 7dd2d371d2
commit 1310e4cfb7

View File

@ -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 "" normalized = vm_contacts.normalize_uk(vm_contacts.digits_of(body.number)) if body.number else ""
con = vm_store.connect() con = vm_store.connect()
try: try:
con.execute( if normalized:
"INSERT INTO contacts (number_e164, name, email) VALUES (?, ?, ?)", row = con.execute("SELECT id FROM contacts WHERE number_e164 = ?", (normalized,)).fetchone()
(normalized, body.name, body.email), if row and row.get("id"):
) con.execute(
con.commit() "UPDATE contacts SET name=?, email=?, updated_at=NOW() WHERE id=?",
cid = con.lastrowid (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: finally:
con.close() con.close()
return {"id": cid, "number_e164": normalized, "name": body.name, "email": body.email} 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( con.execute(
"UPDATE messages SET contact_name=?, contact_email=? " "UPDATE messages SET contact_name=?, contact_email=? "
"WHERE callerid LIKE ? AND (contact_name IS NULL OR contact_name = '')", "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? "
(body.name, body.email, "%%%s%%" % normalized), " OR REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ?) "
"AND (contact_name IS NULL OR contact_name = '')",
("%%%s%%" % normalized, "%%%s%%" % normalized.replace("+44", "0"),),
) )
con.commit() con.commit()
finally: finally:
@ -537,8 +562,10 @@ async def add_contact(body: AddContactIn, vm_session: str = Cookie(default=None)
) )
con.execute( con.execute(
"UPDATE messages SET contact_name=?, contact_email=? " "UPDATE messages SET contact_name=?, contact_email=? "
"WHERE callerid LIKE ? AND (contact_name IS NULL OR contact_name = '')", "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? "
(name, email, "%%%s%%" % normalized), " 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.commit()
con.close() con.close()
@ -587,8 +614,10 @@ async def add_contact(body: AddContactIn, vm_session: str = Cookie(default=None)
) )
con.execute( con.execute(
"UPDATE messages SET contact_name=?, contact_email=? " "UPDATE messages SET contact_name=?, contact_email=? "
"WHERE callerid LIKE ? AND (contact_name IS NULL OR contact_name = '')", "WHERE (REPLACE(REPLACE(callerid, ' ', ''), '\"', '') LIKE ? "
(name, email, "%%%s%%" % normalized), " 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.commit()
con.close() con.close()