新增:半自动履约工作台与虎牙客户授权流程

- 新增 153 半自动履约迁移、后台任务接口与客户履约接口
- 接入虎牙 Connector 登录会话、绑定与一次性 CK 注入票据
- 新增后台半自动履约页面和客户履约入口
- 完善手工履约配置、成本确认事务与运行时连接器配置
- 同步权威文档、环境示例、部署脚本和迁移展示逻辑
This commit is contained in:
yml2213
2026-11-09 15:29:20 +08:00
parent 4159a435d5
commit cb9538a775
38 changed files with 3697 additions and 30 deletions
@@ -66,10 +66,19 @@ test('人工确认必须单事务:扣款与快照同事务,且使用 with-cl
const end = source.indexOf('async function resolveFundAccountId')
const body = source.slice(start, end)
assert.ok(body.includes('withTransaction('), '人工确认必须包在事务里')
const executeStart = body.indexOf('const execute = async (client: PoolClient)')
const executeEnd = body.indexOf('const result = options.client', executeStart)
const transactionalBody = body.slice(executeStart, executeEnd)
assert.ok(executeStart >= 0 && executeEnd > executeStart, '人工确认必须抽出事务执行体')
assert.ok(
body.indexOf('withTransaction(') < body.indexOf('writeFundExpense(client'),
'备用金扣款必须在同一事务内执行',
body.includes(
'options.client ? await execute(options.client) : await withTransaction(execute)',
),
'默认路径必须包在事务里,外部事务必须复用调用方 client',
)
assert.ok(
transactionalBody.includes('writeFundExpense(client'),
'备用金扣款必须在调用方事务内执行',
)
assert.ok(
body.includes('consumePlatformCreditWithClient(client'),
@@ -65,10 +65,13 @@ type ManualConfiguredChannel = {
enabled: boolean
}
async function loadManualPlatformUnitPrices(): Promise<Record<ManualExecutionPlatformKey, number>> {
const result = await query<{ profile_key: string; config_json: Record<string, unknown> }>(
`SELECT profile_key, config_json FROM fulfillment_profiles WHERE profile_key LIKE 'manual_%'`,
)
async function loadManualPlatformUnitPrices(
client?: Pick<PoolClient, 'query'>,
): Promise<Record<ManualExecutionPlatformKey, number>> {
const sql = `SELECT profile_key, config_json FROM fulfillment_profiles WHERE profile_key LIKE 'manual_%'`
const result = client
? await client.query<{ profile_key: string; config_json: Record<string, unknown> }>(sql)
: await query<{ profile_key: string; config_json: Record<string, unknown> }>(sql)
const prices: Record<ManualExecutionPlatformKey, number> = {
nanhang: 1,
lewan: 1,
@@ -299,17 +302,26 @@ export async function listManualFulfillmentPending(
* 固定成本(消耗积分 > 0):直接用配置值,零输入。
* 成本不固定(消耗积分 = 0):客服必须填实际成本金额。
*/
export async function confirmManualFulfillment(payload: JsonObject, session: AdminSession) {
export type ConfirmManualFulfillmentOptions = {
/** 由调用方提供事务连接;此时调用方负责提交/回滚和提交后的报表刷新。 */
client?: PoolClient
}
export async function confirmManualFulfillment(
payload: JsonObject,
session: AdminSession,
options: ConfirmManualFulfillmentOptions = {},
) {
const orderItemId = normalizeRequiredId(
payload.orderItemId ?? payload.order_item_id,
'请选择订单项',
)
const now = nowIso()
const platformUnitPrices = await loadManualPlatformUnitPrices()
// 钱与成本必须同一事务:先锁订单项、复查未履约,再扣款、最后写快照。
// 任何一步失败(余额不足、成本版本缺失、重复快照)都整体回滚,不会留下「扣了钱没记成本」。
const result = await withTransaction(async (client) => {
const execute = async (client: PoolClient) => {
const now = nowIso()
const platformUnitPrices = await loadManualPlatformUnitPrices(client)
// 配置可能晚于订单创建:确认前按 SKU 再匹配一次(与定时 rematch 同口径),
// 保证「配置好之后的新单」不需要人工再点一次「重新匹配」。
await matchManualFulfillmentConfigWithClient(client, orderItemId)
@@ -716,14 +728,26 @@ export async function confirmManualFulfillment(payload: JsonObject, session: Adm
onAccountBillId,
costPending: onAccountCostPending,
}
})
}
await scheduleVoucherConsumeForOrderItems([orderItemId], 'order_received').catch(() => undefined)
// 成本已落快照,立刻重算该业务日的事实与总账;失败不阻断确认(定时任务会兜底)。
await refreshFactsForOrderItem(orderItemId)
const result = options.client ? await execute(options.client) : await withTransaction(execute)
if (!options.client) {
await runManualFulfillmentPostCommit(orderItemId)
}
return result
}
/**
* 成本事务提交后的非关键旁路动作。
*
* 调用方若把 confirmManualFulfillment 纳入更大的事务,必须在提交后显式调用本函数。
* 这里的失败只影响报表时效,不反写已经提交的成本事实。
*/
export async function runManualFulfillmentPostCommit(orderItemId: number): Promise<void> {
await scheduleVoucherConsumeForOrderItems([orderItemId], 'order_received').catch(() => undefined)
await refreshFactsForOrderItem(orderItemId)
}
/**
* 人工确认后按「订单业务日期」重算销售事实与总账。
*