entityDetailApi.js 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /** 统一实体详情只读 API。路径规划与响应正规化集中在此,组件/store 不认识后端信封。 */
  2. import { requestCockpitJson } from './cockpitApi.js'
  3. import { ENTITY_DETAIL_KIND, entityIdentity, normalizeEntityDetail } from '../domain/entityDetail.js'
  4. export const ENTITY_DETAIL_PATHS = Object.freeze({
  5. [ENTITY_DETAIL_KIND.FACILITY]: (id, target) => target.category
  6. ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '',
  7. [ENTITY_DETAIL_KIND.EMERGENCY]: (id, target) => target.category
  8. ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '',
  9. [ENTITY_DETAIL_KIND.EVENT]: (id, target) => target.family
  10. ? `/events/${encodeURIComponent(id)}/detail?family=${encodeURIComponent(target.family)}` : '',
  11. [ENTITY_DETAIL_KIND.WORK_ORDER]: (id) => `/work-orders/${encodeURIComponent(id)}/detail`,
  12. })
  13. /** 工单处理流程:后端按 orderId 返回 workOrder + timelineEntries(票 129)。 */
  14. export function workOrderTimelinePath(orderId) {
  15. return orderId ? `/work-orders/${encodeURIComponent(orderId)}/timeline` : ''
  16. }
  17. export function entityDetailPath(identityInput) {
  18. const identity = entityIdentity(identityInput)
  19. const build = ENTITY_DETAIL_PATHS[identity.kind]
  20. return identity.valid && build ? build(identity.id, identityInput || {}) : ''
  21. }
  22. export async function fetchEntityDetail(identityInput, options = {}) {
  23. const identity = entityIdentity(identityInput)
  24. const path = options.path || entityDetailPath(identity)
  25. if (!identity.valid || !path) return { ok: false, identity, reason: 'invalid-identity', mode: '', data: null }
  26. // 工单:列表已下发 orderId(票 129),详情 + 处理流程都按主键取。
  27. // 旧调用方没有 orderId 时仍退回列表 prefill,避免刚升级后端前整卡不可用。
  28. if (identity.kind === ENTITY_DETAIL_KIND.WORK_ORDER) {
  29. const prefill = options.prefill && typeof options.prefill === 'object' ? options.prefill : {}
  30. const orderId = String(prefill.orderId || prefill.identityPayload?.orderId || identity.id || '').trim()
  31. if (!orderId || !/^\d+$/.test(orderId)) {
  32. const detail = normalizeEntityDetail(identity, {}, prefill)
  33. return detail
  34. ? { ok: true, identity, mode: prefill ? 'prefill' : 'unavailable', path: '', data: detail }
  35. : { ok: false, identity, mode: '', reason: 'bad-payload', data: null }
  36. }
  37. const detailPath = `/work-orders/${encodeURIComponent(orderId)}/detail`
  38. const timelinePath = workOrderTimelinePath(orderId)
  39. const [detailResult, timelineResult] = await Promise.all([
  40. requestCockpitJson(detailPath, { config: options.config, fetchImpl: options.fetchImpl }),
  41. requestCockpitJson(timelinePath, { config: options.config, fetchImpl: options.fetchImpl }),
  42. ])
  43. if (!detailResult.ok && !timelineResult.ok) {
  44. const fallback = normalizeEntityDetail(identity, {}, prefill)
  45. return fallback
  46. ? { ok: true, identity, mode: prefill ? 'prefill' : 'unavailable', path: '', data: fallback }
  47. : { ok: false, identity, mode: '', reason: detailResult.reason || timelineResult.reason || 'unavailable', data: null }
  48. }
  49. const detailData = detailResult.ok && detailResult.data ? detailResult.data : {}
  50. const timelineData = timelineResult.ok && timelineResult.data ? timelineResult.data : {}
  51. const order = {
  52. ...prefill,
  53. ...(timelineData.workOrder && typeof timelineData.workOrder === 'object' ? timelineData.workOrder : {}),
  54. ...(detailData.order && typeof detailData.order === 'object' ? detailData.order : {}),
  55. }
  56. const source = {
  57. ...prefill,
  58. ...detailData,
  59. ...timelineData,
  60. order,
  61. }
  62. // 时间线端点成功时以它为准;只有它失败才保留 detail 自带的 logs 兜底。
  63. if (timelineResult.ok) {
  64. source.timelineEntries = Array.isArray(timelineData.timelineEntries) ? timelineData.timelineEntries : []
  65. delete source.logs
  66. }
  67. const detail = normalizeEntityDetail(identity, source, prefill)
  68. return detail
  69. ? {
  70. ok: true,
  71. identity,
  72. mode: detailResult.mode || timelineResult.mode || '',
  73. path: detailResult.path || detailPath,
  74. data: detail,
  75. }
  76. : { ok: false, identity, mode: '', path: detailPath, reason: 'bad-payload', data: null }
  77. }
  78. const result = await requestCockpitJson(path, { config: options.config, fetchImpl: options.fetchImpl })
  79. if (!result.ok) return { ...result, identity, data: null }
  80. const detail = normalizeEntityDetail(identity, result.data, options.prefill)
  81. return detail
  82. ? { ok: true, identity, mode: result.mode || '', path: result.path || path, data: detail }
  83. : { ok: false, identity, mode: result.mode || '', path: result.path || path, reason: 'bad-payload', data: null }
  84. }