Spaces:
Sleeping
signer: stop the infinite "กำลังดึงชื่อผู้ลงนาม…" spinner
Browse filesRoot cause: the browser-side resolver had no fetch timeout, so a hung
request (e.g. a corporate proxy blackholing the socket) left the IIFE
Promise unsettled — streamlit-javascript never posted a value, st_javascript
stayed at its 0/None default, and fetch_signers_for_po returned None forever.
The loading branch had no manual fallback, so the whole form was blocked.
- signer_lookup.py: every fetch now has an AbortController timeout (9s) plus
an overall 14s race-guard; the IIFE always resolves to a JSON string
(starts with "{"), making the Python parse deterministic and removing the
dependency on streamlit-javascript returning a real object.
- streamlit_app.py: the loading branch now also renders the manual
ชื่อผู้ลงนาม text box (same widget key) so the user is never fully blocked.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- signer_lookup.py +90 -58
- streamlit_app.py +6 -1
|
@@ -47,70 +47,102 @@ def pick_position(signers, role):
|
|
| 47 |
|
| 48 |
|
| 49 |
# The browser-side resolver. __PO__ / __TOKEN__ / __GW__ are substituted in build_signers_js.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
_JS_TEMPLATE = r"""
|
| 51 |
(async () => {
|
| 52 |
const GW = "__GW__";
|
| 53 |
const H = {"Authorization": "__TOKEN__", "Content-Type": "application/json", "Accept": "application/json"};
|
| 54 |
const PO = "__PO__";
|
| 55 |
-
const
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
const
|
| 64 |
-
|
| 65 |
-
const
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
{
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
const
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
if (ac[0]) {
|
| 104 |
-
const c = ac[0];
|
| 105 |
-
out.chairman = (((c.name || "") + " " + (c.surname || "")).trim()) || null;
|
| 106 |
-
const u = (c.user && typeof c.user === "object") ? c.user : {};
|
| 107 |
-
out.chairmanPosition = u.PositionNameTH || c.position_name || null;
|
| 108 |
-
}
|
| 109 |
-
return out;
|
| 110 |
-
} catch (err) {
|
| 111 |
-
out.error = String(err);
|
| 112 |
-
return out;
|
| 113 |
-
}
|
| 114 |
})()
|
| 115 |
"""
|
| 116 |
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
# The browser-side resolver. __PO__ / __TOKEN__ / __GW__ are substituted in build_signers_js.
|
| 50 |
+
#
|
| 51 |
+
# CONTRACT: this IIFE ALWAYS resolves to a JSON STRING (never hangs, never returns a bare
|
| 52 |
+
# object) within ~14s. Two guarantees prevent the "infinite กำลังดึง… spinner" bug:
|
| 53 |
+
# (a) every fetch has an AbortController timeout, so no single request can hang the chain
|
| 54 |
+
# (a corporate proxy that blackholes the socket would otherwise never settle the Promise);
|
| 55 |
+
# (b) an overall race-guard caps total time and returns an error dict if the work overruns.
|
| 56 |
+
# Returning JSON.stringify(out) (always starts with "{") makes the Python side's parse
|
| 57 |
+
# deterministic — it no longer depends on streamlit-javascript returning a real object.
|
| 58 |
_JS_TEMPLATE = r"""
|
| 59 |
(async () => {
|
| 60 |
const GW = "__GW__";
|
| 61 |
const H = {"Authorization": "__TOKEN__", "Content-Type": "application/json", "Accept": "application/json"};
|
| 62 |
const PO = "__PO__";
|
| 63 |
+
const TIMEOUT_MS = 9000; // per-request ceiling
|
| 64 |
+
const OVERALL_MS = 14000; // whole-chain ceiling
|
| 65 |
+
const TIMEOUT_MSG = "หมดเวลาเชื่อมต่อระบบ SMP (ตรวจสอบว่าอยู่ในเครือข่าย PTT แล้วลองใหม่)";
|
| 66 |
+
const blank = () => ({pr:null, approver:null, approverPosition:null,
|
| 67 |
+
chairman:null, chairmanPosition:null, error:null});
|
| 68 |
+
|
| 69 |
+
// fetch that can never hang: aborts after TIMEOUT_MS, then parses JSON.
|
| 70 |
+
const jfetch = async (url, opts) => {
|
| 71 |
+
const ac = new AbortController();
|
| 72 |
+
const t = setTimeout(() => ac.abort(), TIMEOUT_MS);
|
| 73 |
+
try { const r = await fetch(url, Object.assign({signal: ac.signal}, opts || {})); return await r.json(); }
|
| 74 |
+
finally { clearTimeout(t); }
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
// The work never rejects: any failure is folded into out.error so the race resolves cleanly.
|
| 78 |
+
const work = (async () => {
|
| 79 |
+
const out = blank();
|
| 80 |
+
try {
|
| 81 |
+
// 1) PO -> SAP PR
|
| 82 |
+
const repBody = {poNo: PO, showMultiplePR: true, sapPrNo:[], prStatus:[], user_id_or_emp_id:[],
|
| 83 |
+
unitCode:[], role:"", procmethod_unit_code:[], projectNo:[], projectStatus:[], title:"",
|
| 84 |
+
commandMethod:"", commandType:"", procurementMethod:"", subProcurementMethod:"",
|
| 85 |
+
procurementCondition:"", purOrgUnitCode:[], torNo:[], torStatus:[], anNo:[], eaStatus:[],
|
| 86 |
+
teNo:[], teStatus:[]};
|
| 87 |
+
const rep = await jfetch(GW + "/gateway-service/v2/Organization_Tracking_Report",
|
| 88 |
+
{method:"POST", headers:H, body: JSON.stringify(repBody)});
|
| 89 |
+
const rows = Array.isArray(rep) ? rep : [];
|
| 90 |
+
const row = rows.find(r => String(r["PO No."]) === PO) || rows[0];
|
| 91 |
+
if (!row) { out.error = "ไม่พบ PO นี้ในระบบ"; return out; }
|
| 92 |
+
const pr = String(row["SAP PR No."] || "");
|
| 93 |
+
out.pr = pr;
|
| 94 |
+
if (!pr) { out.error = "PO ไม่มี SAP PR No."; return out; }
|
| 95 |
+
|
| 96 |
+
// 2) PR -> approver (ผู้มีอำนาจอนุมัติ) from the tracking detail
|
| 97 |
+
const det = await jfetch(GW + "/gateway-service/Organization_Tracking_Details",
|
| 98 |
+
{method:"POST", headers:H, body: JSON.stringify({sapPrNo: pr})});
|
| 99 |
+
const sels = (((det || {})["Selection/PO"] || {}).Selection) || [];
|
| 100 |
+
const sel = sels[0] || {};
|
| 101 |
+
const apprArr = (((sel.detail || [])[0] || {})["ผู้มีอำนาจอนุมัติ"]) || [];
|
| 102 |
+
const apprRaw = String(apprArr[0] || ""); // "<code> <name>"
|
| 103 |
+
const apprCode = (apprRaw.match(/^(\d+)/) || [])[1] || "";
|
| 104 |
+
let apprName = apprRaw.replace(/^\d+\s*/, "").trim();
|
| 105 |
+
|
| 106 |
+
// 3) approver title (นาย/นาง) + position via the HR directory (best effort)
|
| 107 |
+
if (apprCode) {
|
| 108 |
+
try {
|
| 109 |
+
const dir = await jfetch(GW + "/ea-service/master/search_directoryInfo?EmployeeID=" + apprCode,
|
| 110 |
+
{headers:H});
|
| 111 |
+
const e = ((((dir || {}).entries || {}).entry) || [])[0];
|
| 112 |
+
if (e) {
|
| 113 |
+
const full = ((e.INAME || "") + " " + (e.EmployeeNameTH || "")).trim();
|
| 114 |
+
if (full) apprName = full;
|
| 115 |
+
out.approverPosition = e.PositionNameTH || null;
|
| 116 |
+
}
|
| 117 |
+
} catch (_) {}
|
| 118 |
+
}
|
| 119 |
+
out.approver = apprName || null;
|
| 120 |
+
|
| 121 |
+
// 4) PR -> chairman (ประธานกรรมการตรวจรับ = first auditCommittee row)
|
| 122 |
+
const flt = encodeURIComponent(JSON.stringify({eager: {"$where": {sapPrNo: pr}}}));
|
| 123 |
+
const pron = await jfetch(GW + "/master-service/PROnline?filter=" + flt, {headers:H});
|
| 124 |
+
const rec = (Array.isArray(pron) && pron[0]) ? pron[0] : null;
|
| 125 |
+
const inner = rec ? (typeof rec.json === "string" ? JSON.parse(rec.json) : rec.json) : null;
|
| 126 |
+
const ac = (inner && inner.auditCommittee) || [];
|
| 127 |
+
if (ac[0]) {
|
| 128 |
+
const c = ac[0];
|
| 129 |
+
out.chairman = (((c.name || "") + " " + (c.surname || "")).trim()) || null;
|
| 130 |
+
const u = (c.user && typeof c.user === "object") ? c.user : {};
|
| 131 |
+
out.chairmanPosition = u.PositionNameTH || c.position_name || null;
|
| 132 |
+
}
|
| 133 |
+
return out;
|
| 134 |
+
} catch (err) {
|
| 135 |
+
out.error = (err && err.name === "AbortError") ? TIMEOUT_MSG : String(err);
|
| 136 |
+
return out;
|
| 137 |
}
|
| 138 |
+
})();
|
| 139 |
+
|
| 140 |
+
const guard = new Promise(res => setTimeout(() => {
|
| 141 |
+
const o = blank(); o.error = TIMEOUT_MSG; res(o);
|
| 142 |
+
}, OVERALL_MS));
|
| 143 |
+
|
| 144 |
+
const result = await Promise.race([work, guard]);
|
| 145 |
+
return JSON.stringify(result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
})()
|
| 147 |
"""
|
| 148 |
|
|
@@ -1293,7 +1293,12 @@ with right_col:
|
|
| 1293 |
po_val = po_number.strip()
|
| 1294 |
signers = fetch_signers_for_po(po_val) if po_val else {"error": "no po"}
|
| 1295 |
if signers is None:
|
| 1296 |
-
st.info(":material/hourglass_top: กำลังดึงชื่อผู้ลงนามจากระบบ…"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1297 |
elif signers.get("error"):
|
| 1298 |
st.warning(":material/wifi_off: ดึงชื่ออัตโนมัติไม่ได้ "
|
| 1299 |
"(ต้องอยู่ในเครือข่าย PTT) — กรอกชื่อผู้ลงนามเอง")
|
|
|
|
| 1293 |
po_val = po_number.strip()
|
| 1294 |
signers = fetch_signers_for_po(po_val) if po_val else {"error": "no po"}
|
| 1295 |
if signers is None:
|
| 1296 |
+
st.info(":material/hourglass_top: กำลังดึงชื่อผู้ลงนามจากระบบ… "
|
| 1297 |
+
"(ถ้าค้างนานเกินไป ให้กรอกชื่อผู้ลงนามเองด้านล่าง)")
|
| 1298 |
+
# Escape hatch: never leave the form fully blocked while the browser fetch is
|
| 1299 |
+
# in flight (or if streamlit-javascript fails to post its value). Same widget
|
| 1300 |
+
# key as the other fallbacks, so a typed name persists if the state changes.
|
| 1301 |
+
signer = st.text_input("ชื่อผู้ลงนาม", key="signer_manual_widget").strip()
|
| 1302 |
elif signers.get("error"):
|
| 1303 |
st.warning(":material/wifi_off: ดึงชื่ออัตโนมัติไม่ได้ "
|
| 1304 |
"(ต้องอยู่ในเครือข่าย PTT) — กรอกชื่อผู้ลงนามเอง")
|