mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 12:32:10 +01:00
feat: Load environment variables from .env.local and update session processing logic
This commit is contained in:
@ -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: {
|
||||||
|
|||||||
@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user