"""Mark all companies using the given SARS login usernames as OTP-required. Usage: .venv\Scripts\python mark_otp_required.py """ import os import requests from pathlib import Path from dotenv import load_dotenv load_dotenv(Path(r"C:\Users\SJ\.minimax-agent\projects\Workflow\.env.local").resolve()) pb_url = os.environ["PB_URL"].rstrip("/") pb_token = os.environ["PB_TOKEN"] H = {"Authorization": f"Bearer {pb_token}", "Content-Type": "application/json"} CID = "pbc_3748838150" target_users = [ "Vendre@4043", "tmetrading@gmail.com", "cpkloppers1241", ] # Find all companies matching any of the usernames. matched = [] for u in target_users: f = f'sars_login_username="{u}"' r = requests.get( f"{pb_url}/api/collections/{CID}/records", headers=H, params={"filter": f, "perPage": 200}, ) r.raise_for_status() items = r.json().get("items", []) print(f"{u}: {len(items)} record(s)") matched.extend(items) # Dedupe by id seen = set() unique = [] for rec in matched: if rec["id"] not in seen: seen.add(rec["id"]) unique.append(rec) print(f"\nTotal unique companies to mark otp=true: {len(unique)}") for rec in unique: print( f" - {rec['id']:18s} " f"{rec.get('enterprise_name')!r:42s} " f"(login={rec.get('sars_login_username')})" ) # Patch each with otp=true. ok = 0 for rec in unique: r = requests.patch( f"{pb_url}/api/collections/{CID}/records/{rec['id']}", headers=H, json={"otp": True}, ) if r.status_code == 200: ok += 1 print(f" PATCH OK {rec['id']}") else: print(f" PATCH FAIL {rec['id']}: {r.status_code} {r.text[:200]}") print(f"\nDone. {ok}/{len(unique)} records marked otp=true.")