diff --git a/connectors/src/connectors/google_drive/temporal/activities.ts b/connectors/src/connectors/google_drive/temporal/activities.ts index 4e6c57017a09..1a158035facd 100644 --- a/connectors/src/connectors/google_drive/temporal/activities.ts +++ b/connectors/src/connectors/google_drive/temporal/activities.ts @@ -132,11 +132,12 @@ export async function getDrivesIds(nangoConnectionId: string): Promise< { id: string; name: string; + sharedDrive: boolean; }[] > { const drive = await getDriveClient(nangoConnectionId); let nextPageToken = undefined; - const ids = []; + const ids: { id: string; name: string; sharedDrive: boolean }[] = []; do { const res = await drive.drives.list({ pageSize: 100, @@ -152,12 +153,23 @@ export async function getDrivesIds(nangoConnectionId: string): Promise< } for (const drive of res.data.drives) { if (drive.id && drive.name) { - ids.push({ id: drive.id, name: drive.name }); + ids.push({ id: drive.id, name: drive.name, sharedDrive: true }); } } nextPageToken = res.data.nextPageToken; } while (nextPageToken); + const myDriveRes = await drive.files.get({ fileId: "root" }); + if (myDriveRes.status !== 200) { + throw new Error( + `Error getting my drive. status_code: ${myDriveRes.status}. status_text: ${myDriveRes.statusText}` + ); + } + if (!myDriveRes.data.id) { + throw new Error("My drive id is undefined"); + } + ids.push({ id: myDriveRes.data.id, name: "My Drive", sharedDrive: false }); + return ids; } @@ -490,6 +502,7 @@ export async function incrementalSync( nangoConnectionId: string, dataSourceConfig: DataSourceConfig, driveId: string, + sharedDrive: boolean, startSyncTs: number, nextPageToken?: string ): Promise { @@ -506,7 +519,8 @@ export async function incrementalSync( nextPageToken = await getSyncPageToken( connectorId, nangoConnectionId, - driveId + driveId, + sharedDrive ); } @@ -515,15 +529,21 @@ export async function incrementalSync( const authCredentials = await getAuthObject(nangoConnectionId); const driveClient = await getDriveClient(authCredentials); - const changesRes: GaxiosResponse = - await driveClient.changes.list({ + let opts: drive_v3.Params$Resource$Changes$List = { + pageToken: nextPageToken, + pageSize: 100, + fields: "*", + }; + if (sharedDrive) { + opts = { + ...opts, driveId: driveId, - pageToken: nextPageToken, - pageSize: 100, - fields: "*", includeItemsFromAllDrives: true, supportsAllDrives: true, - }); + }; + } + const changesRes: GaxiosResponse = + await driveClient.changes.list(opts); if (changesRes.status !== 200) { throw new Error( @@ -644,7 +664,8 @@ export async function incrementalSync( async function getSyncPageToken( connectorId: ModelId, nangoConnectionId: string, - driveId: string + driveId: string, + sharedDrive: boolean ) { const last = await GoogleDriveSyncToken.findOne({ where: { @@ -658,10 +679,14 @@ async function getSyncPageToken( const driveClient = await getDriveClient(nangoConnectionId); let lastSyncToken = undefined; if (!lastSyncToken) { - const startTokenRes = await driveClient.changes.getStartPageToken({ - driveId: driveId, - supportsAllDrives: true, - }); + let opts = {}; + if (sharedDrive) { + opts = { + driveId: driveId, + supportsAllDrives: true, + }; + } + const startTokenRes = await driveClient.changes.getStartPageToken(opts); if (startTokenRes.status !== 200) { throw new Error( `Error getting start page token. status_code: ${startTokenRes.status}. status_text: ${startTokenRes.statusText}` @@ -842,7 +867,8 @@ export async function populateSyncTokens(connectorId: ModelId) { const lastSyncToken = await getSyncPageToken( connectorId, connector.connectionId, - drive.id + drive.id, + drive.sharedDrive ); await GoogleDriveSyncToken.upsert({ connectorId: connectorId, diff --git a/connectors/src/connectors/google_drive/temporal/workflows.ts b/connectors/src/connectors/google_drive/temporal/workflows.ts index 303808865ea2..6a4f951875d2 100644 --- a/connectors/src/connectors/google_drive/temporal/workflows.ts +++ b/connectors/src/connectors/google_drive/temporal/workflows.ts @@ -137,6 +137,7 @@ export async function googleDriveIncrementalSync( nangoConnectionId, dataSourceConfig, googleDrive.id, + googleDrive.sharedDrive, startSyncTs, nextPageToken );