@ -1,8 +1,9 @@
import { prisma } from "@/services/db" ;
import { writeAudit } from "@/services/audit" ;
import { logger } from "@/lib/logger" ;
import { runWithMailCcOwner } from "@/lib/cc-owner" ;
import { runWithMailTrace } from "@/lib/mail-trace" ;
import { assertTransition } from "@/services/state-machine" ;
import { assertTransition , canTransition } from "@/services/state-machine" ;
import {
saveCustomerMessage ,
type CustomerMessageEntity ,
@ -124,6 +125,7 @@ export async function confirmWorkOrder(input: {
input . baseline || null ,
submitted ,
) ;
try {
await markInstructionImportedAndResolveMail ( {
mailId : input.mailId ,
instructionId : checked.unit.id ,
@ -136,7 +138,35 @@ export async function confirmWorkOrder(input: {
fieldMarks : diff.fieldMarks ,
userSupplemented : diff.userSupplemented ,
} ) ;
// 按指令落导入日志(不等整封邮件全部指令完成)
} catch ( err ) {
// CC 已成功:禁止再抛 500 把页面打成「服务异常」;尽量落到可恢复状态
logger . error (
{ err , mailId : String ( input . mailId ) , instructionId : checked.unit.id } ,
"work_order.local_resolve_after_cc_ok" ,
) ;
try {
const to : MailStatus = canTransition ( "IMPORTING" , "PARTIAL_SUCCESS" )
? "PARTIAL_SUCCESS"
: "FAILED" ;
await prisma . mailMessage . update ( {
where : { id : input.mailId } ,
data : {
status : to ,
lastError : ` WO_LOCAL_SYNC_AFTER_CC: ${
err instanceof Error ? err.message : String ( err )
} ` .slice(0, 1000),
version : { increment : 1 } ,
} ,
} ) ;
} catch ( persistErr ) {
logger . error (
{ err : persistErr , mailId : String ( input . mailId ) } ,
"work_order.local_status_recover_failed" ,
) ;
}
}
// 按指令落导入日志(不等整封邮件全部指令完成);库缺列时也不要盖过 CC 成功
try {
await recordInstructionImportLog ( {
mailId : input.mailId ,
instructionId : checked.unit.id ,
@ -148,6 +178,12 @@ export async function confirmWorkOrder(input: {
requestBody : result.request ? JSON . stringify ( result . request ) : null ,
responseBody : result.response ? JSON . stringify ( result . response ) : null ,
} ) ;
} catch ( err ) {
logger . error (
{ err , mailId : String ( input . mailId ) , instructionId : checked.unit.id } ,
"work_order.import_log_after_cc_ok" ,
) ;
}
return { externalId : result.externalId || null , mode : result.mode } ;
}