Skip to content

The open alternative to Salesforce, designed for AI.

[messaging] add better logs to messaging sync jobs (#4245)

Weiko8a669cc540ac
diff --git a/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts
index 5137d4bea1a..c20fd952992 100644
--- a/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts
@@ -60,7 +60,9 @@ export class ConnectedAccountService {
);
if (!connectedAccounts || connectedAccounts.length === 0) {
- throw new NotFoundException('No connected account found');
+ throw new NotFoundException(
+ `No connected account found for id ${connectedAccountId} in workspace ${workspaceId}`,
+ );
}
return connectedAccounts[0];
diff --git a/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts
index 60e635a8b91..1ca85c5b158 100644
--- a/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts
@@ -38,7 +38,9 @@ export class MessageChannelService {
);
if (!messageChannels || messageChannels.length === 0) {
- throw new Error('No message channel found');
+ throw new Error(
+ `No message channel found for connected account ${connectedAccountId} in workspace ${workspaceId}`,
+ );
}
return messageChannels[0];
diff --git a/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts
index ab39b276465..42b67059976 100644
--- a/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts
@@ -44,7 +44,9 @@ export class WorkspaceMemberService {
);
if (!workspaceMembers || workspaceMembers.length === 0) {
- throw new NotFoundException('No workspace member found');
+ throw new NotFoundException(
+ `No workspace member found for user ${userId} in workspace ${workspaceId}`,
+ );
}
return workspaceMembers[0];
diff --git a/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts b/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts
index dd8cace8d0e..fd4d813919a 100644
--- a/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts
@@ -47,7 +47,9 @@ export class GmailFullSyncService {
const workspaceMemberId = connectedAccount.accountOwnerId;
if (!refreshToken) {
- throw new Error('No refresh token found');
+ throw new Error(
+ `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during full-sync`,
+ );
}
const gmailMessageChannel =
@@ -149,33 +151,36 @@ export class GmailFullSyncService {
}ms.`,
);
- if (messagesToSave.length === 0) {
- if (errors.length) throw new Error('Error fetching messages');
-
+ if (messagesToSave.length > 0) {
+ this.saveMessagesAndCreateContactsService.saveMessagesAndCreateContacts(
+ messagesToSave,
+ connectedAccount,
+ workspaceId,
+ gmailMessageChannelId,
+ 'gmail full-sync',
+ );
+ } else {
this.logger.log(
`gmail full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`,
);
-
- return;
}
- this.saveMessagesAndCreateContactsService.saveMessagesAndCreateContacts(
- messagesToSave,
- connectedAccount,
- workspaceId,
- gmailMessageChannelId,
- 'gmail full-sync',
- );
-
- if (errors.length) throw new Error('Error fetching messages');
-
+ if (errors.length) {
+ throw new Error(
+ `Error fetching messages for ${connectedAccountId} in workspace ${workspaceId} during full-sync`,
+ );
+ }
const lastModifiedMessageId = messagesToFetch[0];
const historyId = messagesToSave.find(
(message) => message.externalId === lastModifiedMessageId,
)?.historyId;
- if (!historyId) throw new Error('No history id found');
+ if (!historyId) {
+ throw new Error(
+ `No historyId found for ${connectedAccountId} in workspace ${workspaceId} during full-sync`,
+ );
+ }
startTime = Date.now();
diff --git a/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts b/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts
index fbd03010dd5..889346d4a7c 100644
--- a/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts
@@ -61,7 +61,9 @@ export class GmailPartialSyncService {
const refreshToken = connectedAccount.refreshToken;
if (!refreshToken) {
- throw new Error('No refresh token found');
+ throw new Error(
+ `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during partial-sync`,
+ );
}
let startTime = Date.now();
@@ -96,7 +98,9 @@ export class GmailPartialSyncService {
}
if (!historyId) {
- throw new Error('No history id found');
+ throw new Error(
+ `No historyId found for ${connectedAccountId} in workspace ${workspaceId} during partial-sync`,
+ );
}
if (historyId === lastSyncHistoryId || !history?.length) {
@@ -173,8 +177,11 @@ export class GmailPartialSyncService {
);
}
- if (errors.length) throw new Error('Error fetching messages');
-
+ if (errors.length) {
+ throw new Error(
+ `Error fetching messages for ${connectedAccountId} in workspace ${workspaceId} during partial-sync`,
+ );
+ }
startTime = Date.now();
await this.connectedAccountService.updateLastSyncHistoryId(
diff --git a/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts b/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts
index eeb25de9ae6..7a01b663bfe 100644
--- a/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts
@@ -24,7 +24,9 @@ export class GmailRefreshAccessTokenService {
const refreshToken = connectedAccount.refreshToken;
if (!refreshToken) {
- throw new Error('No refresh token found');
+ throw new Error(
+ `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId}`,
+ );
}
const accessToken = await this.refreshAccessToken(refreshToken);