This commit is contained in:
alma 2026-01-18 14:50:33 +01:00
parent a7153c3b26
commit 3dd9193c20

View File

@ -194,10 +194,12 @@ export async function fetchGraphEmail(
try {
const client = await getMicrosoftGraphClient(mailCredentialId);
// First, get the message without attachments to check if it has attachments
// First, try to get the message with attachments included
// Microsoft Graph API may include attachments in the initial response if we request them
const response = await client.get(`/me/messages/${messageId}`, {
params: {
'$select': 'id,subject,from,toRecipients,ccRecipients,bccRecipients,body,bodyPreview,receivedDateTime,sentDateTime,isRead,hasAttachments,importance,flag',
'$expand': 'attachments',
'$select': 'id,subject,from,toRecipients,ccRecipients,bccRecipients,body,bodyPreview,receivedDateTime,sentDateTime,isRead,hasAttachments,importance,flag,attachments',
},
});
@ -206,24 +208,22 @@ export async function fetchGraphEmail(
logger.debug('Fetched email from Graph API', {
messageId,
hasAttachments: message.hasAttachments,
attachmentsInResponse: !!message.attachments,
attachmentsCount: message.attachments?.length || 0,
mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12),
});
// If email has attachments, fetch them separately
// Microsoft Graph API sometimes doesn't include attachments in the initial response
if (message.hasAttachments) {
// If email has attachments but they weren't included in the response, fetch them separately
if (message.hasAttachments && (!message.attachments || message.attachments.length === 0)) {
try {
logger.debug('Fetching attachments list from Graph API', {
messageId,
mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12),
});
// Fetch attachments separately
const attachmentsResponse = await client.get(`/me/messages/${messageId}/attachments`, {
params: {
'$select': 'id,name,contentType,size,contentBytes,isInline',
},
});
// Fetch attachments separately - don't use $select as it may cause issues
// Microsoft Graph API will return all attachment properties by default
const attachmentsResponse = await client.get(`/me/messages/${messageId}/attachments`);
const attachments = attachmentsResponse.data.value || [];
@ -279,6 +279,9 @@ export async function fetchGraphEmail(
messageId,
error: attachmentsError instanceof Error ? attachmentsError.message : String(attachmentsError),
status: attachmentsError.response?.status,
statusText: attachmentsError.response?.statusText,
responseData: attachmentsError.response?.data,
url: `/me/messages/${messageId}/attachments`,
});
// Continue without attachments if fetch fails
message.attachments = [];