@@ -2287,6 +2287,65 @@ export class SubmissionService {
22872287 } ) ;
22882288 }
22892289 }
2290+ const TERMINAL_STATUSES = [ 'COMPLETED' , 'FAILED' , 'CANCELLED' ] ;
2291+
2292+ const runs = await this . prisma . aiWorkflowRun . findMany ( {
2293+ where : { submissionId : id } ,
2294+ select : { id : true , status : true } ,
2295+ } ) ;
2296+
2297+ if ( runs . length > 0 ) {
2298+ const nonTerminal = runs . filter (
2299+ ( r ) => ! TERMINAL_STATUSES . includes ( r . status ) ,
2300+ ) ;
2301+
2302+ if ( nonTerminal . length > 0 ) {
2303+ throw new Error (
2304+ `Cannot delete submission: ${ nonTerminal . length } workflow run(s) still active.` ,
2305+ ) ;
2306+ }
2307+
2308+ await this . prisma . $transaction ( async ( tx ) => {
2309+ for ( const run of runs ) {
2310+ const runId = run . id ;
2311+
2312+ const items = await tx . aiWorkflowRunItem . findMany ( {
2313+ where : { workflowRunId : runId } ,
2314+ select : { id : true } ,
2315+ } ) ;
2316+
2317+ const itemIds = items . map ( ( i ) => i . id ) ;
2318+
2319+ const comments = await tx . aiWorkflowRunItemComment . findMany ( {
2320+ where : { workflowRunItemId : { in : itemIds } } ,
2321+ select : { id : true } ,
2322+ } ) ;
2323+
2324+ const commentIds = comments . map ( ( c ) => c . id ) ;
2325+
2326+ await tx . aiWorkflowRunItemVote . deleteMany ( {
2327+ where : { workflowRunItemId : { in : itemIds } } ,
2328+ } ) ;
2329+
2330+ await tx . aiWorkflowRunItemCommentVote . deleteMany ( {
2331+ where : { workflowRunItemCommentId : { in : commentIds } } ,
2332+ } ) ;
2333+
2334+ await tx . aiWorkflowRunItemComment . deleteMany ( {
2335+ where : { workflowRunItemId : { in : itemIds } } ,
2336+ } ) ;
2337+
2338+ await tx . aiWorkflowRunItem . deleteMany ( {
2339+ where : { workflowRunId : runId } ,
2340+ } ) ;
2341+
2342+ await tx . aiWorkflowRun . deleteMany ( {
2343+ where : { id : runId } ,
2344+ } ) ;
2345+ }
2346+ } ) ;
2347+ }
2348+
22902349 await this . prisma . submission . delete ( {
22912350 where : { id } ,
22922351 } ) ;
0 commit comments