-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
74 lines (68 loc) · 2.22 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import dotenv from "dotenv";
dotenv.config();
import GmailService from "./services/GmailService.js";
import GeminiService from "./services/GeminiService.js";
import NotionService from "./services/NotionService.js";
import inquirer from "inquirer";
import { parseGeminiResponse } from "./utils/helper.js";
import { markEmailAsProcessed } from "./utils/ProcessedEmails.js";
import { markEmailAsFailed } from "./utils/FailedEmails.js";
const Options = {
"Fetch Oldest Unprocessed Emails": 1,
"Fetch Latest Unprocessed Emails": 2,
};
const getChoiceFromUser = async () => {
const { Choice } = await inquirer.prompt([
{
type: "list",
name: "Choice",
message: "Choose an option:",
choices: Object.keys(Options),
// Center the menu using padding
prefix: " ".repeat(process.stdout.columns / 4),
},
]);
return Choice;
};
async function main() {
const gmailService = new GmailService();
await gmailService.initialize();
const geminiService = new GeminiService();
const notionService = new NotionService();
const choice = await getChoiceFromUser();
console.log("Getting Gmail Service");
let emails;
if (choice === "Fetch Oldest Unprocessed Emails") {
console.log("Fetching Oldest Unprocessed Emails");
emails = await gmailService.searchUnprocessedEmails();
} else {
console.log("Fetching Latest Unprocessed Emails");
emails = await gmailService.searchUnprocessedLatestEmails();
}
if (emails.length === 0) {
console.log("No emails to process");
return;
}
console.log("Parsing emails with Gemini");
for (const email of emails) {
const response = await geminiService.parseEmailWithGemini(email);
const data = parseGeminiResponse(response.text());
console.log("******************************************************");
console.log("Company Name:", data.Company_Name);
console.log("Adding to Notion");
const success = await notionService.addToNotion(data);
if (success) {
await markEmailAsProcessed(email);
} else {
await markEmailAsFailed(email);
}
console.log("******************************************************");
}
}
main()
.then(() => {
console.log("Done");
})
.catch((err) => {
console.error(err);
});