From c177a4dc682006110c2489cbf85f14641f53bf02 Mon Sep 17 00:00:00 2001 From: amit <1mitccc@gmail.com> Date: Mon, 16 Dec 2024 21:09:17 +0530 Subject: [PATCH] feat: add incremental sync for google drive folders --- .../folder/services/googledrive/index.ts | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/api/src/filestorage/folder/services/googledrive/index.ts b/packages/api/src/filestorage/folder/services/googledrive/index.ts index 0b4987399..3dbdb127f 100644 --- a/packages/api/src/filestorage/folder/services/googledrive/index.ts +++ b/packages/api/src/filestorage/folder/services/googledrive/index.ts @@ -102,9 +102,15 @@ export class GoogleDriveFolderService implements IFolderService { async recursiveGetGoogleDriveFolders( auth: OAuth2Client, + connectionId: string, ): Promise { const drive = google.drive({ version: 'v3', auth }); + const lastSyncTime = await this.getLastSyncTime(connectionId); + if (lastSyncTime) { + console.log(`Last sync time is ${lastSyncTime.toISOString()}`); + } + const rootDriveId = await drive.files .get({ fileId: 'root', @@ -143,8 +149,10 @@ export class GoogleDriveFolderService implements IFolderService { let pageToken: string | null = null; const buildQuery = (parentId: string | null, driveId: string): string => { - const baseQuery = - "mimeType='application/vnd.google-apps.folder' and trashed=false"; + let baseQuery = `mimeType='application/vnd.google-apps.folder' and trashed=false`; + if (lastSyncTime) { + baseQuery += ` and modifiedTime >= '${lastSyncTime.toISOString()}'`; + } return parentId ? `${baseQuery} and '${parentId}' in parents` : `${baseQuery} and '${driveId}' in parents`; @@ -371,7 +379,10 @@ export class GoogleDriveFolderService implements IFolderService { access_token: this.cryptoService.decrypt(connection.access_token), }); - const folders = await this.recursiveGetGoogleDriveFolders(auth); + const folders = await this.recursiveGetGoogleDriveFolders( + auth, + connection.id_connection, + ); await this.ingestPermissionsForFolders(folders, connection.id_connection); this.logger.log(`Synced ${folders.length} Google Drive folders!`); @@ -387,6 +398,14 @@ export class GoogleDriveFolderService implements IFolderService { throw error; } } + + private async getLastSyncTime(connectionId: string): Promise { + const lastSync = await this.prisma.fs_folders.findFirst({ + where: { id_connection: connectionId }, + orderBy: { remote_modified_at: 'desc' }, + }); + return lastSync ? lastSync.remote_modified_at : null; + } } // Type guard for Google API errors