fix(order-service): 回填未提交工单的最新资料配置

This commit is contained in:
yml2213
2026-11-04 12:02:46 +08:00
parent d576e8a2b1
commit b83741cbf7
6 changed files with 81 additions and 3 deletions
@@ -0,0 +1,26 @@
-- 132_backfill_pending_order_service_snapshots.sql
--
-- 旧工单创建于“配置快照”上线之前,打开 /o 时会回退到旧的 requirement_json。
-- 对本分类仍待客户提交资料、且未指派打手的工单,回填当前发布版本快照。
UPDATE work_orders wo
SET service_profile_id = p.id,
service_profile_version_id = v.id,
service_profile_snapshot_json = jsonb_build_object(
'profileId', p.id,
'categoryId', p.category_id,
'versionId', v.id,
'versionNo', v.version_no,
'config', v.config_json
),
requirement_json = jsonb_build_object(
'fields', COALESCE(v.config_json -> 'fields', '[]'::jsonb)
),
updated_at = NOW()
FROM order_service_profiles p
JOIN order_service_profile_versions v ON v.id = p.current_version_id
WHERE wo.category_id = p.category_id
AND p.status <> 'archived'
AND wo.status = 'pending_material'
AND wo.assigned_worker_id IS NULL
AND wo.service_profile_snapshot_json IS NULL;
@@ -202,6 +202,41 @@ export async function setOrderServiceProfileCurrentVersion(
)
}
export async function backfillPendingOrderServiceSnapshotsForCategory(
client: PoolClient,
input: { categoryId: number; profileId: number; versionId: number },
): Promise<number> {
const result = await client.query(
`
UPDATE work_orders wo
SET service_profile_id = $2,
service_profile_version_id = $3,
service_profile_snapshot_json = jsonb_build_object(
'profileId', p.id,
'categoryId', p.category_id,
'versionId', v.id,
'versionNo', v.version_no,
'config', v.config_json
),
requirement_json = jsonb_build_object(
'fields', COALESCE(v.config_json -> 'fields', '[]'::jsonb)
),
updated_at = NOW()
FROM order_service_profiles p
JOIN order_service_profile_versions v ON v.id = p.current_version_id
WHERE p.id = $2
AND p.category_id = $1
AND v.id = $3
AND wo.category_id = p.category_id
AND wo.status = 'pending_material'
AND wo.assigned_worker_id IS NULL
AND wo.service_profile_snapshot_json IS NULL
`,
[input.categoryId, input.profileId, input.versionId],
)
return Number(result.rowCount || 0)
}
export async function getOrderServiceProfileVersion(
versionId: number | string,
client?: Pick<PoolClient, 'query'>,
@@ -7,6 +7,7 @@ import { createHttpError } from '../../utils/http.js'
import { nowIso } from '../../utils/time.js'
import {
archiveOrderServiceProfile,
backfillPendingOrderServiceSnapshotsForCategory,
createOrderServiceProfileVersion,
getOrderServiceProfileVersion,
getPublishedOrderServiceProfileByCategory,
@@ -171,7 +172,12 @@ export async function publishAdminOrderServiceProfile(
adminUserId,
})
await setOrderServiceProfileCurrentVersion(client, { profileId, versionId })
return { versionNo, profileId, versionId }
const backfilledPendingOrders = await backfillPendingOrderServiceSnapshotsForCategory(client, {
categoryId: Number(profile.category_id),
profileId,
versionId,
})
return { versionNo, profileId, versionId, backfilledPendingOrders }
})
}