mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-02-13 17:35:44 +01:00
- Fix syntax errors in skills markdown files (.github/skills, .opencode/skills) - Change typescript to tsx for code blocks with JSX - Replace ellipsis (...) in array examples with valid syntax - Separate CSS from TypeScript into distinct code blocks - Convert JavaScript object examples to valid JSON in docs - Fix enum definitions with proper comma separation
157 lines
5.1 KiB
TypeScript
157 lines
5.1 KiB
TypeScript
import type { Prisma, SessionCategory } from "@prisma/client";
|
|
import { type NextRequest, NextResponse } from "next/server";
|
|
import { neonAuth } from "@/lib/auth/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import type { ChatSession } from "@/lib/types";
|
|
|
|
// MIGRATED: Removed "export const dynamic = 'force-dynamic'" - dynamic by default with Cache Components
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const { session: authSession, user: authUser } = await neonAuth();
|
|
if (!authSession || !authUser?.email) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
// Look up user in our database to get companyId
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: authUser.email },
|
|
select: { companyId: true },
|
|
});
|
|
|
|
if (!user || !user.companyId) {
|
|
return NextResponse.json(
|
|
{ error: "User not found or no company" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const companyId = user.companyId;
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
const searchTerm = searchParams.get("searchTerm");
|
|
const category = searchParams.get("category");
|
|
const language = searchParams.get("language");
|
|
const startDate = searchParams.get("startDate");
|
|
const endDate = searchParams.get("endDate");
|
|
const sortKey = searchParams.get("sortKey");
|
|
const sortOrder = searchParams.get("sortOrder");
|
|
const queryPage = searchParams.get("page");
|
|
const queryPageSize = searchParams.get("pageSize");
|
|
|
|
const page = Number(queryPage) || 1;
|
|
const pageSize = Number(queryPageSize) || 10;
|
|
|
|
try {
|
|
const whereClause: Prisma.SessionWhereInput = { companyId };
|
|
|
|
// Search Term
|
|
if (searchTerm && searchTerm.trim() !== "") {
|
|
const searchConditions = [
|
|
{ id: { contains: searchTerm } },
|
|
{ initialMsg: { contains: searchTerm } },
|
|
{ summary: { contains: searchTerm } },
|
|
];
|
|
whereClause.OR = searchConditions;
|
|
}
|
|
|
|
// Category Filter
|
|
if (category && category.trim() !== "") {
|
|
// Cast to SessionCategory enum if it's a valid value
|
|
whereClause.category = category as SessionCategory;
|
|
}
|
|
|
|
// Language Filter
|
|
if (language && language.trim() !== "") {
|
|
whereClause.language = language;
|
|
}
|
|
|
|
// Date Range Filter
|
|
if (startDate) {
|
|
whereClause.startTime = {
|
|
...((whereClause.startTime as object) || {}),
|
|
gte: new Date(startDate),
|
|
};
|
|
}
|
|
if (endDate) {
|
|
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 && 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" },
|
|
];
|
|
}
|
|
|
|
const prismaSessions = await prisma.session.findMany({
|
|
where: whereClause,
|
|
orderBy: orderByCondition,
|
|
skip: (page - 1) * pageSize,
|
|
take: pageSize,
|
|
});
|
|
|
|
const totalSessions = await prisma.session.count({ where: whereClause });
|
|
|
|
const sessions: ChatSession[] = prismaSessions.map((ps) => ({
|
|
id: ps.id,
|
|
sessionId: ps.id,
|
|
companyId: ps.companyId,
|
|
startTime: new Date(ps.startTime),
|
|
endTime: ps.endTime ? new Date(ps.endTime) : null,
|
|
createdAt: new Date(ps.createdAt),
|
|
updatedAt: new Date(ps.createdAt),
|
|
userId: null,
|
|
category: ps.category ?? null,
|
|
language: ps.language ?? null,
|
|
country: ps.country ?? null,
|
|
ipAddress: ps.ipAddress ?? null,
|
|
sentiment: ps.sentiment ?? null,
|
|
messagesSent: ps.messagesSent ?? undefined,
|
|
avgResponseTime: ps.avgResponseTime ?? null,
|
|
escalated: ps.escalated ?? undefined,
|
|
forwardedHr: ps.forwardedHr ?? undefined,
|
|
initialMsg: ps.initialMsg ?? undefined,
|
|
fullTranscriptUrl: ps.fullTranscriptUrl ?? null,
|
|
transcriptContent: null, // Transcript content is now fetched from fullTranscriptUrl when needed
|
|
}));
|
|
|
|
return NextResponse.json({ sessions, totalSessions });
|
|
} catch (error) {
|
|
const errorMessage =
|
|
error instanceof Error ? error.message : "An unknown error occurred";
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch sessions", details: errorMessage },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|