@@ -102,9 +102,15 @@ export class GoogleDriveFolderService implements IFolderService {
102
102
103
103
async recursiveGetGoogleDriveFolders (
104
104
auth : OAuth2Client ,
105
+ connectionId : string ,
105
106
) : Promise < GoogleDriveFolderOutput [ ] > {
106
107
const drive = google . drive ( { version : 'v3' , auth } ) ;
107
108
109
+ const lastSyncTime = await this . getLastSyncTime ( connectionId ) ;
110
+ if ( lastSyncTime ) {
111
+ console . log ( `Last sync time is ${ lastSyncTime . toISOString ( ) } ` ) ;
112
+ }
113
+
108
114
const rootDriveId = await drive . files
109
115
. get ( {
110
116
fileId : 'root' ,
@@ -143,8 +149,10 @@ export class GoogleDriveFolderService implements IFolderService {
143
149
let pageToken : string | null = null ;
144
150
145
151
const buildQuery = ( parentId : string | null , driveId : string ) : string => {
146
- const baseQuery =
147
- "mimeType='application/vnd.google-apps.folder' and trashed=false" ;
152
+ let baseQuery = `mimeType='application/vnd.google-apps.folder' and trashed=false` ;
153
+ if ( lastSyncTime ) {
154
+ baseQuery += ` and modifiedTime >= '${ lastSyncTime . toISOString ( ) } '` ;
155
+ }
148
156
return parentId
149
157
? `${ baseQuery } and '${ parentId } ' in parents`
150
158
: `${ baseQuery } and '${ driveId } ' in parents` ;
@@ -371,7 +379,10 @@ export class GoogleDriveFolderService implements IFolderService {
371
379
access_token : this . cryptoService . decrypt ( connection . access_token ) ,
372
380
} ) ;
373
381
374
- const folders = await this . recursiveGetGoogleDriveFolders ( auth ) ;
382
+ const folders = await this . recursiveGetGoogleDriveFolders (
383
+ auth ,
384
+ connection . id_connection ,
385
+ ) ;
375
386
await this . ingestPermissionsForFolders ( folders , connection . id_connection ) ;
376
387
377
388
this . logger . log ( `Synced ${ folders . length } Google Drive folders!` ) ;
@@ -387,6 +398,14 @@ export class GoogleDriveFolderService implements IFolderService {
387
398
throw error ;
388
399
}
389
400
}
401
+
402
+ private async getLastSyncTime ( connectionId : string ) : Promise < Date | null > {
403
+ const lastSync = await this . prisma . fs_folders . findFirst ( {
404
+ where : { id_connection : connectionId } ,
405
+ orderBy : { remote_modified_at : 'desc' } ,
406
+ } ) ;
407
+ return lastSync ? lastSync . remote_modified_at : null ;
408
+ }
390
409
}
391
410
392
411
// Type guard for Google API errors
0 commit comments