优化
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { http } from '@/shared/api/http'
|
||||
import { changeSupplierStatus, createSupplier, getSupplier, getSupplierStats, listSuppliers, updateSupplier } from '@/modules/suppliers/api/platformSuppliers'
|
||||
import type { SaveSupplierRequest } from '@/modules/suppliers/types'
|
||||
|
||||
vi.mock('@/shared/api/http', async (original) => ({
|
||||
...await original<typeof import('@/shared/api/http')>(),
|
||||
http: { get: vi.fn(), post: vi.fn(), put: vi.fn(), patch: vi.fn() },
|
||||
}))
|
||||
|
||||
const payload: SaveSupplierRequest = { name: '测试供应商', category: 'BOOKING', scope: 'DOMESTIC', region: '宁波', legalRepresentative: '张三', contactName: '李四', phone: '13800000000', email: null, address: null, businessLicenseNo: null, licenseFileId: '9007199254740995', status: 'ACTIVE', settlementPeriod: 'MONTHLY_30', remark: null }
|
||||
const resource = { id: 9007199254740993, tenantId: 0, ...payload, createdBy: 9007199254740994, createdAt: '2026-08-13T10:00:00', updatedBy: null, updatedAt: '2026-08-13T10:00:00', deleted: false, version: 1 }
|
||||
|
||||
describe('platform supplier API', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('uses real list and stats endpoints and preserves long identifiers as strings', async () => {
|
||||
vi.mocked(http.get)
|
||||
.mockResolvedValueOnce({ code: '200', data: { records: [resource], page: 1, size: 20, total: 1, pages: 1 } })
|
||||
.mockResolvedValueOnce({ code: '200', data: { booking: 1, customs: 2, trucking: 3, certificate: 4 } })
|
||||
const page = await listSuppliers({ page: 1, size: 20, category: 'BOOKING', scope: 'DOMESTIC', keyword: '测试' })
|
||||
expect(http.get).toHaveBeenNthCalledWith(1, '/platform/v1/suppliers', { params: expect.objectContaining({ category: 'BOOKING', scope: 'DOMESTIC', keyword: '测试' }) })
|
||||
expect(page.records[0]).toMatchObject({ id: expect.any(String), tenantId: '0', licenseFileId: expect.any(String), createdBy: expect.any(String) })
|
||||
expect(await getSupplierStats()).toEqual({ booking: 1, customs: 2, trucking: 3, certificate: 4 })
|
||||
expect(http.get).toHaveBeenNthCalledWith(2, '/platform/v1/suppliers/stats')
|
||||
})
|
||||
|
||||
it('sends tenantId=0 on detail and every write', async () => {
|
||||
vi.mocked(http.get).mockResolvedValue({ code: '200', data: resource })
|
||||
vi.mocked(http.post).mockResolvedValue({ code: '200', data: '77' })
|
||||
vi.mocked(http.put).mockResolvedValue({ code: '200', data: null })
|
||||
vi.mocked(http.patch).mockResolvedValue({ code: '200', data: null })
|
||||
await getSupplier('77'); await createSupplier(payload); await updateSupplier('77', payload); await changeSupplierStatus('77', 'INACTIVE')
|
||||
expect(http.get).toHaveBeenCalledWith('/platform/v1/suppliers/77', { params: { tenantId: 0 } })
|
||||
expect(http.post).toHaveBeenCalledWith('/platform/v1/suppliers', payload, { params: { tenantId: 0 } })
|
||||
expect(http.put).toHaveBeenCalledWith('/platform/v1/suppliers/77', payload, { params: { tenantId: 0 } })
|
||||
expect(http.patch).toHaveBeenCalledWith('/platform/v1/suppliers/77/status', { status: 'INACTIVE' }, { params: { tenantId: 0 } })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,53 @@
|
||||
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
||||
import type { SaveSupplierRequest, Supplier, SupplierPage, SupplierQuery, SupplierStats, SupplierStatus } from '@/modules/suppliers/types'
|
||||
|
||||
const basePath = '/platform/v1/suppliers'
|
||||
const platformScope = 0
|
||||
const id = (value: string | number | null | undefined) => value == null ? null : String(value)
|
||||
|
||||
function normalizeSupplier(raw: Supplier): Supplier {
|
||||
return {
|
||||
...raw,
|
||||
id: String(raw.id), tenantId: String(raw.tenantId),
|
||||
licenseFileId: id(raw.licenseFileId), createdBy: id(raw.createdBy), updatedBy: id(raw.updatedBy),
|
||||
version: Number(raw.version), deleted: Boolean(raw.deleted),
|
||||
}
|
||||
}
|
||||
|
||||
export async function listSuppliers(query: SupplierQuery): Promise<SupplierPage> {
|
||||
const response = await http.get<ApiEnvelope<SupplierPage>, ApiEnvelope<SupplierPage>>(basePath, {
|
||||
params: {
|
||||
page: query.page ?? 1, size: query.size ?? 20, category: query.category,
|
||||
scope: query.scope || undefined, region: query.region?.trim() || undefined,
|
||||
status: query.status || undefined, keyword: query.keyword?.trim() || undefined,
|
||||
},
|
||||
})
|
||||
const data = unwrapApiResponse(response)
|
||||
return { records: data.records.map(normalizeSupplier), page: Number(data.page), size: Number(data.size), total: Number(data.total), pages: Number(data.pages) }
|
||||
}
|
||||
|
||||
export async function getSupplierStats(): Promise<SupplierStats> {
|
||||
const response = await http.get<ApiEnvelope<SupplierStats>, ApiEnvelope<SupplierStats>>(`${basePath}/stats`)
|
||||
const data = unwrapApiResponse(response)
|
||||
return { booking: Number(data.booking), customs: Number(data.customs), trucking: Number(data.trucking), certificate: Number(data.certificate) }
|
||||
}
|
||||
|
||||
export async function getSupplier(supplierId: string): Promise<Supplier> {
|
||||
const response = await http.get<ApiEnvelope<Supplier>, ApiEnvelope<Supplier>>(`${basePath}/${encodeURIComponent(supplierId)}`, { params: { tenantId: platformScope } })
|
||||
return normalizeSupplier(unwrapApiResponse(response))
|
||||
}
|
||||
|
||||
export async function createSupplier(payload: SaveSupplierRequest): Promise<string> {
|
||||
const response = await http.post<ApiEnvelope<string | number>, ApiEnvelope<string | number>>(basePath, payload, { params: { tenantId: platformScope } })
|
||||
return String(unwrapApiResponse(response))
|
||||
}
|
||||
|
||||
export async function updateSupplier(supplierId: string, payload: SaveSupplierRequest): Promise<null> {
|
||||
const response = await http.put<ApiEnvelope<null>, ApiEnvelope<null>>(`${basePath}/${encodeURIComponent(supplierId)}`, payload, { params: { tenantId: platformScope } })
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export async function changeSupplierStatus(supplierId: string, status: SupplierStatus): Promise<null> {
|
||||
const response = await http.patch<ApiEnvelope<null>, ApiEnvelope<null>>(`${basePath}/${encodeURIComponent(supplierId)}/status`, { status }, { params: { tenantId: platformScope } })
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
Reference in New Issue
Block a user