61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
|
import type {
|
|
CustomerDimensionConfig,
|
|
CustomerDimensionCalculationMode,
|
|
CustomerDimensionRule,
|
|
UpdateCustomerDimensionConfigRequest,
|
|
} from './types'
|
|
|
|
const dimensionConfigPath = '/platform/v1/first-leg/customer-dimension-config'
|
|
|
|
type RawRule = Omit<CustomerDimensionRule, 'id' | 'version'> & {
|
|
id: string | number
|
|
version: string | number
|
|
}
|
|
type RawConfig = Omit<CustomerDimensionConfig, 'tenantId' | 'rules'> & {
|
|
tenantId: string | number
|
|
rules: RawRule[]
|
|
}
|
|
|
|
function normalizeRule(rule: RawRule): CustomerDimensionRule {
|
|
return { ...rule, id: String(rule.id), version: Number(rule.version) }
|
|
}
|
|
|
|
function normalizeConfig(config: RawConfig): CustomerDimensionConfig {
|
|
return {
|
|
tenantId: String(config.tenantId),
|
|
defaultCalculationMode: config.defaultCalculationMode === 'MULTIPLY' ? 'MULTIPLY'
|
|
: config.defaultCalculationMode === 'ACTUAL' ? 'ACTUAL' : 'ADD',
|
|
rules: config.rules.map(normalizeRule),
|
|
}
|
|
}
|
|
|
|
export async function getCustomerDimensionConfig(tenantId: string): Promise<CustomerDimensionConfig> {
|
|
const response = await http.get<never, ApiEnvelope<RawConfig>>(dimensionConfigPath, {
|
|
params: { tenantId },
|
|
})
|
|
return normalizeConfig(unwrapApiResponse(response))
|
|
}
|
|
|
|
export async function updateCustomerDimensionConfig(
|
|
tenantId: string,
|
|
request: UpdateCustomerDimensionConfigRequest,
|
|
): Promise<CustomerDimensionConfig> {
|
|
const response = await http.put<never, ApiEnvelope<RawConfig>>(dimensionConfigPath, request, {
|
|
params: { tenantId },
|
|
})
|
|
return normalizeConfig(unwrapApiResponse(response))
|
|
}
|
|
|
|
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))
|
|
}
|