This commit is contained in:
Max Kowalski
2025-06-26 22:43:22 +02:00
parent 8774a1f155
commit fd55b30398
30 changed files with 1458 additions and 1859 deletions

50
lib/admin-service.ts Normal file
View File

@ -0,0 +1,50 @@
import { getServerSession } from "next-auth";
import { authOptions } from "../app/api/auth/[...nextauth]/route"; // Adjust path as needed
import { prisma } from "./prisma";
import { processUnprocessedSessions } from "./processingSchedulerNoCron";
export async function getAdminUser() {
const session = await getServerSession(authOptions);
if (!session?.user) {
throw new Error("Not logged in");
}
const user = await prisma.user.findUnique({
where: { email: session.user.email as string },
include: { company: true },
});
if (!user) {
throw new Error("No user found");
}
if (user.role !== "admin") {
throw new Error("Admin access required");
}
return user;
}
export async function triggerSessionProcessing(batchSize?: number, maxConcurrency?: number) {
const unprocessedCount = await prisma.session.count({
where: {
processed: false,
messages: { some: {} }, // Must have messages
},
});
if (unprocessedCount === 0) {
return { message: "No unprocessed sessions found", unprocessedCount: 0, processedCount: 0 };
}
processUnprocessedSessions(batchSize, maxConcurrency)
.then(() => {
console.log(`[Manual Trigger] Processing completed`);
})
.catch((error) => {
console.error(`[Manual Trigger] Processing failed:`, error);
});
return { message: `Started processing ${unprocessedCount} unprocessed sessions`, unprocessedCount };
}

7
lib/auth-service.ts Normal file
View File

@ -0,0 +1,7 @@
import { prisma } from "./prisma";
export async function findUserByEmail(email: string) {
return prisma.user.findUnique({
where: { email },
});
}

332
lib/data-service.ts Normal file
View File

