Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 0d7564ada6 IMAP sync broken after entity migration to core schema
https://sonarly.com/issue/23367?type=bug

IMAP email sync fails because commit d3f0162cf5 removed the connected account feature flag, forcing all reads from the core schema where some IMAP accounts were never migrated due to unresolvable accountOwnerId→userWorkspaceId mappings in the 1.20 upgrade command.

Fix: Added null guards for `messageChannel.connectedAccount` at two critical points in the IMAP sync pipeline:

1. **`messaging-message-list-fetch.job.ts`** (line 83-92): After loading the message channel with its `connectedAccount` relation, check if the connected account exists before proceeding. If missing, track the error via the monitoring service and return early. This prevents the crash at line 110 where `messageChannel.connectedAccount.id` would throw on null access.

2. **`messaging-message-list-fetch.service.ts`** (line 100-107): Same guard after re-fetching the fresh message channel following pending action processing. Without this, `freshMessageChannel.connectedAccount` would crash when passed to `validateAndRefreshConnectedAccountAuthentication`.

Both guards follow the exact same pattern as the existing `!messageChannel` null check: track via monitoring service, return early. This ensures the error is observable (operators can query for `connected_account_not_found` events) while preventing the crash that blocks the entire messaging queue worker.

**Important**: This fix prevents the crash but does NOT restore sync for affected accounts. IMAP connected accounts that were skipped during the 1.20 data migration (because their `accountOwnerId` couldn't be resolved to a `userWorkspaceId`) still need to be manually migrated to the core schema. This requires running a data backfill operation — either re-running the upgrade command with fixes for the owner resolution, or manually inserting the missing rows into `core.connectedAccount`.
2026-04-09 12:33:26 +00:00
2 changed files with 19 additions and 0 deletions
@@ -80,6 +80,17 @@ export class MessagingMessageListFetchJob {
return;
}
if (!messageChannel.connectedAccount) {
await this.messagingMonitoringService.track({
eventName:
'message_list_fetch_job.error.connected_account_not_found',
messageChannelId,
workspaceId,
});
return;
}
if (
messageChannel.syncStage !==
MessageChannelSyncStage.MESSAGE_LIST_FETCH_SCHEDULED
@@ -100,6 +100,14 @@ export class MessagingMessageListFetchService {
return;
}
if (!isDefined(freshMessageChannel.connectedAccount)) {
this.logger.error(
`WorkspaceId: ${workspaceId}, MessageChannelId: ${messageChannel.id} - Connected account not found for message channel`,
);
return;
}
const { accessToken, refreshToken } =
await this.messagingAccountAuthenticationService.validateAndRefreshConnectedAccountAuthentication(
{