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 { PrismaClient } from "@prisma/client";
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 OPENAI_API_KEY = process.env.OPENAI_API_KEY;
@ -202,7 +227,13 @@ export async function processUnprocessedSessions() {
const sessionsToProcess = await prisma.session.findMany({
where: {
AND: [
{ processed: { not: true } }, // Either false or null
{ messages: { some: {} } }, // Must have messages
{
OR: [
{ processed: false },
{ processed: null }
]
}
],
},
include: {