外部测试内部绑定ok
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { runtimeConfig } from '../../config/runtime.js'
|
||||
import {
|
||||
getFulfillmentProfileByKey,
|
||||
listFulfillmentProfileRequirements,
|
||||
@@ -6,6 +5,10 @@ import {
|
||||
upsertFulfillmentProfile,
|
||||
upsertSkuFulfillmentBinding,
|
||||
} 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 { getOrderFulfillmentBindingConfigs } from '../order/fulfillment-binding-config-service.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
|
||||
const CORE_PROFILES = [
|
||||
@@ -37,6 +40,11 @@ const CORE_PROFILES = [
|
||||
]
|
||||
|
||||
export async function ensureFulfillmentCatalogBootstrapped() {
|
||||
const profileMap = await ensureCoreProfiles()
|
||||
await syncConfiguredFulfillmentBindings(profileMap)
|
||||
}
|
||||
|
||||
async function ensureCoreProfiles() {
|
||||
const timestamp = nowIso()
|
||||
const profileMap = {}
|
||||
|
||||
@@ -64,7 +72,15 @@ export async function ensureFulfillmentCatalogBootstrapped() {
|
||||
}
|
||||
}
|
||||
|
||||
const bindingsToApply = normalizeConfiguredBindings(runtimeConfig.orders?.fulfillmentBindings)
|
||||
return profileMap
|
||||
}
|
||||
|
||||
export async function syncConfiguredFulfillmentBindings(profileMap = {}) {
|
||||
const timestamp = nowIso()
|
||||
const bindingsToApply = getOrderFulfillmentBindingConfigs()
|
||||
|
||||
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)
|
||||
@@ -84,24 +100,30 @@ export async function ensureFulfillmentCatalogBootstrapped() {
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
})
|
||||
|
||||
const match = binding.match || {}
|
||||
const externalSkuName = String(match.externalSkuName || '').trim()
|
||||
const externalItemId = String(match.externalItemId || '').trim()
|
||||
const externalSkuCode = String(match.externalSkuCode || '').trim()
|
||||
|
||||
if (!externalSkuName && !externalItemId && !externalSkuCode) {
|
||||
continue
|
||||
}
|
||||
|
||||
await upsertProductMatchRule({
|
||||
provider: binding.provider,
|
||||
platform: binding.platform,
|
||||
shopId: binding.shopId,
|
||||
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 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user