diff --git a/components/EOISection.vue b/components/EOISection.vue index 225ec92..dd1ff06 100644 --- a/components/EOISection.vue +++ b/components/EOISection.vue @@ -193,14 +193,27 @@ const copyLink = async (link: string | undefined) => { }; const formatDate = (dateString: string) => { - const date = new Date(dateString); - return date.toLocaleString('en-US', { - month: 'short', - day: 'numeric', - year: 'numeric', - hour: '2-digit', - minute: '2-digit' - }); + if (!dateString) return ''; + + try { + const date = new Date(dateString); + // Check if date is valid + if (isNaN(date.getTime())) { + return dateString; // Return original if parsing fails + } + + return date.toLocaleString('en-GB', { + day: '2-digit', + month: '2-digit', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + hour12: false + }); + } catch (error) { + console.error('Date formatting error:', error); + return dateString; + } }; const getStatusColor = (status: string) => { diff --git a/server/api/email/fetch-thread.ts b/server/api/email/fetch-thread.ts index f1f714c..ff0e866 100644 --- a/server/api/email/fetch-thread.ts +++ b/server/api/email/fetch-thread.ts @@ -210,14 +210,9 @@ async function fetchImapEmails( return; } - const searchCriteria = [ - 'OR', - ['FROM', clientEmail], - ['TO', clientEmail], - ['CC', clientEmail] - ]; - - imap.search(searchCriteria, (err, results) => { + // Use ALL to get all messages, then filter manually + // This avoids the complex search criteria issues + imap.search(['ALL'], (err, results) => { if (err) { console.error(`Search error in ${folderName}:`, err); searchNextFolder(); @@ -252,17 +247,38 @@ async function fetchImapEmails( return; } + // Check if this email involves the client + const fromEmail = parsed.from?.text || ''; + const toEmails = Array.isArray(parsed.to) + ? parsed.to.map((addr: any) => addr.text).join(', ') + : parsed.to?.text || ''; + const ccEmails = Array.isArray(parsed.cc) + ? parsed.cc.map((addr: any) => addr.text).join(', ') + : parsed.cc?.text || ''; + + // Filter to only include emails to/from the client + const involvesClient = + fromEmail.toLowerCase().includes(clientEmail.toLowerCase()) || + toEmails.toLowerCase().includes(clientEmail.toLowerCase()) || + ccEmails.toLowerCase().includes(clientEmail.toLowerCase()); + + if (!involvesClient) { + messagesProcessed++; + if (messagesProcessed === messagesToFetch.length) { + searchNextFolder(); + } + return; + } + const email: EmailMessage = { id: parsed.messageId || `${Date.now()}-${seqno}`, - from: parsed.from?.text || '', - to: Array.isArray(parsed.to) - ? parsed.to.map((addr: any) => addr.text).join(', ') - : parsed.to?.text || '', + from: fromEmail, + to: toEmails, subject: parsed.subject || '', body: parsed.text || '', html: parsed.html || undefined, timestamp: parsed.date?.toISOString() || new Date().toISOString(), - direction: parsed.from?.text.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received' + direction: fromEmail.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received' }; if (parsed.headers.has('in-reply-to')) {