droboticflow-new/scripts/preview_employee_supplier_migration.py

45 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Read an exported JSON inventory from stdin; print a plan, never write to APIs/DB."""
import json
import sys
EMPLOYEE_ROLES = {"annotator", "quality_reviewer", "acceptance_reviewer", "supplier_manager", "member"}
def build_plan(inventory):
org = inventory["organizationId"]
default_id = "supplier-default-" + org
suppliers = {s["id"]: s for s in inventory.get("suppliers", []) if s.get("organizationId") == org}
members = [m for m in inventory.get("members", []) if m.get("organizationId") == org]
users = [u for u in inventory.get("users", []) if u.get("organizationId") == org]
plan = {"organizationId": org, "dryRun": True, "defaultSupplier": {"id": default_id, "name": "默认供应商", "exists": default_id in suppliers}, "changes": [], "conflicts": [], "unchangedUsers": [], "excludedUsers": []}
for user in users:
identity = {k: user.get(k, "") for k in ("id", "name", "email", "status")}
if user.get("isSuperAdmin") or user.get("role") not in EMPLOYEE_ROLES:
plan["excludedUsers"].append(identity)
continue
mine = [m for m in members if m.get("userId") == user["id"]]
supplier_id = (user.get("supplierId") or "").strip()
if supplier_id and (supplier_id not in suppliers or suppliers[supplier_id].get("status") != "active"):
plan["conflicts"].append({"user": identity, "reason": "现有供应商不存在、不属于本组织或不可用", "supplierId": supplier_id})
continue
target = supplier_id or default_id
before_count = len(plan["changes"])
if not supplier_id:
plan["changes"].append({"collection": "users", "id": user["id"], "user": identity, "beforeSupplierId": "", "afterSupplierId": target})
for member in mine:
current = (member.get("supplierId") or "").strip()
if not current:
plan["changes"].append({"collection": "projectMembers", "id": member["id"], "user": identity, "userId": user["id"], "projectId": member["projectId"], "beforeSupplierId": "", "afterSupplierId": target})
elif current != target:
plan["conflicts"].append({"user": identity, "memberId": member["id"], "projectId": member["projectId"], "reason": "账号与项目成员供应商不同;保留现值,需确认", "accountSupplierId": target, "memberSupplierId": current})
if len(plan["changes"]) == before_count:
plan["unchangedUsers"].append(identity)
plan["summary"] = {"accountUpdates": sum(c["collection"] == "users" for c in plan["changes"]), "memberUpdates": sum(c["collection"] == "projectMembers" for c in plan["changes"]), "conflicts": len(plan["conflicts"])}
return plan
if __name__ == "__main__":
json.dump(build_plan(json.load(sys.stdin)), sys.stdout, ensure_ascii=False, indent=2)
print()