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.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/", 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//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", )