Skip to content

Commit

Permalink
fix: gooledrive: update remote cursor even if no files are updated
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Dec 24, 2024
1 parent 11ee725 commit 7c28b0b
Showing 1 changed file with 37 additions and 51 deletions.
88 changes: 37 additions & 51 deletions packages/api/src/filestorage/file/services/googledrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,65 +60,49 @@ export class GoogleDriveService implements IFileService {
),
);

if (permissionsIds.length === 0) {
this.logger.log('No permissions found in the provided files.');
return this.ingestService.ingestData<
UnifiedFilestorageFileOutput,
GoogleDriveFileOutput
>(
if (permissionsIds.length) {
const uniquePermissions = await this.fetchPermissions(
permissionsIds,
sourceData,
drive,
);

// Ingest permissions using the ingestService
const syncedPermissions = await this.ingestService.ingestData<
UnifiedFilestoragePermissionOutput,
GoogledrivePermissionOutput
>(
uniquePermissions,
'googledrive',
connectionId,
'filestorage',
'file',
customFieldMappings,
extraParams,
'permission',
);
}

const uniquePermissions = await this.fetchPermissions(
permissionsIds,
sourceData,
drive,
);

// Ingest permissions using the ingestService
const syncedPermissions = await this.ingestService.ingestData<
UnifiedFilestoragePermissionOutput,
GoogledrivePermissionOutput
>(
uniquePermissions,
'googledrive',
connectionId,
'filestorage',
'permission',
);

this.logger.log(
`Ingested ${uniquePermissions.length} permissions for googledrive files.`,
);

// Create a map of original permission ID to synced permission ID
const permissionIdMap: Map<string, string> = new Map(
syncedPermissions.map((permission) => [
permission.remote_id,
permission.id_fs_permission,
]),
);
this.logger.log(
`Ingested ${uniquePermissions.length} permissions for googledrive files.`,
);

// Update each file's permissions with the synced permission IDs
sourceData.forEach((file) => {
if (file.permissionIds?.length) {
file.internal_permissions = file.permissionIds
.map((permissionId) => permissionIdMap.get(permissionId))
.filter(
(permissionId): permissionId is string =>
permissionId !== undefined,
);
}
});
// Create a map of original permission ID to synced permission ID
const permissionIdMap: Map<string, string> = new Map(
syncedPermissions.map((permission) => [
permission.remote_id,
permission.id_fs_permission,
]),
);

await this.assignDriveIds(sourceData, drive);
// Update each file's permissions with the synced permission IDs
sourceData.forEach((file) => {
if (file.permissionIds?.length) {
file.internal_permissions = file.permissionIds
.map((permissionId) => permissionIdMap.get(permissionId))
.filter(
(permissionId): permissionId is string =>
permissionId !== undefined,
);
}
});
}

// Ingest files with updated permissions
const syncedFiles = await this.ingestService.ingestData<
Expand All @@ -138,6 +122,8 @@ export class GoogleDriveService implements IFileService {
`Ingested a batch of ${syncedFiles.length} googledrive files.`,
);

await this.assignDriveIds(sourceData, drive);

if (remote_cursor) {
await this.prisma.fs_drives.updateMany({
where: {
Expand Down

0 comments on commit 7c28b0b

Please sign in to comment.