feat: Load environment variables from .env.local and update session processing logic

This commit is contained in:
Max Kowalski
2025-06-26 10:57:05 +02:00
parent 1afe15df85
commit 944431fea3
2 changed files with 89 additions and 5 deletions

View File

@ -2,6 +2,31 @@
import cron from "node-cron"; import cron from "node-cron";
import { PrismaClient } from "@prisma/client"; import { PrismaClient } from "@prisma/client";
import fetch from "node-fetch"; import fetch from "node-fetch";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
// Load environment variables from .env.local
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const envPath = join(__dirname, '..', '.env.local');
try {
const envFile = readFileSync(envPath, 'utf8');
const envVars = envFile.split('\n').filter(line => line.trim() && !line.startsWith('#'));
envVars.forEach(line => {
const [key, ...valueParts] = line.split('=');
if (key && valueParts.length > 0) {
const value = valueParts.join('=').trim();
if (!process.env[key.trim()]) {
process.env[key.trim()] = value;
}
}
});
} catch (error) {
// Silently fail if .env.local doesn't exist
}
const prisma = new PrismaClient(); const prisma = new PrismaClient();
const OPENAI_API_KEY = process.env.OPENAI_API_KEY; const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
@ -202,7 +227,13 @@ export async function processUnprocessedSessions() {
const sessionsToProcess = await prisma.session.findMany({ const sessionsToProcess = await prisma.session.findMany({
where: { where: {
AND: [ AND: [
{ processed: { not: true } }, // Either false or null { messages: { some: {} } }, // Must have messages
{
OR: [
{ processed: false },
{ processed: null }
]
}
], ],
}, },
include: { include: {

View File

@ -3,6 +3,33 @@ import { fetchAndStoreSessionsForAllCompanies } from "../lib/csvFetcher.js";
import { processAllUnparsedTranscripts } from "../lib/transcriptParser.js"; import { processAllUnparsedTranscripts } from "../lib/transcriptParser.js";
import { PrismaClient } from "@prisma/client"; import { PrismaClient } from "@prisma/client";
import fetch from "node-fetch"; import fetch from "node-fetch";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
// Load environment variables from .env.local
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const envPath = join(__dirname, '..', '.env.local');
try {
const envFile = readFileSync(envPath, 'utf8');
const envVars = envFile.split('\n').filter(line => line.trim() && !line.startsWith('#'));
envVars.forEach(line => {
const [key, ...valueParts] = line.split('=');
if (key && valueParts.length > 0) {
const value = valueParts.join('=').trim();
if (!process.env[key.trim()]) {
process.env[key.trim()] = value;
}
}
});
console.log("✅ Environment variables loaded from .env.local");
} catch (error) {
console.warn("⚠️ Could not load .env.local file:", error.message);
}
const prisma = new PrismaClient(); const prisma = new PrismaClient();
@ -37,7 +64,12 @@ async function triggerProcessingScheduler() {
where: { where: {
AND: [ AND: [
{ messages: { some: {} } }, { messages: { some: {} } },
{ processed: { not: true } }, // Either false or null {
OR: [
{ processed: false },
{ processed: null }
]
}
], ],
}, },
select: { select: {
@ -96,7 +128,12 @@ async function showProcessingStatus() {
where: { processed: true }, where: { processed: true },
}); });
const unprocessedSessions = await prisma.session.count({ const unprocessedSessions = await prisma.session.count({
where: { processed: { not: true } }, where: {
OR: [
{ processed: false },
{ processed: null }
]
},
}); });
const withMessages = await prisma.session.count({ const withMessages = await prisma.session.count({
where: { where: {
@ -107,7 +144,15 @@ async function showProcessingStatus() {
}); });
const readyForProcessing = await prisma.session.count({ const readyForProcessing = await prisma.session.count({
where: { where: {
AND: [{ messages: { some: {} } }, { processed: { not: true } }], AND: [
{ messages: { some: {} } },
{
OR: [
{ processed: false },
{ processed: null }
]
}
],
}, },
}); });
@ -122,7 +167,15 @@ async function showProcessingStatus() {
console.log("\n📋 Sample unprocessed sessions:"); console.log("\n📋 Sample unprocessed sessions:");
const samples = await prisma.session.findMany({ const samples = await prisma.session.findMany({
where: { where: {
AND: [{ messages: { some: {} } }, { processed: { not: true } }], AND: [
{ messages: { some: {} } },
{
OR: [
{ processed: false },
{ processed: null }
]
}
],
}, },
select: { select: {
id: true, id: true,