@ -1,8 +1,9 @@
import { prisma } from "@/services/db" ;
import { prisma } from "@/services/db" ;
import { writeAudit } from "@/services/audit" ;
import { writeAudit } from "@/services/audit" ;
import { logger } from "@/lib/logger" ;
import { runWithMailCcOwner } from "@/lib/cc-owner" ;
import { runWithMailCcOwner } from "@/lib/cc-owner" ;
import { runWithMailTrace } from "@/lib/mail-trace" ;
import { runWithMailTrace } from "@/lib/mail-trace" ;
import { assertTransition } from "@/services/state-machine" ;
import { assertTransition , canTransition } from "@/services/state-machine" ;
import {
import {
saveCustomerMessage ,
saveCustomerMessage ,
type CustomerMessageEntity ,
type CustomerMessageEntity ,
@ -124,30 +125,65 @@ export async function confirmWorkOrder(input: {
input . baseline || null ,
input . baseline || null ,
submitted ,
submitted ,
) ;
) ;
await markInstructionImportedAndResolveMail ( {
try {
mailId : input.mailId ,
await markInstructionImportedAndResolveMail ( {
instructionId : checked.unit.id ,
mailId : input.mailId ,
actor : input.actor ,
instructionId : checked.unit.id ,
mode : result.mode ,
actor : input.actor ,
externalId : result.externalId || null ,
mode : result.mode ,
successLastError : result.mode === "mock" ? "WO_MOCK_OK" : null ,
externalId : result.externalId || null ,
submitted ,
successLastError : result.mode === "mock" ? "WO_MOCK_OK" : null ,
supplementedFields : diff.supplementedFields ,
submitted ,
fieldMarks : diff.fieldMarks ,
supplementedFields : diff.supplementedFields ,
userSupplemented : diff.userSupplemented ,
fieldMarks : diff.fieldMarks ,
} ) ;
userSupplemented : diff.userSupplemented ,
// 按指令落导入日志(不等整封邮件全部指令完成)
} ) ;
await recordInstructionImportLog ( {
} catch ( err ) {
mailId : input.mailId ,
// CC 已成功:禁止再抛 500 把页面打成「服务异常」;尽量落到可恢复状态
instructionId : checked.unit.id ,
logger . error (
kind : "work_order" ,
{ err , mailId : String ( input . mailId ) , instructionId : checked.unit.id } ,
display : logDisplay ,
"work_order.local_resolve_after_cc_ok" ,
status : "SUCCESS" ,
) ;
externalId : result.externalId || null ,
try {
lastError : result.mode === "mock" ? "WO_MOCK_OK" : null ,
const to : MailStatus = canTransition ( "IMPORTING" , "PARTIAL_SUCCESS" )
requestBody : result.request ? JSON . stringify ( result . request ) : null ,
? "PARTIAL_SUCCESS"
responseBody : result.response ? JSON . stringify ( result . response ) : null ,
: "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 ,
kind : "work_order" ,
display : logDisplay ,
status : "SUCCESS" ,
externalId : result.externalId || null ,
lastError : result.mode === "mock" ? "WO_MOCK_OK" : null ,
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 } ;
return { externalId : result.externalId || null , mode : result.mode } ;
}
}