From 85e86181141c908b5a7f14bb300d44c3ccb63cd5 Mon Sep 17 00:00:00 2001 From: amit <1mitccc@gmail.com> Date: Mon, 23 Dec 2024 09:11:10 +0530 Subject: [PATCH] refactor: googledrive: create changes token immediately after first sync --- .../folder/services/googledrive/index.ts | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/api/src/filestorage/folder/services/googledrive/index.ts b/packages/api/src/filestorage/folder/services/googledrive/index.ts index 56b927cf2..39bf1b277 100644 --- a/packages/api/src/filestorage/folder/services/googledrive/index.ts +++ b/packages/api/src/filestorage/folder/services/googledrive/index.ts @@ -146,6 +146,11 @@ export class GoogleDriveFolderService implements IFolderService { this.logger.log(`Synced ${folders.length} Google Drive folders!`); + if (isFirstSync) { + const drive = google.drive({ version: 'v3', auth }); + await this.createAndSetRemoteCursor(drive, connection.id_connection); + } + return { data: folders, message: 'Google Drive folders retrieved', @@ -751,19 +756,25 @@ export class GoogleDriveFolderService implements IFolderService { }); let remoteCursor = internalDrive?.remote_cursor; if (!remoteCursor) { - const startPageToken = await this.executeWithRetry(() => - drive.changes - .getStartPageToken({ supportsAllDrives: true }) // one cursor for all drives - .then((response) => response.data.startPageToken), - ); - remoteCursor = startPageToken; - - // for first incremental sync - await this.updateRemoteCursor(remoteCursor, connectionId); + remoteCursor = await this.createAndSetRemoteCursor(drive, connectionId); } return remoteCursor; } + private async createAndSetRemoteCursor( + drive: ReturnType, + connectionId: string, + ): Promise { + const startPageToken = await this.executeWithRetry(() => + drive.changes.getStartPageToken({ supportsAllDrives: true }), + ); + await this.updateRemoteCursor( + startPageToken.data.startPageToken, + connectionId, + ); + return startPageToken.data.startPageToken; + } + private async updateRemoteCursor( remoteCursor: string, connectionId: string,