refactor(fulfillment): 收敛配置与商品关联写入
This commit is contained in:
@@ -105,7 +105,8 @@ test('人工确认前必须自动匹配履约配置,配置保存/定时任务
|
||||
),
|
||||
])
|
||||
assert.ok(
|
||||
manualSource.includes('matchManualFulfillmentConfigWithClient(client, orderItemId, now)'),
|
||||
manualSource.includes('matchManualFulfillmentConfigWithClient(client, orderItemId)') &&
|
||||
manualSource.includes('attachFulfillmentConfigurationsWithExecutor(client'),
|
||||
'确认时必须按 SKU 自动挂渠道',
|
||||
)
|
||||
assert.ok(
|
||||
|
||||
@@ -17,7 +17,10 @@ import {
|
||||
import { rebuildChannelSalesDailyFacts } from './sales-facts-channel-service.js'
|
||||
import { syncFinanceLedgerFromFacts } from './finance-ledger-service.js'
|
||||
import { scheduleVoucherConsumeForOrderItems } from '../fulfillment/voucher-consume-queue-service.js'
|
||||
import { transitionFulfillmentStateWithExecutor } from '../fulfillment/fulfillment-state-service.js'
|
||||
import {
|
||||
attachFulfillmentConfigurationsWithExecutor,
|
||||
transitionFulfillmentStateWithExecutor,
|
||||
} from '../fulfillment/fulfillment-state-service.js'
|
||||
import {
|
||||
calculateManualCostFen,
|
||||
normalizeManualCreditQuantity,
|
||||
@@ -308,7 +311,7 @@ export async function confirmManualFulfillment(payload: JsonObject, session: Adm
|
||||
const result = await withTransaction(async (client) => {
|
||||
// 配置可能晚于订单创建:确认前按 SKU 再匹配一次(与定时 rematch 同口径),
|
||||
// 保证「配置好之后的新单」不需要人工再点一次「重新匹配」。
|
||||
await matchManualFulfillmentConfigWithClient(client, orderItemId, now)
|
||||
await matchManualFulfillmentConfigWithClient(client, orderItemId)
|
||||
|
||||
const context = await client.query<ConfirmContextRow>(
|
||||
`
|
||||
@@ -791,11 +794,7 @@ async function resolveCreditOwnerAdminUserId(
|
||||
* 与 `rematchManualFulfillments` 使用同一套键:规范中文内部 SKU。
|
||||
* 这样即使配置晚于订单创建,新单在确认时也能自动挂上渠道。
|
||||
*/
|
||||
async function matchManualFulfillmentConfigWithClient(
|
||||
client: PoolClient,
|
||||
orderItemId: number,
|
||||
now: string,
|
||||
) {
|
||||
async function matchManualFulfillmentConfigWithClient(client: PoolClient, orderItemId: number) {
|
||||
const matched = await client.query<{
|
||||
id: number
|
||||
canonical_product_id: number | null
|
||||
@@ -826,17 +825,19 @@ async function matchManualFulfillmentConfigWithClient(
|
||||
)
|
||||
const match = matched.rows[0]
|
||||
if (!match) return false
|
||||
await client.query(
|
||||
`
|
||||
UPDATE order_items
|
||||
SET fulfillment_config_id = $2,
|
||||
canonical_product_id = COALESCE($3, canonical_product_id),
|
||||
updated_at = $4
|
||||
WHERE id = $1
|
||||
AND fulfillment_config_id IS NULL
|
||||
`,
|
||||
[orderItemId, match.id, match.canonical_product_id, now],
|
||||
)
|
||||
await attachFulfillmentConfigurationsWithExecutor(client, {
|
||||
mappings: [
|
||||
{
|
||||
orderItemId,
|
||||
configId: Number(match.id),
|
||||
...(match.canonical_product_id
|
||||
? { canonicalProductId: Number(match.canonical_product_id) }
|
||||
: {}),
|
||||
},
|
||||
],
|
||||
onlyWhenConfigNull: true,
|
||||
onlyWhenCanonicalProductNull: true,
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user