From dd0bd79c36c61229a01b6a835ec018f7bde5ba25 Mon Sep 17 00:00:00 2001 From: jasonMPM Date: Wed, 4 Mar 2026 22:13:39 -0600 Subject: [PATCH] Update app.py --- app.py | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index cebc0b0..051bc88 100644 --- a/app.py +++ b/app.py @@ -62,23 +62,35 @@ def sync_unifi_users(): users = r.json().get("data", []) with get_db() as db: for u in users: - full_name = (u.get("full_name") or "").strip() - if not full_name: - full_name = f"{u.get('first_name','')} {u.get('last_name','')}".strip() - db.execute( - """ - INSERT INTO user_cache (actor_id, full_name, updated_at) - VALUES (?, ?, ?) - ON CONFLICT(actor_id) DO UPDATE SET - full_name = excluded.full_name, - updated_at = excluded.updated_at - """, - ( - u["id"], - full_name or f"User {u['id'][:8]}", - datetime.utcnow().isoformat(), - ), - ) + # Prefer the same ID used in webhooks (identity ID) + actor_id = ( + u.get("id") or + u.get("identity_id") or + u.get("user_id") + ) + + if not actor_id: + continue # skip malformed entries + + full_name = (u.get("full_name") or "").strip() + if not full_name: + full_name = f"{u.get('first_name','')} {u.get('last_name','')}".strip() + + db.execute( + """ + INSERT INTO user_cache (actor_id, full_name, updated_at) + VALUES (?, ?, ?) + ON CONFLICT(actor_id) DO UPDATE SET + full_name = excluded.full_name, + updated_at = excluded.updated_at + """, + ( + actor_id, + full_name or f"User {actor_id[:8]}", + datetime.utcnow().isoformat(), + ), + ) + db.commit() log.info("Synced %d users from UniFi Access", len(users)) except Exception as e: