diff --git a/server/plugins/eoi-reminders.ts b/server/plugins/eoi-reminders.ts index 1b000c1..3dd11eb 100644 --- a/server/plugins/eoi-reminders.ts +++ b/server/plugins/eoi-reminders.ts @@ -2,18 +2,28 @@ import { scheduleEOIReminders } from '~/server/tasks/eoi-reminders'; import { scheduleEmailProcessing } from '~/server/tasks/process-sales-emails'; export default defineNitroPlugin((nitroApp) => { + console.log('[Plugin] Initializing automated tasks...'); + // Schedule EOI reminders when server starts console.log('[Plugin] Initializing EOI reminder scheduler...'); // Add a small delay to ensure all services are ready - setTimeout(() => { - scheduleEOIReminders(); + setTimeout(async () => { + try { + await scheduleEOIReminders(); + } catch (error) { + console.error('[Plugin] Failed to schedule EOI reminders:', error); + } }, 5000); // Schedule email processing for EOI attachments console.log('[Plugin] Initializing email processing scheduler...'); - setTimeout(() => { - scheduleEmailProcessing(); + setTimeout(async () => { + try { + await scheduleEmailProcessing(); + } catch (error) { + console.error('[Plugin] Failed to schedule email processing:', error); + } }, 7000); }); diff --git a/server/tasks/eoi-reminders.ts b/server/tasks/eoi-reminders.ts index 4aa1283..817d83a 100644 --- a/server/tasks/eoi-reminders.ts +++ b/server/tasks/eoi-reminders.ts @@ -1,11 +1,10 @@ -import cron from 'node-cron'; import { getInterests } from '~/server/utils/nocodb'; import { checkDocumentSignatureStatus } from '~/server/utils/documeso'; // Track if tasks are already scheduled let tasksScheduled = false; -export function scheduleEOIReminders() { +export async function scheduleEOIReminders() { if (tasksScheduled) { console.log('[EOI Reminders] Tasks already scheduled'); return; @@ -13,6 +12,9 @@ export function scheduleEOIReminders() { console.log('[EOI Reminders] Scheduling reminder tasks...'); + // Dynamic import for node-cron to avoid ESM issues + const cron = await import('node-cron'); + // Schedule for 9am daily cron.schedule('0 9 * * *', async () => { console.log('[EOI Reminders] Running 9am reminder check...'); diff --git a/server/utils/email-utils.ts b/server/utils/email-utils.ts index 2b21ac2..27d3c46 100644 --- a/server/utils/email-utils.ts +++ b/server/utils/email-utils.ts @@ -1,6 +1,6 @@ import { simpleParser } from 'mailparser'; import type { ParsedMail } from 'mailparser'; -import Imap from 'imap'; +const Imap = require('imap'); export type { ParsedMail }; @@ -16,7 +16,7 @@ export async function parseEmail(emailContent: string): Promise { return await simpleParser(emailContent); } -export function getIMAPConnection(credentials: EmailCredentials): Promise { +export function getIMAPConnection(credentials: EmailCredentials): Promise { return new Promise((resolve, reject) => { const imap = new Imap({ user: credentials.user, @@ -41,7 +41,7 @@ export function getIMAPConnection(credentials: EmailCredentials): Promise }); } -export function searchEmails(imap: Imap, criteria: any[]): Promise { +export function searchEmails(imap: any, criteria: any[]): Promise { return new Promise((resolve, reject) => { imap.search(criteria, (err: Error | null, results: number[]) => { if (err) reject(err); @@ -50,7 +50,7 @@ export function searchEmails(imap: Imap, criteria: any[]): Promise { }); } -export function fetchEmail(imap: Imap, msgId: number, options: any): Promise { +export function fetchEmail(imap: any, msgId: number, options: any): Promise { return new Promise((resolve, reject) => { let emailData = '';