修复订单发布与核销并发状态覆盖

This commit is contained in:
yml2213
2026-09-01 14:13:10 +08:00
parent d47360f484
commit c6dd6a8b7c
8 changed files with 667 additions and 57 deletions
@@ -4,6 +4,7 @@ import {
deleteWorkOrder,
listWorkOrderShares,
listWorkOrders,
publishUnassignedWorkOrder,
reopenCancelledWorkOrder,
resolveProblemWorkOrder,
updateWorkOrder,
@@ -15,7 +16,10 @@ import { createHttpError } from '../../utils/http.js'
import { nowIso } from '../../utils/time.js'
import { resolveAdminNotificationEntity } from '../admin/admin-notification-service.js'
import { publishWorkOrderRealtimeChange } from '../realtime/realtime-event-service.js'
import { consumeIndustryVouchersBeforeWorkOrderPublish } from './publish-voucher-service.js'
import {
consumeIndustryVouchersBeforeWorkOrderPublish,
type WorkOrderVoucherConsumeResult,
} from './publish-voucher-service.js'
import {
mapWorkOrderAdmin,
normalizeAmountFen,
@@ -228,55 +232,47 @@ export async function publishAdminWorkOrder(workOrderId: number | string, actorN
})
}
const voucherConsume = await consumeIndustryVouchersBeforeWorkOrderPublish(workOrder)
if (!voucherConsume.ok) {
const failedAt = nowIso()
await createWorkOrderEvent({
workOrderId: workOrder.id,
actorType: 'admin',
actorId: actorName,
eventType: 'publish_voucher_consume_failed',
fromStatus: workOrder.status,
toStatus: workOrder.status,
payloadJson: JSON.stringify(voucherConsume),
now: failedAt,
})
throw createHttpError(voucherConsume.errorMessage || '电子凭证核销失败,订单未发布', {
const now = nowIso()
const published = await publishUnassignedWorkOrder({
workOrderId: workOrder.id,
now,
actorName,
})
if (!published.order) {
throw createHttpError('订单状态已变化,无法发布,请刷新后重试', {
statusCode: 409,
errorCode: 'work_order_publish_voucher_consume_failed',
context: voucherConsume,
errorCode: 'work_order_publish_conflict',
})
}
const now = nowIso()
const updated = await updateWorkOrder(workOrder.id, {
status: WORK_ORDER_STATUS.OPEN,
published_at: now,
hall_queued_at: now,
updated_at: now,
})
if (!updated) {
throw createHttpError(
voucherConsume.voucherCount > 0
? '电子凭证已核销,但订单发布失败,请重试发布'
: '订单发布失败,请重试',
{
statusCode: 409,
errorCode: 'work_order_publish_update_failed',
context: voucherConsume,
},
)
let voucherConsume: WorkOrderVoucherConsumeResult
try {
voucherConsume = await consumeIndustryVouchersBeforeWorkOrderPublish(published.order)
} catch (error) {
voucherConsume = {
ok: false,
voucherCount: 0,
consumedCount: 0,
alreadyConsumedCount: 0,
failedCount: 1,
voucherCodes: [],
errorMessage: error instanceof Error ? error.message : '电子凭证核销失败',
}
}
await createWorkOrderEvent({
workOrderId: workOrder.id,
actorType: 'admin',
actorId: actorName,
eventType: 'published',
fromStatus: workOrder.status,
toStatus: WORK_ORDER_STATUS.OPEN,
payloadJson: JSON.stringify({ voucherConsume }),
now,
})
if (!voucherConsume.ok) {
await createWorkOrderEvent({
workOrderId: workOrder.id,
actorType: 'system',
actorId: 'kuaishou_send_code',
eventType: 'publish_voucher_consume_failed',
fromStatus: WORK_ORDER_STATUS.OPEN,
toStatus: WORK_ORDER_STATUS.OPEN,
payloadJson: JSON.stringify(voucherConsume),
now: nowIso(),
})
}
const currentOrder = await getRequiredWorkOrder(workOrder.id)
publishWorkOrderRealtimeChange({ workOrderId: Number(workOrder.id), hallChanged: true })
const hallConfig = getWorkerHallConfig()
const hallEntry = await listWorkOrders({
@@ -289,7 +285,7 @@ export async function publishAdminWorkOrder(workOrderId: number | string, actorN
excludeFilledSharing: true,
})
return {
order: mapWorkOrderAdmin(updated),
order: mapWorkOrderAdmin(currentOrder),
voucherConsume,
hallVisible: hallEntry.total > 0,
}