彻底重构-2

This commit is contained in:
yml
2026-04-09 10:17:50 +08:00
parent df2eb34478
commit 606ef875e2
4 changed files with 155 additions and 12 deletions
@@ -0,0 +1,126 @@
import { runtimeConfig } from '../../config/runtime.js'
import {
getFulfillmentProfileByKey,
listFulfillmentProfileRequirements,
replaceFulfillmentProfileRequirements,
upsertFulfillmentProfile,
upsertSkuFulfillmentBinding,
} from '../../repositories/fulfillment-profile-repo.js'
import { nowIso } from '../../utils/time.js'
const CORE_PROFILES = [
{
profileKey: 'manual_review',
name: '人工发货',
executorKey: 'manual_dispatch',
requiresClaim: false,
autoDispatch: false,
inventoryStrategy: 'static_pool',
requirements: [],
},
{
profileKey: 'tencent_claim_redeem',
name: '腾讯领取兑换',
executorKey: 'tencent_claim_redeem',
requiresClaim: true,
autoDispatch: false,
inventoryStrategy: 'static_pool',
requirements: [
{
roleKey: 'primary_code',
credentialType: 'tencent_code',
quantityPerUnit: 1,
isRequired: true,
},
],
},
]
export async function ensureFulfillmentCatalogBootstrapped() {
const timestamp = nowIso()
const profileMap = {}
for (const profile of CORE_PROFILES) {
const saved = await upsertFulfillmentProfile({
profileKey: profile.profileKey,
name: profile.name,
executorKey: profile.executorKey,
requiresClaim: profile.requiresClaim,
autoDispatch: profile.autoDispatch,
inventoryStrategy: profile.inventoryStrategy,
configJson: '{}',
createdAt: timestamp,
updatedAt: timestamp,
})
if (!saved) {
continue
}
profileMap[profile.profileKey] = saved
const currentRequirements = await listFulfillmentProfileRequirements(saved.id)
if (currentRequirements.length !== profile.requirements.length) {
await replaceFulfillmentProfileRequirements(saved.id, profile.requirements, timestamp)
}
}
const configuredBindings = normalizeConfiguredBindings(runtimeConfig.orders?.fulfillmentBindings)
const bindingsToApply = configuredBindings.length > 0
? configuredBindings
: inferDefaultBindingsFromSkuMappings(runtimeConfig.orders?.skuMappings)
for (const binding of bindingsToApply) {
const profile = profileMap[binding.profileKey] || await getFulfillmentProfileByKey(binding.profileKey)
if (!profile) {
continue
}
await upsertSkuFulfillmentBinding({
skuCode: binding.skuCode,
provider: binding.provider,
platform: binding.platform,
shopId: binding.shopId,
profileId: profile.id,
enabled: binding.enabled,
priority: binding.priority,
configJson: JSON.stringify(binding.config || {}),
createdAt: timestamp,
updatedAt: timestamp,
})
}
}
function normalizeConfiguredBindings(bindings) {
if (!Array.isArray(bindings)) {
return []
}
return bindings
.map((binding) => ({
skuCode: String(binding?.skuCode || '').trim(),
provider: String(binding?.provider || '').trim(),
platform: String(binding?.platform || '').trim(),
shopId: String(binding?.shopId || '').trim(),
profileKey: String(binding?.profileKey || '').trim() || 'manual_review',
enabled: binding?.enabled !== false,
priority: Number(binding?.priority || 100),
config: binding?.config || {},
}))
.filter((binding) => binding.skuCode)
}
function inferDefaultBindingsFromSkuMappings(skuMappings) {
const values = Object.values(skuMappings || {})
const uniqueSkuCodes = [...new Set(values.map((value) => String(value || '').trim()).filter(Boolean))]
return uniqueSkuCodes.map((skuCode) => ({
skuCode,
provider: '',
platform: '',
shopId: '',
profileKey: 'tencent_claim_redeem',
enabled: true,
priority: 100,
config: {},
}))
}