Sync contacts: role depuis Teams iTop (SecOps→referent_technique, iPOP→responsable_applicatif)
This commit is contained in:
parent
3707308063
commit
32d602975b
@ -154,21 +154,38 @@ def sync_from_itop(db, itop_url, itop_user, itop_pass):
|
|||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
|
|
||||||
# ─── 5. Contacts ───
|
# ─── 5. Contacts + Teams ───
|
||||||
persons = client.get_all("Person", "name,first_name,email,phone,org_name")
|
persons = client.get_all("Person", "name,first_name,email,phone,org_name")
|
||||||
|
|
||||||
|
# Get team memberships to determine role
|
||||||
|
team_members = {} # person_fullname_lower -> team_name
|
||||||
|
teams = client.get_all("Team", "name,persons_list")
|
||||||
|
for t in teams:
|
||||||
|
team_name = t.get("name", "")
|
||||||
|
for member in t.get("persons_list", []):
|
||||||
|
pname = member.get("person_id_friendlyname", "").lower()
|
||||||
|
if pname:
|
||||||
|
team_members[pname] = team_name
|
||||||
|
|
||||||
|
team_role_map = {"SecOps": "referent_technique", "iPOP": "responsable_applicatif", "Externe": "referent_technique"}
|
||||||
|
|
||||||
for p in persons:
|
for p in persons:
|
||||||
fullname = f"{p.get('first_name','')} {p.get('name','')}".strip()
|
fullname = f"{p.get('first_name','')} {p.get('name','')}".strip()
|
||||||
email = p.get("email", "")
|
email = p.get("email", "")
|
||||||
if not email:
|
if not email:
|
||||||
continue
|
continue
|
||||||
|
# Determine role from team
|
||||||
|
team = team_members.get(fullname.lower(), "")
|
||||||
|
role = team_role_map.get(team, "referent_technique")
|
||||||
|
|
||||||
existing = db.execute(text("SELECT id FROM contacts WHERE LOWER(email)=LOWER(:e)"), {"e": email}).fetchone()
|
existing = db.execute(text("SELECT id FROM contacts WHERE LOWER(email)=LOWER(:e)"), {"e": email}).fetchone()
|
||||||
if existing:
|
if existing:
|
||||||
db.execute(text("UPDATE contacts SET name=:n, updated_at=NOW() WHERE id=:id"),
|
db.execute(text("UPDATE contacts SET name=:n, role=:r, updated_at=NOW() WHERE id=:id"),
|
||||||
{"id": existing.id, "n": fullname})
|
{"id": existing.id, "n": fullname, "r": role})
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
db.execute(text("INSERT INTO contacts (name, email, role) VALUES (:n, :e, 'referent_technique')"),
|
db.execute(text("INSERT INTO contacts (name, email, role) VALUES (:n, :e, :r)"),
|
||||||
{"n": fullname, "e": email})
|
{"n": fullname, "e": email, "r": role})
|
||||||
stats["contacts"] += 1
|
stats["contacts"] += 1
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user