Skip to content

The open alternative to Salesforce, designed for AI.

Skip missing Microsoft messages instead of resetting the mailbox sync (#26107)

neo773c747dbc2d943
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-fetch-by-batch.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-fetch-by-batch.service.ts
index 77929e65340..19f8fa05033 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-fetch-by-batch.service.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-fetch-by-batch.service.ts
@@ -53,7 +53,7 @@ export class MicrosoftFetchByBatchService {
url: `/me/messages/${messageId}?$select=${selectedFields}`,
headers: {
'Content-Type': 'application/json',
- Prefer: 'outlook.body-content-type="text"',
+ Prefer: 'outlook.body-content-type="text", IdType="ImmutableId"',
},
}));
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts
index 25bf924f4c4..4c892270e28 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts
@@ -76,6 +76,10 @@ export class MicrosoftGetMessagesService {
const messages = parsedResponses.map((response) => {
if ('error' in response) {
+ if (response.error.statusCode === 404) {
+ return undefined;
+ }
+
throw new MicrosoftImportDriverException(
response.error.message,
response.error.code,
diff --git a/packages/twenty-server/test/integration/microsoft/messaging/import-error-mapping.integration-spec.ts b/packages/twenty-server/test/integration/microsoft/messaging/import-error-mapping.integration-spec.ts
index ebb8e6de9b6..20d5b88cbca 100644
--- a/packages/twenty-server/test/integration/microsoft/messaging/import-error-mapping.integration-spec.ts
+++ b/packages/twenty-server/test/integration/microsoft/messaging/import-error-mapping.integration-spec.ts
@@ -1,3 +1,6 @@
+import { randomUUID } from 'node:crypto';
+
+import { isNonEmptyString } from '@sniptt/guards';
import {
ConnectedAccountProvider,
MessageChannelSyncStage,
@@ -6,6 +9,7 @@ import {
import { ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity';
import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
+import { MessageFolderEntity } from 'src/engine/metadata-modules/message-folder/entities/message-folder.entity';
import { MICROSOFT_PERMANENT_ACCOUNT_ERROR_CODES } from 'src/modules/connected-account/constants/microsoft-permanent-account-error-codes.constant';
import {
@@ -13,6 +17,7 @@ import {
setupMicrosoftMock,
} from 'test/integration/microsoft/mocks/setup-microsoft-mock.util';
import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';
+import { findImportedMessageSubjects } from 'test/integration/utils/find-imported-records.util';
import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';
import {
queryConnectedAccount,
@@ -23,6 +28,31 @@ import { runMessageChannelSync } from 'test/integration/utils/run-message-channe
const HANDLE = '[email protected]';
+const FETCHABLE_SUBJECT = `Microsoft fetchable message ${randomUUID()}`;
+const UNFETCHABLE_SUBJECT = `Microsoft unfetchable message ${randomUUID()}`;
+
+const inboxMessage = (id: string, subject: string) => ({
+ id,
+ subject,
+ body: { contentType: 'text', content: subject },
+ receivedDateTime: '2026-08-13T00:00:00.000Z',
+ internetMessageId: `<${id}@example.com>`,
+ conversationId: `${id}-conversation`,
+ parentFolderId: 'inbox',
+ isDraft: false,
+ from: { emailAddress: { address: '[email protected]' } },
+ toRecipients: [{ emailAddress: { address: HANDLE } }],
+});
+
+const FETCHABLE_MESSAGE = inboxMessage(
+ 'microsoft-import-error-mapping-fetchable-message',
+ FETCHABLE_SUBJECT,
+);
+const UNFETCHABLE_MESSAGE = inboxMessage(
+ 'microsoft-import-error-mapping-unfetchable-message',
+ UNFETCHABLE_SUBJECT,
+);
+
const TEMPORARY_FAILURES: [string, MicrosoftGraphFailure][] = [
[
'a 429 throttled response',
@@ -43,7 +73,10 @@ const TEMPORARY_FAILURES: [string, MicrosoftGraphFailure][] = [
];
describe('Microsoft messaging import error mapping (integration)', () => {
- const microsoft = setupMicrosoftMock({ handle: HANDLE });
+ const microsoft = setupMicrosoftMock({
+ handle: HANDLE,
+ messages: [FETCHABLE_MESSAGE, UNFETCHABLE_MESSAGE],
+ });
let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;
@@ -170,4 +203,26 @@ describe('Microsoft messaging import error mapping (integration)', () => {
expect(storedChannel.syncCursor).toBeFalsy();
}, 60000);
+
+ it('imports the rest of the batch and keeps the folder cursors when one listed message is not found on fetch', async () => {
+ microsoft.failMessageFetch(UNFETCHABLE_MESSAGE.id);
+
+ await runMessageChannelSync(channel.channelId);
+
+ expect(
+ await findImportedMessageSubjects([
+ FETCHABLE_SUBJECT,
+ UNFETCHABLE_SUBJECT,
+ ]),
+ ).toEqual([FETCHABLE_SUBJECT]);
+
+ const syncedFolders = await getCoreRepository<MessageFolderEntity>(
+ MessageFolderEntity,
+ ).findBy({ messageChannelId: channel.channelId, isSynced: true });
+
+ expect(syncedFolders.length).toBeGreaterThan(0);
+ expect(
+ syncedFolders.every((folder) => isNonEmptyString(folder.syncCursor)),
+ ).toBe(true);
+ }, 60000);
});
diff --git a/packages/twenty-server/test/integration/microsoft/mocks/microsoft-mailbox-handlers.util.ts b/packages/twenty-server/test/integration/microsoft/mocks/microsoft-mailbox-handlers.util.ts
index 7f0e51be042..10de4cdd1c1 100644
--- a/packages/twenty-server/test/integration/microsoft/mocks/microsoft-mailbox-handlers.util.ts
+++ b/packages/twenty-server/test/integration/microsoft/mocks/microsoft-mailbox-handlers.util.ts
@@ -9,6 +9,7 @@ export const microsoftMailboxHandlers = (
folderStore: MockEntityStore<MailFolder>,
messages: Array<Record<string, unknown>> = [],
removedMessageIdsByFolderId: Record<string, string[]> = {},
+ { unfetchableMessageIds = [] }: { unfetchableMessageIds?: string[] } = {},
): MswHandler[] => [
http.get('*/me/mailFolders', () =>
HttpResponse.json<{ value: MailFolder[] }>({ value: folderStore.list() }),
@@ -55,7 +56,8 @@ export const microsoftMailboxHandlers = (
(candidate) => candidate.id === messageId,
);
- return isDefined(message)
+ return isDefined(message) &&
+ !unfetchableMessageIds.includes(messageId ?? '')
? { id, status: 200, body: message }
: { id, status: 404, body: { error: { message: 'Not Found' } } };
}),
diff --git a/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts b/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
index f29751de3c3..7766d9eff7f 100644
--- a/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
+++ b/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
@@ -39,6 +39,7 @@ export type MicrosoftMock = {
) => void;
moveMessageToFolder: (messageId: string, targetFolderId: string) => void;
deleteMessage: (messageId: string) => void;
+ failMessageFetch: (messageId: string) => void;
failSubscriptionRenewal: () => void;
failMessageDelta: (failure: MicrosoftGraphFailure) => void;
failCalendarDelta: (failure: MicrosoftGraphFailure) => void;
@@ -220,6 +221,15 @@ export const setupMicrosoftMock = ({
messages.splice(messages.indexOf(message), 1);
},
+ failMessageFetch: (messageId) =>
+ httpMock.use(
+ ...microsoftMailboxHandlers(
+ folderStore,
+ messages,
+ removedMessageIdsByFolderId,
+ { unfetchableMessageIds: [messageId] },
+ ),
+ ),
failSubscriptionRenewal: () =>
httpMock.use(
...microsoftWebhookSubscriptionHandlers(subscriptionStore, {