Skip to content

Commit

Permalink
refactor: googledrive: create changes token immediately after first sync
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Dec 23, 2024
1 parent 9dedce5 commit 85e8618
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/api/src/filestorage/folder/services/googledrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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<typeof google.drive>,
connectionId: string,
): Promise<string> {
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,
Expand Down

0 comments on commit 85e8618

Please sign in to comment.