""" De-identify help feedback older than 30 days: delete the free-text comment and the anonymous session id, keep page / vote / reason / device for trend reporting. Run on a schedule (it is safe to run repeatedly): python -m scripts.deidentify_help_feedback Needs the same FIREBASE_SERVICE_ACCOUNT environment as the API. """ import json import os import time import firebase_admin from firebase_admin import credentials, firestore def run() -> int: if not firebase_admin._apps: sa = os.environ.get("FIREBASE_SERVICE_ACCOUNT") cred = credentials.Certificate(json.loads(sa)) if sa else credentials.ApplicationDefault() firebase_admin.initialize_app(cred) db = firestore.client() # One range filter only (no composite index needed); skip finished records here. due = db.collection("help_feedback").where("deidentifyAfter", "<=", time.time()).stream() count = 0 for doc in due: if doc.to_dict().get("deidentified"): continue doc.reference.update({"comment": firestore.DELETE_FIELD, "sessionId": firestore.DELETE_FIELD, "deidentified": True}) count += 1 return count if __name__ == "__main__": print(f"De-identified {run()} help feedback record(s).")