mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 15:12:09 +01:00
- Updated formatting in SessionDetails component for better readability. - Enhanced documentation in scheduler-fixes.md to clarify issues and solutions. - Improved error handling and logging in csvFetcher.js and processingScheduler.js. - Standardized code formatting across various scripts and components for consistency. - Added validation checks for CSV URLs and transcript content to prevent processing errors. - Enhanced logging messages for better tracking of processing status and errors.
87 lines
3.5 KiB
TypeScript
87 lines
3.5 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
import { prisma } from "../../../../lib/prisma";
|
|
import { ChatSession } from "../../../../lib/types";
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse
|
|
) {
|
|
if (req.method !== "GET") {
|
|
return res.status(405).json({ error: "Method not allowed" });
|
|
}
|
|
|
|
const { id } = req.query;
|
|
|
|
if (!id || typeof id !== "string") {
|
|
return res.status(400).json({ error: "Session ID is required" });
|
|
}
|
|
|
|
try {
|
|
const prismaSession = await prisma.session.findUnique({
|
|
where: { id },
|
|
include: {
|
|
messages: {
|
|
orderBy: { order: "asc" },
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!prismaSession) {
|
|
return res.status(404).json({ error: "Session not found" });
|
|
}
|
|
|
|
// Map Prisma session object to ChatSession type
|
|
const session: ChatSession = {
|
|
// Spread prismaSession to include all its properties
|
|
...prismaSession,
|
|
// Override properties that need conversion or specific mapping
|
|
id: prismaSession.id, // ChatSession.id from Prisma.Session.id
|
|
sessionId: prismaSession.id, // ChatSession.sessionId from Prisma.Session.id
|
|
startTime: new Date(prismaSession.startTime),
|
|
endTime: prismaSession.endTime ? new Date(prismaSession.endTime) : null,
|
|
createdAt: new Date(prismaSession.createdAt),
|
|
// Prisma.Session does not have an `updatedAt` field. We'll use `createdAt` as a fallback.
|
|
// Or, if your business logic implies an update timestamp elsewhere, use that.
|
|
updatedAt: new Date(prismaSession.createdAt), // Fallback to createdAt
|
|
// Prisma.Session does not have a `userId` field.
|
|
userId: null, // Explicitly set to null or map if available from another source
|
|
// Ensure nullable fields from Prisma are correctly mapped to ChatSession's optional or nullable fields
|
|
category: prismaSession.category ?? null,
|
|
language: prismaSession.language ?? null,
|
|
country: prismaSession.country ?? null,
|
|
ipAddress: prismaSession.ipAddress ?? null,
|
|
sentiment: prismaSession.sentiment ?? null,
|
|
sentimentCategory: prismaSession.sentimentCategory ?? null, // New field
|
|
messagesSent: prismaSession.messagesSent ?? undefined, // Use undefined if ChatSession expects number | undefined
|
|
avgResponseTime: prismaSession.avgResponseTime ?? null,
|
|
escalated: prismaSession.escalated ?? undefined,
|
|
forwardedHr: prismaSession.forwardedHr ?? undefined,
|
|
tokens: prismaSession.tokens ?? undefined,
|
|
tokensEur: prismaSession.tokensEur ?? undefined,
|
|
initialMsg: prismaSession.initialMsg ?? undefined,
|
|
fullTranscriptUrl: prismaSession.fullTranscriptUrl ?? null,
|
|
processed: prismaSession.processed ?? null, // New field
|
|
questions: prismaSession.questions ?? null, // New field
|
|
summary: prismaSession.summary ?? null, // New field
|
|
messages:
|
|
prismaSession.messages?.map((msg) => ({
|
|
id: msg.id,
|
|
sessionId: msg.sessionId,
|
|
timestamp: new Date(msg.timestamp),
|
|
role: msg.role,
|
|
content: msg.content,
|
|
order: msg.order,
|
|
createdAt: new Date(msg.createdAt),
|
|
})) ?? [], // New field - parsed messages
|
|
};
|
|
|
|
return res.status(200).json({ session });
|
|
} catch (error) {
|
|
const errorMessage =
|
|
error instanceof Error ? error.message : "An unknown error occurred";
|
|
return res
|
|
.status(500)
|
|
.json({ error: "Failed to fetch session", details: errorMessage });
|
|
}
|
|
}
|