Skip to content

Commit

Permalink
add merge logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiko committed Jan 23, 2024
1 parent fa863dc commit dbde765
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,6 @@ export class GmailFullSyncService {
return;
}

// const {
// savedMessageIds,
// savedThreadIds
// } = await this.utils.getSavedMessageIdsAndThreadIds(
// messageExternalIds,
// connectedAccountId,
// dataSourceMetadata,
// workspaceDataSource,
// );

// const messageIdsToSave = messageExternalIds.filter(
// (messageId) => !savedMessageIds.includes(messageId),
// );

const messageQueries =
this.utils.createQueriesFromMessageIds(messageExternalIds);

Expand All @@ -88,19 +74,6 @@ export class GmailFullSyncService {
return;
}

// const threads = this.utils.getThreadsFromMessages(messagesToSave);

// const threadsToSave = threads.filter(
// (threadId) => !savedThreadIds.includes(threadId.id),
// );

// await this.utils.saveMessageThreads(
// threadsToSave,
// dataSourceMetadata,
// workspaceDataSource,
// connectedAccount.id,
// );

await this.utils.saveMessages(
messagesToSave,
dataSourceMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,57 +127,15 @@ export class GmailPartialSyncService {
const { messagesAdded, messagesDeleted } =
await this.getMessageIdsAndThreadIdsFromHistory(history);

const {
savedMessageIds: messagesAddedAlreadySaved,
savedThreadIds: threadsAddedAlreadySaved,
} = await this.utils.getSavedMessageIdsAndThreadIds(
messagesAdded,
connectedAccountId,
dataSourceMetadata,
workspaceDataSource,
);

const messageExternalIdsToSave = messagesAdded.filter(
(messageId) =>
!messagesAddedAlreadySaved.includes(messageId) &&
!messagesDeleted.includes(messageId),
);

const { savedMessageIds: messagesDeletedAlreadySaved } =
await this.utils.getSavedMessageIdsAndThreadIds(
messagesDeleted,
connectedAccountId,
dataSourceMetadata,
workspaceDataSource,
);

const messageExternalIdsToDelete = messagesDeleted.filter((messageId) =>
messagesDeletedAlreadySaved.includes(messageId),
);

const messageQueries = this.utils.createQueriesFromMessageIds(
messageExternalIdsToSave,
);
const messageQueries =
this.utils.createQueriesFromMessageIds(messagesAdded);

const { messages: messagesToSave, errors } =
await this.fetchMessagesByBatchesService.fetchAllMessages(
messageQueries,
accessToken,
);

const threads = this.utils.getThreadsFromMessages(messagesToSave);

const threadsToSave = threads.filter(
(thread) => !threadsAddedAlreadySaved.includes(thread.id),
);

await this.utils.saveMessageThreads(
threadsToSave,
dataSourceMetadata,
workspaceDataSource,
connectedAccount.id,
);

await this.utils.saveMessages(
messagesToSave,
dataSourceMetadata,
Expand All @@ -186,15 +144,9 @@ export class GmailPartialSyncService {
gmailMessageChannelId,
);

await this.utils.deleteMessages(
messageExternalIdsToDelete,
dataSourceMetadata,
workspaceDataSource,
);

await this.utils.deleteEmptyThreads(
await this.utils.deleteMessageChannelMessages(
messagesDeleted,
connectedAccountId,
gmailMessageChannelId,
dataSourceMetadata,
workspaceDataSource,
);
Expand Down
Loading

0 comments on commit dbde765

Please sign in to comment.