11import { db } from '@sim/db'
2- import { subscription , user , workflow , workflowExecutionLogs } from '@sim/db/schema'
2+ import { subscription , user , workflowExecutionLogs , workspace } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
44import { and , eq , inArray , lt , sql } from 'drizzle-orm'
55import { type NextRequest , NextResponse } from 'next/server'
@@ -40,17 +40,17 @@ export async function GET(request: NextRequest) {
4040
4141 const freeUserIds = freeUsers . map ( ( u ) => u . userId )
4242
43- const workflowsQuery = await db
44- . select ( { id : workflow . id } )
45- . from ( workflow )
46- . where ( inArray ( workflow . userId , freeUserIds ) )
43+ const workspacesQuery = await db
44+ . select ( { id : workspace . id } )
45+ . from ( workspace )
46+ . where ( inArray ( workspace . billedAccountUserId , freeUserIds ) )
4747
48- if ( workflowsQuery . length === 0 ) {
49- logger . info ( 'No workflows found for free users' )
50- return NextResponse . json ( { message : 'No workflows found for cleanup' } )
48+ if ( workspacesQuery . length === 0 ) {
49+ logger . info ( 'No workspaces found for free users' )
50+ return NextResponse . json ( { message : 'No workspaces found for cleanup' } )
5151 }
5252
53- const workflowIds = workflowsQuery . map ( ( w ) => w . id )
53+ const workspaceIds = workspacesQuery . map ( ( w ) => w . id )
5454
5555 const results = {
5656 enhancedLogs : {
@@ -77,7 +77,7 @@ export async function GET(request: NextRequest) {
7777 let batchesProcessed = 0
7878 let hasMoreLogs = true
7979
80- logger . info ( `Starting enhanced logs cleanup for ${ workflowIds . length } workflows ` )
80+ logger . info ( `Starting enhanced logs cleanup for ${ workspaceIds . length } workspaces ` )
8181
8282 while ( hasMoreLogs && batchesProcessed < MAX_BATCHES ) {
8383 const oldEnhancedLogs = await db
@@ -99,7 +99,7 @@ export async function GET(request: NextRequest) {
9999 . from ( workflowExecutionLogs )
100100 . where (
101101 and (
102- inArray ( workflowExecutionLogs . workflowId , workflowIds ) ,
102+ inArray ( workflowExecutionLogs . workspaceId , workspaceIds ) ,
103103 lt ( workflowExecutionLogs . createdAt , retentionDate )
104104 )
105105 )
@@ -127,7 +127,7 @@ export async function GET(request: NextRequest) {
127127 customKey : enhancedLogKey ,
128128 metadata : {
129129 logId : String ( log . id ) ,
130- workflowId : String ( log . workflowId ) ,
130+ workflowId : String ( log . workflowId ?? '' ) ,
131131 executionId : String ( log . executionId ) ,
132132 logType : 'enhanced' ,
133133 archivedAt : new Date ( ) . toISOString ( ) ,
0 commit comments