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 { try {
const client = await getMicrosoftGraphClient(mailCredentialId); 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}`, { const response = await client.get(`/me/messages/${messageId}`, {
params: { 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', { logger.debug('Fetched email from Graph API', {
messageId, messageId,
hasAttachments: message.hasAttachments, hasAttachments: message.hasAttachments,
attachmentsInResponse: !!message.attachments,
attachmentsCount: message.attachments?.length || 0,
mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12), mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12),
}); });
// If email has attachments, fetch them separately // If email has attachments but they weren't included in the response, fetch them separately
// Microsoft Graph API sometimes doesn't include attachments in the initial response if (message.hasAttachments && (!message.attachments || message.attachments.length === 0)) {
if (message.hasAttachments) {
try { try {
logger.debug('Fetching attachments list from Graph API', { logger.debug('Fetching attachments list from Graph API', {
messageId, messageId,
mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12), mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12),
}); });
// Fetch attachments separately // Fetch attachments separately - don't use $select as it may cause issues
const attachmentsResponse = await client.get(`/me/messages/${messageId}/attachments`, { // Microsoft Graph API will return all attachment properties by default
params: { const attachmentsResponse = await client.get(`/me/messages/${messageId}/attachments`);
'$select': 'id,name,contentType,size,contentBytes,isInline',
},
});
const attachments = attachmentsResponse.data.value || []; const attachments = attachmentsResponse.data.value || [];
@ -279,6 +279,9 @@ export async function fetchGraphEmail(
messageId, messageId,
error: attachmentsError instanceof Error ? attachmentsError.message : String(attachmentsError), error: attachmentsError instanceof Error ? attachmentsError.message : String(attachmentsError),
status: attachmentsError.response?.status, status: attachmentsError.response?.status,
statusText: attachmentsError.response?.statusText,
responseData: attachmentsError.response?.data,
url: `/me/messages/${messageId}/attachments`,
}); });
// Continue without attachments if fetch fails // Continue without attachments if fetch fails
message.attachments = []; message.attachments = [];