attachments courrier

This commit is contained in:
alma 2026-01-18 14:53:59 +01:00
parent 3dd9193c20
commit 807200d9e4

View File

@ -210,11 +210,66 @@ export async function fetchGraphEmail(
hasAttachments: message.hasAttachments,
attachmentsInResponse: !!message.attachments,
attachmentsCount: message.attachments?.length || 0,
attachmentsType: typeof message.attachments,
attachmentsIsArray: Array.isArray(message.attachments),
attachmentsKeys: message.attachments ? Object.keys(message.attachments) : [],
mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12),
});
// If email has attachments but they weren't included in the response, fetch them separately
if (message.hasAttachments && (!message.attachments || message.attachments.length === 0)) {
// Handle case where $expand returns attachments in a different structure
// Sometimes attachments might be in message.attachments.value instead of message.attachments
if (message.attachments && !Array.isArray(message.attachments) && message.attachments.value) {
logger.debug('Attachments found in .value property', {
messageId,
count: message.attachments.value?.length || 0,
});
message.attachments = message.attachments.value;
}
// Process attachments - either from initial response or fetch separately
if (message.hasAttachments) {
// If attachments are already in the response, process them
if (message.attachments && Array.isArray(message.attachments) && message.attachments.length > 0) {
logger.debug('Processing attachments from initial response', {
messageId,
attachmentsCount: message.attachments.length,
});
// Process attachments - fetch content if not included
const attachmentsWithContent = await Promise.all(
message.attachments.map(async (attachment: any) => {
// If contentBytes is missing, fetch the attachment content
if (!attachment.contentBytes && attachment.id) {
try {
logger.debug('Fetching attachment content from Graph API', {
messageId,
attachmentId: attachment.id,
attachmentName: attachment.name,
});
const attachmentData = await fetchGraphAttachment(mailCredentialId, messageId, attachment.id);
return {
...attachment,
contentBytes: attachmentData.contentBytes,
};
} catch (error) {
logger.error('Error fetching attachment content', {
messageId,
attachmentId: attachment.id,
error: error instanceof Error ? error.message : String(error),
});
// Return attachment without content if fetch fails
return attachment;
}
}
// Already has contentBytes, return as-is
return attachment;
})
);
message.attachments = attachmentsWithContent;
} else {
// Attachments not in initial response, fetch them separately
try {
logger.debug('Fetching attachments list from Graph API', {
messageId,
@ -286,6 +341,7 @@ export async function fetchGraphEmail(
// Continue without attachments if fetch fails
message.attachments = [];
}
}
} else {
message.attachments = [];
}