72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
|
import type {
|
|
MeasurementApprovalResponse,
|
|
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<MeasurementApprovalResponse>(
|
|
`/platform/v1/inbounds/${inboundId}/measurements/approve`,
|
|
tenantId,
|
|
request,
|
|
key,
|
|
)
|
|
}
|