@ -0,0 +1,332 @@
import { prisma } from "./prisma";
// Example: Function to get a user by ID
export async function getUserById(id: string) {
return prisma.user.findUnique({ where: { id } });
}
export async function getCompanyByUserId(userId: string) {
const user = await prisma.user.findUnique({
where: { id: userId },
});
if (!user) return null;
return prisma.company.findUnique({
where: { id: user.companyId },
});
}
export async function updateCompanyCsvUrl(companyId: string, csvUrl: string) {
return prisma.company.update({
where: { id: companyId },
data: { csvUrl },
});
}
export async function findUserByEmailWithCompany(email: string) {
return prisma.user.findUnique({
where: { email },
include: { company: true },
});
}
export async function findSessionsByCompanyIdAndDateRange(companyId: string, startDate?: string, endDate?: string) {
const whereClause: any = {
companyId,
processed: true,
};
if (startDate && endDate) {
whereClause.startTime = {
gte: new Date(startDate),
lte: new Date(endDate + "T23:59:59.999Z"),
};
}
return prisma.session.findMany({
where: whereClause,
include: {
messages: true,
},
});
}
export async function getDistinctSessionCategories(companyId: string) {
const categories = await prisma.session.findMany({
where: {
companyId,
category: {
not: null,
},
},
distinct: ["category"],
select: {
category: true,
},
orderBy: {
category: "asc",
},
});
return categories.map((s) => s.category).filter(Boolean) as string[];
}
export async function getDistinctSessionLanguages(companyId: string) {
const languages = await prisma.session.findMany({
where: {
companyId,
language: {
not: null,
},
},
distinct: ["language"],
select: {
language: true,
},
orderBy: {
language: "asc",
},
});
return languages.map((s) => s.language).filter(Boolean) as string[];
}
export async function getSessionById(id: string) {
return prisma.session.findUnique({
where: { id },
include: {
messages: {
orderBy: { order: "asc" },
},
},
});
}
export async function getFilteredAndPaginatedSessions(
companyId: string,
searchTerm: string | null,
category: string | null,
language: string | null,
startDate: string | null,
endDate: string | null,
sortKey: string | null,
sortOrder: string | null,
page: number,
pageSize: number
) {
const whereClause: Prisma.SessionWhereInput = { companyId };
// Search Term
if (
searchTerm &&
typeof searchTerm === "string" &&
searchTerm.trim() !== ""
) {
const searchConditions = [
{ id: { contains: searchTerm } },
{ category: { contains: searchTerm } },
{ initialMsg: { contains: searchTerm } },
];
whereClause.OR = searchConditions;
}
// Category Filter
if (category && typeof category === "string" && category.trim() !== "") {
whereClause.category = category;
}
// Language Filter
if (language && typeof language === "string" && language.trim() !== "") {
whereClause.language = language;
}
// Date Range Filter
if (startDate && typeof startDate === "string") {
whereClause.startTime = {
...((whereClause.startTime as object) || {}),
gte: new Date(startDate),
};
}
if (endDate && typeof endDate === "string") {
const inclusiveEndDate = new Date(endDate);
inclusiveEndDate.setDate(inclusiveEndDate.getDate() + 1);
whereClause.startTime = {
...((whereClause.startTime as object) || {}),
lt: inclusiveEndDate,
};
}
// Sorting
const validSortKeys: { [key: string]: string } = {
startTime: "startTime",
category: "category",
language: "language",
sentiment: "sentiment",
messagesSent: "messagesSent",
avgResponseTime: "avgResponseTime",
};
let orderByCondition:
| Prisma.SessionOrderByWithRelationInput
| Prisma.SessionOrderByWithRelationInput[];
const primarySortField =
sortKey && typeof sortKey === "string" && validSortKeys[sortKey]
? validSortKeys[sortKey]
: "startTime"; // Default to startTime field if sortKey is invalid/missing
const primarySortOrder =
sortOrder === "asc" || sortOrder === "desc" ? sortOrder : "desc"; // Default to desc order
if (primarySortField === "startTime") {
// If sorting by startTime, it's the only sort criteria
orderByCondition = { [primarySortField]: primarySortOrder };
} else {
// If sorting by another field, use startTime: "desc" as secondary sort
orderByCondition = [
{ [primarySortField]: primarySortOrder },
{ startTime: "desc" },
];
}
return prisma.session.findMany({
where: whereClause,
orderBy: orderByCondition,
skip: (page - 1) * pageSize,
take: pageSize,
});
}
export async function countFilteredSessions(
companyId: string,
searchTerm: string | null,
category: string | null,
language: string | null,
startDate: string | null,
endDate: string | null
) {
const whereClause: Prisma.SessionWhereInput = { companyId };
// Search Term
if (
searchTerm &&
typeof searchTerm === "string" &&
searchTerm.trim() !== ""
) {
const searchConditions = [
{ id: { contains: searchTerm } },
{ category: { contains: searchTerm } },
{ initialMsg: { contains: searchTerm } },
];
whereClause.OR = searchConditions;
}
// Category Filter
if (category && typeof category === "string" && category.trim() !== "") {
whereClause.category = category;
}
// Language Filter
if (language && typeof language === "string" && language.trim() !== "") {
whereClause.language = language;
}
// Date Range Filter
if (startDate && typeof startDate === "string") {
whereClause.startTime = {
...((whereClause.startTime as object) || {}),
gte: new Date(startDate),
};
}
if (endDate && typeof endDate === "string") {
const inclusiveEndDate = new Date(endDate);
inclusiveEndDate.setDate(inclusiveEndDate.getDate() + 1);
whereClause.startTime = {
...((whereClause.startTime as object) || {}),
lt: inclusiveEndDate,
};
}
return prisma.session.count({ where: whereClause });
}
export async function updateCompanySettings(
companyId: string,
data: {
csvUrl?: string;
csvUsername?: string;
csvPassword?: string;
sentimentAlert?: number | null;
}
) {
return prisma.company.update({
where: { id: companyId },
data,
});
}
export async function getUsersByCompanyId(companyId: string) {
return prisma.user.findMany({
where: { companyId },
});
}
export async function userExistsByEmail(email: string) {
return prisma.user.findUnique({ where: { email } });
}
export async function createUser(email: string, passwordHash: string, companyId: string, role: string) {
return prisma.user.create({
data: {
email,
password: passwordHash,
companyId,
role,
},
});
}
export async function updateUserResetToken(email: string, token: string, expiry: Date) {
return prisma.user.update({
where: { email },
data: { resetToken: token, resetTokenExpiry: expiry },
});
}
export async function createCompany(name: string, csvUrl: string) {
return prisma.company.create({
data: { name, csvUrl },
});
}
export async function findUserByResetToken(token: string) {
return prisma.user.findFirst({
where: {
resetToken: token,
resetTokenExpiry: { gte: new Date() },
},
});
}
export async function updateUserPasswordAndResetToken(userId: string, passwordHash: string) {
return prisma.user.update({
where: { id: userId },
data: {
password: passwordHash,
resetToken: null,
resetTokenExpiry: null,
},
});
}
// Add more data fetching functions here as needed
import { Prisma } from "@prisma/client";
export async function getSessionByCompanyId(where: Prisma.SessionWhereInput) {
return prisma.session.findFirst({
orderBy: { createdAt: "desc" },
where,
});
}
export async function getCompanyById(companyId: string) {
return prisma.company.findUnique({ where: { id: companyId } });
}

