Files
admin-web/src/modules/inbounds/fee/api.ts
T

88 lines
3.4 KiB
TypeScript

import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
import type { AdjustPublishedInboundFeeRequest, InboundFeePreview, InboundFeePreviewRequest, InboundFeeSelectionRequest, InboundFeeSnapshot } from './types'
function feeBasePath(inboundId: string) {
const identifier = String(inboundId ?? '').trim()
if (!identifier || identifier === 'null' || identifier === 'undefined') throw new TypeError('inboundId is required')
return `/platform/v1/inbounds/${encodeURIComponent(identifier)}/fees`
}
export async function getLatestInboundFee(tenantId: string, inboundId: string) {
const response = await http.get<unknown, ApiEnvelope<InboundFeeSnapshot>>(
`${feeBasePath(inboundId)}/latest`,
{ params: { tenantId } },
)
return unwrapApiResponse(response)
}
export async function previewInboundFee(tenantId: string, inboundId: string, payload: InboundFeePreviewRequest) {
const response = await http.post<unknown, ApiEnvelope<InboundFeePreview>>(
`${feeBasePath(inboundId)}/preview`, payload, { params: { tenantId } },
)
return unwrapApiResponse(response)
}
export async function calculateInboundFee(tenantId: string, inboundId: string, payload: InboundFeeSelectionRequest, idempotencyKey: string) {
const response = await http.post<unknown, ApiEnvelope<InboundFeeSnapshot>>(
`${feeBasePath(inboundId)}/calculate`, payload,
{ params: { tenantId }, headers: { 'Idempotency-Key': idempotencyKey } },
)
return unwrapApiResponse(response)
}
export async function createManualInboundQuote(tenantId: string, inboundId: string, payload: {
selectedWeightKg: number; selectedVolumeCbm: number; unitPrice: number; reason: string
}, idempotencyKey: string) {
const response = await http.post<unknown, ApiEnvelope<InboundFeeSnapshot>>(
`${feeBasePath(inboundId)}/manual-quote`, payload,
{ params: { tenantId }, headers: { 'Idempotency-Key': idempotencyKey } },
)
return unwrapApiResponse(response)
}
async function transitionInboundFee(
tenantId: string,
inboundId: string,
action: 'confirm' | 'send',
idempotencyKey: string,
) {
const response = await http.post<unknown, ApiEnvelope<InboundFeeSnapshot>>(
`${feeBasePath(inboundId)}/${action}`,
undefined,
{ params: { tenantId }, headers: { 'Idempotency-Key': idempotencyKey } },
)
return unwrapApiResponse(response)
}
export function confirmInboundFee(tenantId: string, inboundId: string, idempotencyKey: string) {
return transitionInboundFee(tenantId, inboundId, 'confirm', idempotencyKey)
}
export function sendInboundFee(tenantId: string, inboundId: string, idempotencyKey: string) {
return transitionInboundFee(tenantId, inboundId, 'send', idempotencyKey)
}
export async function adjustPublishedInboundFee(
tenantId: string,
inboundId: string,
payload: AdjustPublishedInboundFeeRequest,
idempotencyKey: string,
) {
const response = await http.put<unknown, ApiEnvelope<InboundFeeSnapshot>>(
`${feeBasePath(inboundId)}/published`,
payload,
{ params: { tenantId }, headers: { 'Idempotency-Key': idempotencyKey } },
)
return unwrapApiResponse(response)
}
export function createFeeAdjustmentIdempotencyKey() {
return createFeeActionIdempotencyKey('adjust')
}
export function createFeeActionIdempotencyKey(action: 'adjust' | 'calculate' | 'manual' | 'confirm' | 'send') {
const uuid = globalThis.crypto?.randomUUID?.()
if (uuid) return `fee-${action}-${uuid}`
return `fee-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 12)}`
}