263 lines
8.6 KiB
TypeScript
263 lines
8.6 KiB
TypeScript
import { APP_CONFIG_KEYS } from '../../config/app-config-keys.js'
|
|
import type { JsonObject } from '../../types/json.js'
|
|
import { readAppConfigEntry, saveAppConfigEntry } from '../config/app-config-store.js'
|
|
|
|
export const VOUCHER_CONSUME_PROVIDERS = ['kuaishou_industry', 'douyin_mashida'] as const
|
|
export type VoucherConsumeProvider = (typeof VOUCHER_CONSUME_PROVIDERS)[number]
|
|
|
|
export const VOUCHER_CONSUME_CHANNELS = [
|
|
'affiliate_dash',
|
|
'work_order',
|
|
'manual',
|
|
'third_party_voucher',
|
|
'on_account',
|
|
'unmatched',
|
|
] as const
|
|
export type VoucherConsumeChannel = (typeof VOUCHER_CONSUME_CHANNELS)[number]
|
|
|
|
export const VOUCHER_CONSUME_EVENTS = [
|
|
'order_received',
|
|
'link_generated',
|
|
'task_created',
|
|
'work_order_published',
|
|
'work_order_assigned',
|
|
] as const
|
|
export type VoucherConsumeEvent = (typeof VOUCHER_CONSUME_EVENTS)[number]
|
|
|
|
export const VOUCHER_CONSUME_MODES = [
|
|
'manual',
|
|
'after_order_received',
|
|
'after_link_generated',
|
|
'after_task_created',
|
|
'after_work_order_published',
|
|
'after_work_order_assigned',
|
|
'after_work_order_published_or_assigned',
|
|
] as const
|
|
export type VoucherConsumeMode = (typeof VOUCHER_CONSUME_MODES)[number]
|
|
|
|
export type VoucherChannelConsumePolicy = {
|
|
enabled: boolean
|
|
mode: VoucherConsumeMode
|
|
delaySeconds: number
|
|
}
|
|
|
|
export type VoucherPlatformConsumePolicy = {
|
|
enabled: boolean
|
|
channels: Record<VoucherConsumeChannel, VoucherChannelConsumePolicy>
|
|
}
|
|
|
|
export type VoucherConsumePolicy = {
|
|
version: number
|
|
enabled: boolean
|
|
platforms: Record<VoucherConsumeProvider, VoucherPlatformConsumePolicy>
|
|
}
|
|
|
|
export function createDefaultVoucherConsumePolicy(): VoucherConsumePolicy {
|
|
return {
|
|
version: 2,
|
|
enabled: true,
|
|
platforms: {
|
|
kuaishou_industry: createDefaultPlatformPolicy(),
|
|
douyin_mashida: createDefaultPlatformPolicy(),
|
|
},
|
|
}
|
|
}
|
|
|
|
export function getVoucherConsumePolicy(): VoucherConsumePolicy {
|
|
return readAppConfigEntry({
|
|
configKey: APP_CONFIG_KEYS.voucherConsumePolicy,
|
|
fallback: createDefaultVoucherConsumePolicy,
|
|
normalize: normalizeVoucherConsumePolicy,
|
|
})
|
|
}
|
|
|
|
export async function saveVoucherConsumePolicy(rawValue: unknown): Promise<VoucherConsumePolicy> {
|
|
return saveAppConfigEntry({
|
|
configKey: APP_CONFIG_KEYS.voucherConsumePolicy,
|
|
value: rawValue,
|
|
normalize: normalizeVoucherConsumePolicy,
|
|
})
|
|
}
|
|
|
|
export function normalizeVoucherConsumePolicy(rawValue: unknown): VoucherConsumePolicy {
|
|
const defaults = createDefaultVoucherConsumePolicy()
|
|
const source = isPlainObject(rawValue) ? rawValue : {}
|
|
const rawPlatforms = isPlainObject(source.platforms) ? source.platforms : {}
|
|
const version = normalizeInteger(source.version, 1, 1, 2)
|
|
|
|
return {
|
|
version: 2,
|
|
enabled: normalizeBoolean(source.enabled, defaults.enabled),
|
|
platforms: {
|
|
kuaishou_industry: normalizePlatformPolicy(
|
|
rawPlatforms.kuaishou_industry,
|
|
defaults.platforms.kuaishou_industry,
|
|
version,
|
|
),
|
|
douyin_mashida: normalizePlatformPolicy(
|
|
rawPlatforms.douyin_mashida,
|
|
defaults.platforms.douyin_mashida,
|
|
version,
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
export function resolveVoucherConsumePolicy(input: {
|
|
provider: VoucherConsumeProvider
|
|
channel: VoucherConsumeChannel
|
|
event: VoucherConsumeEvent
|
|
policy?: VoucherConsumePolicy
|
|
}): { enabled: boolean; delaySeconds: number; mode: VoucherConsumeMode } {
|
|
const policy = input.policy || getVoucherConsumePolicy()
|
|
const platform = policy.platforms[input.provider]
|
|
const channel = platform.channels[input.channel]
|
|
|
|
if (!policy.enabled || !platform.enabled || !channel.enabled || channel.mode === 'manual') {
|
|
return { enabled: false, delaySeconds: 0, mode: channel.mode }
|
|
}
|
|
|
|
return {
|
|
enabled: modeMatchesEvent(channel.mode, input.event),
|
|
delaySeconds: channel.delaySeconds,
|
|
mode: channel.mode,
|
|
}
|
|
}
|
|
|
|
export function isVoucherConsumeProvider(value: unknown): value is VoucherConsumeProvider {
|
|
return (VOUCHER_CONSUME_PROVIDERS as readonly string[]).includes(String(value || '').trim())
|
|
}
|
|
|
|
function createDefaultPlatformPolicy(): VoucherPlatformConsumePolicy {
|
|
return {
|
|
enabled: true,
|
|
channels: {
|
|
affiliate_dash: {
|
|
enabled: true,
|
|
mode: 'after_link_generated',
|
|
delaySeconds: 5,
|
|
},
|
|
work_order: {
|
|
enabled: true,
|
|
mode: 'after_work_order_published_or_assigned',
|
|
delaySeconds: 10,
|
|
},
|
|
manual: {
|
|
enabled: true,
|
|
mode: 'after_task_created',
|
|
delaySeconds: 10,
|
|
},
|
|
third_party_voucher: {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 5,
|
|
},
|
|
on_account: {
|
|
enabled: false,
|
|
mode: 'manual',
|
|
delaySeconds: 0,
|
|
},
|
|
unmatched: {
|
|
enabled: false,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 5,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
function normalizePlatformPolicy(
|
|
rawValue: unknown,
|
|
fallback: VoucherPlatformConsumePolicy,
|
|
version: number,
|
|
): VoucherPlatformConsumePolicy {
|
|
const source = isPlainObject(rawValue) ? rawValue : {}
|
|
const rawChannels = isPlainObject(source.channels) ? source.channels : {}
|
|
const legacyThirdPartyDisabled = isLegacyThirdPartyDisabled(rawValue, version)
|
|
|
|
return {
|
|
enabled: normalizeBoolean(source.enabled, fallback.enabled),
|
|
channels: {
|
|
affiliate_dash: normalizeChannelPolicy(
|
|
rawChannels.affiliate_dash,
|
|
fallback.channels.affiliate_dash,
|
|
['manual', 'after_order_received', 'after_link_generated', 'after_task_created'],
|
|
),
|
|
work_order: normalizeChannelPolicy(rawChannels.work_order, fallback.channels.work_order, [
|
|
'manual',
|
|
'after_order_received',
|
|
'after_task_created',
|
|
'after_work_order_published',
|
|
'after_work_order_assigned',
|
|
'after_work_order_published_or_assigned',
|
|
]),
|
|
manual: normalizeChannelPolicy(rawChannels.manual, fallback.channels.manual, [
|
|
'manual',
|
|
'after_order_received',
|
|
'after_task_created',
|
|
]),
|
|
third_party_voucher: normalizeChannelPolicy(
|
|
legacyThirdPartyDisabled ? {} : rawChannels.third_party_voucher,
|
|
fallback.channels.third_party_voucher,
|
|
['manual', 'after_order_received'],
|
|
),
|
|
on_account: normalizeChannelPolicy(rawChannels.on_account, fallback.channels.on_account, [
|
|
'manual',
|
|
'after_order_received',
|
|
]),
|
|
unmatched: normalizeChannelPolicy(rawChannels.unmatched, fallback.channels.unmatched, [
|
|
'manual',
|
|
'after_order_received',
|
|
]),
|
|
},
|
|
}
|
|
}
|
|
|
|
function isLegacyThirdPartyDisabled(rawPlatform: unknown, version: number): boolean {
|
|
if (version >= 2 || !isPlainObject(rawPlatform)) return false
|
|
const rawChannels = isPlainObject(rawPlatform.channels) ? rawPlatform.channels : {}
|
|
const raw = isPlainObject(rawChannels.third_party_voucher) ? rawChannels.third_party_voucher : {}
|
|
return raw.enabled === false && String(raw.mode || '').trim() === 'manual'
|
|
}
|
|
|
|
function normalizeChannelPolicy(
|
|
rawValue: unknown,
|
|
fallback: VoucherChannelConsumePolicy,
|
|
allowedModes: readonly VoucherConsumeMode[],
|
|
): VoucherChannelConsumePolicy {
|
|
const source = isPlainObject(rawValue) ? rawValue : {}
|
|
const mode = String(source.mode || '').trim() as VoucherConsumeMode
|
|
const fallbackMode = allowedModes.includes(fallback.mode) ? fallback.mode : 'manual'
|
|
|
|
return {
|
|
enabled: normalizeBoolean(source.enabled, fallback.enabled),
|
|
mode: allowedModes.includes(mode) ? mode : fallbackMode,
|
|
delaySeconds: normalizeInteger(source.delaySeconds, fallback.delaySeconds, 0, 3600),
|
|
}
|
|
}
|
|
|
|
function modeMatchesEvent(mode: VoucherConsumeMode, event: VoucherConsumeEvent): boolean {
|
|
if (mode === 'manual') return false
|
|
if (mode === 'after_order_received') return event === 'order_received'
|
|
if (mode === 'after_link_generated') return event === 'link_generated'
|
|
if (mode === 'after_task_created') return event === 'task_created'
|
|
if (mode === 'after_work_order_published') return event === 'work_order_published'
|
|
if (mode === 'after_work_order_assigned') return event === 'work_order_assigned'
|
|
return event === 'work_order_published' || event === 'work_order_assigned'
|
|
}
|
|
|
|
function normalizeBoolean(value: unknown, fallback: boolean): boolean {
|
|
if (value === undefined || value === null || value === '') return fallback
|
|
return value !== false && String(value) !== 'false'
|
|
}
|
|
|
|
function normalizeInteger(value: unknown, fallback: number, min: number, max: number): number {
|
|
const parsed = Number(value)
|
|
if (!Number.isFinite(parsed)) return fallback
|
|
return Math.min(max, Math.max(min, Math.trunc(parsed)))
|
|
}
|
|
|
|
function isPlainObject(value: unknown): value is JsonObject {
|
|
return Object.prototype.toString.call(value) === '[object Object]'
|
|
}
|