Files
admin-web/src/modules/dimension-config/api.ts
T

61 lines
2.0 KiB
TypeScript
Raw Normal View History

2026-08-07 19:23:43 +08:00
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
import type {
CustomerDimensionConfig,
2026-09-03 10:46:07 +08:00
CustomerDimensionCalculationMode,
2026-08-07 20:07:37 +08:00
CustomerDimensionRule,
2026-08-07 19:23:43 +08:00
UpdateCustomerDimensionConfigRequest,
} from './types'
const dimensionConfigPath = '/platform/v1/first-leg/customer-dimension-config'
2026-08-07 20:07:37 +08:00
type RawRule = Omit<CustomerDimensionRule, 'id' | 'version'> & {
id: string | number
version: string | number
}
type RawConfig = Omit<CustomerDimensionConfig, 'tenantId' | 'rules'> & {
2026-08-07 19:23:43 +08:00
tenantId: string | number
2026-08-07 20:07:37 +08:00
rules: RawRule[]
2026-08-07 19:23:43 +08:00
}
2026-08-07 20:07:37 +08:00
function normalizeRule(rule: RawRule): CustomerDimensionRule {
return { ...rule, id: String(rule.id), version: Number(rule.version) }
}
function normalizeConfig(config: RawConfig): CustomerDimensionConfig {
2026-08-07 19:23:43 +08:00
return {
tenantId: String(config.tenantId),
2026-09-03 17:49:01 +08:00
defaultCalculationMode: config.defaultCalculationMode === 'MULTIPLY' ? 'MULTIPLY'
: config.defaultCalculationMode === 'ACTUAL' ? 'ACTUAL' : 'ADD',
2026-08-07 20:07:37 +08:00
rules: config.rules.map(normalizeRule),
2026-08-07 19:23:43 +08:00
}
}
export async function getCustomerDimensionConfig(tenantId: string): Promise<CustomerDimensionConfig> {
2026-08-07 20:07:37 +08:00
const response = await http.get<never, ApiEnvelope<RawConfig>>(dimensionConfigPath, {
2026-08-07 19:23:43 +08:00
params: { tenantId },
})
return normalizeConfig(unwrapApiResponse(response))
}
export async function updateCustomerDimensionConfig(
tenantId: string,
request: UpdateCustomerDimensionConfigRequest,
): Promise<CustomerDimensionConfig> {
2026-08-07 20:07:37 +08:00
const response = await http.put<never, ApiEnvelope<RawConfig>>(dimensionConfigPath, request, {
2026-08-07 19:23:43 +08:00
params: { tenantId },
})
return normalizeConfig(unwrapApiResponse(response))
}
2026-09-03 10:46:07 +08:00
export async function updateCustomerDimensionDefaultCalculationMode(
tenantId: string,
defaultCalculationMode: CustomerDimensionCalculationMode,
): Promise<CustomerDimensionConfig> {
const response = await http.put<never, ApiEnvelope<RawConfig>>(
`${dimensionConfigPath}/default-calculation-mode`,
{ defaultCalculationMode },
{ params: { tenantId } },
)
return normalizeConfig(unwrapApiResponse(response))
}