113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
from flask import Blueprint, jsonify, request, send_file
|
|
from pathlib import Path
|
|
|
|
from app.auth_jwt import get_current_user
|
|
from app.services.shipping_notice.chat_actions import build_chat_actions_payload
|
|
from app.services.shipping_notice.query_service import (
|
|
get_case_for_user,
|
|
list_cases_for_user,
|
|
serialize_case_detail,
|
|
)
|
|
|
|
workspace_shipping_notice_bp = Blueprint("workspace_shipping_notice", __name__)
|
|
|
|
|
|
def _require_user():
|
|
user = get_current_user()
|
|
if not user:
|
|
return None, (jsonify({"error": "Unauthorized"}), 401)
|
|
return user, None
|
|
|
|
|
|
@workspace_shipping_notice_bp.route("/shipping-cases", methods=["GET"])
|
|
def workspace_shipping_cases():
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
process_status = str(request.args.get("process_status") or "").strip() or None
|
|
limit = int(request.args.get("limit") or 100)
|
|
return jsonify(list_cases_for_user(user.id, process_status=process_status, limit=limit))
|
|
|
|
|
|
@workspace_shipping_notice_bp.route("/shipping-cases/<case_no>", methods=["GET"])
|
|
def workspace_shipping_case_detail(case_no: str):
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
case = get_case_for_user(case_no, user.id)
|
|
if not case:
|
|
return jsonify({"error": "Not found"}), 404
|
|
return jsonify(serialize_case_detail(case))
|
|
|
|
|
|
@workspace_shipping_notice_bp.route("/shipping-cases/<case_no>/chat-actions", methods=["GET"])
|
|
def workspace_shipping_case_chat_actions(case_no: str):
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
case = get_case_for_user(case_no, user.id)
|
|
if not case:
|
|
return jsonify({"error": "Not found"}), 404
|
|
return jsonify(build_chat_actions_payload(case))
|
|
|
|
|
|
@workspace_shipping_notice_bp.route("/shipping-cases/<case_no>/upload-inventory", methods=["POST"])
|
|
def workspace_upload_shipping_inventory(case_no: str):
|
|
"""Hxops chat UI: upload inventory Excel with user auth (not Tool Key)."""
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
case = get_case_for_user(case_no, user.id)
|
|
if not case:
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
upload = request.files.get("file")
|
|
from app.services.shipping_notice import tools as shipping_tools
|
|
|
|
return jsonify(shipping_tools.upload_inventory_file(case.case_no, upload))
|
|
|
|
|
|
@workspace_shipping_notice_bp.route("/shipping-cases/<case_no>/submit-decision", methods=["POST"])
|
|
def workspace_submit_shipping_decision(case_no: str):
|
|
"""Hxops chat UI: submit shipping decision with user auth (bypass Dify tool routing)."""
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
case = get_case_for_user(case_no, user.id)
|
|
if not case:
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
data = request.get_json(silent=True) or {}
|
|
decision = str(data.get("decision") or "").strip()
|
|
if not decision:
|
|
return jsonify({"error": "decision is required"}), 400
|
|
|
|
from app.services.shipping_notice import tools as shipping_tools
|
|
|
|
payload = {"case_id": case.case_no, "decision": decision}
|
|
comment = str(data.get("comment") or "").strip()
|
|
if comment:
|
|
payload["comment"] = comment
|
|
return jsonify(shipping_tools.submit_decision(payload))
|
|
|
|
|
|
@workspace_shipping_notice_bp.route("/shipping-cases/<case_no>/export", methods=["GET"])
|
|
def workspace_shipping_case_export(case_no: str):
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
case = get_case_for_user(case_no, user.id)
|
|
if not case or not case.export_file_path:
|
|
return jsonify({"error": "export file not found"}), 404
|
|
path = Path(case.export_file_path)
|
|
if not path.is_file():
|
|
return jsonify({"error": "export file missing on disk"}), 404
|
|
return send_file(
|
|
path,
|
|
as_attachment=True,
|
|
download_name=case.export_file_name or path.name,
|
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
)
|