fixes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Imap from 'imap';
|
||||
import { simpleParser } from 'mailparser';
|
||||
import { getCredentialsFromSession, decryptCredentials } from '~/server/utils/encryption';
|
||||
import { listFiles, getFileStats } from '~/server/utils/minio';
|
||||
import { listFiles, getFileStats, getMinioClient, uploadFile } from '~/server/utils/minio';
|
||||
|
||||
interface EmailMessage {
|
||||
id: string;
|
||||
@@ -72,12 +72,25 @@ export default defineEventHandler(async (event) => {
|
||||
for (const file of files) {
|
||||
if (file.name.endsWith('.json') && !file.isFolder) {
|
||||
try {
|
||||
// Use the getDownloadUrl function to get a proper presigned URL
|
||||
const { getDownloadUrl } = await import('~/server/utils/minio');
|
||||
const downloadUrl = await getDownloadUrl(file.name);
|
||||
// Read file directly on server using MinIO client (works with private buckets)
|
||||
const client = getMinioClient();
|
||||
const bucketName = useRuntimeConfig().minio.bucketName;
|
||||
|
||||
const response = await fetch(downloadUrl);
|
||||
const emailData = await response.json();
|
||||
// Get object as stream
|
||||
const stream = await client.getObject(bucketName, file.name);
|
||||
|
||||
// Convert stream to string
|
||||
let data = '';
|
||||
stream.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
stream.on('end', () => resolve(data));
|
||||
stream.on('error', reject);
|
||||
});
|
||||
|
||||
const emailData = JSON.parse(data);
|
||||
cachedEmails.push(emailData);
|
||||
} catch (err) {
|
||||
console.error('Failed to read cached email:', file.name, err);
|
||||
@@ -103,15 +116,15 @@ export default defineEventHandler(async (event) => {
|
||||
authTimeout: 5000 // 5 seconds auth timeout
|
||||
};
|
||||
|
||||
// Fetch emails from IMAP with timeout
|
||||
// Fetch emails from IMAP with timeout (increased to 30 seconds)
|
||||
let imapEmails: EmailMessage[] = [];
|
||||
const timeoutPromise = new Promise<EmailMessage[]>((_, reject) =>
|
||||
setTimeout(() => reject(new Error('IMAP connection timeout')), 15000)
|
||||
setTimeout(() => reject(new Error('IMAP connection timeout')), 30000)
|
||||
);
|
||||
|
||||
try {
|
||||
imapEmails = await Promise.race([
|
||||
fetchImapEmails(imapConfig, userEmail, clientEmail, limit),
|
||||
fetchImapEmails(imapConfig, userEmail, clientEmail, limit, interestId),
|
||||
timeoutPromise
|
||||
]);
|
||||
} catch (imapError) {
|
||||
@@ -159,7 +172,8 @@ async function fetchImapEmails(
|
||||
imapConfig: any,
|
||||
userEmail: string,
|
||||
clientEmail: string,
|
||||
limit: number
|
||||
limit: number,
|
||||
interestId?: string
|
||||
): Promise<EmailMessage[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const emails: EmailMessage[] = [];
|
||||
@@ -210,9 +224,16 @@ async function fetchImapEmails(
|
||||
return;
|
||||
}
|
||||
|
||||
// Use ALL to get all messages, then filter manually
|
||||
// This avoids the complex search criteria issues
|
||||
imap.search(['ALL'], (err, results) => {
|
||||
// Use date-based search to reduce the number of emails fetched
|
||||
// Search for emails from the last 30 days
|
||||
const thirtyDaysAgo = new Date();
|
||||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
|
||||
|
||||
// Format date for IMAP (e.g., "1-Jan-2024")
|
||||
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
const searchDate = `${thirtyDaysAgo.getDate()}-${months[thirtyDaysAgo.getMonth()]}-${thirtyDaysAgo.getFullYear()}`;
|
||||
|
||||
imap.search(['SINCE', searchDate], (err, results) => {
|
||||
if (err) {
|
||||
console.error(`Search error in ${folderName}:`, err);
|
||||
searchNextFolder();
|
||||
@@ -286,6 +307,27 @@ async function fetchImapEmails(
|
||||
}
|
||||
|
||||
allEmails.push(email);
|
||||
|
||||
// Cache this email if we have an interestId
|
||||
if (interestId && involvesClient) {
|
||||
try {
|
||||
const emailData = {
|
||||
...email,
|
||||
interestId: interestId
|
||||
};
|
||||
|
||||
const objectName = `client-emails/interest-${interestId}/${Date.now()}-${email.direction}.json`;
|
||||
const buffer = Buffer.from(JSON.stringify(emailData, null, 2));
|
||||
|
||||
// Fire and forget - don't wait for upload
|
||||
uploadFile(objectName, buffer, 'application/json').catch(err => {
|
||||
console.error('Failed to cache email:', err);
|
||||
});
|
||||
} catch (cacheError) {
|
||||
console.error('Failed to cache email:', cacheError);
|
||||
}
|
||||
}
|
||||
|
||||
messagesProcessed++;
|
||||
|
||||
if (messagesProcessed === messagesToFetch.length) {
|
||||
|
||||
Reference in New Issue
Block a user