98
lib/session-service.ts Normal file
View File

@ -0,0 +1,98 @@
import { prisma } from "./prisma";
import { fetchAndParseCsv } from "./csvFetcher";
import { triggerCompleteWorkflow } from "./workflow";
interface SessionCreateData {
id: string;
startTime: Date;
companyId: string;
sessionId?: string;
[key: string]: unknown;
}
export async function processSessions(company: any) {
const sessions = await fetchAndParseCsv(
company.csvUrl,
company.csvUsername as string | undefined,
company.csvPassword as string | undefined
);
for (const session of sessions) {
const sessionData: SessionCreateData = {
...session,
companyId: company.id,
id:
session.id ||
session.sessionId ||
`sess_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
// Ensure startTime is not undefined
startTime: session.startTime || new Date(),
};
// Validate dates to prevent "Invalid Date" errors
const startTime =
sessionData.startTime instanceof Date &&
!isNaN(sessionData.startTime.getTime())
? sessionData.startTime
: new Date();
const endTime =
session.endTime instanceof Date && !isNaN(session.endTime.getTime())
? session.endTime
: new Date();
// Check if the session already exists
const existingSession = await prisma.session.findUnique({
where: { id: sessionData.id },
});
if (existingSession) {
// Skip this session as it already exists
continue;
}
// Only include fields that are properly typed for Prisma
await prisma.session.create({
data: {
id: sessionData.id,
companyId: sessionData.companyId,
startTime: startTime,
endTime: endTime,
ipAddress: session.ipAddress || null,
country: session.country || null,
language: session.language || null,
messagesSent:
typeof session.messagesSent === "number" ? session.messagesSent : 0,
sentiment:
typeof session.sentiment === "number" ? session.sentiment : null,
escalated:
typeof session.escalated === "boolean" ? session.escalated : null,
forwardedHr:
typeof session.forwardedHr === "boolean"
? session.forwardedHr
: null,
fullTranscriptUrl: session.fullTranscriptUrl || null,
avgResponseTime:
typeof session.avgResponseTime === "number"
? session.avgResponseTime
: null,
tokens: typeof session.tokens === "number" ? session.tokens : null,
tokensEur:
typeof session.tokensEur === "number" ? session.tokensEur : null,
category: session.category || null,
initialMsg: session.initialMsg || null,
},
});
}
// After importing sessions, automatically trigger complete workflow (fetch transcripts + process)
// This runs in the background without blocking the response
triggerCompleteWorkflow()
.then((result) => {
console.log(`[Refresh Sessions] Complete workflow finished: ${result.message}`);
})
.catch((error) => {
console.error(`[Refresh Sessions] Complete workflow failed:`, error);
});
return sessions.length;
}

6
lib/utils.ts Normal file
View File

@ -0,0 +1,6 @@
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

1
lib/workflow.ts Normal file
View File

@ -0,0 +1 @@
import { prisma } from "./prisma";import { processUnprocessedSessions } from "./processingSchedulerNoCron";import { fileURLToPath } from "url";import { dirname, join } from "path";import { readFileSync } from "fs";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) {}async function fetchTranscriptContent( url: string, username?: string, password?: string): Promise<string | null> { try { const authHeader = username && password ? "Basic " + Buffer.from(`${username}:${password}`).toString("base64") : undefined; const response = await fetch(url, { headers: authHeader ? { Authorization: authHeader } : {}, }); if (!response.ok) { process.stderr.write( `Error fetching transcript: ${response.statusText}\n` ); return null; } return await response.text(); } catch (error) { process.stderr.write(`Failed to fetch transcript: ${error}\n`); return null; }}export async function triggerCompleteWorkflow(): Promise<{ message: string }> { try { const sessionsWithoutMessages = await prisma.session.count({ where: { messages: { none: {} }, fullTranscriptUrl: { not: null } } }); if (sessionsWithoutMessages > 0) { console.log(`[Complete Workflow] Fetching transcripts for ${sessionsWithoutMessages} sessions`); const sessionsToProcess = await prisma.session.findMany({ where: { AND: [ { fullTranscriptUrl: { not: null } }, { messages: { none: {} } }, ], }, include: { company: true, }, take: 20, }); for (const session of sessionsToProcess) { try { if (!session.fullTranscriptUrl) continue; const transcriptContent = await fetchTranscriptContent( session.fullTranscriptUrl, session.company.csvUsername || undefined, session.company.csvPassword || undefined ); if (!transcriptContent) { console.log(`No transcript content for session ${session.id}`); continue; } const lines = transcriptContent.split("\n").filter((line) => line.trim()); const messages: Array<{ sessionId: string; role: string; content: string; timestamp: Date; order: number; }> = []; let messageOrder = 0; for (const line of lines) { const timestampMatch = line.match(/^\\[([^\]]+)\\]\\s*([^:]+):\\s*(.+)$/); if (timestampMatch) { const [, timestamp, role, content] = timestampMatch; const dateMatch = timestamp.match(/^(\\d{1,2})-(\\d{1,2})-(\\d{4}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/); let parsedTimestamp = new Date(); if (dateMatch) { const [, day, month, year, hour, minute, second] = dateMatch; parsedTimestamp = new Date( parseInt(year), parseInt(month) - 1, parseInt(day), parseInt(hour), parseInt(minute), parseInt(second) ); } messages.push({ sessionId: session.id, role: role.trim().toLowerCase(), content: content.trim(), timestamp: parsedTimestamp, order: messageOrder++, }); } } if (messages.length > 0) { await prisma.message.createMany({ data: messages as any, }); console.log(`Added ${messages.length} messages for session ${session.id}`); } } catch (error) { console.error(`Error processing session ${session.id}:`, error); } } } const unprocessedWithMessages = await prisma.session.count({ where: { processed: false, messages: { some: {} } } }); if (unprocessedWithMessages > 0) { console.log(`[Complete Workflow] Processing ${unprocessedWithMessages} sessions`); await processUnprocessedSessions(); } return { message: `Complete workflow finished successfully` }; } catch (error) { console.error('[Complete Workflow] Error:', error); throw error; }}