简化履约匹配并支持mock订单测试

This commit is contained in:
yml
2026-05-27 12:55:44 +08:00
parent e8010728a4
commit dd6776c4e4
21 changed files with 1892 additions and 683 deletions
@@ -1,18 +1,9 @@
import {
getFulfillmentProfileByKey,
listFulfillmentProfileRequirements,
replaceFulfillmentProfileRequirements,
upsertFulfillmentProfile,
upsertSkuFulfillmentBinding,
type FulfillmentProfileRequirementInput,
} from '../../repositories/fulfillment-profile-repo.js'
import { upsertProductMatchRule } from '../../repositories/product-match-rule-repo.js'
import { normalizeProductName } from '../order/product-match-service.js'
import { query } from '../../db/client.js'
import {
getKuaishouCloudFulfillmentConfig,
mapKuaishouCloudFulfillmentItemsToBindings,
} from '../order/kuaishou-cloud-fulfillment-config-service.js'
import { nowIso } from '../../utils/time.js'
type JsonObject = Record<string, any>
@@ -48,8 +39,7 @@ const CORE_PROFILES: CoreProfile[] = [
]
export async function ensureFulfillmentCatalogBootstrapped() {
const profileMap = await ensureCoreProfiles()
await syncConfiguredFulfillmentBindings(profileMap)
await ensureCoreProfiles()
}
async function ensureCoreProfiles() {
@@ -82,82 +72,3 @@ async function ensureCoreProfiles() {
return profileMap
}
export async function syncConfiguredFulfillmentBindings(profileMap: JsonObject = {}) {
const timestamp = nowIso()
const bindingsToApply = mapKuaishouCloudFulfillmentItemsToBindings(getKuaishouCloudFulfillmentConfig())
await query('DELETE FROM product_match_rules')
await query('DELETE FROM sku_fulfillment_bindings')
for (const binding of bindingsToApply) {
const profile = profileMap[binding.profileKey] || await getFulfillmentProfileByKey(binding.profileKey)
if (!profile) {
continue
}
const bindingConfig = resolveBindingRuntimeConfig(binding)
const matchShopIds = resolveBindingMatchShopIds(binding)
const match: JsonObject = isPlainObject(binding.match) ? binding.match : {}
const externalSkuName = String(match.externalSkuName || '').trim()
const externalItemId = String(match.externalItemId || '').trim()
const externalSkuCode = String(match.externalSkuCode || '').trim()
for (const matchShopId of matchShopIds) {
await upsertSkuFulfillmentBinding({
skuCode: binding.skuCode,
provider: binding.provider,
platform: binding.platform,
shopId: matchShopId,
profileId: profile.id,
enabled: binding.enabled,
priority: binding.priority,
configJson: JSON.stringify(bindingConfig),
createdAt: timestamp,
updatedAt: timestamp,
})
if (!externalSkuName && !externalItemId && !externalSkuCode) {
continue
}
await upsertProductMatchRule({
provider: binding.provider,
platform: binding.platform,
shopId: matchShopId,
externalItemId,
externalSkuCode,
externalSkuName,
externalSkuNameNormalized: normalizeProductName(externalSkuName),
resolvedSkuCode: binding.skuCode,
enabled: binding.enabled,
priority: binding.priority,
configJson: JSON.stringify(match.config || {}),
createdAt: timestamp,
updatedAt: timestamp,
})
}
}
}
function resolveBindingMatchShopIds(binding: JsonObject = {}) {
return [String(binding.shopId || '').trim()]
}
function resolveBindingRuntimeConfig(binding: JsonObject = {}) {
const baseConfig = isPlainObject(binding.config) ? { ...binding.config } : {}
if (String(binding.provider || '').trim() === '91kaquan' && String(binding.platform || '').trim() === 'kuaishou') {
baseConfig.kuaishouShop = {
...(isPlainObject(baseConfig.kuaishouShop) ? baseConfig.kuaishouShop : {}),
shopId: String(binding.shopId || '').trim(),
shopName: String(binding.shopName || '').trim(),
}
}
return baseConfig
}
function isPlainObject(value: unknown): value is JsonObject {
return Object.prototype.toString.call(value) === '[object Object]'
}