优化
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
import { setTimeout as sleep } from 'node:timers/promises'
|
||||
|
||||
export async function readAfterSynchronization(context, path, { attempts = 49, delay = () => sleep(5000) } = {}) {
|
||||
let response
|
||||
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
||||
response = await context.request('GET', path)
|
||||
const coordinating = response.http === 200 && String(response.body?.code) === 'A0409'
|
||||
&& ['预约批次或运输关联已变化,服务端正在协调完整工作清单',
|
||||
'已开船工作范围变化,请先完成服务端同步'].includes(response.body?.message)
|
||||
if (!coordinating || attempt === attempts) return response
|
||||
await delay()
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
function count(value) {
|
||||
const number = Number(value)
|
||||
return Number.isFinite(number) ? number : NaN
|
||||
@@ -15,7 +30,7 @@ export async function run(context) {
|
||||
const from = shanghaiDate(-7)
|
||||
const to = shanghaiDate(7)
|
||||
|
||||
const calendarResponse = await context.request('GET', `${base}/calendar?from=${from}&to=${to}`)
|
||||
const calendarResponse = await readAfterSynchronization(context, `${base}/calendar?from=${from}&to=${to}`)
|
||||
const calendar = context.apiSuccess(module, '查询预约工作日历', calendarResponse)
|
||||
const days = Array.isArray(calendar?.days) ? calendar.days : []
|
||||
const dayTotal = days.reduce((sum, day) => sum + Number(day?.total || 0), 0)
|
||||
@@ -29,14 +44,14 @@ export async function run(context) {
|
||||
`date=${day?.date || 'missing'},total=${day?.total ?? 'missing'},typeTotal=${typeTotal}`)
|
||||
}
|
||||
|
||||
const summaryResponse = await context.request('GET', `${base}/summary`)
|
||||
const summaryResponse = await readAfterSynchronization(context, `${base}/summary`)
|
||||
const summary = context.apiSuccess(module, '查询预约工作汇总', summaryResponse)
|
||||
const summaryFields = ['all', 'followToday', 'overdue', 'undated', 'review']
|
||||
context.assert(module, '工作汇总五项计数完整', summaryResponse,
|
||||
summaryFields.every((field) => Number.isFinite(Number(summary?.[field]))),
|
||||
summaryFields.map((field) => `${field}=${summary?.[field] ?? 'missing'}`).join(','))
|
||||
|
||||
const pageResponse = await context.request('GET', `${base}/items?view=ALL&page=1&size=20`)
|
||||
const pageResponse = await readAfterSynchronization(context, `${base}/items?view=ALL&page=1&size=20`)
|
||||
const page = context.apiSuccess(module, '查询预约工作事项首页', pageResponse)
|
||||
const records = Array.isArray(page?.records) ? page.records : []
|
||||
const total = count(page?.total)
|
||||
|
||||
Reference in New Issue
Block a user