attachments courrier
This commit is contained in:
parent
3dd9193c20
commit
807200d9e4
@ -210,11 +210,66 @@ export async function fetchGraphEmail(
|
|||||||
hasAttachments: message.hasAttachments,
|
hasAttachments: message.hasAttachments,
|
||||||
attachmentsInResponse: !!message.attachments,
|
attachmentsInResponse: !!message.attachments,
|
||||||
attachmentsCount: message.attachments?.length || 0,
|
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),
|
mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12),
|
||||||
});
|
});
|
||||||
|
|
||||||
// If email has attachments but they weren't included in the response, fetch them separately
|
// Handle case where $expand returns attachments in a different structure
|
||||||
if (message.hasAttachments && (!message.attachments || message.attachments.length === 0)) {
|
// 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 {
|
try {
|
||||||
logger.debug('Fetching attachments list from Graph API', {
|
logger.debug('Fetching attachments list from Graph API', {
|
||||||
messageId,
|
messageId,
|
||||||
@ -286,6 +341,7 @@ export async function fetchGraphEmail(
|
|||||||
// Continue without attachments if fetch fails
|
// Continue without attachments if fetch fails
|
||||||
message.attachments = [];
|
message.attachments = [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
message.attachments = [];
|
message.attachments = [];
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user