@@ -228,37 +228,40 @@ export class CloudSecurityService {
228228 const passedCount = findings . filter ( ( f ) => f . passed ) . length ;
229229 const failedCount = findings . filter ( ( f ) => ! f . passed ) . length ;
230230
231- // Create a scan run record
232- const scanRun = await db . integrationCheckRun . create ( {
233- data : {
234- connectionId,
235- checkId : `${ provider } -security-scan` ,
236- checkName : `${ provider . toUpperCase ( ) } Security Scan` ,
237- status : 'success' ,
238- startedAt : new Date ( ) ,
239- completedAt : new Date ( ) ,
240- totalChecked : findings . length ,
241- passedCount,
242- failedCount,
243- } ,
244- } ) ;
245-
246- // Store each finding as a check result
247- if ( findings . length > 0 ) {
248- await db . integrationCheckResult . createMany ( {
249- data : findings . map ( ( finding ) => ( {
250- checkRunId : scanRun . id ,
251- passed : finding . passed ?? false ,
252- resourceType : finding . resourceType ,
253- resourceId : finding . resourceId ,
254- title : finding . title ,
255- description : finding . description ,
256- severity : finding . passed ? 'info' : finding . severity , // Passed checks are info level
257- remediation : finding . remediation ,
258- evidence : ( finding . evidence || { } ) as object ,
259- collectedAt : new Date ( finding . createdAt ) ,
260- } ) ) ,
231+ // Use a transaction to ensure atomicity - both run and results are created together
232+ await db . $transaction ( async ( tx ) => {
233+ // Create a scan run record
234+ const scanRun = await tx . integrationCheckRun . create ( {
235+ data : {
236+ connectionId,
237+ checkId : `${ provider } -security-scan` ,
238+ checkName : `${ provider . toUpperCase ( ) } Security Scan` ,
239+ status : 'success' ,
240+ startedAt : new Date ( ) ,
241+ completedAt : new Date ( ) ,
242+ totalChecked : findings . length ,
243+ passedCount,
244+ failedCount,
245+ } ,
261246 } ) ;
262- }
247+
248+ // Store each finding as a check result
249+ if ( findings . length > 0 ) {
250+ await tx . integrationCheckResult . createMany ( {
251+ data : findings . map ( ( finding ) => ( {
252+ checkRunId : scanRun . id ,
253+ passed : finding . passed ?? false ,
254+ resourceType : finding . resourceType ,
255+ resourceId : finding . resourceId ,
256+ title : finding . title ,
257+ description : finding . description ?? '' ,
258+ severity : finding . passed ? 'info' : finding . severity ,
259+ remediation : finding . remediation ?? null ,
260+ evidence : ( finding . evidence || { } ) as object ,
261+ collectedAt : new Date ( finding . createdAt ) ,
262+ } ) ) ,
263+ } ) ;
264+ }
265+ } ) ;
263266 }
264267}
0 commit comments