Skip to content

Commit

Permalink
modify fetch items to retrieve all items
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldesarrollosoftware committed Mar 12, 2024
1 parent 1e3128e commit 8e1b035
Showing 1 changed file with 89 additions and 19 deletions.
108 changes: 89 additions & 19 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,37 +335,75 @@ export class RemoteSyncManager {
const params = {
limit: this.config.fetchFilesLimitPerRequest,
offset: 0,
status: '',
status: 'ALL',
updatedAt: updatedAtCheckpoint
? updatedAtCheckpoint.toISOString()
: undefined,
};
return (await this.fetchItems(params, 'files')) as {
hasMore: boolean;
result: RemoteSyncedFile[];
const allFilesResponse = await this.fetchItems(params, 'files');
if (allFilesResponse.status > 299) {
throw new Error(
`Fetch files response not ok with body ${JSON.stringify(
allFilesResponse.data,
null,
2
)} and status ${allFilesResponse.status}`
);
}

if (Array.isArray(allFilesResponse.data)) {
Logger.info(`Received ${allFilesResponse.data.length} fetched files`);
} else {
Logger.info(
`Expected to receive an array of files, but instead received ${JSON.stringify(
allFilesResponse,
null,
2
)}`
);

throw new Error('Did not receive an array of files');
}

const hasMore =
allFilesResponse.data.length === this.config.fetchFilesLimitPerRequest;

return {
hasMore,
result:
allFilesResponse.data && Array.isArray(allFilesResponse.data)
? allFilesResponse.data.map(this.patchDriveFileResponseItem)
: [],
};
}

private async fetchItems(
private fetchItems = async (
params: {
limit: number;
offset: number;
status: string;
updatedAt: string | undefined;
},
type: 'files' | 'folders'
) {
const fetchWithStatus = async (status: string) => {
params.status = status;
return await this.config.httpClient.get(
`${process.env.NEW_DRIVE_URL}/drive/${type}`,
{ params }
);
};
) => {
return await this.config.httpClient.get(
`${process.env.NEW_DRIVE_URL}/drive/${type}`,
{ params }
);
};

private async fetchItemsExistsAndTrashed(
params: {
limit: number;
offset: number;
status: string;
updatedAt: string | undefined;
},
type: 'files' | 'folders'
) {
const [existingItemsResponse, trashedItemsResponse] = await Promise.all([
fetchWithStatus('EXISTS'),
fetchWithStatus('TRASHED'),
this.fetchItems({ ...params, status: 'EXISTS' }, type),
this.fetchItems({ ...params, status: 'TRASHED' }, type),
]);

if (
Expand Down Expand Up @@ -431,15 +469,47 @@ export class RemoteSyncManager {
const params = {
limit: this.config.fetchFilesLimitPerRequest,
offset: 0,
status: '',
status: 'ALL',
updatedAt: updatedAtCheckpoint
? updatedAtCheckpoint.toISOString()
: undefined,
};

return (await this.fetchItems(params, 'folders')) as {
hasMore: boolean;
result: RemoteSyncedFolder[];
const allFoldersResponse = await this.fetchItems(params, 'folders');

if (allFoldersResponse.status > 299) {
throw new Error(
`Fetch files response not ok with body ${JSON.stringify(
allFoldersResponse.data,
null,
2
)} and status ${allFoldersResponse.status}`
);
}

if (Array.isArray(allFoldersResponse.data)) {
Logger.info(`Received ${allFoldersResponse.data.length} fetched files`);
} else {
Logger.info(
`Expected to receive an array of files, but instead received ${JSON.stringify(
allFoldersResponse,
null,
2
)}`
);

throw new Error('Did not receive an array of files');
}

const hasMore =
allFoldersResponse.data.length === this.config.fetchFilesLimitPerRequest;

return {
hasMore,
result:
allFoldersResponse.data && Array.isArray(allFoldersResponse.data)
? allFoldersResponse.data.map(this.patchDriveFolderResponseItem)
: [],
};
}

Expand Down

0 comments on commit 8e1b035

Please sign in to comment.