Skip to content

Commit

Permalink
build: add RESEND API key to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
timeowilliams committed Sep 21, 2024
1 parent 1995bdc commit c8d65a3
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 155 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ jobs:
env:
MONGODB_URI: ${{ secrets.MONGODB_URI }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
RESEND_API_KEY: ${{secrets.RESEND_API_KEY}}
run: npm run test:integration
working-directory: ./backend
2 changes: 1 addition & 1 deletion .github/workflows/release-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
body: |
Please refer to [CHANGELOG.md](https://github.com/timeowilliams/deepWork/blob/${{ github.ref_name }}/packages/${{ steps.tag.outputs.pkgName }}/CHANGELOG.md) for details.
Please refer to [CHANGELOG.md](https://github.com/Tech-Nest-Ventures/deepFocus/blob/${{ github.ref_name }}/packages/${{ steps.tag.outputs.pkgName }}/CHANGELOG.md) for details.
21 changes: 13 additions & 8 deletions electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path';
import { defineConfig, externalizeDepsPlugin, bytecodePlugin } from 'electron-vite';
import solid from 'vite-plugin-solid';
import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, bytecodePlugin } from 'electron-vite'
import solid from 'vite-plugin-solid'

export default defineConfig({
main: {
Expand All @@ -10,19 +10,24 @@ export default defineConfig({
rollupOptions: {
input: {
main: resolve(__dirname, 'src/main/index.ts'),
worker: resolve(__dirname, 'src/main/schedulerWorker.js')
worker: resolve(__dirname, 'src/main/schedulerWorker.js') // Correct path to worker file
},
output: {
format: 'es',
entryFileNames: '[name].js',
}
entryFileNames: '[name].js'
},
// Externalize unnecessary dependencies from the worker
external: ['electron', 'path', 'fs', 'dotenv', '@electron-toolkit/utils', 'electron-store']
}
}
},
preload: {
plugins: [externalizeDepsPlugin(), bytecodePlugin()],
build: {
outDir: 'out/preload'
outDir: 'out/preload',
rollupOptions: {
external: ['electron']
}
}
},
renderer: {
Expand All @@ -36,4 +41,4 @@ export default defineConfig({
outDir: 'out/renderer'
}
}
});
})
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"tailwindcss": "^3.4.12",
"ts-node": "^10.9.2",
"tsx": "^4.19.1",
"typescript": "^5.5.2",
"typescript": "^5.6.2",
"valibot": "^0.42.0",
"vinxi": "^0.4.3",
"vite": "^5.3.1",
Expand Down
99 changes: 49 additions & 50 deletions src/main/emailService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from 'axios';
import * as schedule from 'node-schedule';
import { TypedStore } from './types';
import { app } from 'electron';
import path from 'path';
import axios from 'axios'
import * as schedule from 'node-schedule'
import { TypedStore } from './types'
import { app } from 'electron'
import path from 'path'
import fs from 'fs'

interface TopSite {
Expand All @@ -11,79 +11,78 @@ interface TopSite {
}

export class EmailService {
private userEmail: string;
private store: TypedStore;
private userEmail: string
private store: TypedStore

constructor(userEmail: string, store: TypedStore) {
this.userEmail = userEmail || '';
this.store = store;
this.userEmail = userEmail || ''
this.store = store
}

public scheduleEmailSend(): void {
// Schedule the email to be sent every 4 hours
schedule.scheduleJob('0 */4 * * *', () => {
this.sendDailySummary();
});
this.sendDailySummary()
})

// Schedule the email to be sent at the end of each day (11:59 PM)
schedule.scheduleJob('59 23 * * *', () => {
this.sendDailySummary();
});
this.sendDailySummary()
})
}

private async sendDailySummary(): Promise<void> {
console.log('Sending daily summary...');
console.log('Sending daily summary...')

const deepWorkHours = await this.getDeepWorkHours();
const topSites = await this.getTopSites();
const deepWorkHours = await this.getDeepWorkHours()
const topSites = await this.getTopSites()

if (topSites.length === 0) {
console.log('Not enough data to send email');
return;
console.log('Not enough data to send email')
return
}

const emailBody = this.composeEmailBody(deepWorkHours, topSites);
const emailBody = this.composeEmailBody(deepWorkHours, topSites)

try {
const response = await axios.post('http://localhost:5000/api/v1/emails/send-email', {
emailBody,
userEmail: this.userEmail,
});
userEmail: this.userEmail
})

console.log('Email sent successfully:', response.data);
console.log('Email sent successfully:', response.data)
} catch (error) {
console.error('Error sending email to backend:', error);
console.error('Error sending email to backend:', error)
}
}

public async testEmailSend(): Promise<void> {
console.log('Testing email send...');
console.log('Testing email send...')

// Get the app path and resolve the email template file path
const emailTemplatePath = path.join(app.getAppPath(), '/src/main/emailTemplates/welcome.html');
console.log('app.getAppPath() ', app.getAppPath());
console.log('Resolved email template path: ', emailTemplatePath);
let emailBody: string;
const emailTemplatePath = path.join(app.getAppPath(), '/src/main/emailTemplates/welcome.html')
console.log('app.getAppPath() ', app.getAppPath())
console.log('Resolved email template path: ', emailTemplatePath)

let emailBody: string
try {
emailBody = fs.readFileSync(emailTemplatePath, 'utf8');
emailBody = fs.readFileSync(emailTemplatePath, 'utf8')
} catch (err) {
console.error('Error reading email template file:', err);
return;
console.error('Error reading email template file:', err)
return
}

try {
const response = await axios.post('http://localhost:5000/api/v1/emails/send-email', {
emailBody,
userEmail: this.userEmail,
});
console.log('Response from backend email send:', response.data);
emailBody,
userEmail: this.userEmail
})

console.log('Response from backend email send:', response.data)
} catch (error) {
console.error('Error sending email to backend:', error);
console.error('Error sending email to backend:', error)
}
}


public composeEmailBody(deepWorkHours: number, topSites: TopSite[]): string {
return `
Expand All @@ -105,31 +104,31 @@ export class EmailService {
.join('')}
</ul>
</div>
`;
`
}

private async getDeepWorkHours(): Promise<number> {
const siteTimeTrackers = this.store.get('siteTimeTrackers', []);
const unproductiveSites = this.store.get('unproductiveSites', []);
const siteTimeTrackers = this.store.get('siteTimeTrackers', [])
const unproductiveSites = this.store.get('unproductiveSites', [])

const productiveTime = siteTimeTrackers
.filter((tracker) => !unproductiveSites?.includes(tracker.url))
.reduce((total, tracker) => total + tracker.timeSpent, 0);
.reduce((total, tracker) => total + tracker.timeSpent, 0)

return parseFloat((productiveTime / (1000 * 60 * 60)).toFixed(1));
return parseFloat((productiveTime / (1000 * 60 * 60)).toFixed(1))
}

private async getTopSites(): Promise<TopSite[]> {
const siteTimeTrackers = this.store.get('siteTimeTrackers', []);
const siteTimeTrackers = this.store.get('siteTimeTrackers', [])

const topSites = siteTimeTrackers
.sort((a, b) => b.timeSpent - a.timeSpent)
.slice(0, 3)
.map((tracker) => ({
url: tracker.url,
timeSpent: Math.round(tracker.timeSpent / (1000 * 60)),
}));
timeSpent: Math.round(tracker.timeSpent / (1000 * 60))
}))

return topSites;
return topSites
}
}
Loading

0 comments on commit c8d65a3

Please sign in to comment.