From e9abc9840ae97f1a0fa462ae45567465a58bb0ed Mon Sep 17 00:00:00 2001 From: mohammad sedighi Date: Mon, 7 Sep 2026 22:49:32 +0330 Subject: [PATCH] Close colleague wait modal when order is no longer pending. Settle on pending:false or any non-waiting status, and stop polling after a max wait so rejected orders do not leave the overlay stuck. Co-authored-by: Cursor --- src/composables/useColleagueOrderWait.ts | 43 ++++++++++++++++-------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/composables/useColleagueOrderWait.ts b/src/composables/useColleagueOrderWait.ts index 5e40bbe..e74fd7a 100644 --- a/src/composables/useColleagueOrderWait.ts +++ b/src/composables/useColleagueOrderWait.ts @@ -10,6 +10,7 @@ import { getToken } from '@/utils' export const colleagueOrderWaiting = ref(false) const CLIENT_POLL_MS = 5000 +const MAX_WAIT_MS = 15 * 60 * 1000 const STREAM_EVENT = 'colleague-order' type WaitResult = 'success' | 'failed' | 'none' @@ -68,17 +69,31 @@ function stsFromPayload(payload: Record | null | undefined): number return Number.isFinite(sts) ? sts : null } -async function readStatus(orderId: number | null): Promise { +/** نتیجه قطعی از پاسخ status/pending — null یعنی هنوز باید صبر کرد */ +function settleFromPayload(payload: Record | null | undefined): WaitResult | null { + if (!payload || typeof payload !== 'object') return null + + const pending = payload.pending + const sts = stsFromPayload(payload) + + if (sts === 2) return 'success' + if (sts === 3) return 'failed' + if (sts !== null && sts !== 4) return 'failed' + if (pending === false) { + if (sts === 2) return 'success' + return 'failed' + } + return null +} + +async function readSettle(orderId: number | null): Promise { try { if (orderId && orderId > 0) { const row = (await getColleagueOrderStatus(orderId)) as Record - return stsFromPayload(row) + return settleFromPayload(row) } const row = (await getPendingColleagueOrder()) as Record - if (row?.pending === false && row?.sts == null) { - return null - } - return stsFromPayload(row) + return settleFromPayload(row) } catch { return null } @@ -90,6 +105,7 @@ export async function waitForColleagueOrder(orderId?: number | null): Promise { colleagueOrderWaiting.value = true let settled: WaitResult | null = null + const startedAt = Date.now() const finish = (result: WaitResult) => { if (settled) return @@ -101,22 +117,21 @@ export async function waitForColleagueOrder(orderId?: number | null): Promise 0 && Number(detail.order_id) !== orderId) { return } - const sts = stsFromPayload(detail) - if (sts === 2) finish('success') - if (sts === 3) finish('failed') + const result = settleFromPayload(detail ?? null) + if (result) finish(result) } window.addEventListener(STREAM_EVENT, onStream) try { while (!settled) { - const sts = await readStatus(orderId ?? null) - if (sts === 2) { - finish('success') + if (Date.now() - startedAt > MAX_WAIT_MS) { + finish('failed') break } - if (sts === 3) { - finish('failed') + const result = await readSettle(orderId ?? null) + if (result) { + finish(result) break } await new Promise((resolve) => {