""" Per-organization Google Sheet sync. Each university keeps its own Sheet and edits it directly — no training, no account on our side, no export step. This module walks the organizations that have a `sheet_url` configured and syncs each one into THAT university's own collection, so a registrar editing a deadline changes the answer their own students get and nobody else's. Why an orchestrator rather than changes inside SheetSync: SheetSync's job is "sync one Sheet into one collection", and it does that well already — version markers, chunking, pruning. This module's job is "do that once per university, and make sure one university's failure doesn't stop the others." Keeping them separate means the hard part (per-org isolation) is testable without touching the part that already works. Isolation properties this guarantees: - Each organization's rows are written to its own collection, resolved through org_registry — the same path retrieval and uploads now use. - Each organization has its own version marker, so an unchanged Sheet costs one small HTTP request and no re-embedding. Sharing one marker would make the first org's version look current for everyone and silently skip the rest. - A failure for one organization is caught, logged and reported, and the loop continues. A university with a broken Sheet cannot stop another university's update, and its own previous good data stays in place. """ from __future__ import annotations import logging from typing import Any, Dict, List, Optional logger = logging.getLogger(__name__) def sync_all_organizations( alu_organization_id: Optional[str] = None, force: bool = False, ) -> Dict[str, Any]: """Sync every organization that has a Sheet configured. Returns a per-organization summary. Never raises: a caller on a startup hook or a poll loop should not be taken down by one bad Sheet. """ import db import org_registry from sheet_sync import SheetSync try: orgs: List[dict] = db.list_organizations_with_sheets() except Exception as e: logger.warning("[org_sheet_sync] could not list organizations: %s", e) return {"ok": False, "error": str(e), "organizations": []} if not orgs: logger.info("[org_sheet_sync] no organizations have a sheet_url configured") return {"ok": True, "organizations": []} results: List[Dict[str, Any]] = [] for org in orgs: org_id = str(org.get("id")) name = org.get("name") or org_id entry: Dict[str, Any] = {"organization_id": org_id, "name": name} try: engine = org_registry.get_engine_for_org( org_id, alu_organization_id=alu_organization_id ) if engine is None: raise RuntimeError("no retrieval engine available") syncer = SheetSync( engine, json_url=org.get("sheet_url"), organization_id=org_id, ) result = syncer.refresh(force=force) entry["result"] = result.to_dict() entry["ok"] = result.ok logger.info( "[org_sheet_sync] %s: ok=%s seen=%s upserted=%s deleted=%s skipped=%s", name, result.ok, result.entries_seen, result.entries_upserted, result.entries_deleted, result.skipped_unchanged, ) except Exception as e: # Deliberately swallowed: one university's misconfigured Sheet must # not stop the others from updating. Their previous data is # untouched, so the failure degrades to "stale", never to "wrong". entry["ok"] = False entry["error"] = str(e) logger.warning("[org_sheet_sync] %s failed: %s", name, e) results.append(entry) return { "ok": all(r.get("ok") for r in results), "organizations": results, "synced": sum(1 for r in results if r.get("ok")), "failed": sum(1 for r in results if not r.get("ok")), } def sync_one_organization( organization_id: str, alu_organization_id: Optional[str] = None, force: bool = True, ) -> Dict[str, Any]: """Sync a single organization's Sheet on demand. Used by the admin "refresh now" action so a registrar who has just edited their Sheet doesn't have to wait for the next poll to see the change. """ import db import org_registry from sheet_sync import SheetSync try: orgs = db.list_organizations_with_sheets() except Exception as e: return {"ok": False, "error": str(e)} match = next((o for o in orgs if str(o.get("id")) == str(organization_id)), None) if match is None: return {"ok": False, "error": "organization has no sheet_url configured"} try: engine = org_registry.get_engine_for_org( organization_id, alu_organization_id=alu_organization_id ) if engine is None: return {"ok": False, "error": "no retrieval engine available"} syncer = SheetSync( engine, json_url=match.get("sheet_url"), organization_id=str(organization_id) ) return syncer.refresh(force=force).to_dict() except Exception as e: logger.warning("[org_sheet_sync] single sync failed for %s: %s", organization_id, e) return {"ok": False, "error": str(e)}