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:
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user