优化
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { http } from '@/shared/api/http'
|
||||
import { listPlatformTenants } from '@/modules/iam'
|
||||
import { listPlatformUnifiedOrders } from '@/modules/unified-orders'
|
||||
import {
|
||||
getActiveCustomerCount,
|
||||
getDashboardOperationalMetrics,
|
||||
getTodayNewOrderCount,
|
||||
} from './dashboard'
|
||||
|
||||
vi.mock('@/modules/iam', () => ({ listPlatformTenants: vi.fn() }))
|
||||
vi.mock('@/modules/unified-orders', () => ({ listPlatformUnifiedOrders: vi.fn() }))
|
||||
vi.mock('@/shared/api/http', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('@/shared/api/http')>(),
|
||||
http: { get: vi.fn() },
|
||||
}))
|
||||
|
||||
describe('dashboard metrics api', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('normalizes the operational metrics response', async () => {
|
||||
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'ok', data: {
|
||||
inTransitContainerCount: '3', inTransitTeu: '4.25', unknownContainerTypeCount: '1',
|
||||
pendingCustomsOrderCount: '7', asOf: '2026-09-11T12:00:00',
|
||||
} })
|
||||
|
||||
await expect(getDashboardOperationalMetrics()).resolves.toEqual({
|
||||
inTransitContainerCount: 3, inTransitTeu: 4.25, unknownContainerTypeCount: 1,
|
||||
pendingCustomsOrderCount: 7, asOf: '2026-09-11T12:00:00',
|
||||
})
|
||||
expect(http.get).toHaveBeenCalledWith('/platform/v1/first-leg/dashboard/operational-metrics')
|
||||
})
|
||||
|
||||
it('uses list totals for today orders and active customers', async () => {
|
||||
vi.mocked(listPlatformUnifiedOrders).mockResolvedValue({ records: [], page: 1, size: 1, total: 12, pages: 12 })
|
||||
vi.mocked(listPlatformTenants).mockResolvedValue({ records: [], page: 1, size: 1, total: 8, pages: 8 })
|
||||
|
||||
await expect(getTodayNewOrderCount()).resolves.toBe(12)
|
||||
await expect(getActiveCustomerCount()).resolves.toBe(8)
|
||||
|
||||
expect(listPlatformUnifiedOrders).toHaveBeenCalledWith(expect.objectContaining({ page: 1, size: 1 }))
|
||||
const orderQuery = vi.mocked(listPlatformUnifiedOrders).mock.calls[0]![0]
|
||||
expect(orderQuery.createdFrom).toMatch(/T00:00:00\.000$/)
|
||||
expect(orderQuery.createdTo).toMatch(/T23:59:59\.999$/)
|
||||
expect(listPlatformTenants).toHaveBeenCalledWith({ page: 1, size: 1, status: 'ACTIVE' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,55 @@
|
||||
import dayjs from 'dayjs'
|
||||
import { listPlatformTenants } from '@/modules/iam'
|
||||
import { listPlatformUnifiedOrders } from '@/modules/unified-orders'
|
||||
import { http, type ApiEnvelope, unwrapApiResponse } from '@/shared/api/http'
|
||||
|
||||
export interface DashboardOperationalMetrics {
|
||||
inTransitContainerCount: number
|
||||
inTransitTeu: number
|
||||
unknownContainerTypeCount: number
|
||||
pendingCustomsOrderCount: number
|
||||
asOf: string | null
|
||||
}
|
||||
|
||||
interface RawDashboardOperationalMetrics {
|
||||
inTransitContainerCount: number | string
|
||||
inTransitTeu: number | string
|
||||
unknownContainerTypeCount: number | string
|
||||
pendingCustomsOrderCount: number | string
|
||||
asOf?: string | null
|
||||
}
|
||||
|
||||
function numberValue(value: number | string | null | undefined) {
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0
|
||||
}
|
||||
|
||||
export async function getDashboardOperationalMetrics(): Promise<DashboardOperationalMetrics> {
|
||||
const response = await http.get<ApiEnvelope<RawDashboardOperationalMetrics>, ApiEnvelope<RawDashboardOperationalMetrics>>(
|
||||
'/platform/v1/first-leg/dashboard/operational-metrics',
|
||||
)
|
||||
const data = unwrapApiResponse(response)
|
||||
return {
|
||||
inTransitContainerCount: numberValue(data.inTransitContainerCount),
|
||||
inTransitTeu: numberValue(data.inTransitTeu),
|
||||
unknownContainerTypeCount: numberValue(data.unknownContainerTypeCount),
|
||||
pendingCustomsOrderCount: numberValue(data.pendingCustomsOrderCount),
|
||||
asOf: data.asOf || null,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTodayNewOrderCount() {
|
||||
const today = dayjs()
|
||||
const result = await listPlatformUnifiedOrders({
|
||||
createdFrom: today.startOf('day').format('YYYY-MM-DDTHH:mm:ss.SSS'),
|
||||
createdTo: today.endOf('day').format('YYYY-MM-DDTHH:mm:ss.SSS'),
|
||||
page: 1,
|
||||
size: 1,
|
||||
})
|
||||
return result.total
|
||||
}
|
||||
|
||||
export async function getActiveCustomerCount() {
|
||||
const result = await listPlatformTenants({ page: 1, size: 1, status: 'ACTIVE' })
|
||||
return result.total
|
||||
}
|
||||
Reference in New Issue
Block a user