From 7f5a8f8d22ba0e7390684a81099baaf1d6103fd7 Mon Sep 17 00:00:00 2001 From: Timeo Williams Date: Mon, 18 Nov 2024 09:23:59 -0500 Subject: [PATCH] chore: revised logic --- src/main.ts | 58 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9a02b7f..39837c6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -462,8 +462,7 @@ schedule.scheduleJob('0 0 12 * * *', () => { log.info('Scheduled daily reset at 12 PM') stopActivityMonitoring() checkAndSendMissedEmails() - resetCounters('daily') - // startActivityMonitoring() + startActivityMonitoring() log.info('new reset date is ', store.get('lastResetDate')) }) @@ -650,7 +649,7 @@ export async function handleDailyReset() { } } -async function sendDailyEmail() { +async function sendDailyEmail(): Promise { const now = dayjs() log.info('currentSiteTimeTrackers:', currentSiteTimeTrackers) @@ -684,30 +683,47 @@ async function sendDailyEmail() { body: JSON.stringify(dailyData) }) console.log('Email sent status:', response.status) + if (response.status === 200) { + return true // Indicate success + } else { + return false // Indicate failure + } } catch (error) { console.error('Error sending email:', error) + return false } } async function checkAndSendMissedEmails(): Promise { - const lastEmailDate = dayjs(store.get('lastEmailDate', null) || dayjs().subtract(1, 'day')) - const today = dayjs().startOf('day') - log.info('checking and sending missed emails') - log.info('lastEmailDate', lastEmailDate.format('YYYY-MM-DD'), 'today', today.format('YYYY-MM-DD')) - new Notification({ - title: 'Deep Focus', - body: 'Checking for missed emails...' - }).show() - if (!lastEmailDate.isSame(today, 'day')) { - let dateToProcess = lastEmailDate.add(1, 'day') - - while (dateToProcess.isBefore(today) || dateToProcess.isSame(today, 'day')) { - const formattedDate = dateToProcess.format('YYYY-MM-DD') - console.log(`Sending missed email for date: ${formattedDate}`) - await sendDailyEmail() - // Update the last email date after each successful send - store.set('lastEmailDate', dateToProcess.toISOString()) - dateToProcess = dateToProcess.add(1, 'day') + try { + const lastEmailDate = dayjs(store.get('lastEmailDate', null) || dayjs().subtract(1, 'day')) + const today = dayjs().startOf('day') + log.info('checking and sending missed emails') + log.info( + 'lastEmailDate', + lastEmailDate.format('YYYY-MM-DD'), + 'today', + today.format('YYYY-MM-DD') + ) + new Notification({ + title: 'Deep Focus', + body: 'Checking for missed emails...' + }).show() + if (!lastEmailDate.isSame(today, 'day')) { + let dateToProcess = lastEmailDate.add(1, 'day') + + while (dateToProcess.isBefore(today) || dateToProcess.isSame(today, 'day')) { + const formattedDate = dateToProcess.format('YYYY-MM-DD') + log.info(`Sending missed email for date: ${formattedDate}`) + const sendEmailResponse = await sendDailyEmail() + if (sendEmailResponse) { + // Update the last email date after each successful send + store.set('lastEmailDate', dateToProcess.toISOString()) + dateToProcess = dateToProcess.add(1, 'day') + } + } } + } catch (error) { + console.error('Error checking and sending missed emails:', error) } }