Add live progress for historical Purchase and Sales sync
This commit is contained in:
@@ -4,7 +4,7 @@ from datetime import date, timedelta
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from fastapi import APIRouter, Form, Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
from fastapi.responses import JSONResponse, RedirectResponse
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.db.common import CommonSessionLocal
|
||||
@@ -20,6 +20,7 @@ from app.modules.accounting.sales_learning_service import (
|
||||
)
|
||||
from app.modules.accounting.ui import _accounting_storage_payload, _find_visible_client, _require_partner, _visible_clients
|
||||
from app.modules.accounting.historical_sync_support import eligible_tally_workstations, resolve_workstation_company, workstation_companies
|
||||
from app.modules.accounting.historical_progress import historical_run_status
|
||||
from app.modules.core.rbac.deps import get_user_permissions, get_user_roles
|
||||
from app.modules.documents.agent_jobs import enqueue_agent_job
|
||||
from app.modules.documents.models import ERPWorkstationAgent
|
||||
@@ -104,6 +105,7 @@ def page(
|
||||
"workstations": workstations,
|
||||
"workstation_companies": {w.id: workstation_companies(w) for w in workstations},
|
||||
"runs": runs,
|
||||
"latest_run": runs[0] if runs else None,
|
||||
"message": message,
|
||||
"error": error,
|
||||
},
|
||||
@@ -191,3 +193,30 @@ def collect(
|
||||
return _go(client_id, error=str(exc))
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/run/{run_id}/status")
|
||||
def run_status(request: Request, run_id: int):
|
||||
db = CommonSessionLocal()
|
||||
try:
|
||||
user, denied = _require_partner(request, db, "accounting.learning.view")
|
||||
if denied:
|
||||
return JSONResponse({"ok": False, "error": "Access denied."}, status_code=403)
|
||||
|
||||
clients, scope = _visible_clients(db, request, user)
|
||||
visible_client_ids = {int(c.id) for c in clients}
|
||||
|
||||
run = db.get(AccountingSalesHistoricalRun, int(run_id))
|
||||
if (
|
||||
not run
|
||||
or int(run.tenant_id) != int(scope.tenant_id)
|
||||
or int(run.client_id) not in visible_client_ids
|
||||
):
|
||||
return JSONResponse({"ok": False, "error": "Historical Sales run not found."}, status_code=404)
|
||||
|
||||
if run.status not in {"completed", "failed"}:
|
||||
ingest_completed_sales_run(db, run)
|
||||
|
||||
return JSONResponse({"ok": True, **historical_run_status(db, run)})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user