优化
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
||||
import type {
|
||||
ExceptionReleaseRequest,
|
||||
FeeSnapshotAfterApprove,
|
||||
MeasurementReviewDetail,
|
||||
MeasurementReviewHistoryPage,
|
||||
MeasurementReviewHistoryPayload,
|
||||
MeasurementReviewRequest,
|
||||
} from './types'
|
||||
|
||||
async function getData<T>(path: string, params: Record<string, unknown>) {
|
||||
const response = await http.get<ApiEnvelope<T>, ApiEnvelope<T>>(path, { params })
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
function idempotencyKey() {
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') return crypto.randomUUID()
|
||||
return `review-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
||||
}
|
||||
|
||||
async function postReview<T>(path: string, tenantId: string, body: unknown, key = idempotencyKey()) {
|
||||
const response = await http.post<ApiEnvelope<T>, ApiEnvelope<T>>(path, body, {
|
||||
params: { tenantId },
|
||||
headers: { 'Idempotency-Key': key },
|
||||
})
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
function paginationNumber(value: number | string, fallback: number) {
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? Math.trunc(parsed) : fallback
|
||||
}
|
||||
|
||||
export function getMeasurementReviewDetail(tenantId: string, inboundId: string) {
|
||||
return getData<MeasurementReviewDetail>(
|
||||
`/platform/v1/first-leg/inbounds/${inboundId}/measurement`,
|
||||
{ tenantId },
|
||||
)
|
||||
}
|
||||
|
||||
export async function listMeasurementReviewHistory(
|
||||
tenantId: string,
|
||||
inboundId: string,
|
||||
page = 1,
|
||||
size = 20,
|
||||
) {
|
||||
const result = await getData<MeasurementReviewHistoryPayload>(
|
||||
`/platform/v1/inbounds/${inboundId}/measurement-review-history`,
|
||||
{ tenantId, page, size },
|
||||
)
|
||||
return {
|
||||
...result,
|
||||
page: paginationNumber(result.page, page),
|
||||
size: paginationNumber(result.size, size),
|
||||
total: paginationNumber(result.total, 0),
|
||||
pages: paginationNumber(result.pages, 0),
|
||||
} satisfies MeasurementReviewHistoryPage
|
||||
}
|
||||
|
||||
export function approveMeasurement(
|
||||
tenantId: string,
|
||||
inboundId: string,
|
||||
request: MeasurementReviewRequest,
|
||||
key?: string,
|
||||
) {
|
||||
return postReview<FeeSnapshotAfterApprove>(
|
||||
`/platform/v1/inbounds/${inboundId}/measurements/approve`,
|
||||
tenantId,
|
||||
request,
|
||||
key,
|
||||
)
|
||||
}
|
||||
|
||||
export function rejectMeasurement(
|
||||
tenantId: string,
|
||||
inboundId: string,
|
||||
request: MeasurementReviewRequest,
|
||||
key?: string,
|
||||
) {
|
||||
return postReview<void>(
|
||||
`/platform/v1/inbounds/${inboundId}/measurements/reject`,
|
||||
tenantId,
|
||||
request,
|
||||
key,
|
||||
)
|
||||
}
|
||||
|
||||
export function releaseRejectedMeasurement(
|
||||
tenantId: string,
|
||||
inboundId: string,
|
||||
request: ExceptionReleaseRequest,
|
||||
key?: string,
|
||||
) {
|
||||
return postReview<void>(
|
||||
`/platform/v1/inbounds/${inboundId}/measurements/exception-release`,
|
||||
tenantId,
|
||||
request,
|
||||
key,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user