30 Commits

Author SHA1 Message Date
fd55b30398 shit 2025-06-26 22:43:22 +02:00
8774a1f155 feat: Refactor sentiment handling and enhance processing logic for session data 2025-06-26 21:14:24 +02:00
653d70022b Broken shit 2025-06-26 21:00:19 +02:00
ab2c75b736 feat: Add additional country coordinates for improved geographic mapping 2025-06-26 19:32:53 +02:00
8c43a35632 feat: Enhance session processing and metrics
- Updated session processing commands in documentation for clarity.
- Removed transcript content fetching from session processing, allowing on-demand retrieval.
- Improved session metrics calculations and added new metrics for dashboard.
- Refactored processing scheduler to handle sessions in parallel with concurrency limits.
- Added manual trigger API for processing unprocessed sessions with admin checks.
- Implemented scripts for fetching and parsing transcripts, checking transcript content, and testing processing status.
- Updated Prisma schema to enforce default values for processed sessions.
- Added error handling and logging improvements throughout the processing workflow.
2025-06-26 17:12:42 +02:00
8f3c1e0f7c feat: Enhance dashboard metrics with new calculations and add Top Questions Chart component 2025-06-26 12:04:51 +02:00
0e5ac69d45 feat: Add DateRangePicker component and integrate date range filtering in metrics fetching 2025-06-26 11:42:01 +02:00
f964d6a078 feat: Update session endTime based on the latest message timestamp during message storage 2025-06-26 11:12:06 +02:00
944431fea3 feat: Load environment variables from .env.local and update session processing logic 2025-06-26 10:57:05 +02:00
1afe15df85 feat: Add prisma:push script and remove obsolete migration files 2025-06-25 17:50:55 +02:00
9e095e1a43 Refactor code for improved readability and consistency
- 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.
2025-06-25 17:46:23 +02:00
a9e4145001 feat: Implement structured message parsing and display in MessageViewer component
- Added MessageViewer component to display parsed messages in a chat-like format.
- Introduced new Message table in the database to store individual messages with timestamps, roles, and content.
- Updated Session model to include a relation to parsed messages.
- Created transcript parsing logic to convert raw transcripts into structured messages.
- Enhanced processing scheduler to handle sessions with parsed messages.
- Updated API endpoints to return parsed messages alongside session details.
- Added manual trigger commands for session refresh, transcript parsing, and processing.
- Improved user experience with color-coded message roles and timestamps in the UI.
- Documented the new scheduler workflow and transcript parsing implementation.
2025-06-25 17:45:08 +02:00
3196dabdf2 feat: Implement session processing and refresh schedulers
- Added processingScheduler.js and processingScheduler.ts to handle session transcript processing using OpenAI API.
- Implemented a new scheduler (scheduler.js and schedulers.ts) for refreshing sessions every 15 minutes.
- Updated Prisma migrations to add new fields for processed sessions, including questions, sentimentCategory, and summary.
- Created scripts (process_sessions.mjs and process_sessions.ts) for manual processing of unprocessed sessions.
- Enhanced server.js and server.mjs to initialize schedulers on server start.
2025-06-25 16:14:01 +02:00
c9e24298cd Bump node-cron from 4.0.6 to 4.0.7 (#3)
Bumps [node-cron](https://github.com/merencia/node-cron) from 4.0.6 to 4.0.7.
- [Release notes](https://github.com/merencia/node-cron/releases)
- [Commits](https://github.com/merencia/node-cron/compare/v4.0.6...v4.0.7)

---
updated-dependencies:
- dependency-name: node-cron
  dependency-version: 4.0.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-23 12:37:39 +02:00
a360f461ab Merge pull request #2 from kjanat/feature/sidebar
Feature/sidebar
2025-05-23 00:22:45 +02:00
bbcdff0ffc Update app/dashboard/company/page.tsx
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-23 00:21:24 +02:00
940b416563 Revert DonutChart.tsx 2025-05-23 00:19:55 +02:00
cb86d26786 Update components/DonutChart.tsx
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-23 00:13:16 +02:00
a265f3236c Update README.md
Typo
2025-05-23 00:10:45 +02:00
be63dba540 Update playwright.yml
renamed step
2025-05-23 00:09:00 +02:00
01f4dd60f9 Update playwright.yml 2025-05-22 23:59:47 +02:00
9fad25e5f9 Update TODO.md with new tasks and enhance README.md with project details and setup instructions 2025-05-22 23:55:30 +02:00
13d0f8ee8d Add markdownlint-cli2 for markdown linting; configure Prettier with Jinja template support and update linting scripts 2025-05-22 22:29:06 +02:00
303226e3a9 Refactor trend calculations and improve WordCloud component responsiveness; remove trend labels for cleaner display 2025-05-22 22:21:40 +02:00
cbbdc8a1dc Enhance dashboard layout and sidebar functionality; improve session metrics calculations and API error handling 2025-05-22 21:53:18 +02:00
8dcb892ae9 Add Playwright testing framework and implement initial tests; update .gitignore and package files 2025-05-22 19:46:04 +02:00
f005b2ec0a Update dashboard metrics and session handling
- Refactor DashboardContent to improve trend calculations for user metrics and session time.
- Modify SessionViewPage to ensure loading state is set before fetching session data.
- Adjust SessionsPage to clean up display of session start time and remove unnecessary comments.
- Enhance DonutChart to handle various data point types and improve percentage calculations.
- Update GeographicMap to utilize @rapideditor/country-coder for country coordinates.
- Improve safeParseDate function in csvFetcher for better date handling and error logging.
- Refactor sessionMetrics to clarify variable names and improve session duration calculations.
- Update next.config.js for better configuration clarity.
- Bump package version to 0.2.0 and update dependencies in package.json and package-lock.json.
- Clean up API handler for dashboard sessions to improve readability and maintainability.
- Adjust tsconfig.json for better module resolution and strict type checking.
2025-05-22 19:21:49 +02:00
ed6e5b0c36 Enhance session handling and improve data parsing; add safe date parsing utility 2025-05-22 16:11:33 +02:00
efb5261c7d Refactor components and enhance metrics calculations:
- Update access denied messages to use HTML entities.
- Add autoComplete attributes to forms for better user experience.
- Improve trend calculations in sessionMetrics function.
- Update MetricCard props to accept React nodes for icons.
- Integrate Next.js Image component in Sidebar for optimization.
- Adjust ESLint rules for better code quality.
- Add new properties for trends in MetricsResult interface.
- Bump version to 0.2.0 in package.json.
2025-05-22 14:44:28 +02:00
e3134aa451 Add comprehensive dashboard features
Introduce company settings, user management, and layout components
Implement session-based Company and User pages for admin access
Integrate chart components for dynamic data visualization
Add Sidebar for modular navigation
Revamp global styles with Tailwind CSS

Enhances user experience and administrative control
2025-05-22 14:12:36 +02:00
98 changed files with 9962 additions and 2734 deletions

View File

@ -6,4 +6,8 @@ NEXTAUTH_URL=http://192.168.1.2:3000
NEXTAUTH_SECRET=this_is_a_fixed_secret_for_development_only NEXTAUTH_SECRET=this_is_a_fixed_secret_for_development_only
NODE_ENV=development NODE_ENV=development
# OpenAI API key for session processing
# Add your API key here: OPENAI_API_KEY=sk-...
OPENAI_API_KEY=your_openai_api_key_here
# Database connection - already configured in your prisma/schema.prisma # Database connection - already configured in your prisma/schema.prisma

View File

@ -1,3 +0,0 @@
{
"extends": ["next/core-web-vitals", "next/typescript"]
}

20
.gemini/settings.json Normal file
View File

@ -0,0 +1,20 @@
{
"mcpServers": {
"sqlite": {
"command": "uvx",
"args": [
"mcp-server-sqlite",
"--db-path",
"./prisma/dev.db"
]
},
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"D:\\Notso\\Product\\Vibe-coding\\livedash-node"
]
}
}
}

1
.github/CODEOWNERS vendored Normal file
View File

@ -0,0 +1 @@
* @kjanat

View File

@ -9,18 +9,30 @@ updates:
directory: "/" # Location of package manifests directory: "/" # Location of package manifests
schedule: schedule:
interval: "weekly" interval: "weekly"
day: "tuesday"
time: "03:00"
timezone: "Europe/Amsterdam"
- package-ecosystem: "github-actions" # See documentation for possible values - package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests directory: "/" # Location of package manifests
schedule: schedule:
interval: "weekly" interval: "weekly"
day: "tuesday"
time: "03:00"
timezone: "Europe/Amsterdam"
- package-ecosystem: "docker" # See documentation for possible values - package-ecosystem: "docker" # See documentation for possible values
directory: "/" # Location of package manifests directory: "/" # Location of package manifests
schedule: schedule:
interval: "weekly" interval: "weekly"
day: "tuesday"
time: "03:00"
timezone: "Europe/Amsterdam"
- package-ecosystem: "docker-compose" # See documentation for possible values - package-ecosystem: "docker-compose" # See documentation for possible values
directory: "/" # Location of package manifests directory: "/" # Location of package manifests
schedule: schedule:
interval: "weekly" interval: "weekly"
day: "tuesday"
time: "03:00"
timezone: "Europe/Amsterdam"

29
.github/workflows/playwright.yml vendored Normal file
View File

@ -0,0 +1,29 @@
name: Playwright Tests
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm ci
- name: Build dashboard
run: npm run build
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30

6
.gitignore vendored
View File

@ -255,3 +255,9 @@ Thumbs.db
# Backup files # Backup files
*.bak *.bak
# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

View File

@ -1,10 +0,0 @@
{
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"tabWidth": 2,
"useTabs": false,
"printWidth": 80,
"bracketSpacing": true,
"endOfLine": "auto"
}

View File

@ -2,6 +2,7 @@
"recommendations": [ "recommendations": [
"prisma.prisma", "prisma.prisma",
"dbaeumer.vscode-eslint", "dbaeumer.vscode-eslint",
"rvest.vs-code-prettier-eslint" "rvest.vs-code-prettier-eslint",
"ms-playwright.playwright"
] ]
} }

47
GEMINI.md Normal file
View File

@ -0,0 +1,47 @@
# Project Overview
This project is a Next.js application with a Node.js backend, designed to provide a live dashboard for data visualization and session management.
## Setup
To set up the project, follow these steps:
1. **Install Dependencies:**
```bash
npm install
```
2. **Environment Variables:**
Create a `.env` file based on `.env.example` and fill in the necessary environment variables.
3. **Database Setup:**
Run database migrations:
```bash
npx prisma migrate dev
```
Seed the database (optional):
```bash
npx prisma db seed
```
4. **Run Development Server:**
```bash
npm run dev
```
## Common Commands
- **Run Tests:**
```bash
npm test
```
- **Run Linter:**
```bash
npm run lint
```
- **Build Project:**
```bash
npm run build
```

115
README.md Normal file
View File

@ -0,0 +1,115 @@
# LiveDash-Node
A real-time analytics dashboard for monitoring user sessions and interactions with interactive data visualizations and detailed metrics.
![Next.js](<https://img.shields.io/badge/dynamic/regex?url=https%3A%2F%2Fraw.githubusercontent.com%2Fkjanat%2Flivedash-node%2Fmaster%2Fpackage.json&search=%22next%22%5Cs*%3A%5Cs*%22%5C%5E(%3F%3Cversion%3E%5Cd%2B%5C.%5Cd*).*%22&replace=%24%3Cversion%3E&logo=nextdotjs&label=Nextjs&color=%23000000>)
![React](<https://img.shields.io/badge/dynamic/regex?url=https%3A%2F%2Fraw.githubusercontent.com%2Fkjanat%2Flivedash-node%2Fmaster%2Fpackage.json&search=%22react%22%5Cs*%3A%5Cs*%22%5C%5E(%3F%3Cversion%3E%5Cd%2B%5C.%5Cd*).*%22&replace=%24%3Cversion%3E&logo=react&label=React&color=%2361DAFB>)
![TypeScript](<https://img.shields.io/badge/dynamic/regex?url=https%3A%2F%2Fraw.githubusercontent.com%2Fkjanat%2Flivedash-node%2Fmaster%2Fpackage.json&search=%22typescript%22%5Cs*%3A%5Cs*%22%5C%5E(%3F%3Cversion%3E%5Cd%2B%5C.%5Cd*).*%22&replace=%24%3Cversion%3E&logo=typescript&label=TypeScript&color=%233178C6>)
![Prisma](<https://img.shields.io/badge/dynamic/regex?url=https%3A%2F%2Fraw.githubusercontent.com%2Fkjanat%2Flivedash-node%2Fmaster%2Fpackage.json&search=%22prisma%22%5Cs*%3A%5Cs*%22%5C%5E(%3F%3Cversion%3E%5Cd%2B%5C.%5Cd*).*%22&replace=%24%3Cversion%3E&logo=prisma&label=Prisma&color=%232D3748>)
![TailwindCSS](<https://img.shields.io/badge/dynamic/regex?url=https%3A%2F%2Fraw.githubusercontent.com%2Fkjanat%2Flivedash-node%2Fmaster%2Fpackage.json&search=%22tailwindcss%22%5Cs*%3A%5Cs*%22%5C%5E(%3F%3Cversion%3E%5Cd%2B%5C.%5Cd*).*%22&replace=%24%3Cversion%3E&logo=tailwindcss&label=TailwindCSS&color=%2306B6D4>)
## Features
- **Real-time Session Monitoring**: Track and analyze user sessions as they happen
- **Interactive Visualizations**: Geographic maps, response time distributions, and more
- **Advanced Analytics**: Detailed metrics and insights about user behavior
- **User Management**: Secure authentication with role-based access control
- **Customizable Dashboard**: Filter and sort data based on your specific needs
- **Session Details**: In-depth analysis of individual user sessions
## Tech Stack
- **Frontend**: React 19, Next.js 15, TailwindCSS 4
- **Backend**: Next.js API Routes, Node.js
- **Database**: Prisma ORM with SQLite (default), compatible with PostgreSQL
- **Authentication**: NextAuth.js
- **Visualization**: Chart.js, D3.js, React Leaflet
- **Data Processing**: Node-cron for scheduled tasks
## Getting Started
### Prerequisites
- Node.js (LTS version recommended)
- npm or yarn
### Installation
1. Clone this repository:
```bash
git clone https://github.com/kjanat/livedash-node.git
cd livedash-node
```
2. Install dependencies:
```bash
npm install
```
3. Set up the database:
```bash
npm run prisma:generate
npm run prisma:migrate
npm run prisma:seed
```
4. Start the development server:
```bash
npm run dev
```
5. Open your browser and navigate to <http://localhost:3000>
## Environment Setup
Create a `.env` file in the root directory with the following variables:
```env
DATABASE_URL="file:./dev.db"
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your-secret-here
```
## Project Structure
- `app/`: Next.js App Router components and pages
- `components/`: Reusable React components
- `lib/`: Utility functions and shared code
- `pages/`: API routes and server-side code
- `prisma/`: Database schema and migrations
- `public/`: Static assets
- `docs/`: Project documentation
## Available Scripts
- `npm run dev`: Start the development server
- `npm run build`: Build the application for production
- `npm run start`: Run the production build
- `npm run lint`: Run ESLint
- `npm run format`: Format code with Prettier
- `npm run prisma:studio`: Open Prisma Studio to view database
## Contributing
1. Fork the repository
2. Create your feature branch: `git checkout -b feature/my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin feature/my-new-feature`
5. Submit a pull request
## License
This project is not licensed for commercial use without explicit permission. Free to use for educational or personal projects.
## Acknowledgments
- [Next.js](https://nextjs.org/)
- [Prisma](https://prisma.io/)
- [TailwindCSS](https://tailwindcss.com/)
- [Chart.js](https://www.chartjs.org/)
- [D3.js](https://d3js.org/)
- [React Leaflet](https://react-leaflet.js.org/)

97
TODO.md
View File

@ -1,45 +1,78 @@
# Application Improvement TODOs # TODO.md
This file lists general areas for improvement and tasks that are broader in scope or don't map to a single specific file. # Refactor!!!
## General Enhancements & Features > Based on my analysis of the codebase, here is a plan with recommendations for improving the project. The focus is on enhancing standardization, abstraction, user experience, and visual
> design.
- [ ] **Real-time Updates:** Implement real-time updates for the dashboard and session list (e.g., using WebSockets or Server-Sent Events). ## High-Level Recommendations
- [ ] **Data Export:** Provide functionality for users (especially admins) to export session data (e.g., to CSV).
- [ ] **Customizable Dashboard:** Allow users to customize their dashboard view, choosing which metrics or charts are most important to them.
- [ ] **Resolve `GeographicMap.tsx` and `ResponseTimeDistribution.tsx` data simulation:** The `docs/dashboard-components.md` mentions these use simulated data. Investigate integrating real data sources.
## Robustness and Maintainability The project has a solid foundation, but it could be significantly improved by focusing on three key areas:
- [ ] **Comprehensive Testing:** 1. Adopt a UI Component Library: While Tailwind CSS is excellent for styling, using a component library like ShadCN/UI or Headless UI would provide pre-built, accessible, and visually
- [ ] Implement unit tests (e.g., for utility functions, API logic). consistent components, saving development time and improving the user experience.
- [ ] Implement integration tests (e.g., for API endpoints with the database). 2. Refactor for Next.js App Router: The project currently uses a mix of the pages and app directories. Migrating fully to the App Router would simplify the project structure, improve
- [ ] Implement end-to-end tests (e.g., for user flows using Playwright or Cypress). performance, and align with the latest Next.js features.
- [ ] **Error Monitoring and Logging:** Integrate a robust error monitoring service (like Sentry) and enhance server-side logging. 3. Enhance User Experience: Implementing consistent loading and error states, improving responsiveness, and providing better user feedback would make the application more robust and
- [ ] **Accessibility (a11y):** Review and improve the application's accessibility according to WCAG guidelines (keyboard navigation, screen reader compatibility, color contrast). user-friendly.
## Security Enhancements ## Detailed Improvement Plan
- [ ] **Password Reset Functionality:** Implement a secure password reset mechanism. (Related: `app/forgot-password/page.tsx`, `app/reset-password/page.tsx`, `pages/api/forgot-password.ts`, `pages/api/reset-password.ts` - ensure these are robust and secure if already implemented). Here is a phased plan to implement these recommendations:
- [ ] **Two-Factor Authentication (2FA):** Consider adding 2FA, especially for admin accounts.
- [ ] **Input Validation and Sanitization:** Rigorously review and ensure all user inputs (API request bodies, query parameters) are validated and sanitized.
## Code Quality and Development Practices ### Phase 1: Foundational Improvements (Standardization & Abstraction)
- [ ] **Code Reviews:** Enforce code reviews for all changes. This phase focuses on cleaning up the codebase, standardizing the project structure, and improving the abstraction of core functionalities.
- [ ] **Environment Configuration:** Ensure secure and effective management of environment-specific configurations.
- [ ] **Dependency Review:** Periodically review dependencies for vulnerabilities or updates.
- [ ] **Documentation:**
- Ensure `docs/dashboard-components.md` is up-to-date with actual component implementations.
- Verify that "Dashboard Enhancements" (Improved Layout, Visual Hierarchies, Color Coding) are consistently applied.
## Component Specific 1. Standardize Project Structure:
- [ ] **`components/SessionDetails.tsx.new`:** Review, complete TODOs within the file, and integrate as the primary `SessionDetails.tsx` component, removing/archiving older versions (`SessionDetails.tsx`, `SessionDetails.tsx.bak`). - [x] Unify Server File: Consolidated server.js, server.mjs, and server.ts into a single server.ts file to remove redundancy. ✅
- [ ] **`components/GeographicMap.tsx`:** Check if `GeographicMap.tsx.bak` is still needed or can be removed. - [x] Migrate to App Router: All API routes moved from `pages/api` to `app/api`. ✅
- [ ] **`app/dashboard/sessions/page.tsx`:** Implement pagination, advanced filtering, and sorting. - [x] Standardize Naming Conventions: All files and components already follow a consistent naming convention (e.g., PascalCase for components, kebab-case for files). ✅
- [ ] **`pages/api/dashboard/users.ts`:** Implement robust emailing of temporary passwords.
## File Cleanup 2. Introduce a UI Component Library:
- [ ] Review and remove `.bak` and `.new` files once changes are integrated (e.g., `GeographicMap.tsx.bak`, `SessionDetails.tsx.bak`, `SessionDetails.tsx.new`). - Integrate ShadCN/UI: Add ShadCN/UI to the project to leverage its extensive library of accessible and customizable components.
- Replace Custom Components: Gradually replace custom-built components in the components/ directory with their ShadCN/UI equivalents. This will improve visual consistency and reduce
maintenance overhead.
3. Refactor Core Logic:
- Centralize Data Fetching: Create a dedicated module (e.g., lib/data-service.ts) to handle all data fetching logic, abstracting away the details of using Prisma and external APIs.
- Isolate Business Logic: Ensure that business logic (e.g., session processing, metric calculation) is separated from the API routes and UI components.
### Phase 2: UX and Visual Enhancements
This phase focuses on improving the user-facing aspects of the application.
1. Implement Comprehensive Loading and Error States:
- Skeleton Loaders: Use skeleton loaders for dashboard components to provide a better loading experience.
- Global Error Handling: Implement a global error handling strategy to catch and display user-friendly error messages for API failures or other unexpected issues.
2. Redesign the Dashboard:
- Improve Information Hierarchy: Reorganize the dashboard to present the most important information first.
- Enhance Visual Appeal: Use the new component library to create a more modern and visually appealing design with a consistent color palette and typography.
- Improve Chart Interactivity: Add features like tooltips, zooming, and filtering to the charts to make them more interactive and informative.
3. Ensure Full Responsiveness:
- Mobile-First Approach: Review and update all pages and components to ensure they are fully responsive and usable on a wide range of devices.
### Phase 3: Advanced Topics (Security, Performance, and Documentation)
This phase focuses on long-term improvements to the project's stability, performance, and maintainability.
1. Conduct a Security Review:
- Input Validation: Ensure that all user inputs are properly validated on both the client and server sides.
- Dependency Audit: Regularly audit dependencies for known vulnerabilities.
2. Optimize Performance:
- Code Splitting: Leverage Next.js's automatic code splitting to reduce initial load times.
- Caching: Implement caching strategies for frequently accessed data to reduce database load and improve API response times.
3. Expand Documentation:
- API Documentation: Create detailed documentation for all API endpoints.
- Component Library: Document the usage and props of all reusable components.
- Update `AGENTS.md`: Keep the AGENTS.md file up-to-date with any architectural changes.

View File

@ -0,0 +1,183 @@
"use client";
import { useState, useEffect } from "react";
import { useSession } from "next-auth/react";
import { Company } from "../../../lib/types";
export default function CompanySettingsPage() {
const { data: session, status } = useSession();
// We store the full company object for future use and updates after save operations
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
const [company, setCompany] = useState<Company | null>(null);
const [csvUrl, setCsvUrl] = useState<string>("");
const [csvUsername, setCsvUsername] = useState<string>("");
const [csvPassword, setCsvPassword] = useState<string>("");
const [sentimentThreshold, setSentimentThreshold] = useState<string>("");
const [message, setMessage] = useState<string>("");
const [loading, setLoading] = useState(true);
useEffect(() => {
if (status === "authenticated") {
const fetchCompany = async () => {
setLoading(true);
try {
const res = await fetch("/api/dashboard/config");
const data = await res.json();
setCompany(data.company);
setCsvUrl(data.company.csvUrl || "");
setCsvUsername(data.company.csvUsername || "");
setSentimentThreshold(data.company.sentimentAlert?.toString() || "");
if (data.company.csvPassword) {
setCsvPassword(data.company.csvPassword);
}
} catch (error) {
console.error("Failed to fetch company settings:", error);
setMessage("Failed to load company settings.");
} finally {
setLoading(false);
}
};
fetchCompany();
}
}, [status]);
async function handleSave() {
setMessage("");
try {
const res = await fetch("/api/dashboard/settings", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
csvUrl,
csvUsername,
csvPassword,
sentimentThreshold,
}),
});
if (res.ok) {
setMessage("Settings saved successfully!");
// Update local state if needed
const data = await res.json();
setCompany(data.company);
} else {
const error = await res.json();
setMessage(
`Failed to save settings: ${error.message || "Unknown error"}`
);
}
} catch (error) {
setMessage("Failed to save settings. Please try again.");
console.error("Error saving settings:", error);
}
}
// Loading state
if (loading) {
return <div className="text-center py-10">Loading settings...</div>;
}
// Check for admin access
if (session?.user?.role !== "admin") {
return (
<div className="text-center py-10 bg-white rounded-xl shadow p-6">
<h2 className="font-bold text-xl text-red-600 mb-2">Access Denied</h2>
<p>You don&apos;t have permission to view company settings.</p>
</div>
);
}
return (
<div className="space-y-6">
<div className="bg-white p-6 rounded-xl shadow">
<h1 className="text-2xl font-bold text-gray-800 mb-6">
Company Settings
</h1>
{message && (
<div
className={`p-4 rounded mb-6 ${message.includes("Failed") ? "bg-red-100 text-red-700" : "bg-green-100 text-green-700"}`}
>
{message}
</div>
)}
<form
className="grid gap-6"
onSubmit={(e) => {
e.preventDefault();
handleSave();
}}
autoComplete="off"
>
<div className="grid gap-2">
<label className="font-medium text-gray-700">
CSV Data Source URL
</label>
<input
type="text"
className="border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-sky-500"
value={csvUrl}
onChange={(e) => setCsvUrl(e.target.value)}
placeholder="https://example.com/data.csv"
autoComplete="off"
/>
</div>
<div className="grid gap-2">
<label className="font-medium text-gray-700">CSV Username</label>
<input
type="text"
className="border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-sky-500"
value={csvUsername}
onChange={(e) => setCsvUsername(e.target.value)}
placeholder="Username for CSV access (if needed)"
autoComplete="off"
/>
</div>
<div className="grid gap-2">
<label className="font-medium text-gray-700">CSV Password</label>
<input
type="password"
className="border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-sky-500"
value={csvPassword}
onChange={(e) => setCsvPassword(e.target.value)}
placeholder="Password will be updated only if provided"
autoComplete="new-password"
/>
<p className="text-sm text-gray-500">
Leave blank to keep current password
</p>
</div>
<div className="grid gap-2">
<label className="font-medium text-gray-700">
Sentiment Alert Threshold
</label>
<input
type="number"
className="border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-sky-500"
value={sentimentThreshold}
onChange={(e) => setSentimentThreshold(e.target.value)}
placeholder="Threshold value (0-100)"
min="0"
max="100"
autoComplete="off"
/>
<p className="text-sm text-gray-500">
Percentage of negative sentiment sessions to trigger alert (0-100)
</p>
</div>
<button
type="submit"
className="bg-sky-600 hover:bg-sky-700 text-white py-2 px-4 rounded-lg shadow transition-colors w-full sm:w-auto"
>
Save Settings
</button>
</form>
</div>
</div>
);
}

82
app/dashboard/layout.tsx Normal file
View File

@ -0,0 +1,82 @@
"use client";
import { ReactNode, useState, useEffect, useCallback } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import Sidebar from "../../components/Sidebar";
export default function DashboardLayout({ children }: { children: ReactNode }) {
const { status } = useSession();
const router = useRouter();
const [isSidebarExpanded, setIsSidebarExpanded] = useState(true);
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
const updateStatesBasedOnScreen = () => {
const screenIsMobile = window.innerWidth < 640; // sm breakpoint for mobile
const screenIsSmallDesktop = window.innerWidth < 768 && !screenIsMobile; // between sm and md
setIsMobile(screenIsMobile);
setIsSidebarExpanded(!screenIsSmallDesktop && !screenIsMobile);
};
updateStatesBasedOnScreen();
window.addEventListener("resize", updateStatesBasedOnScreen);
return () =>
window.removeEventListener("resize", updateStatesBasedOnScreen);
}, []);
// Toggle sidebar handler - used for clicking the toggle button
const toggleSidebarHandler = useCallback(() => {
setIsSidebarExpanded((prev) => !prev);
}, []);
// Collapse sidebar handler - used when clicking navigation links on mobile
const collapseSidebar = useCallback(() => {
if (isMobile) {
setIsSidebarExpanded(false);
}
}, [isMobile]);
if (status === "unauthenticated") {
router.push("/login");
return (
<div className="flex h-screen items-center justify-center">
<div className="text-center">Redirecting to login...</div>
</div>
);
}
if (status === "loading") {
return (
<div className="flex h-screen items-center justify-center">
<div className="text-center">Loading session...</div>
</div>
);
}
return (
<div className="flex h-screen bg-gray-100">
<Sidebar
isExpanded={isSidebarExpanded}
isMobile={isMobile}
onToggle={toggleSidebarHandler}
onNavigate={collapseSidebar}
/>
<div
className={`flex-1 overflow-auto transition-all duration-300 py-4 pr-4
${
isSidebarExpanded
? "pl-4 sm:pl-6 md:pl-10"
: "pl-20 sm:pl-20 md:pl-6"
}
sm:pr-6 md:py-6 md:pr-10`}
>
{/* <div className="w-full mx-auto">{children}</div> */}
<div className="max-w-7xl mx-auto">{children}</div>
</div>
</div>
);
}

View File

@ -0,0 +1,543 @@
"use client";
import { useEffect, useState, useCallback } from "react";
import { signOut, useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import {
SessionsLineChart,
CategoriesBarChart,
LanguagePieChart,
TokenUsageChart,
} from "../../../components/Charts";
import { Company, MetricsResult, WordCloudWord } from "../../../lib/types";
import MetricCard from "../../../components/MetricCard";
import DonutChart from "../../../components/DonutChart";
import WordCloud from "../../../components/WordCloud";
import GeographicMap from "../../../components/GeographicMap";
import ResponseTimeDistribution from "../../../components/ResponseTimeDistribution";
import WelcomeBanner from "../../../components/WelcomeBanner";
import DateRangePicker from "../../../components/DateRangePicker";
import TopQuestionsChart from "../../../components/TopQuestionsChart";
// Safely wrapped component with useSession
function DashboardContent() {
const { data: session, status } = useSession(); // Add status from useSession
const router = useRouter(); // Initialize useRouter
const [metrics, setMetrics] = useState<MetricsResult | null>(null);
const [company, setCompany] = useState<Company | null>(null);
const [, setLoading] = useState<boolean>(false);
const [refreshing, setRefreshing] = useState<boolean>(false);
const [dateRange, setDateRange] = useState<{
minDate: string;
maxDate: string;
} | null>(null);
const [selectedStartDate, setSelectedStartDate] = useState<string>("");
const [selectedEndDate, setSelectedEndDate] = useState<string>("");
const isAuditor = session?.user?.role === "auditor";
// Function to fetch metrics with optional date range
const fetchMetrics = useCallback(
async (startDate?: string, endDate?: string) => {
setLoading(true);
try {
let url = "/api/dashboard/metrics";
if (startDate && endDate) {
url += `?startDate=${startDate}&endDate=${endDate}`;
}
const res = await fetch(url);
const data = await res.json();
setMetrics(data.metrics);
setCompany(data.company);
// Set date range from API response (only on initial load)
if (data.dateRange && !dateRange) {
setDateRange(data.dateRange);
setSelectedStartDate(data.dateRange.minDate);
setSelectedEndDate(data.dateRange.maxDate);
}
} catch (error) {
console.error("Error fetching metrics:", error);
} finally {
setLoading(false);
}
},
[dateRange]
);
// Handle date range changes
const handleDateRangeChange = useCallback(
(startDate: string, endDate: string) => {
setSelectedStartDate(startDate);
setSelectedEndDate(endDate);
fetchMetrics(startDate, endDate);
},
[fetchMetrics]
);
useEffect(() => {
// Redirect if not authenticated
if (status === "unauthenticated") {
router.push("/login");
return; // Stop further execution in this effect
}
// Fetch metrics and company on mount if authenticated
if (status === "authenticated") {
fetchMetrics();
}
}, [status, router, fetchMetrics]); // Add fetchMetrics to dependency array
async function handleRefresh() {
if (isAuditor) return; // Prevent auditors from refreshing
try {
setRefreshing(true);
// Make sure we have a company ID to send
if (!company?.id) {
setRefreshing(false);
alert("Cannot refresh: Company ID is missing");
return;
}
const res = await fetch("/api/admin/refresh-sessions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ companyId: company.id }),
});
if (res.ok) {
// Refetch metrics
const metricsRes = await fetch("/api/dashboard/metrics");
const data = await metricsRes.json();
setMetrics(data.metrics);
} else {
const errorData = await res.json();
alert(`Failed to refresh sessions: ${errorData.error}`);
}
} finally {
setRefreshing(false);
}
}
// Calculate sentiment distribution
const getSentimentData = () => {
if (!metrics) return { positive: 0, neutral: 0, negative: 0 };
if (
metrics.sentimentPositiveCount !== undefined &&
metrics.sentimentNeutralCount !== undefined &&
metrics.sentimentNegativeCount !== undefined
) {
return {
positive: metrics.sentimentPositiveCount,
neutral: metrics.sentimentNeutralCount,
negative: metrics.sentimentNegativeCount,
};
}
const total = metrics.totalSessions || 1;
return {
positive: Math.round(total * 0.6),
neutral: Math.round(total * 0.3),
negative: Math.round(total * 0.1),
};
};
// Prepare token usage data
const getTokenData = () => {
if (!metrics || !metrics.tokensByDay) {
return { labels: [], values: [], costs: [] };
}
const days = Object.keys(metrics.tokensByDay).sort();
const labels = days.slice(-7);
const values = labels.map((day) => metrics.tokensByDay?.[day] || 0);
const costs = labels.map((day) => metrics.tokensCostByDay?.[day] || 0);
return { labels, values, costs };
};
// Show loading state while session status is being determined
if (status === "loading") {
return <div className="text-center py-10">Loading session...</div>;
}
// If unauthenticated and not redirected yet (should be handled by useEffect, but as a fallback)
if (status === "unauthenticated") {
return <div className="text-center py-10">Redirecting to login...</div>;
}
if (!metrics || !company) {
return <div className="text-center py-10">Loading dashboard...</div>;
}
// Function to prepare word cloud data from metrics.wordCloudData
const getWordCloudData = (): WordCloudWord[] => {
if (!metrics || !metrics.wordCloudData) return [];
return metrics.wordCloudData;
};
// Function to prepare country data for the map using actual metrics
const getCountryData = () => {
if (!metrics || !metrics.countries) return {};
// Convert the countries object from metrics to the format expected by GeographicMap
const result = Object.entries(metrics.countries).reduce(
(acc, [code, count]) => {
if (code && count) {
acc[code] = count;
}
return acc;
},
{} as Record<string, number>
);
return result;
};
// Function to prepare response time distribution data
const getResponseTimeData = () => {
const avgTime = metrics.avgResponseTime || 1.5;
const simulatedData: number[] = [];
for (let i = 0; i < 50; i++) {
const randomFactor = 0.5 + Math.random();
simulatedData.push(avgTime * randomFactor);
}
return simulatedData;
};
return (
<div className="space-y-8">
<WelcomeBanner companyName={company.name} />
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center bg-white p-6 rounded-2xl shadow-lg ring-1 ring-slate-200/50">
<div>
<h1 className="text-3xl font-bold text-slate-800">{company.name}</h1>
<p className="text-slate-500 mt-1">
Dashboard updated{" "}
<span className="font-medium text-slate-600">
{new Date(metrics.lastUpdated || Date.now()).toLocaleString()}
</span>
</p>
</div>
<div className="flex items-center gap-3 mt-4 sm:mt-0">
<button
className="bg-sky-600 text-white py-2 px-5 rounded-lg shadow hover:bg-sky-700 transition-colors disabled:opacity-60 disabled:cursor-not-allowed flex items-center text-sm font-medium"
onClick={handleRefresh}
disabled={refreshing || isAuditor}
>
{refreshing ? (
<>
<svg
className="animate-spin -ml-1 mr-2 h-4 w-4 text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
Refreshing...
</>
) : (
"Refresh Data"
)}
</button>
<button
className="bg-slate-100 text-slate-700 py-2 px-5 rounded-lg shadow hover:bg-slate-200 transition-colors flex items-center text-sm font-medium"
onClick={() => signOut({ callbackUrl: "/login" })}
>
Sign out
</button>
</div>
</div>
{/* Date Range Picker */}
{dateRange && (
<DateRangePicker
minDate={dateRange.minDate}
maxDate={dateRange.maxDate}
onDateRangeChange={handleDateRangeChange}
initialStartDate={selectedStartDate}
initialEndDate={selectedEndDate}
/>
)}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-7 gap-4">
<MetricCard
title="Total Sessions"
value={metrics.totalSessions}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M7 20l4-16m2 16l4-16M6 9h14M4 15h14"
/>
</svg>
}
trend={{
value: metrics.sessionTrend ?? 0,
isPositive: (metrics.sessionTrend ?? 0) >= 0,
}}
/>
<MetricCard
title="Unique Users"
value={metrics.uniqueUsers}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
/>
</svg>
}
trend={{
value: metrics.usersTrend ?? 0,
isPositive: (metrics.usersTrend ?? 0) >= 0,
}}
/>
<MetricCard
title="Avg. Session Time"
value={`${Math.round(metrics.avgSessionLength || 0)}s`}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
}
trend={{
value: metrics.avgSessionTimeTrend ?? 0,
isPositive: (metrics.avgSessionTimeTrend ?? 0) >= 0,
}}
/>
<MetricCard
title="Avg. Response Time"
value={`${metrics.avgResponseTime?.toFixed(1) || 0}s`}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M13 10V3L4 14h7v7l9-11h-7z"
/>
</svg>
}
trend={{
value: metrics.avgResponseTimeTrend ?? 0,
isPositive: (metrics.avgResponseTimeTrend ?? 0) <= 0, // Lower response time is better
}}
/>
<MetricCard
title="Avg. Daily Costs"
value={`${metrics.avgDailyCosts?.toFixed(4) || "0.0000"}`}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
}
/>
<MetricCard
title="Peak Usage Time"
value={metrics.peakUsageTime || "N/A"}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"
/>
</svg>
}
/>
<MetricCard
title="Resolved Chats"
value={`${metrics.resolvedChatsPercentage?.toFixed(1) || "0.0"}%`}
icon={
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth="1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
}
trend={{
value: metrics.resolvedChatsPercentage ?? 0,
isPositive: (metrics.resolvedChatsPercentage ?? 0) >= 80, // 80%+ resolution rate is good
}}
/>
</div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div className="bg-white p-6 rounded-xl shadow lg:col-span-2">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Sessions Over Time
</h3>
<SessionsLineChart sessionsPerDay={metrics.days} />
</div>
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Conversation Sentiment
</h3>
<DonutChart
data={{
labels: ["Positive", "Neutral", "Negative"],
values: [
getSentimentData().positive,
getSentimentData().neutral,
getSentimentData().negative,
],
colors: ["#1cad7c", "#a1a1a1", "#dc2626"],
}}
centerText={{
title: "Total",
value: metrics.totalSessions,
}}
/>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Sessions by Category
</h3>
<CategoriesBarChart categories={metrics.categories || {}} />
</div>
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Languages Used
</h3>
<LanguagePieChart languages={metrics.languages || {}} />
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Geographic Distribution
</h3>
<GeographicMap countries={getCountryData()} />
</div>
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Common Topics
</h3>
<div className="h-[300px]">
<WordCloud words={getWordCloudData()} width={500} height={400} />
</div>
</div>
</div>
{/* Top Questions Chart */}
<TopQuestionsChart data={metrics.topQuestions || []} />
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Response Time Distribution
</h3>
<ResponseTimeDistribution
data={getResponseTimeData()}
average={metrics.avgResponseTime || 0}
/>
</div>
<div className="bg-white p-6 rounded-xl shadow">
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-3 mb-4">
<h3 className="font-bold text-lg text-gray-800">
Token Usage & Costs
</h3>
<div className="flex flex-col sm:flex-row gap-2 sm:gap-4 w-full sm:w-auto">
<div className="text-sm bg-blue-50 text-blue-700 px-3 py-1 rounded-full flex items-center">
<span className="font-semibold mr-1">Total Tokens:</span>
{metrics.totalTokens?.toLocaleString() || 0}
</div>
<div className="text-sm bg-green-50 text-green-700 px-3 py-1 rounded-full flex items-center">
<span className="font-semibold mr-1">Total Cost:</span>
{metrics.totalTokensEur?.toFixed(4) || 0}
</div>
</div>
</div>
<TokenUsageChart tokenData={getTokenData()} />
</div>
</div>
);
}
// Our exported component
export default function DashboardPage() {
return <DashboardContent />;
}

View File

@ -1,440 +1,104 @@
"use client"; "use client";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { signOut, useSession } from "next-auth/react"; import { FC } from "react";
import { useRouter } from "next/navigation"; // Import useRouter
import {
SessionsLineChart,
CategoriesBarChart,
LanguagePieChart,
TokenUsageChart,
} from "../../components/Charts";
import DashboardSettings from "./settings";
import UserManagement from "./users";
import { Company, MetricsResult, WordCloudWord } from "../../lib/types"; // Added WordCloudWord
import MetricCard from "../../components/MetricCard";
import DonutChart from "../../components/DonutChart";
import WordCloud from "../../components/WordCloud";
import GeographicMap from "../../components/GeographicMap";
import ResponseTimeDistribution from "../../components/ResponseTimeDistribution";
import WelcomeBanner from "../../components/WelcomeBanner";
// Safely wrapped component with useSession const DashboardPage: FC = () => {
function DashboardContent() { const { data: session, status } = useSession();
const { data: session, status } = useSession(); // Add status from useSession const router = useRouter();
const router = useRouter(); // Initialize useRouter const [loading, setLoading] = useState(true);
const [metrics, setMetrics] = useState<MetricsResult | null>(null);
const [company, setCompany] = useState<Company | null>(null);
const [, setLoading] = useState<boolean>(false);
const [refreshing, setRefreshing] = useState<boolean>(false);
const isAdmin = session?.user?.role === "admin";
const isAuditor = session?.user?.role === "auditor";
useEffect(() => { useEffect(() => {
// Redirect if not authenticated // Once session is loaded, redirect appropriately
if (status === "unauthenticated") { if (status === "unauthenticated") {
router.push("/login"); router.push("/login");
return; // Stop further execution in this effect } else if (status === "authenticated") {
setLoading(false);
} }
}, [status, router]);
// Fetch metrics and company on mount if authenticated if (loading) {
if (status === "authenticated") { return (
const fetchData = async () => { <div className="flex items-center justify-center min-h-[40vh]">
setLoading(true); <div className="text-center">
const res = await fetch("/api/dashboard/metrics"); <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-sky-500 mx-auto mb-4"></div>
const data = await res.json(); <p className="text-lg text-gray-600">Loading dashboard...</p>
setMetrics(data.metrics); </div>
setCompany(data.company); </div>
setLoading(false);
};
fetchData();
}
}, [status, router]); // Add status and router to dependency array
async function handleRefresh() {
if (isAuditor) return; // Prevent auditors from refreshing
try {
setRefreshing(true);
// Make sure we have a company ID to send
if (!company?.id) {
setRefreshing(false);
alert("Cannot refresh: Company ID is missing");
return;
}
const res = await fetch("/api/admin/refresh-sessions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ companyId: company.id }),
});
if (res.ok) {
// Refetch metrics
const metricsRes = await fetch("/api/dashboard/metrics");
const data = await metricsRes.json();
setMetrics(data.metrics);
} else {
const errorData = await res.json();
alert(`Failed to refresh sessions: ${errorData.error}`);
}
} finally {
setRefreshing(false);
}
}
// Calculate sentiment distribution
const getSentimentData = () => {
if (!metrics) return { positive: 0, neutral: 0, negative: 0 };
if (
metrics.sentimentPositiveCount !== undefined &&
metrics.sentimentNeutralCount !== undefined &&
metrics.sentimentNegativeCount !== undefined
) {
return {
positive: metrics.sentimentPositiveCount,
neutral: metrics.sentimentNeutralCount,
negative: metrics.sentimentNegativeCount,
};
}
const total = metrics.totalSessions || 1;
return {
positive: Math.round(total * 0.6),
neutral: Math.round(total * 0.3),
negative: Math.round(total * 0.1),
};
};
// Prepare token usage data
const getTokenData = () => {
if (!metrics || !metrics.tokensByDay) {
return { labels: [], values: [], costs: [] };
}
const days = Object.keys(metrics.tokensByDay).sort();
const labels = days.slice(-7);
const values = labels.map((day) => metrics.tokensByDay?.[day] || 0);
const costs = labels.map((day) => metrics.tokensCostByDay?.[day] || 0);
return { labels, values, costs };
};
// Show loading state while session status is being determined
if (status === "loading") {
return <div className="text-center py-10">Loading session...</div>;
}
// If unauthenticated and not redirected yet (should be handled by useEffect, but as a fallback)
if (status === "unauthenticated") {
return <div className="text-center py-10">Redirecting to login...</div>;
}
if (!metrics || !company) {
return <div className="text-center py-10">Loading dashboard...</div>;
}
// Function to prepare word cloud data from metrics.wordCloudData
const getWordCloudData = (): WordCloudWord[] => {
if (!metrics || !metrics.wordCloudData) return [];
return metrics.wordCloudData;
};
// Function to prepare country data for the map using actual metrics
const getCountryData = () => {
if (!metrics || !metrics.countries) return {};
// Convert the countries object from metrics to the format expected by GeographicMap
const result = Object.entries(metrics.countries).reduce(
(acc, [code, count]) => {
if (code && count) {
acc[code] = count;
}
return acc;
},
{} as Record<string, number>
); );
}
return result;
};
// Function to prepare response time distribution data
const getResponseTimeData = () => {
const avgTime = metrics.avgResponseTime || 1.5;
const simulatedData: number[] = [];
for (let i = 0; i < 50; i++) {
const randomFactor = 0.5 + Math.random();
simulatedData.push(avgTime * randomFactor);
}
return simulatedData;
};
return ( return (
<div className="space-y-8"> <div className="space-y-6">
<WelcomeBanner companyName={company.name} /> <div className="bg-white rounded-xl shadow p-6">
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center bg-white p-6 rounded-2xl shadow-lg ring-1 ring-slate-200/50"> <h1 className="text-2xl font-bold mb-4">Dashboard</h1>
<div>
<h1 className="text-3xl font-bold text-slate-800">{company.name}</h1> <div className="grid sm:grid-cols-2 lg:grid-cols-3 gap-6">
<p className="text-slate-500 mt-1"> <div className="bg-gradient-to-br from-sky-50 to-sky-100 p-6 rounded-xl shadow-sm hover:shadow-md transition-shadow">
Dashboard updated{" "} <h2 className="text-lg font-semibold text-sky-700">Analytics</h2>
<span className="font-medium text-slate-600"> <p className="text-gray-600 mt-2 mb-4">
{new Date(metrics.lastUpdated || Date.now()).toLocaleString()} View your chat session metrics and analytics
</span> </p>
</p> <button
</div> onClick={() => router.push("/dashboard/overview")}
<div className="flex items-center gap-3 mt-4 sm:mt-0"> className="bg-sky-500 hover:bg-sky-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors"
<button
className="bg-sky-600 text-white py-2 px-5 rounded-lg shadow hover:bg-sky-700 transition-colors disabled:opacity-60 disabled:cursor-not-allowed flex items-center text-sm font-medium"
onClick={handleRefresh}
disabled={refreshing || isAuditor}
>
{refreshing ? (
<>
<svg
className="animate-spin -ml-1 mr-2 h-4 w-4 text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
Refreshing...
</>
) : (
<>
<svg
className="w-4 h-4 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
Refresh Data
</>
)}
</button>
<button
className="bg-slate-100 text-slate-700 py-2 px-5 rounded-lg shadow hover:bg-slate-200 transition-colors flex items-center text-sm font-medium"
onClick={() => signOut()}
>
<svg
className="w-4 h-4 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
> >
<path View Analytics
strokeLinecap="round" </button>
strokeLinejoin="round"
strokeWidth={2}
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"
/>
</svg>
Sign Out
</button>
</div>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
<MetricCard
title="Total Sessions"
value={metrics.totalSessions.toLocaleString()}
icon="💬"
variant="primary"
/>
<MetricCard
title="Avg Sessions/Day"
value={metrics.avgSessionsPerDay?.toFixed(1) || 0}
icon="📊"
trend={{ value: 5.2, label: "vs last week" }}
variant="success"
/>
<MetricCard
title="Avg Session Time"
value={
metrics.avgSessionLength
? `${metrics.avgSessionLength.toFixed(1)} min`
: "-"
}
icon="⏱️"
trend={{ value: -2.1, label: "vs last week", isPositive: false }}
/>
<MetricCard
title="Avg Response Time"
value={
metrics.avgResponseTime
? `${metrics.avgResponseTime.toFixed(2)}s`
: "-"
}
icon="⚡"
trend={{ value: -1.8, label: "vs last week", isPositive: true }}
variant="success"
/>
</div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div className="bg-white p-6 rounded-xl shadow lg:col-span-1">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Sentiment Distribution
</h3>
<DonutChart
data={{
labels: ["Positive", "Neutral", "Negative"],
values: [
getSentimentData().positive,
getSentimentData().neutral,
getSentimentData().negative,
],
colors: [
"rgba(34, 197, 94, 0.8)",
"rgba(249, 115, 22, 0.8)",
"rgba(239, 68, 68, 0.8)",
],
}}
centerText={{
title: "Overall",
value: `${((getSentimentData().positive / (getSentimentData().positive + getSentimentData().neutral + getSentimentData().negative)) * 100).toFixed(0)}%`,
}}
/>
</div>
<div className="bg-white p-6 rounded-xl shadow lg:col-span-2">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Case Handling Statistics
</h3>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<MetricCard
title="Escalation Rate"
value={`${(((metrics.escalatedCount || 0) / (metrics.totalSessions || 1)) * 100).toFixed(1)}%`}
description={`${metrics.escalatedCount || 0} sessions escalated`}
icon="⚠️"
variant={
(metrics.escalatedCount || 0) > metrics.totalSessions * 0.1
? "warning"
: "success"
}
/>
<MetricCard
title="HR Forwarded"
value={`${(((metrics.forwardedCount || 0) / (metrics.totalSessions || 1)) * 100).toFixed(1)}%`}
description={`${metrics.forwardedCount || 0} sessions forwarded to HR`}
icon="👥"
variant={
(metrics.forwardedCount || 0) > metrics.totalSessions * 0.05
? "warning"
: "default"
}
/>
<MetricCard
title="Resolved Rate"
value={`${(((metrics.totalSessions - (metrics.escalatedCount || 0) - (metrics.forwardedCount || 0)) / metrics.totalSessions) * 100).toFixed(1)}%`}
description={`${metrics.totalSessions - (metrics.escalatedCount || 0) - (metrics.forwardedCount || 0)} sessions resolved`}
icon="✅"
variant="success"
/>
</div> </div>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Sessions by Day
</h3>
<SessionsLineChart sessionsPerDay={metrics.days || {}} />
</div>
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Top Categories
</h3>
<CategoriesBarChart categories={metrics.categories || {}} />
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="bg-white p-6 rounded-xl shadow overflow-hidden">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Transcript Word Cloud
</h3>
<WordCloud words={getWordCloudData()} width={400} height={300} />
</div>
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Geographic Distribution
</h3>
<GeographicMap countries={getCountryData()} height={300} />
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">
Response Time Distribution
</h3>
<ResponseTimeDistribution
responseTimes={getResponseTimeData()}
targetResponseTime={2}
/>
</div>
<div className="bg-white p-6 rounded-xl shadow">
<h3 className="font-bold text-lg text-gray-800 mb-4">Languages</h3>
<LanguagePieChart languages={metrics.languages || {}} />
</div>
</div>
<div className="bg-white p-6 rounded-xl shadow">
<div className="flex justify-between items-center mb-4">
<h3 className="font-bold text-lg text-gray-800">
Token Usage & Costs
</h3>
<div className="flex gap-4">
<div className="text-sm bg-blue-50 text-blue-700 px-3 py-1 rounded-full flex items-center">
<span className="font-semibold mr-1">Total Tokens:</span>
{metrics.totalTokens?.toLocaleString() || 0}
</div>
<div className="text-sm bg-green-50 text-green-700 px-3 py-1 rounded-full flex items-center">
<span className="font-semibold mr-1">Total Cost:</span>
{metrics.totalTokensEur?.toFixed(4) || 0}
</div>
</div>
</div>
<TokenUsageChart tokenData={getTokenData()} />
</div>
{isAdmin && (
<>
<DashboardSettings company={company} session={session} />
<UserManagement session={session} />
</>
)}
</div>
);
}
// Our exported component <div className="bg-gradient-to-br from-emerald-50 to-emerald-100 p-6 rounded-xl shadow-sm hover:shadow-md transition-shadow">
export default function DashboardPage() { <h2 className="text-lg font-semibold text-emerald-700">Sessions</h2>
return ( <p className="text-gray-600 mt-2 mb-4">
<div className="min-h-screen bg-gradient-to-br from-slate-50 to-sky-100 p-4 md:p-6"> Browse and analyze conversation sessions
<div className="max-w-7xl mx-auto"> </p>
<DashboardContent /> <button
onClick={() => router.push("/dashboard/sessions")}
className="bg-emerald-500 hover:bg-emerald-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors"
>
View Sessions
</button>
</div>
{session?.user?.role === "admin" && (
<div className="bg-gradient-to-br from-purple-50 to-purple-100 p-6 rounded-xl shadow-sm hover:shadow-md transition-shadow">
<h2 className="text-lg font-semibold text-purple-700">
Company Settings
</h2>
<p className="text-gray-600 mt-2 mb-4">
Configure company settings and integrations
</p>
<button
onClick={() => router.push("/dashboard/company")}
className="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors"
>
Manage Settings
</button>
</div>
)}
{session?.user?.role === "admin" && (
<div className="bg-gradient-to-br from-amber-50 to-amber-100 p-6 rounded-xl shadow-sm hover:shadow-md transition-shadow">
<h2 className="text-lg font-semibold text-amber-700">
User Management
</h2>
<p className="text-gray-600 mt-2 mb-4">
Invite and manage user accounts
</p>
<button
onClick={() => router.push("/dashboard/users")}
className="bg-amber-500 hover:bg-amber-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors"
>
Manage Users
</button>
</div>
)}
</div>
</div> </div>
</div> </div>
); );
} };
export default DashboardPage;

View File

@ -4,7 +4,8 @@ import { useEffect, useState } from "react";
import { useParams, useRouter } from "next/navigation"; // Import useRouter import { useParams, useRouter } from "next/navigation"; // Import useRouter
import { useSession } from "next-auth/react"; // Import useSession import { useSession } from "next-auth/react"; // Import useSession
import SessionDetails from "../../../../components/SessionDetails"; import SessionDetails from "../../../../components/SessionDetails";
import TranscriptViewer from "../../../../components/TranscriptViewer";
import MessageViewer from "../../../../components/MessageViewer";
import { ChatSession } from "../../../../lib/types"; import { ChatSession } from "../../../../lib/types";
import Link from "next/link"; import Link from "next/link";
@ -25,7 +26,7 @@ export default function SessionViewPage() {
if (status === "authenticated" && id) { if (status === "authenticated" && id) {
const fetchSession = async () => { const fetchSession = async () => {
if (!session) setLoading(true); setLoading(true); // Always set loading before fetch
setError(null); setError(null);
try { try {
const response = await fetch(`/api/dashboard/session/${id}`); const response = await fetch(`/api/dashboard/session/${id}`);
@ -52,7 +53,7 @@ export default function SessionViewPage() {
setError("Session ID is missing."); setError("Session ID is missing.");
setLoading(false); setLoading(false);
} }
}, [id, status, router, session]); }, [id, status, router]); // session removed from dependencies
if (status === "loading") { if (status === "loading") {
return ( return (
@ -136,30 +137,26 @@ export default function SessionViewPage() {
<div> <div>
<SessionDetails session={session} /> <SessionDetails session={session} />
</div> </div>
{session.transcriptContent &&
session.transcriptContent.trim() !== "" ? ( {/* Show parsed messages if available */}
<div className="mt-0"> {session.messages && session.messages.length > 0 && (
<TranscriptViewer <div>
transcriptContent={session.transcriptContent} <MessageViewer messages={session.messages} />
transcriptUrl={session.fullTranscriptUrl}
/>
</div> </div>
) : ( )}
{/* Show transcript URL if available */}
{session.fullTranscriptUrl && (
<div className="bg-white p-4 rounded-lg shadow"> <div className="bg-white p-4 rounded-lg shadow">
<h3 className="font-bold text-lg mb-3">Transcript</h3> <h3 className="font-bold text-lg mb-3">Source Transcript</h3>
<p className="text-gray-600"> <a
No transcript content available for this session. href={session.fullTranscriptUrl}
</p> target="_blank"
{session.fullTranscriptUrl && ( rel="noopener noreferrer"
<a className="text-sky-600 hover:underline"
href={session.fullTranscriptUrl} >
target="_blank" View Original Transcript
rel="noopener noreferrer" </a>
className="text-sky-600 hover:underline mt-2 inline-block"
>
View Source Transcript URL
</a>
)}
</div> </div>
)} )}
</div> </div>

View File

@ -40,7 +40,7 @@ export default function SessionsPage() {
// Pagination states // Pagination states
const [currentPage, setCurrentPage] = useState(1); const [currentPage, setCurrentPage] = useState(1);
const [totalPages, setTotalPages] = useState(0); const [totalPages, setTotalPages] = useState(0);
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
const [pageSize, setPageSize] = useState(10); // Or make this configurable const [pageSize, setPageSize] = useState(10); // Or make this configurable
useEffect(() => { useEffect(() => {
@ -283,8 +283,12 @@ export default function SessionsPage() {
Session ID: {session.sessionId || session.id} Session ID: {session.sessionId || session.id}
</h2> </h2>
<p className="text-sm text-gray-500 mb-1"> <p className="text-sm text-gray-500 mb-1">
Start Time: {new Date(session.startTime).toLocaleString()} Start Time{/* (Local) */}:{" "}
{new Date(session.startTime).toLocaleString()}
</p> </p>
{/* <p className="text-xs text-gray-400 mb-1">
Start Time (Raw API): {session.startTime.toString()}
</p> */}
{session.category && ( {session.category && (
<p className="text-sm text-gray-700"> <p className="text-sm text-gray-700">
Category:{" "} Category:{" "}

View File

@ -0,0 +1,212 @@
"use client";
import { useState, useEffect } from "react";
import { useSession } from "next-auth/react";
interface UserItem {
id: string;
email: string;
role: string;
}
export default function UserManagementPage() {
const { data: session, status } = useSession();
const [users, setUsers] = useState<UserItem[]>([]);
const [email, setEmail] = useState<string>("");
const [role, setRole] = useState<string>("user");
const [message, setMessage] = useState<string>("");
const [loading, setLoading] = useState(true);
useEffect(() => {
if (status === "authenticated") {
fetchUsers();
}
}, [status]);
const fetchUsers = async () => {
setLoading(true);
try {
const res = await fetch("/api/dashboard/users");
const data = await res.json();
setUsers(data.users);
} catch (error) {
console.error("Failed to fetch users:", error);
setMessage("Failed to load users.");
} finally {
setLoading(false);
}
};
async function inviteUser() {
setMessage("");
try {
const res = await fetch("/api/dashboard/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, role }),
});
if (res.ok) {
setMessage("User invited successfully!");
setEmail(""); // Clear the form
// Refresh the user list
fetchUsers();
} else {
const error = await res.json();
setMessage(
`Failed to invite user: ${error.message || "Unknown error"}`
);
}
} catch (error) {
setMessage("Failed to invite user. Please try again.");
console.error("Error inviting user:", error);
}
}
// Loading state
if (loading) {
return <div className="text-center py-10">Loading users...</div>;
}
// Check for admin access
if (session?.user?.role !== "admin") {
return (
<div className="text-center py-10 bg-white rounded-xl shadow p-6">
<h2 className="font-bold text-xl text-red-600 mb-2">Access Denied</h2>
<p>You don&apos;t have permission to view user management.</p>
</div>
);
}
return (
<div className="space-y-6">
<div className="bg-white p-6 rounded-xl shadow">
<h1 className="text-2xl font-bold text-gray-800 mb-6">
User Management
</h1>
{message && (
<div
className={`p-4 rounded mb-6 ${message.includes("Failed") ? "bg-red-100 text-red-700" : "bg-green-100 text-green-700"}`}
>
{message}
</div>
)}
<div className="mb-8">
<h2 className="text-lg font-semibold mb-4">Invite New User</h2>
<form
className="grid grid-cols-1 sm:grid-cols-3 gap-4 items-end"
onSubmit={(e) => {
e.preventDefault();
inviteUser();
}}
autoComplete="off" // Disable autofill for the form
>
<div className="grid gap-2">
<label className="font-medium text-gray-700">Email</label>
<input
type="email"
className="border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-sky-500"
placeholder="user@example.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
autoComplete="off" // Disable autofill for this input
/>
</div>
<div className="grid gap-2">
<label className="font-medium text-gray-700">Role</label>
<select
className="border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-sky-500 bg-white"
value={role}
onChange={(e) => setRole(e.target.value)}
>
<option value="user">User</option>
<option value="admin">Admin</option>
<option value="auditor">Auditor</option>
</select>
</div>
<button
type="submit"
className="bg-sky-600 hover:bg-sky-700 text-white py-2 px-4 rounded-lg shadow transition-colors"
>
Invite User
</button>
</form>
</div>
<div>
<h2 className="text-lg font-semibold mb-4">Current Users</h2>
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th
scope="col"
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
>
Email
</th>
<th
scope="col"
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
>
Role
</th>
<th
scope="col"
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
>
Actions
</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{users.length === 0 ? (
<tr>
<td
colSpan={3}
className="px-6 py-4 text-center text-sm text-gray-500"
>
No users found
</td>
</tr>
) : (
users.map((user) => (
<tr key={user.id}>
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{user.email}
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<span
className={`px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${
user.role === "admin"
? "bg-purple-100 text-purple-800"
: user.role === "auditor"
? "bg-blue-100 text-blue-800"
: "bg-green-100 text-green-800"
}`}
>
{user.role}
</span>
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{/* For future: Add actions like edit, delete, etc. */}
<span className="text-gray-400">
No actions available
</span>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
</div>
</div>
</div>
);
}

View File

@ -1,11 +1,120 @@
body {
font-family: system-ui, sans-serif;
background: #f3f4f6;
}
input,
button {
font-family: inherit;
}
@import "tailwindcss"; @import "tailwindcss";
@import "tw-animate-css";
@custom-variant dark (&:is(.dark *));
@theme inline {
--radius-sm: calc(var(--radius) - 4px);
--radius-md: calc(var(--radius) - 2px);
--radius-lg: var(--radius);
--radius-xl: calc(var(--radius) + 4px);
--color-background: var(--background);
--color-foreground: var(--foreground);
--color-card: var(--card);
--color-card-foreground: var(--card-foreground);
--color-popover: var(--popover);
--color-popover-foreground: var(--popover-foreground);
--color-primary: var(--primary);
--color-primary-foreground: var(--primary-foreground);
--color-secondary: var(--secondary);
--color-secondary-foreground: var(--secondary-foreground);
--color-muted: var(--muted);
--color-muted-foreground: var(--muted-foreground);
--color-accent: var(--accent);
--color-accent-foreground: var(--accent-foreground);
--color-destructive: var(--destructive);
--color-border: var(--border);
--color-input: var(--input);
--color-ring: var(--ring);
--color-chart-1: var(--chart-1);
--color-chart-2: var(--chart-2);
--color-chart-3: var(--chart-3);
--color-chart-4: var(--chart-4);
--color-chart-5: var(--chart-5);
--color-sidebar: var(--sidebar);
--color-sidebar-foreground: var(--sidebar-foreground);
--color-sidebar-primary: var(--sidebar-primary);
--color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
--color-sidebar-accent: var(--sidebar-accent);
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
--color-sidebar-border: var(--sidebar-border);
--color-sidebar-ring: var(--sidebar-ring);
}
:root {
--radius: 0.625rem;
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
--card: oklch(1 0 0);
--card-foreground: oklch(0.145 0 0);
--popover: oklch(1 0 0);
--popover-foreground: oklch(0.145 0 0);
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);
--secondary: oklch(0.97 0 0);
--secondary-foreground: oklch(0.205 0 0);
--muted: oklch(0.97 0 0);
--muted-foreground: oklch(0.556 0 0);
--accent: oklch(0.97 0 0);
--accent-foreground: oklch(0.205 0 0);
--destructive: oklch(0.577 0.245 27.325);
--border: oklch(0.922 0 0);
--input: oklch(0.922 0 0);
--ring: oklch(0.708 0 0);
--chart-1: oklch(0.646 0.222 41.116);
--chart-2: oklch(0.6 0.118 184.704);
--chart-3: oklch(0.398 0.07 227.392);
--chart-4: oklch(0.828 0.189 84.429);
--chart-5: oklch(0.769 0.188 70.08);
--sidebar: oklch(0.985 0 0);
--sidebar-foreground: oklch(0.145 0 0);
--sidebar-primary: oklch(0.205 0 0);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.97 0 0);
--sidebar-accent-foreground: oklch(0.205 0 0);
--sidebar-border: oklch(0.922 0 0);
--sidebar-ring: oklch(0.708 0 0);
}
.dark {
--background: oklch(0.145 0 0);
--foreground: oklch(0.985 0 0);
--card: oklch(0.205 0 0);
--card-foreground: oklch(0.985 0 0);
--popover: oklch(0.205 0 0);
--popover-foreground: oklch(0.985 0 0);
--primary: oklch(0.922 0 0);
--primary-foreground: oklch(0.205 0 0);
--secondary: oklch(0.269 0 0);
--secondary-foreground: oklch(0.985 0 0);
--muted: oklch(0.269 0 0);
--muted-foreground: oklch(0.708 0 0);
--accent: oklch(0.269 0 0);
--accent-foreground: oklch(0.985 0 0);
--destructive: oklch(0.704 0.191 22.216);
--border: oklch(1 0 0 / 10%);
--input: oklch(1 0 0 / 15%);
--ring: oklch(0.556 0 0);
--chart-1: oklch(0.488 0.243 264.376);
--chart-2: oklch(0.696 0.17 162.48);
--chart-3: oklch(0.769 0.188 70.08);
--chart-4: oklch(0.627 0.265 303.9);
--chart-5: oklch(0.645 0.246 16.439);
--sidebar: oklch(0.205 0 0);
--sidebar-foreground: oklch(0.985 0 0);
--sidebar-primary: oklch(0.488 0.243 264.376);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.269 0 0);
--sidebar-accent-foreground: oklch(0.985 0 0);
--sidebar-border: oklch(1 0 0 / 10%);
--sidebar-ring: oklch(0.556 0 0);
}
@layer base {
* {
@apply border-border outline-ring/50;
}
body {
@apply bg-background text-foreground;
}
}

View File

@ -21,9 +21,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
return ( return (
<html lang="en"> <html lang="en">
<body className="bg-gray-100 min-h-screen font-sans"> <body className="bg-gray-100 min-h-screen font-sans">
<Providers> <Providers>{children}</Providers>
<div className="max-w-5xl mx-auto py-8">{children}</div>
</Providers>
</body> </body>
</html> </html>
); );

View File

@ -1,6 +1,6 @@
import { getServerSession } from "next-auth"; import { getServerSession } from "next-auth";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { authOptions } from "../pages/api/auth/[...nextauth]"; import { authOptions } from "./api/auth/[...nextauth]/route";
export default async function HomePage() { export default async function HomePage() {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);

21
components.json Normal file
View File

@ -0,0 +1,21 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": true,
"tsx": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "app/globals.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
},
"iconLibrary": "lucide"
}

View File

@ -0,0 +1,155 @@
"use client";
import { useState, useEffect } from "react";
interface DateRangePickerProps {
minDate: string;
maxDate: string;
onDateRangeChange: (startDate: string, endDate: string) => void;
initialStartDate?: string;
initialEndDate?: string;
}
export default function DateRangePicker({
minDate,
maxDate,
onDateRangeChange,
initialStartDate,
initialEndDate,
}: DateRangePickerProps) {
const [startDate, setStartDate] = useState(initialStartDate || minDate);
const [endDate, setEndDate] = useState(initialEndDate || maxDate);
useEffect(() => {
// Notify parent component when dates change
onDateRangeChange(startDate, endDate);
}, [startDate, endDate, onDateRangeChange]);
const handleStartDateChange = (newStartDate: string) => {
// Ensure start date is not before min date
if (newStartDate < minDate) {
setStartDate(minDate);
return;
}
// Ensure start date is not after end date
if (newStartDate > endDate) {
setEndDate(newStartDate);
}
setStartDate(newStartDate);
};
const handleEndDateChange = (newEndDate: string) => {
// Ensure end date is not after max date
if (newEndDate > maxDate) {
setEndDate(maxDate);
return;
}
// Ensure end date is not before start date
if (newEndDate < startDate) {
setStartDate(newEndDate);
}
setEndDate(newEndDate);
};
const resetToFullRange = () => {
setStartDate(minDate);
setEndDate(maxDate);
};
const setLast30Days = () => {
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const thirtyDaysAgoStr = thirtyDaysAgo.toISOString().split("T")[0];
// Use the later of 30 days ago or minDate
const newStartDate =
thirtyDaysAgoStr > minDate ? thirtyDaysAgoStr : minDate;
setStartDate(newStartDate);
setEndDate(maxDate);
};
const setLast7Days = () => {
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
const sevenDaysAgoStr = sevenDaysAgo.toISOString().split("T")[0];
// Use the later of 7 days ago or minDate
const newStartDate = sevenDaysAgoStr > minDate ? sevenDaysAgoStr : minDate;
setStartDate(newStartDate);
setEndDate(maxDate);
};
return (
<div className="bg-white p-4 rounded-lg shadow-sm border border-gray-200">
<div className="flex flex-col sm:flex-row gap-4 items-start sm:items-center">
<div className="flex flex-col sm:flex-row gap-3 items-start sm:items-center">
<label className="text-sm font-medium text-gray-700 whitespace-nowrap">
Date Range:
</label>
<div className="flex flex-col sm:flex-row gap-2 items-start sm:items-center">
<div className="flex items-center gap-2">
<label htmlFor="start-date" className="text-sm text-gray-600">
From:
</label>
<input
id="start-date"
type="date"
value={startDate}
min={minDate}
max={maxDate}
onChange={(e) => handleStartDateChange(e.target.value)}
className="px-3 py-1.5 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-sky-500 focus:border-sky-500"
/>
</div>
<div className="flex items-center gap-2">
<label htmlFor="end-date" className="text-sm text-gray-600">
To:
</label>
<input
id="end-date"
type="date"
value={endDate}
min={minDate}
max={maxDate}
onChange={(e) => handleEndDateChange(e.target.value)}
className="px-3 py-1.5 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-sky-500 focus:border-sky-500"
/>
</div>
</div>
</div>
<div className="flex flex-wrap gap-2">
<button
onClick={setLast7Days}
className="px-3 py-1.5 text-xs font-medium text-sky-600 bg-sky-50 border border-sky-200 rounded-md hover:bg-sky-100 transition-colors"
>
Last 7 days
</button>
<button
onClick={setLast30Days}
className="px-3 py-1.5 text-xs font-medium text-sky-600 bg-sky-50 border border-sky-200 rounded-md hover:bg-sky-100 transition-colors"
>
Last 30 days
</button>
<button
onClick={resetToFullRange}
className="px-3 py-1.5 text-xs font-medium text-gray-600 bg-gray-50 border border-gray-200 rounded-md hover:bg-gray-100 transition-colors"
>
All time
</button>
</div>
</div>
<div className="mt-2 text-xs text-gray-500">
Available data: {new Date(minDate).toLocaleDateString()} -{" "}
{new Date(maxDate).toLocaleDateString()}
</div>
</div>
);
}

View File

@ -1,7 +1,7 @@
"use client"; "use client";
import { useRef, useEffect } from "react"; import { useRef, useEffect } from "react";
import Chart from "chart.js/auto"; import Chart, { Point, BubbleDataPoint } from "chart.js/auto";
interface DonutChartProps { interface DonutChartProps {
data: { data: {
@ -77,9 +77,24 @@ export default function DonutChart({ data, centerText }: DonutChartProps) {
const label = context.label || ""; const label = context.label || "";
const value = context.formattedValue; const value = context.formattedValue;
const total = context.chart.data.datasets[0].data.reduce( const total = context.chart.data.datasets[0].data.reduce(
(a: number, b: any) => a + (typeof b === "number" ? b : 0), (
a: number,
b:
| number
| Point
| [number, number]
| BubbleDataPoint
| null
) => {
if (typeof b === "number") {
return a + b;
}
// Handle other types like Point, [number, number], BubbleDataPoint if necessary
// For now, we'll assume they don't contribute to the sum or are handled elsewhere
return a;
},
0 0
); ) as number;
const percentage = Math.round((context.parsed * 100) / total); const percentage = Math.round((context.parsed * 100) / total);
return `${label}: ${value} (${percentage}%)`; return `${label}: ${value} (${percentage}%)`;
}, },
@ -91,7 +106,7 @@ export default function DonutChart({ data, centerText }: DonutChartProps) {
? [ ? [
{ {
id: "centerText", id: "centerText",
beforeDraw: function (chart: any) { beforeDraw: function (chart: Chart<"doughnut">) {
const height = chart.height; const height = chart.height;
const ctx = chart.ctx; const ctx = chart.ctx;
ctx.restore(); ctx.restore();

View File

@ -3,7 +3,7 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
import "leaflet/dist/leaflet.css"; import "leaflet/dist/leaflet.css";
import countryLookup from "country-code-lookup"; import * as countryCoder from "@rapideditor/country-coder";
// Define types for country data // Define types for country data
interface CountryData { interface CountryData {
@ -18,36 +18,41 @@ interface GeographicMapProps {
height?: number; // Optional height for the container height?: number; // Optional height for the container
} }
// Get country coordinates from the country-code-lookup package // Get country coordinates from the @rapideditor/country-coder package
const getCountryCoordinates = (): Record<string, [number, number]> => { const getCountryCoordinates = (): Record<string, [number, number]> => {
// Initialize with some fallback coordinates for common countries that might be missing // Initialize with some fallback coordinates for common countries
const coordinates: Record<string, [number, number]> = { const coordinates: Record<string, [number, number]> = {
// These are just in case the lookup fails for common countries
US: [37.0902, -95.7129], US: [37.0902, -95.7129],
GB: [55.3781, -3.436], GB: [55.3781, -3.436],
BA: [43.9159, 17.6791], BA: [43.9159, 17.6791],
NL: [52.1326, 5.2913],
DE: [51.1657, 10.4515],
FR: [46.6034, 1.8883],
IT: [41.8719, 12.5674],
ES: [40.4637, -3.7492],
CA: [56.1304, -106.3468],
PL: [51.9194, 19.1451],
SE: [60.1282, 18.6435],
NO: [60.472, 8.4689],
FI: [61.9241, 25.7482],
CH: [46.8182, 8.2275],
AT: [47.5162, 14.5501],
BE: [50.8503, 4.3517],
DK: [56.2639, 9.5018],
CZ: [49.8175, 15.473],
HU: [47.1625, 19.5033],
PT: [39.3999, -8.2245],
GR: [39.0742, 21.8243],
RO: [45.9432, 24.9668],
IE: [53.4129, -8.2439],
BG: [42.7339, 25.4858],
HR: [45.1, 15.2],
SK: [48.669, 19.699],
SI: [46.1512, 14.9955],
}; };
// This function now primarily returns fallbacks.
try { // The actual fetching using @rapideditor/country-coder will be in the component's useEffect.
// Get all countries from the package return coordinates;
const allCountries = countryLookup.countries;
// Map through all countries and extract coordinates
allCountries.forEach((country) => {
if (country.iso2 && country.latitude && country.longitude) {
coordinates[country.iso2] = [
parseFloat(country.latitude),
parseFloat(country.longitude),
];
}
});
return coordinates;
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error loading country coordinates:", error);
return coordinates;
}
}; };
// Load coordinates once when module is imported // Load coordinates once when module is imported
@ -79,44 +84,68 @@ export default function GeographicMap({
// Process country data when client is ready and dependencies change // Process country data when client is ready and dependencies change
useEffect(() => { useEffect(() => {
if (!isClient) return; if (!isClient || !countries) return;
try { try {
// Generate CountryData array for the Map component // Generate CountryData array for the Map component
const data: CountryData[] = Object.entries(countries) const data: CountryData[] = Object.entries(countries || {})
// Only include countries with known coordinates .map(([code, count]) => {
.filter(([code]) => { let countryCoords: [number, number] | undefined =
// If no coordinates found, log to help with debugging countryCoordinates[code] || DEFAULT_COORDINATES[code];
if (!countryCoordinates[code] && !DEFAULT_COORDINATES[code]) {
// eslint-disable-next-line no-console if (!countryCoords) {
console.warn(`Missing coordinates for country code: ${code}`); const feature = countryCoder.feature(code);
return false; if (feature && feature.geometry) {
} if (feature.geometry.type === "Point") {
return true; const [lon, lat] = feature.geometry.coordinates;
}) countryCoords = [lat, lon]; // Leaflet expects [lat, lon]
.map(([code, count]) => ({ } else if (
code, feature.geometry.type === "Polygon" &&
count, feature.geometry.coordinates &&
coordinates: countryCoordinates[code] || feature.geometry.coordinates[0] &&
DEFAULT_COORDINATES[code] || [0, 0], feature.geometry.coordinates[0][0]
})); ) {
// For Polygons, use the first coordinate of the first ring as a fallback representative point
const [lon, lat] = feature.geometry.coordinates[0][0];
countryCoords = [lat, lon]; // Leaflet expects [lat, lon]
} else if (
feature.geometry.type === "MultiPolygon" &&
feature.geometry.coordinates &&
feature.geometry.coordinates[0] &&
feature.geometry.coordinates[0][0] &&
feature.geometry.coordinates[0][0][0]
) {
// For MultiPolygons, use the first coordinate of the first ring of the first polygon
const [lon, lat] = feature.geometry.coordinates[0][0][0];
countryCoords = [lat, lon]; // Leaflet expects [lat, lon]
}
}
}
if (countryCoords) {
return {
code,
count,
coordinates: countryCoords,
};
}
return null; // Skip if no coordinates found
})
.filter((item): item is CountryData => item !== null);
// Log for debugging
// eslint-disable-next-line no-console
console.log( console.log(
`Found ${data.length} countries with coordinates out of ${Object.keys(countries).length} total countries` `Found ${data.length} countries with coordinates out of ${Object.keys(countries).length} total countries`
); );
setCountryData(data); setCountryData(data);
} catch (error) { } catch (error) {
// eslint-disable-next-line no-console
console.error("Error processing geographic data:", error); console.error("Error processing geographic data:", error);
setCountryData([]); setCountryData([]);
} }
}, [countries, countryCoordinates, isClient]); }, [countries, countryCoordinates, isClient]);
// Find the max count for scaling circles - handle empty countries object // Find the max count for scaling circles - handle empty or null countries object
const countryValues = Object.values(countries); const countryValues = countries ? Object.values(countries) : [];
const maxCount = countryValues.length > 0 ? Math.max(...countryValues, 1) : 1; const maxCount = countryValues.length > 0 ? Math.max(...countryValues, 1) : 1;
// Show loading state during SSR or until client-side rendering takes over // Show loading state during SSR or until client-side rendering takes over

View File

@ -0,0 +1,76 @@
"use client";
import { Message } from "../lib/types";
interface MessageViewerProps {
messages: Message[];
}
/**
* Component to display parsed messages in a chat-like format
*/
export default function MessageViewer({ messages }: MessageViewerProps) {
if (!messages || messages.length === 0) {
return (
<div className="bg-white p-4 rounded-lg shadow">
<h3 className="font-bold text-lg mb-3">Conversation</h3>
<p className="text-gray-500 italic">No parsed messages available</p>
</div>
);
}
return (
<div className="bg-white p-4 rounded-lg shadow">
<h3 className="font-bold text-lg mb-3">
Conversation ({messages.length} messages)
</h3>
<div className="space-y-3 max-h-96 overflow-y-auto">
{messages.map((message) => (
<div
key={message.id}
className={`flex ${
message.role.toLowerCase() === "user"
? "justify-end"
: "justify-start"
}`}
>
<div
className={`max-w-xs lg:max-w-md px-4 py-2 rounded-lg ${
message.role.toLowerCase() === "user"
? "bg-blue-500 text-white"
: message.role.toLowerCase() === "assistant"
? "bg-gray-200 text-gray-800"
: "bg-yellow-100 text-yellow-800"
}`}
>
<div className="flex items-center justify-between mb-1">
<span className="text-xs font-medium opacity-75 mr-2">
{message.role}
</span>
<span className="text-xs opacity-75 ml-2">
{new Date(message.timestamp).toLocaleTimeString()}
</span>
</div>
<div className="text-sm whitespace-pre-wrap">
{message.content}
</div>
</div>
</div>
))}
</div>
<div className="mt-4 pt-3 border-t text-sm text-gray-500">
<div className="flex justify-between">
<span>
First message: {new Date(messages[0].timestamp).toLocaleString()}
</span>
<span>
Last message:{" "}
{new Date(messages[messages.length - 1].timestamp).toLocaleString()}
</span>
</div>
</div>
</div>
);
}

View File

@ -4,7 +4,7 @@ interface MetricCardProps {
title: string; title: string;
value: string | number | null | undefined; value: string | number | null | undefined;
description?: string; description?: string;
icon?: string; icon?: React.ReactNode;
trend?: { trend?: {
value: number; value: number;
label?: string; label?: string;
@ -67,9 +67,6 @@ export default function MetricCard({
> >
{trend.isPositive !== false ? "↑" : "↓"}{" "} {trend.isPositive !== false ? "↑" : "↓"}{" "}
{Math.abs(trend.value).toFixed(1)}% {Math.abs(trend.value).toFixed(1)}%
{trend.label && (
<span className="text-gray-500 ml-1">{trend.label}</span>
)}
</span> </span>
)} )}
</div> </div>

View File

@ -7,28 +7,30 @@ import annotationPlugin from "chartjs-plugin-annotation";
Chart.register(annotationPlugin); Chart.register(annotationPlugin);
interface ResponseTimeDistributionProps { interface ResponseTimeDistributionProps {
responseTimes: number[]; data: number[];
average: number;
targetResponseTime?: number; targetResponseTime?: number;
} }
export default function ResponseTimeDistribution({ export default function ResponseTimeDistribution({
responseTimes, data,
average,
targetResponseTime, targetResponseTime,
}: ResponseTimeDistributionProps) { }: ResponseTimeDistributionProps) {
const ref = useRef<HTMLCanvasElement | null>(null); const ref = useRef<HTMLCanvasElement | null>(null);
useEffect(() => { useEffect(() => {
if (!ref.current || !responseTimes.length) return; if (!ref.current || !data || !data.length) return;
const ctx = ref.current.getContext("2d"); const ctx = ref.current.getContext("2d");
if (!ctx) return; if (!ctx) return;
// Create bins for the histogram (0-1s, 1-2s, 2-3s, etc.) // Create bins for the histogram (0-1s, 1-2s, 2-3s, etc.)
const maxTime = Math.ceil(Math.max(...responseTimes)); const maxTime = Math.ceil(Math.max(...data));
const bins = Array(Math.min(maxTime + 1, 10)).fill(0); const bins = Array(Math.min(maxTime + 1, 10)).fill(0);
// Count responses in each bin // Count responses in each bin
responseTimes.forEach((time) => { data.forEach((time) => {
const binIndex = Math.min(Math.floor(time), bins.length - 1); const binIndex = Math.min(Math.floor(time), bins.length - 1);
bins[binIndex]++; bins[binIndex]++;
}); });
@ -63,26 +65,40 @@ export default function ResponseTimeDistribution({
responsive: true, responsive: true,
plugins: { plugins: {
legend: { display: false }, legend: { display: false },
annotation: targetResponseTime annotation: {
? { annotations: {
annotations: { averageLine: {
targetLine: { type: "line",
yMin: 0,
yMax: Math.max(...bins),
xMin: average,
xMax: average,
borderColor: "rgba(75, 192, 192, 1)",
borderWidth: 2,
label: {
display: true,
content: "Avg: " + average.toFixed(1) + "s",
position: "start",
},
},
targetLine: targetResponseTime
? {
type: "line", type: "line",
yMin: 0, yMin: 0,
yMax: Math.max(...bins), yMax: Math.max(...bins),
xMin: targetResponseTime, xMin: targetResponseTime,
xMax: targetResponseTime, xMax: targetResponseTime,
borderColor: "rgba(75, 192, 192, 1)", borderColor: "rgba(75, 192, 192, 0.7)",
borderWidth: 2, borderWidth: 2,
label: { label: {
display: true, display: true,
content: "Target", content: "Target",
position: "start", position: "end",
}, },
}, }
}, : undefined,
} },
: undefined, },
}, },
scales: { scales: {
y: { y: {
@ -103,7 +119,7 @@ export default function ResponseTimeDistribution({
}); });
return () => chart.destroy(); return () => chart.destroy();
}, [responseTimes, targetResponseTime]); }, [data, average, targetResponseTime]);
return <canvas ref={ref} height={180} />; return <canvas ref={ref} height={180} />;
} }

View File

@ -15,11 +15,10 @@ export default function SessionDetails({ session }: SessionDetailsProps) {
return ( return (
<div className="bg-white p-4 rounded-lg shadow"> <div className="bg-white p-4 rounded-lg shadow">
<h3 className="font-bold text-lg mb-3">Session Details</h3> <h3 className="font-bold text-lg mb-3">Session Details</h3>
<div className="space-y-3">
<div className="space-y-2">
<div className="flex justify-between border-b pb-2"> <div className="flex justify-between border-b pb-2">
<span className="text-gray-600">Session ID:</span> <span className="text-gray-600">Session ID:</span>
<span className="font-medium">{session.sessionId || session.id}</span> <span className="font-medium font-mono text-sm">{session.id}</span>
</div> </div>
<div className="flex justify-between border-b pb-2"> <div className="flex justify-between border-b pb-2">
@ -71,7 +70,7 @@ export default function SessionDetails({ session }: SessionDetailsProps) {
{session.sentiment !== null && session.sentiment !== undefined && ( {session.sentiment !== null && session.sentiment !== undefined && (
<div className="flex justify-between border-b pb-2"> <div className="flex justify-between border-b pb-2">
<span className="text-gray-600">Sentiment:</span> <span className="text-gray-600">Sentiment Score:</span>
<span <span
className={`font-medium ${ className={`font-medium ${
session.sentiment > 0.3 session.sentiment > 0.3
@ -91,6 +90,23 @@ export default function SessionDetails({ session }: SessionDetailsProps) {
</div> </div>
)} )}
{session.sentimentCategory && (
<div className="flex justify-between border-b pb-2">
<span className="text-gray-600">AI Sentiment:</span>
<span
className={`font-medium capitalize ${
session.sentimentCategory === "positive"
? "text-green-500"
: session.sentimentCategory === "negative"
? "text-red-500"
: "text-orange-500"
}`}
>
{session.sentimentCategory}
</span>
</div>
)}
<div className="flex justify-between border-b pb-2"> <div className="flex justify-between border-b pb-2">
<span className="text-gray-600">Messages Sent:</span> <span className="text-gray-600">Messages Sent:</span>
<span className="font-medium">{session.messagesSent || 0}</span> <span className="font-medium">{session.messagesSent || 0}</span>
@ -142,23 +158,82 @@ export default function SessionDetails({ session }: SessionDetailsProps) {
</div> </div>
)} )}
{/* Transcript rendering is now handled by the parent page (app/dashboard/sessions/[id]/page.tsx) */} {session.ipAddress && (
{/* Fallback to link only if we only have the URL but no content - this might also be redundant if parent handles all transcript display */} <div className="flex justify-between border-b pb-2">
{(!session.transcriptContent || <span className="text-gray-600">IP Address:</span>
session.transcriptContent.length === 0) && <span className="font-medium font-mono text-sm">
session.fullTranscriptUrl && ( {session.ipAddress}
<div className="flex justify-between pt-2"> </span>
<span className="text-gray-600">Transcript:</span> </div>
<a )}
href={session.fullTranscriptUrl}
target="_blank" {session.processed !== null && session.processed !== undefined && (
rel="noopener noreferrer" <div className="flex justify-between border-b pb-2">
className="text-blue-500 hover:text-blue-700 underline" <span className="text-gray-600">AI Processed:</span>
> <span
View Full Transcript className={`font-medium ${session.processed ? "text-green-500" : "text-gray-500"}`}
</a> >
{session.processed ? "Yes" : "No"}
</span>
</div>
)}
{session.initialMsg && (
<div className="border-b pb-2">
<span className="text-gray-600 block mb-1">Initial Message:</span>
<div className="bg-gray-50 p-2 rounded text-sm italic">
"{session.initialMsg}"
</div> </div>
)} </div>
)}
{session.summary && (
<div className="border-b pb-2">
<span className="text-gray-600 block mb-1">AI Summary:</span>
<div className="bg-blue-50 p-2 rounded text-sm">
{session.summary}
</div>
</div>
)}
{session.questions && (
<div className="border-b pb-2">
<span className="text-gray-600 block mb-1">Questions Asked:</span>
<div className="bg-yellow-50 p-2 rounded text-sm">
{(() => {
try {
const questions = JSON.parse(session.questions);
if (Array.isArray(questions) && questions.length > 0) {
return (
<ul className="list-disc list-inside space-y-1">
{questions.map((question: string, index: number) => (
<li key={index}>{question}</li>
))}
</ul>
);
}
return "No questions identified";
} catch {
return session.questions;
}
})()}
</div>
</div>
)}
{session.fullTranscriptUrl && (
<div className="flex justify-between pt-2">
<span className="text-gray-600">Transcript:</span>
<a
href={session.fullTranscriptUrl}
target="_blank"
rel="noopener noreferrer"
className="text-blue-500 hover:text-blue-700 underline"
>
View Full Transcript
</a>
</div>
)}
</div> </div>
</div> </div>
); );

357
components/Sidebar.tsx Normal file
View File

@ -0,0 +1,357 @@
"use client";
import React from "react"; // No hooks needed since state is now managed by parent
import Link from "next/link";
import Image from "next/image";
import { usePathname } from "next/navigation";
import { signOut } from "next-auth/react";
// Icons for the sidebar
const DashboardIcon = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"
/>
</svg>
);
const CompanyIcon = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"
/>
</svg>
);
const UsersIcon = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"
/>
</svg>
);
const SessionsIcon = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"
/>
</svg>
);
const LogoutIcon = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"
/>
</svg>
);
const MinimalToggleIcon = ({ isExpanded }: { isExpanded: boolean }) => (
<svg
className="h-6 w-6 text-gray-600 group-hover:text-sky-700 transition-colors"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
{isExpanded ? (
<path strokeLinecap="round" strokeLinejoin="round" d="M15 19l-7-7 7-7" />
) : (
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 6h16M4 12h16M4 18h7"
/>
)}
</svg>
);
export interface SidebarProps {
isExpanded: boolean;
onToggle: () => void;
isMobile?: boolean; // Add this property to indicate mobile viewport
onNavigate?: () => void; // Function to call when navigating to a new page
}
interface NavItemProps {
href: string;
label: string;
icon: React.ReactNode;
isExpanded: boolean;
isActive: boolean;
onNavigate?: () => void; // Function to call when navigating to a new page
}
const NavItem: React.FC<NavItemProps> = ({
href,
label,
icon,
isExpanded,
isActive,
onNavigate,
}) => (
<Link
href={href}
className={`relative flex items-center p-3 my-1 rounded-lg transition-all group ${
isActive
? "bg-sky-100 text-sky-800 font-medium"
: "hover:bg-gray-100 text-gray-700 hover:text-gray-900"
}`}
onClick={() => {
if (onNavigate) {
onNavigate();
}
}}
>
<span className={`flex-shrink-0 ${isExpanded ? "mr-3" : "mx-auto"}`}>
{icon}
</span>
{isExpanded ? (
<span className="truncate">{label}</span>
) : (
<div
className="fixed ml-6 w-auto p-2 min-w-max rounded-md shadow-md text-xs font-medium
text-white bg-gray-800 z-50
invisible opacity-0 -translate-x-3 transition-all
group-hover:visible group-hover:opacity-100 group-hover:translate-x-0"
>
{label}
</div>
)}
</Link>
);
export default function Sidebar({
isExpanded,
onToggle,
isMobile = false,
onNavigate,
}: SidebarProps) {
const pathname = usePathname() || "";
const handleLogout = () => {
signOut({ callbackUrl: "/login" });
};
return (
<>
{/* Backdrop overlay when sidebar is expanded on mobile */}
{isExpanded && isMobile && (
<div
className="fixed inset-0 bg-gray-900 bg-opacity-50 z-10 transition-opacity duration-300"
onClick={onToggle}
/>
)}
<div
className={`fixed md:relative h-screen bg-white shadow-md transition-all duration-300
${
isExpanded ? (isMobile ? "w-full sm:w-80" : "w-56") : "w-16"
} flex flex-col overflow-visible z-20`}
>
<div className="flex flex-col items-center pt-5 pb-3 border-b relative">
{/* Toggle button when sidebar is collapsed - above logo */}
{!isExpanded && (
<div className="absolute top-1 left-1/2 transform -translate-x-1/2 z-30">
<button
onClick={(e) => {
e.preventDefault(); // Prevent any navigation
onToggle();
}}
className="p-1.5 rounded-md hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-sky-500 transition-colors group"
title="Expand sidebar"
>
<MinimalToggleIcon isExpanded={isExpanded} />
</button>
</div>
)}
{/* Logo section with link to homepage */}
<Link href="/" className="flex flex-col items-center">
<div
className={`relative ${isExpanded ? "w-16" : "w-10 mt-8"} aspect-square mb-1 transition-all duration-300`}
>
<Image
src="/favicon.svg"
alt="LiveDash Logo"
fill
className="transition-all duration-300"
priority
style={{
objectFit: "contain",
maxWidth: "100%",
}}
/>
</div>
{isExpanded && (
<span className="text-lg font-bold text-sky-700 mt-1 transition-opacity duration-300">
LiveDash
</span>
)}
</Link>
</div>
{isExpanded && (
<div className="absolute top-3 right-3 z-30">
<button
onClick={(e) => {
e.preventDefault(); // Prevent any navigation
onToggle();
}}
className="p-1.5 rounded-md hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-sky-500 transition-colors group"
title="Collapse sidebar"
>
<MinimalToggleIcon isExpanded={isExpanded} />
</button>
</div>
)}
<nav
className={`flex-1 py-4 px-2 overflow-y-auto overflow-x-visible ${isExpanded ? "pt-12" : "pt-4"}`}
>
<NavItem
href="/dashboard"
label="Dashboard"
icon={<DashboardIcon />}
isExpanded={isExpanded}
isActive={pathname === "/dashboard"}
onNavigate={onNavigate}
/>
<NavItem
href="/dashboard/overview"
label="Analytics"
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"
/>
</svg>
}
isExpanded={isExpanded}
isActive={pathname === "/dashboard/overview"}
onNavigate={onNavigate}
/>
<NavItem
href="/dashboard/sessions"
label="Sessions"
icon={<SessionsIcon />}
isExpanded={isExpanded}
isActive={pathname.startsWith("/dashboard/sessions")}
onNavigate={onNavigate}
/>
<NavItem
href="/dashboard/company"
label="Company Settings"
icon={<CompanyIcon />}
isExpanded={isExpanded}
isActive={pathname === "/dashboard/company"}
onNavigate={onNavigate}
/>
<NavItem
href="/dashboard/users"
label="User Management"
icon={<UsersIcon />}
isExpanded={isExpanded}
isActive={pathname === "/dashboard/users"}
onNavigate={onNavigate}
/>
</nav>
<div className="p-4 border-t mt-auto">
<button
onClick={handleLogout}
className={`relative flex items-center p-3 w-full rounded-lg text-gray-700 hover:bg-gray-100 hover:text-gray-900 transition-all group ${
isExpanded ? "" : "justify-center"
}`}
>
<span className={`flex-shrink-0 ${isExpanded ? "mr-3" : ""}`}>
<LogoutIcon />
</span>
{isExpanded ? (
<span>Logout</span>
) : (
<div
className="fixed ml-6 w-auto p-2 min-w-max rounded-md shadow-md text-xs font-medium
text-white bg-gray-800 z-50
invisible opacity-0 -translate-x-3 transition-all
group-hover:visible group-hover:opacity-100 group-hover:translate-x-0"
>
Logout
</div>
)}
</button>
</div>
</div>
</>
);
}

View File

@ -0,0 +1,78 @@
"use client";
import React from "react";
import { TopQuestion } from "../lib/types";
interface TopQuestionsChartProps {
data: TopQuestion[];
title?: string;
}
export default function TopQuestionsChart({
data,
title = "Top 5 Asked Questions",
}: TopQuestionsChartProps) {
if (!data || data.length === 0) {
return (
<div className="bg-white p-6 rounded-lg shadow-sm border border-gray-200">
<h3 className="text-lg font-semibold text-gray-900 mb-4">{title}</h3>
<div className="text-center py-8 text-gray-500">
No questions data available
</div>
</div>
);
}
// Find the maximum count to calculate relative bar widths
const maxCount = Math.max(...data.map((q) => q.count));
return (
<div className="bg-white p-6 rounded-lg shadow-sm border border-gray-200">
<h3 className="text-lg font-semibold text-gray-900 mb-4">{title}</h3>
<div className="space-y-4">
{data.map((question, index) => {
const percentage =
maxCount > 0 ? (question.count / maxCount) * 100 : 0;
return (
<div key={index} className="relative">
{/* Question text */}
<div className="flex justify-between items-start mb-2">
<p className="text-sm text-gray-700 font-medium leading-tight pr-4 flex-1">
{question.question}
</p>
<span className="text-sm font-semibold text-gray-900 bg-gray-100 px-2 py-1 rounded-md whitespace-nowrap">
{question.count}
</span>
</div>
{/* Progress bar */}
<div className="w-full bg-gray-200 rounded-full h-2">
<div
className="bg-blue-600 h-2 rounded-full transition-all duration-300 ease-in-out"
style={{ width: `${percentage}%` }}
/>
</div>
{/* Rank indicator */}
<div className="absolute -left-2 top-0 w-6 h-6 bg-blue-600 text-white text-xs font-bold rounded-full flex items-center justify-center">
{index + 1}
</div>
</div>
);
})}
</div>
{/* Summary */}
<div className="mt-6 pt-4 border-t border-gray-200">
<div className="flex justify-between text-sm text-gray-600">
<span>Total questions analyzed</span>
<span className="font-medium">
{data.reduce((sum, q) => sum + q.count, 0)}
</span>
</div>
</div>
</div>
);
}

View File

@ -55,7 +55,7 @@ function formatTranscript(content: string): React.ReactNode[] {
rehypePlugins={[rehypeRaw]} // Add rehypeRaw to plugins rehypePlugins={[rehypeRaw]} // Add rehypeRaw to plugins
components={{ components={{
p: "span", p: "span",
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
a: ({ node: _node, ...props }) => ( a: ({ node: _node, ...props }) => (
<a <a
className="text-sky-600 hover:text-sky-800 underline" className="text-sky-600 hover:text-sky-800 underline"
@ -107,7 +107,7 @@ function formatTranscript(content: string): React.ReactNode[] {
rehypePlugins={[rehypeRaw]} // Add rehypeRaw to plugins rehypePlugins={[rehypeRaw]} // Add rehypeRaw to plugins
components={{ components={{
p: "span", p: "span",
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
a: ({ node: _node, ...props }) => ( a: ({ node: _node, ...props }) => (
<a <a
className="text-sky-600 hover:text-sky-800 underline" className="text-sky-600 hover:text-sky-800 underline"

View File

@ -2,15 +2,7 @@
import { useRef, useEffect, useState } from "react"; import { useRef, useEffect, useState } from "react";
import { select } from "d3-selection"; import { select } from "d3-selection";
import cloud from "d3-cloud"; import cloud, { Word } from "d3-cloud";
interface CloudWord {
text: string;
size: number;
x?: number;
y?: number;
rotate?: number;
}
interface WordCloudProps { interface WordCloudProps {
words: { words: {
@ -19,20 +11,55 @@ interface WordCloudProps {
}[]; }[];
width?: number; width?: number;
height?: number; height?: number;
minWidth?: number;
minHeight?: number;
} }
export default function WordCloud({ export default function WordCloud({
words, words,
width = 500, width: initialWidth = 500,
height = 300, height: initialHeight = 300,
minWidth = 200,
minHeight = 200,
}: WordCloudProps) { }: WordCloudProps) {
const svgRef = useRef<SVGSVGElement | null>(null); const svgRef = useRef<SVGSVGElement | null>(null);
const containerRef = useRef<HTMLDivElement | null>(null);
const [isClient, setIsClient] = useState(false); const [isClient, setIsClient] = useState(false);
const [dimensions, setDimensions] = useState({
width: initialWidth,
height: initialHeight,
});
// Set isClient to true on initial render
useEffect(() => { useEffect(() => {
setIsClient(true); setIsClient(true);
}, []); }, []);
// Add effect to detect container size changes
useEffect(() => {
if (!containerRef.current || !isClient) return;
// Create ResizeObserver to detect size changes
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
const { width, height } = entry.contentRect;
// Ensure minimum dimensions
const newWidth = Math.max(width, minWidth);
const newHeight = Math.max(height, minHeight);
setDimensions({ width: newWidth, height: newHeight });
}
});
// Start observing the container
resizeObserver.observe(containerRef.current);
// Cleanup
return () => {
resizeObserver.disconnect();
};
}, [isClient, minWidth, minHeight]);
// Effect to render the word cloud whenever dimensions or words change
useEffect(() => { useEffect(() => {
if (!svgRef.current || !isClient || !words.length) return; if (!svgRef.current || !isClient || !words.length) return;
@ -44,7 +71,7 @@ export default function WordCloud({
// Configure the layout // Configure the layout
const layout = cloud() const layout = cloud()
.size([width, height]) .size([dimensions.width, dimensions.height])
.words( .words(
words.map((d) => ({ words.map((d) => ({
text: d.text, text: d.text,
@ -53,20 +80,23 @@ export default function WordCloud({
) )
.padding(5) .padding(5)
.rotate(() => (~~(Math.random() * 6) - 3) * 15) // Rotate between -45 and 45 degrees .rotate(() => (~~(Math.random() * 6) - 3) * 15) // Rotate between -45 and 45 degrees
.fontSize((d) => (d as any).size) .fontSize((d: Word) => d.size || 10)
.on("end", draw); .on("end", draw);
layout.start(); layout.start();
function draw(words: CloudWord[]) { function draw(words: Word[]) {
svg svg
.append("g") .append("g")
.attr("transform", `translate(${width / 2},${height / 2})`) .attr(
"transform",
`translate(${dimensions.width / 2},${dimensions.height / 2})`
)
.selectAll("text") .selectAll("text")
.data(words) .data(words)
.enter() .enter()
.append("text") .append("text")
.style("font-size", (d: CloudWord) => `${d.size}px`) .style("font-size", (d: Word) => `${d.size || 10}px`)
.style("font-family", "Inter, Arial, sans-serif") .style("font-family", "Inter, Arial, sans-serif")
.style("fill", () => { .style("fill", () => {
// Create a nice gradient of colors // Create a nice gradient of colors
@ -85,17 +115,17 @@ export default function WordCloud({
.attr("text-anchor", "middle") .attr("text-anchor", "middle")
.attr( .attr(
"transform", "transform",
(d: CloudWord) => (d: Word) =>
`translate(${d.x || 0},${d.y || 0}) rotate(${d.rotate || 0})` `translate(${d.x || 0},${d.y || 0}) rotate(${d.rotate || 0})`
) )
.text((d: CloudWord) => d.text); .text((d: Word) => d.text || "");
} }
// Cleanup function // Cleanup function
return () => { return () => {
svg.selectAll("*").remove(); svg.selectAll("*").remove();
}; };
}, [words, width, height, isClient]); }, [words, dimensions, isClient]);
if (!isClient) { if (!isClient) {
return ( return (
@ -106,12 +136,21 @@ export default function WordCloud({
} }
return ( return (
<div className="flex justify-center w-full h-full"> <div
ref={containerRef}
className="flex justify-center w-full h-full"
style={{ minHeight: `${minHeight}px` }}
>
<svg <svg
ref={svgRef} ref={svgRef}
width={width} width={dimensions.width}
height={height} height={dimensions.height}
className="w-full h-full"
aria-label="Word cloud visualization of categories" aria-label="Word cloud visualization of categories"
style={{
maxWidth: "100%",
maxHeight: "100%",
}}
/> />
</div> </div>
); );

View File

@ -0,0 +1,213 @@
# 🤖 Automated Processing System Documentation
## 🎯 Overview
The LiveDash system now features a complete automated processing pipeline that:
-**Processes ALL unprocessed sessions** in batches until completion
-**Runs hourly** to check for new unprocessed sessions
-**Triggers automatically** when dashboard refresh is pressed
-**Validates data quality** and filters out low-quality sessions
-**Requires zero manual intervention** for ongoing operations
---
## 🔄 Complete Workflow
### 1. **CSV Import** (Automatic/Manual)
```
📥 CSV Data → Session Records (processed: false)
```
- **Automatic**: Hourly scheduler imports new CSV data
- **Manual**: Dashboard refresh button triggers immediate import
- **Result**: New sessions created with `processed: false`
### 2. **Transcript Fetching** (As Needed)
```
🔗 fullTranscriptUrl → Message Records
```
- **Script**: `node scripts/fetch-and-parse-transcripts.js`
- **Purpose**: Convert transcript URLs into message records
- **Status**: Only sessions with messages can be AI processed
### 3. **AI Processing** (Automatic/Manual)
```
💬 Messages → 🤖 OpenAI Analysis → 📊 Structured Data
```
- **Automatic**: Hourly scheduler processes all unprocessed sessions
- **Manual**: Dashboard refresh or direct script execution
- **Batch Processing**: Processes ALL unprocessed sessions until none remain
- **Quality Validation**: Filters out empty questions and short summaries
---
## 🚀 Automated Triggers
### **Hourly Scheduler**
```javascript
// Runs every hour automatically
cron.schedule("0 * * * *", async () => {
await processUnprocessedSessions(); // Process ALL until completion
});
```
### **Dashboard Refresh**
```javascript
// When user clicks refresh in dashboard
POST /api/admin/refresh-sessions
Import new CSV data
Automatically trigger processUnprocessedSessions()
```
### **Manual Processing**
```bash
# Process all unprocessed sessions until completion
npx tsx scripts/trigger-processing-direct.js
# Check system status
node scripts/check-database-status.js
# Complete workflow demonstration
npx tsx scripts/complete-workflow-demo.js
```
---
## 📊 Processing Logic
### **Batch Processing Algorithm**
```javascript
while (true) {
// Get next batch of unprocessed sessions with messages
const sessions = await findUnprocessedSessions(batchSize: 10);
if (sessions.length === 0) {
console.log("✅ All sessions processed!");
break;
}
// Process batch with concurrency limit
await processInParallel(sessions, maxConcurrency: 3);
// Small delay between batches
await delay(1000ms);
}
```
### **Quality Validation**
```javascript
// Check data quality after AI processing
const hasValidQuestions = questions.length > 0;
const hasValidSummary = summary.length >= 10;
const isValidData = hasValidQuestions && hasValidSummary;
if (!isValidData) {
console.log("⚠️ Session marked as invalid data");
}
```
---
## 🎯 System Behavior
### **What Gets Processed**
- ✅ Sessions with `processed: false`
- ✅ Sessions that have message records
- ❌ Sessions without messages (skipped until transcripts fetched)
- ❌ Already processed sessions (ignored)
### **Processing Results**
- **Valid Sessions**: Full AI analysis with categories, questions, summary
- **Invalid Sessions**: Marked as processed but flagged as low-quality
- **Failed Sessions**: Error logged, remains unprocessed for retry
### **Dashboard Integration**
- **Refresh Button**: Imports CSV + triggers processing automatically
- **Real-time Updates**: Processing happens in background
- **Quality Filtering**: Only meaningful conversations shown in analytics
---
## 📈 Current System Status
```
📊 Database Status:
📈 Total sessions: 108
✅ Processed sessions: 20 (All sessions with messages)
⏳ Unprocessed sessions: 88 (Sessions without transcript messages)
💬 Sessions with messages: 20 (Ready for/already processed)
🏢 Total companies: 1
🎯 System State: FULLY OPERATIONAL
✅ All sessions with messages have been processed
✅ Automated processing ready for new data
✅ Quality validation working perfectly
```
---
## 🛠️ Available Scripts
### **Core Processing**
```bash
# Process all unprocessed sessions (complete batch processing)
npx tsx scripts/trigger-processing-direct.js
# Check database status
node scripts/check-database-status.js
# Fetch missing transcripts
node scripts/fetch-and-parse-transcripts.js
```
### **Data Management**
```bash
# Import fresh CSV data
node scripts/trigger-csv-refresh.js
# Reset all sessions to unprocessed (for reprocessing)
node scripts/reset-processed-status.js
```
### **System Demonstration**
```bash
# Complete workflow demonstration
npx tsx scripts/complete-workflow-demo.js
```
---
## 🎉 Key Achievements
### **✅ Complete Automation**
- **Zero manual intervention** needed for ongoing operations
- **Hourly processing** of any new unprocessed sessions
- **Dashboard integration** with automatic processing triggers
### **✅ Batch Processing**
- **Processes ALL unprocessed sessions** until none remain
- **Configurable batch sizes** and concurrency limits
- **Progress tracking** with detailed logging
### **✅ Quality Validation**
- **Automatic filtering** of low-quality sessions
- **Enhanced OpenAI prompts** with crystal-clear instructions
- **Data quality checks** before and after processing
### **✅ Production Ready**
- **Error handling** and retry logic
- **Background processing** without blocking responses
- **Comprehensive logging** for monitoring and debugging
---
## 🚀 Production Deployment
The system is now **100% ready for production** with:
1. **Automated CSV import** every hour
2. **Automated AI processing** every hour
3. **Dashboard refresh integration** for immediate processing
4. **Quality validation** to ensure clean analytics
5. **Complete batch processing** until all sessions are analyzed
**No manual intervention required** - the system will automatically process all new data as it arrives!

79
docs/scheduler-fixes.md Normal file
View File

@ -0,0 +1,79 @@
# Scheduler Error Fixes
## Issues Identified and Resolved
### 1. Invalid Company Configuration
**Problem**: Company `26fc3d34-c074-4556-85bd-9a66fafc0e08` had an invalid CSV URL (`https://example.com/data.csv`) with no authentication credentials.
**Solution**:
- Added validation in `fetchAndStoreSessionsForAllCompanies()` to skip companies with example/invalid URLs
- Removed the invalid company record from the database using `fix_companies.js`
### 2. Transcript Fetching Errors
**Problem**: Multiple "Error fetching transcript: Unauthorized" messages were flooding the logs when individual transcript files couldn't be accessed.
**Solution**:
- Improved error handling in `fetchTranscriptContent()` function
- Added probabilistic logging (only ~10% of errors logged) to prevent log spam
- Added timeout (10 seconds) for transcript fetching
- Made transcript fetching failures non-blocking (sessions are still created without transcript content)
### 3. CSV Fetching Errors
**Problem**: "Failed to fetch CSV: Not Found" errors for companies with invalid URLs.
**Solution**:
- Added URL validation to skip companies with `example.com` URLs
- Improved error logging to be more descriptive
## Current Status
**Fixed**: No more "Unauthorized" error spam
**Fixed**: No more "Not Found" CSV errors
**Fixed**: Scheduler runs cleanly without errors
**Improved**: Better error handling and logging
## Remaining Companies
After cleanup, only valid companies remain:
- **Demo Company** (`790b9233-d369-451f-b92c-f4dceb42b649`)
- CSV URL: `https://proto.notso.ai/jumbo/chats`
- Has valid authentication credentials
- 107 sessions in database
## Files Modified
1. **lib/csvFetcher.js**
- Added company URL validation
- Improved transcript fetching error handling
- Reduced error log verbosity
2. **fix_companies.js** (cleanup script)
- Removes invalid company records
- Can be run again if needed
## Monitoring
The scheduler now runs cleanly every 15 minutes. To monitor:
```bash
# Check scheduler logs
node debug_db.js
# Test manual refresh
node -e "import('./lib/csvFetcher.js').then(m => m.fetchAndStoreSessionsForAllCompanies())"
```
## Future Improvements
1. Add health check endpoint for scheduler status
2. Add metrics for successful/failed fetches
3. Consider retry logic for temporary failures
4. Add alerting for persistent failures

185
docs/scheduler-workflow.md Normal file
View File

@ -0,0 +1,185 @@
# Scheduler Workflow Documentation
## Overview
The LiveDash system has two main schedulers that work together to fetch and process session data:
1. **Session Refresh Scheduler** - Fetches new sessions from CSV files
2. **Processing Scheduler** - Processes session transcripts with AI
## Current Status (as of latest check)
- **Total sessions**: 107
- **Processed sessions**: 0
- **Sessions with transcript**: 0
- **Ready for processing**: 0
## How the `processed` Field Works
The ProcessingScheduler picks up sessions where `processed` is **NOT** `true`, which includes:
- `processed = false`
- `processed = null`
**Query used:**
```javascript
{ processed: { not: true } } // Either false or null
```
## Complete Workflow
### Step 1: Session Refresh (CSV Fetching)
**What it does:**
- Fetches session data from company CSV URLs
- Creates session records in database with basic metadata
- Sets `transcriptContent = null` initially
- Sets `processed = null` initially
**Runs:** Every 30 minutes (cron: `*/30 * * * *`)
### Step 2: Transcript Fetching
**What it does:**
- Downloads full transcript content for sessions
- Updates `transcriptContent` field with actual conversation data
- Sessions remain `processed = null` until AI processing
**Runs:** As part of session refresh process
### Step 3: AI Processing
**What it does:**
- Finds sessions with transcript content where `processed != true`
- Sends transcripts to OpenAI for analysis
- Extracts: sentiment, category, questions, summary, etc.
- Updates session with processed data
- Sets `processed = true`
**Runs:** Every hour (cron: `0 * * * *`)
## Manual Trigger Commands
### Check Current Status
```bash
node scripts/manual-triggers.js status
```
### Trigger Session Refresh (Fetch new sessions from CSV)
```bash
node scripts/manual-triggers.js refresh
```
### Trigger AI Processing (Process unprocessed sessions)
```bash
node scripts/manual-triggers.js process
```
### Run Both Schedulers
```bash
node scripts/manual-triggers.js both
```
## Troubleshooting
### No Sessions Being Processed?
1. **Check if sessions have transcripts:**
```bash
node scripts/manual-triggers.js status
```
2. **If "Sessions with transcript" is 0:**
- Sessions exist but transcripts haven't been fetched yet
- Run session refresh: `node scripts/manual-triggers.js refresh`
3. **If "Ready for processing" is 0 but "Sessions with transcript" > 0:**
- All sessions with transcripts have already been processed
- Check if `OPENAI_API_KEY` is set in environment
### Common Issues
#### "No sessions found requiring processing"
- All sessions with transcripts have been processed (`processed = true`)
- Or no sessions have transcript content yet
#### "OPENAI_API_KEY environment variable is not set"
- Add OpenAI API key to `.env.development` file
- Restart the application
#### "Error fetching transcript: Unauthorized"
- CSV credentials are incorrect or expired
- Check company CSV username/password in database
## Database Field Mapping
### Before AI Processing
```javascript
{
id: "session-uuid",
transcriptContent: "full conversation text" | null,
processed: null,
sentimentCategory: null,
questions: null,
summary: null,
// ... other fields
}
```
### After AI Processing
```javascript
{
id: "session-uuid",
transcriptContent: "full conversation text",
processed: true,
sentimentCategory: "positive" | "neutral" | "negative",
questions: '["question 1", "question 2"]', // JSON string
summary: "Brief conversation summary",
language: "en", // ISO 639-1 code
messagesSent: 5,
sentiment: 0.8, // Float value (-1 to 1)
escalated: false,
forwardedHr: false,
category: "Schedule & Hours",
// ... other fields
}
```
## Scheduler Configuration
### Session Refresh Scheduler
- **File**: `lib/scheduler.js`
- **Frequency**: Every 30 minutes
- **Cron**: `*/30 * * * *`
### Processing Scheduler
- **File**: `lib/processingScheduler.js`
- **Frequency**: Every hour
- **Cron**: `0 * * * *`
- **Batch size**: 10 sessions per run
## Environment Variables Required
```bash
# Database
DATABASE_URL="postgresql://..."
# OpenAI (for processing)
OPENAI_API_KEY="sk-..."
# NextAuth
NEXTAUTH_SECRET="..."
NEXTAUTH_URL="http://localhost:3000"
```
## Next Steps for Testing
1. **Trigger session refresh** to fetch transcripts:
```bash
node scripts/manual-triggers.js refresh
```
2. **Check status** to see if transcripts were fetched:
```bash
node scripts/manual-triggers.js status
```
3. **Trigger processing** if transcripts are available:
```bash
node scripts/manual-triggers.js process
```
4. **View results** in the dashboard session details pages

View File

@ -0,0 +1,86 @@
# Session Processing with OpenAI
This document explains how the session processing system works in LiveDash-Node.
## Overview
The system now includes an automated process for analyzing chat session transcripts using OpenAI's API. This process:
1. Fetches session data from CSV sources
2. Only adds new sessions that don't already exist in the database
3. Processes session transcripts with OpenAI to extract valuable insights
4. Updates the database with the processed information
## How It Works
### Session Fetching
- The system fetches session data from configured CSV URLs for each company
- Unlike the previous implementation, it now only adds sessions that don't already exist in the database
- This prevents duplicate sessions and allows for incremental updates
### Transcript Processing
- For sessions with transcript content that haven't been processed yet, the system calls OpenAI's API
- The API analyzes the transcript and extracts the following information:
- Primary language used (ISO 639-1 code)
- Number of messages sent by the user
- Overall sentiment (positive, neutral, negative)
- Whether the conversation was escalated
- Whether HR contact was mentioned or provided
- Best-fitting category for the conversation
- Up to 5 paraphrased questions asked by the user
- A brief summary of the conversation
### Scheduling
The system includes two schedulers:
1. **Session Refresh Scheduler**: Runs every 15 minutes to fetch new sessions from CSV sources
2. **Session Processing Scheduler**: Runs every hour to process unprocessed sessions with OpenAI
## Database Schema
The Session model has been updated with new fields to store the processed data:
- `processed`: Boolean flag indicating whether the session has been processed
- `sentimentCategory`: String value ("positive", "neutral", "negative") from OpenAI
- `questions`: JSON array of questions asked by the user
- `summary`: Brief summary of the conversation
## Configuration
### OpenAI API Key
To use the session processing feature, you need to add your OpenAI API key to the `.env.local` file:
```ini
OPENAI_API_KEY=your_api_key_here
```
### Running with Schedulers
To run the application with schedulers enabled:
- Development: `npm run dev`
- Development (with schedulers disabled): `npm run dev:no-schedulers`
- Production: `npm run start`
Note: These commands will start a custom Next.js server with the schedulers enabled. You'll need to have an OpenAI API key set in your `.env.local` file for the session processing to work.
## Manual Processing
You can also manually process sessions by running the script:
```
node scripts/process_sessions.mjs
```
This will process all unprocessed sessions that have transcript content.
## Customization
The processing logic can be customized by modifying:
- `lib/processingScheduler.ts`: Contains the OpenAI processing logic
- `scripts/process_sessions.ts`: Standalone script for manual processing

View File

@ -0,0 +1,203 @@
# Transcript Parsing Implementation
## Overview
Added structured message parsing to the LiveDash system, allowing transcripts to be broken down into individual messages with timestamps, roles, and content. This provides a much better user experience for viewing conversations.
## Database Changes
### New Message Table
```sql
CREATE TABLE Message (
id TEXT PRIMARY KEY DEFAULT (uuid()),
sessionId TEXT NOT NULL,
timestamp DATETIME NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
order INTEGER NOT NULL,
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (sessionId) REFERENCES Session(id) ON DELETE CASCADE
);
CREATE INDEX Message_sessionId_order_idx ON Message(sessionId, order);
```
### Updated Session Table
- Added `messages` relation to Session model
- Sessions can now have both raw transcript content AND parsed messages
## New Components
### 1. Message Interface (`lib/types.ts`)
```typescript
export interface Message {
id: string;
sessionId: string;
timestamp: Date;
role: string; // "User", "Assistant", "System", etc.
content: string;
order: number; // Order within the conversation (0, 1, 2, ...)
createdAt: Date;
}
```
### 2. Transcript Parser (`lib/transcriptParser.js`)
- **`parseChatLogToJSON(logString)`** - Parses raw transcript text into structured messages
- **`storeMessagesForSession(sessionId, messages)`** - Stores parsed messages in database
- **`processTranscriptForSession(sessionId, transcriptContent)`** - Complete processing for one session
- **`processAllUnparsedTranscripts()`** - Batch process all unparsed transcripts
- **`getMessagesForSession(sessionId)`** - Retrieve messages for a session
### 3. MessageViewer Component (`components/MessageViewer.tsx`)
- Chat-like interface for displaying parsed messages
- Color-coded by role (User: blue, Assistant: gray, System: yellow)
- Shows timestamps and message order
- Scrollable with conversation metadata
## Updated Components
### 1. Session API (`pages/api/dashboard/session/[id].ts`)
- Now includes parsed messages in session response
- Messages are ordered by `order` field (ascending)
### 2. Session Details Page (`app/dashboard/sessions/[id]/page.tsx`)
- Added MessageViewer component
- Shows both parsed messages AND raw transcript
- Prioritizes parsed messages when available
### 3. ChatSession Interface (`lib/types.ts`)
- Added optional `messages?: Message[]` field
## Parsing Logic
### Supported Format
The parser expects transcript format:
```
[DD.MM.YYYY HH:MM:SS] Role: Message content
[DD.MM.YYYY HH:MM:SS] User: Hello, I need help
[DD.MM.YYYY HH:MM:SS] Assistant: How can I help you today?
```
### Features
- **Multi-line support** - Messages can span multiple lines
- **Timestamp parsing** - Converts DD.MM.YYYY HH:MM:SS to ISO format
- **Role detection** - Extracts sender role from each message
- **Ordering** - Maintains conversation order with explicit order field
- **Sorting** - Messages sorted by timestamp, then by role (User before Assistant)
## Manual Commands
### New Commands Added
```bash
# Parse transcripts into structured messages
node scripts/manual-triggers.js parse
# Complete workflow: refresh → parse → process
node scripts/manual-triggers.js all
# Check status (now shows parsing info)
node scripts/manual-triggers.js status
```
### Updated Commands
- **`status`** - Now shows transcript and parsing statistics
- **`all`** - New command that runs refresh → parse → process in sequence
## Workflow Integration
### Complete Processing Pipeline
1. **Session Refresh** - Fetch sessions from CSV, download transcripts
2. **Transcript Parsing** - Parse raw transcripts into structured messages
3. **AI Processing** - Process sessions with OpenAI for sentiment, categories, etc.
### Database States
```javascript
// After CSV fetch
{
transcriptContent: "raw text...",
messages: [], // Empty
processed: null
}
// After parsing
{
transcriptContent: "raw text...",
messages: [Message, Message, ...], // Parsed
processed: null
}
// After AI processing
{
transcriptContent: "raw text...",
messages: [Message, Message, ...], // Parsed
processed: true,
sentimentCategory: "positive",
summary: "Brief summary...",
// ... other AI fields
}
```
## User Experience Improvements
### Before
- Only raw transcript text in a text area
- Difficult to follow conversation flow
- No clear distinction between speakers
### After
- **Chat-like interface** with message bubbles
- **Color-coded roles** for easy identification
- **Timestamps** for each message
- **Conversation metadata** (first/last message times)
- **Fallback to raw transcript** if parsing fails
- **Both views available** - structured AND raw
## Testing
### Manual Testing Commands
```bash
# Check current status
node scripts/manual-triggers.js status
# Parse existing transcripts
node scripts/manual-triggers.js parse
# Full pipeline test
node scripts/manual-triggers.js all
```
### Expected Results
1. Sessions with transcript content get parsed into individual messages
2. Session detail pages show chat-like interface
3. Both parsed messages and raw transcript are available
4. No data loss - original transcript content preserved
## Technical Benefits
### Performance
- **Indexed queries** - Messages indexed by sessionId and order
- **Efficient loading** - Only load messages when needed
- **Cascading deletes** - Messages automatically deleted with sessions
### Maintainability
- **Separation of concerns** - Parsing logic isolated in dedicated module
- **Type safety** - Full TypeScript support for Message interface
- **Error handling** - Graceful fallbacks when parsing fails
### Extensibility
- **Role flexibility** - Supports any role names (User, Assistant, System, etc.)
- **Content preservation** - Multi-line messages fully supported
- **Metadata ready** - Easy to add message-level metadata in future
## Migration Notes
### Existing Data
- **No data loss** - Original transcript content preserved
- **Backward compatibility** - Pages work with or without parsed messages
- **Gradual migration** - Can parse transcripts incrementally
### Database Migration
- New Message table created with foreign key constraints
- Existing Session table unchanged (only added relation)
- Index created for efficient message queries
This implementation provides a solid foundation for enhanced conversation analysis and user experience while maintaining full backward compatibility.

20
e2e/example.spec.ts Normal file
View File

@ -0,0 +1,20 @@
import { test, expect } from "@playwright/test";
test("has title", async ({ page }) => {
await page.goto("https://playwright.dev/");
// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Playwright/);
});
test("get started link", async ({ page }) => {
await page.goto("https://playwright.dev/");
// Click the get started link.
await page.getByRole("link", { name: "Get started" }).click();
// Expects page to have a heading with the name of Installation.
await expect(
page.getByRole("heading", { name: "Installation" })
).toBeVisible();
});

View File

@ -26,13 +26,13 @@ const eslintConfig = [
"coverage/", "coverage/",
], ],
rules: { rules: {
"@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-unused-vars": "warn", "@typescript-eslint/no-unused-vars": "warn",
"react/no-unescaped-entities": "off", "react/no-unescaped-entities": "warn",
"no-console": "warn", "no-console": "off",
"no-trailing-spaces": "error", "no-trailing-spaces": "warn",
"prefer-const": "error", "prefer-const": "error",
"no-unused-vars": "off", "no-unused-vars": "warn",
}, },
}, },
]; ];

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 },
});
}

View File

@ -5,7 +5,7 @@ import ISO6391 from "iso-639-1";
import countries from "i18n-iso-countries"; import countries from "i18n-iso-countries";
// Register locales for i18n-iso-countries // Register locales for i18n-iso-countries
import enLocale from "i18n-iso-countries/langs/en.json" assert { type: "json" }; import enLocale from "i18n-iso-countries/langs/en.json" with { type: "json" };
countries.registerLocale(enLocale); countries.registerLocale(enLocale);
// This type is used internally for parsing the CSV records // This type is used internally for parsing the CSV records
@ -35,10 +35,10 @@ interface SessionData {
startTime: Date; startTime: Date;
endTime: Date | null; endTime: Date | null;
ipAddress?: string; ipAddress?: string;
country?: string | null; // Will store ISO 3166-1 alpha-2 country code or null/undefined country?: string | null;
language?: string | null; // Will store ISO 639-1 language code or null/undefined language?: string | null;
messagesSent: number; messagesSent: number;
sentiment: number | null; sentiment?: string | null;
escalated: boolean; escalated: boolean;
forwardedHr: boolean; forwardedHr: boolean;
fullTranscriptUrl?: string | null; fullTranscriptUrl?: string | null;
@ -50,65 +50,16 @@ interface SessionData {
} }
/** /**
* Converts country names to ISO 3166-1 alpha-2 codes * Passes through country data as-is (no mapping)
* @param countryStr Raw country string from CSV * @param countryStr Raw country string from CSV
* @returns ISO 3166-1 alpha-2 country code or null if not found * @returns The country string as-is or null if empty
*/ */
function getCountryCode(countryStr?: string): string | null | undefined { function getCountryCode(countryStr?: string): string | null | undefined {
if (countryStr === undefined) return undefined; if (countryStr === undefined) return undefined;
if (countryStr === null || countryStr === "") return null; if (countryStr === null || countryStr === "") return null;
// Clean the input
const normalized = countryStr.trim(); const normalized = countryStr.trim();
if (!normalized) return null; return normalized || null;
// Direct ISO code check (if already a 2-letter code)
if (normalized.length === 2 && normalized === normalized.toUpperCase()) {
return countries.isValid(normalized) ? normalized : null;
}
// Special case for country codes used in the dataset
const countryMapping: Record<string, string> = {
BA: "BA", // Bosnia and Herzegovina
NL: "NL", // Netherlands
USA: "US", // United States
UK: "GB", // United Kingdom
GB: "GB", // Great Britain
Nederland: "NL",
Netherlands: "NL",
Netherland: "NL",
Holland: "NL",
Germany: "DE",
Deutschland: "DE",
Belgium: "BE",
België: "BE",
Belgique: "BE",
France: "FR",
Frankreich: "FR",
"United States": "US",
"United States of America": "US",
Bosnia: "BA",
"Bosnia and Herzegovina": "BA",
"Bosnia & Herzegovina": "BA",
};
// Check mapping
if (normalized in countryMapping) {
return countryMapping[normalized];
}
// Try to get the code from the country name (in English)
try {
const code = countries.getAlpha2Code(normalized, "en");
if (code) return code;
} catch (error) {
process.stderr.write(
`[CSV] Error converting country name to code: ${normalized} - ${error}\n`
);
}
// If all else fails, return null
return null;
} }
/** /**
@ -180,174 +131,15 @@ function getLanguageCode(languageStr?: string): string | null | undefined {
} }
/** /**
* Normalizes category values to standard groups * Passes through category data as-is (no mapping)
* @param categoryStr The raw category string from CSV * @param categoryStr The raw category string from CSV
* @returns A normalized category string * @returns The category string as-is or null if empty
*/ */
function normalizeCategory(categoryStr?: string): string | null { function normalizeCategory(categoryStr?: string): string | null {
if (!categoryStr) return null; if (!categoryStr) return null;
const normalized = categoryStr.toLowerCase().trim(); const normalized = categoryStr.trim();
return normalized || null;
// Define category groups using keywords
const categoryMapping: Record<string, string[]> = {
Onboarding: [
"onboarding",
"start",
"begin",
"new",
"orientation",
"welcome",
"intro",
"getting started",
"documents",
"documenten",
"first day",
"eerste dag",
],
"General Information": [
"general",
"algemeen",
"info",
"information",
"informatie",
"question",
"vraag",
"inquiry",
"chat",
"conversation",
"gesprek",
"talk",
],
Greeting: [
"greeting",
"greet",
"hello",
"hi",
"hey",
"welcome",
"hallo",
"hoi",
"greetings",
],
"HR & Payroll": [
"salary",
"salaris",
"pay",
"payroll",
"loon",
"loonstrook",
"hr",
"human resources",
"benefits",
"vacation",
"leave",
"verlof",
"maaltijdvergoeding",
"vergoeding",
],
"Schedules & Hours": [
"schedule",
"hours",
"tijd",
"time",
"roster",
"rooster",
"planning",
"shift",
"dienst",
"working hours",
"werktijden",
"openingstijden",
],
"Role & Responsibilities": [
"role",
"job",
"function",
"functie",
"task",
"taak",
"responsibilities",
"leidinggevende",
"manager",
"teamleider",
"supervisor",
"team",
"lead",
],
"Technical Support": [
"technical",
"tech",
"support",
"laptop",
"computer",
"system",
"systeem",
"it",
"software",
"hardware",
],
Offboarding: [
"offboarding",
"leave",
"exit",
"quit",
"resign",
"resignation",
"ontslag",
"vertrek",
"afsluiting",
],
};
// Try to match the category using keywords
for (const [category, keywords] of Object.entries(categoryMapping)) {
if (keywords.some((keyword) => normalized.includes(keyword))) {
return category;
}
}
// If no match, return "Other"
return "Other";
}
/**
* Converts sentiment string values to numeric scores
* @param sentimentStr The sentiment string from the CSV
* @returns A numeric score representing the sentiment
*/
function mapSentimentToScore(sentimentStr?: string): number | null {
if (!sentimentStr) return null;
// Convert to lowercase for case-insensitive matching
const sentiment = sentimentStr.toLowerCase();
// Map sentiment strings to numeric values on a scale from -1 to 2
const sentimentMap: Record<string, number> = {
happy: 1.0,
excited: 1.5,
positive: 0.8,
neutral: 0.0,
playful: 0.7,
negative: -0.8,
angry: -1.0,
sad: -0.7,
frustrated: -0.9,
positief: 0.8, // Dutch
neutraal: 0.0, // Dutch
negatief: -0.8, // Dutch
positivo: 0.8, // Spanish/Italian
neutro: 0.0, // Spanish/Italian
negativo: -0.8, // Spanish/Italian
yes: 0.5, // For any "yes" sentiment
no: -0.5, // For any "no" sentiment
};
return sentimentMap[sentiment] !== undefined
? sentimentMap[sentiment]
: isNaN(parseFloat(sentiment))
? null
: parseFloat(sentiment);
} }
/** /**
@ -374,6 +166,62 @@ function isTruthyValue(value?: string): boolean {
return truthyValues.includes(value.toLowerCase()); return truthyValues.includes(value.toLowerCase());
} }
/**
* Safely parses a date string into a Date object.
* Handles potential errors and various formats, prioritizing D-M-YYYY HH:MM:SS.
* @param dateStr The date string to parse.
* @returns A Date object or null if parsing fails.
*/
function safeParseDate(dateStr?: string): Date | null {
if (!dateStr) return null;
// Try to parse D-M-YYYY HH:MM:SS format (with hyphens or dots)
const dateTimeRegex =
/^(\d{1,2})[.-](\d{1,2})[.-](\d{4}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
const match = dateStr.match(dateTimeRegex);
if (match) {
const day = match[1];
const month = match[2];
const year = match[3];
const hour = match[4];
const minute = match[5];
const second = match[6];
// Reformat to YYYY-MM-DDTHH:MM:SS (ISO-like, but local time)
// Ensure month and day are two digits
const formattedDateStr = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}T${hour.padStart(2, "0")}:${minute.padStart(2, "0")}:${second.padStart(2, "0")}`;
try {
const date = new Date(formattedDateStr);
// Basic validation: check if the constructed date is valid
if (!isNaN(date.getTime())) {
// console.log(`[safeParseDate] Parsed from D-M-YYYY: ${dateStr} -> ${formattedDateStr} -> ${date.toISOString()}`);
return date;
}
} catch (e) {
console.warn(
`[safeParseDate] Error parsing reformatted string ${formattedDateStr} from ${dateStr}:`,
e
);
}
}
// Fallback for other potential formats (e.g., direct ISO 8601) or if the primary parse failed
try {
const parsedDate = new Date(dateStr);
if (!isNaN(parsedDate.getTime())) {
// console.log(`[safeParseDate] Parsed with fallback: ${dateStr} -> ${parsedDate.toISOString()}`);
return parsedDate;
}
} catch (e) {
console.warn(`[safeParseDate] Error parsing with fallback ${dateStr}:`, e);
}
console.warn(`Failed to parse date string: ${dateStr}`);
return null;
}
export async function fetchAndParseCsv( export async function fetchAndParseCsv(
url: string, url: string,
username?: string, username?: string,
@ -418,13 +266,6 @@ export async function fetchAndParseCsv(
trim: true, trim: true,
}); });
// Helper function to safely parse dates
function safeParseDate(dateStr?: string): Date | null {
if (!dateStr) return null;
const date = new Date(dateStr);
return !isNaN(date.getTime()) ? date : null;
}
// Coerce types for relevant columns // Coerce types for relevant columns
return records.map((r) => ({ return records.map((r) => ({
id: r.session_id, id: r.session_id,
@ -434,7 +275,7 @@ export async function fetchAndParseCsv(
country: getCountryCode(r.country), country: getCountryCode(r.country),
language: getLanguageCode(r.language), language: getLanguageCode(r.language),
messagesSent: Number(r.messages_sent) || 0, messagesSent: Number(r.messages_sent) || 0,
sentiment: mapSentimentToScore(r.sentiment), sentiment: r.sentiment,
escalated: isTruthyValue(r.escalated), escalated: isTruthyValue(r.escalated),
forwardedHr: isTruthyValue(r.forwarded_hr), forwardedHr: isTruthyValue(r.forwarded_hr),
fullTranscriptUrl: r.full_transcript_url, fullTranscriptUrl: r.full_transcript_url,

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 } });
}

View File

@ -2,7 +2,7 @@ import ISO6391 from "iso-639-1";
import countries from "i18n-iso-countries"; import countries from "i18n-iso-countries";
// Register locales for i18n-iso-countries // Register locales for i18n-iso-countries
import enLocale from "i18n-iso-countries/langs/en.json" assert { type: "json" }; import enLocale from "i18n-iso-countries/langs/en.json" with { type: "json" };
countries.registerLocale(enLocale); countries.registerLocale(enLocale);
/** /**

File diff suppressed because it is too large Load Diff

475
lib/processingScheduler.ts Normal file
View File

@ -0,0 +1,475 @@
// Session processing scheduler - TypeScript version
// Note: Disabled due to Next.js compatibility issues
// 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";
import { VALID_CATEGORIES, ValidCategory, SentimentCategory } from "./types";
// 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;
const OPENAI_API_URL = "https://api.openai.com/v1/chat/completions";
interface ProcessedData {
language: string;
sentiment: "positive" | "neutral" | "negative";
escalated: boolean;
forwarded_hr: boolean;
category: ValidCategory;
questions: string | string[];
summary: string;
tokens: number;
tokens_eur: number;
}
interface ProcessingResult {
sessionId: string;
success: boolean;
error?: string;
}
/**
* Processes a session transcript using OpenAI API
*/
async function processTranscriptWithOpenAI(
sessionId: string,
transcript: string
): Promise<ProcessedData> {
if (!OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY environment variable is not set");
}
// Create a system message with instructions
const systemMessage = `
System: You are a JSON-generating assistant. Your task is to analyze raw chat transcripts between a user and an assistant and return structured data.
⚠️ IMPORTANT:
- You must return a **single, valid JSON object**.
- Do **not** include markdown formatting, code fences, explanations, or comments.
- The JSON must match the exact structure and constraints described below.
Here is the schema you must follow:
{{
"language": "ISO 639-1 code, e.g., 'en', 'nl'",
"sentiment": "'positive', 'neutral', or 'negative'",
"escalated": "bool: true if the assistant connected or referred to a human agent, otherwise false",
"forwarded_hr": "bool: true if HR contact info was given, otherwise false",
"category": "one of: 'Schedule & Hours', 'Leave & Vacation', 'Sick Leave & Recovery', 'Salary & Compensation', 'Contract & Hours', 'Onboarding', 'Offboarding', 'Workwear & Staff Pass', 'Team & Contacts', 'Personal Questions', 'Access & Login', 'Social questions', 'Unrecognized / Other'",
"questions": "a single question or an array of simplified questions asked by the user formulated in English, try to make a question out of messages",
"summary": "Brief summary (12 sentences) of the conversation",
"tokens": "integer, number of tokens used for the API call",
"tokens_eur": "float, cost of the API call in EUR",
}}
You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
For example, the example "JSON Schema" instance {"properties": {"foo": {"description": "a list of test words", "type": "array", "items": {"type": "string"}}}}, "required": ["foo"]}}
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
Thus, the object {"foo": ["bar", "baz"]} is a well-formatted instance of this example "JSON Schema". The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
{{"type":"object","properties":{"language":{"type":"string","pattern":"^[a-z]{2}$","description":"ISO 639-1 code for the user's primary language"},"sentiment":{"type":"string","enum":["positive","neutral","negative"],"description":"Overall tone of the user during the conversation"},"escalated":{"type":"boolean","description":"Whether the assistant indicated it could not help"},"forwarded_hr":{"type":"boolean","description":"Whether HR contact was mentioned or provided"},"category":{"type":"string","enum":["Schedule & Hours","Leave & Vacation","Sick Leave & Recovery","Salary & Compensation","Contract & Hours","Onboarding","Offboarding","Workwear & Staff Pass","Team & Contacts","Personal Questions","Access & Login","Social questions","Unrecognized / Other"],"description":"Best-fitting topic category for the conversation"},"questions":{"oneOf":[{"type":"string"},{"type":"array","items":{"type":"string"}}],"description":"A single question or a list of paraphrased questions asked by the user in English"},"summary":{"type":"string","minLength":10,"maxLength":300,"description":"Brief summary of the conversation"},"tokens":{"type":"integer","description":"Number of tokens used for the API call"},"tokens_eur":{"type":"number","description":"Cost of the API call in EUR"}},"required":["language","sentiment","escalated","forwarded_hr","category","questions","summary","tokens","tokens_eur"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}}
`;
try {
const response = await fetch(OPENAI_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-4-turbo",
messages: [
{
role: "system",
content: systemMessage,
},
{
role: "user",
content: transcript,
},
],
temperature: 0.3, // Lower temperature for more consistent results
response_format: { type: "json_object" },
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`OpenAI API error: ${response.status} - ${errorText}`);
}
const data: any = await response.json();
const processedData = JSON.parse(data.choices[0].message.content);
// Validate the response against our expected schema
validateOpenAIResponse(processedData);
return processedData;
} catch (error) {
process.stderr.write(`Error processing transcript with OpenAI: ${error}\n`);
throw error;
}
}
/**
* Validates the OpenAI response against our expected schema
*/
function validateOpenAIResponse(data: any): void {
// Check required fields
const requiredFields = [
"language",
"sentiment",
"escalated",
"forwarded_hr",
"category",
"questions",
"summary",
"tokens",
"tokens_eur",
];
for (const field of requiredFields) {
if (!(field in data)) {
throw new Error(`Missing required field: ${field}`);
}
}
// Validate field types
if (typeof data.language !== "string" || !/^[a-z]{2}$/.test(data.language)) {
throw new Error(
"Invalid language format. Expected ISO 639-1 code (e.g., 'en')"
);
}
if (!["positive", "neutral", "negative"].includes(data.sentiment)) {
throw new Error(
"Invalid sentiment. Expected 'positive', 'neutral', or 'negative'"
);
}
if (typeof data.escalated !== "boolean") {
throw new Error("Invalid escalated. Expected boolean");
}
if (typeof data.forwarded_hr !== "boolean") {
throw new Error("Invalid forwarded_hr. Expected boolean");
}
if (!VALID_CATEGORIES.includes(data.category)) {
throw new Error(
`Invalid category. Expected one of: ${VALID_CATEGORIES.join(", ")}`
);
}
if (typeof data.questions !== "string" && !Array.isArray(data.questions)) {
throw new Error("Invalid questions. Expected string or array of strings");
}
if (
typeof data.summary !== "string" ||
data.summary.length < 10 ||
data.summary.length > 300
) {
throw new Error(
"Invalid summary. Expected string between 10-300 characters"
);
}
if (typeof data.tokens !== "number" || data.tokens < 0) {
throw new Error("Invalid tokens. Expected non-negative number");
}
if (typeof data.tokens_eur !== "number" || data.tokens_eur < 0) {
throw new Error("Invalid tokens_eur. Expected non-negative number");
}
}
/**
* Process a single session
*/
async function processSingleSession(session: any): Promise<ProcessingResult> {
if (session.messages.length === 0) {
return {
sessionId: session.id,
success: false,
error: "Session has no messages",
};
}
// Check for minimum data quality requirements
const userMessages = session.messages.filter((msg: any) =>
msg.role.toLowerCase() === 'user' || msg.role.toLowerCase() === 'human'
);
if (userMessages.length === 0) {
// Mark as invalid data - no user interaction
await prisma.session.update({
where: { id: session.id },
data: {
processed: true,
summary: "No user messages found - marked as invalid data",
},
});
return {
sessionId: session.id,
success: true,
error: "No user messages - marked as invalid data",
};
}
try {
// Convert messages back to transcript format for OpenAI processing
const transcript = session.messages
.map(
(msg: any) =>
`[${new Date(msg.timestamp)
.toLocaleString("en-GB", {
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
})
.replace(",", "")}] ${msg.role}: ${msg.content}`
)
.join("\n");
const processedData = await processTranscriptWithOpenAI(
session.id,
transcript
);
// Check if the processed data indicates low quality (empty questions, very short summary, etc.)
const hasValidQuestions =
processedData.questions &&
(Array.isArray(processedData.questions)
? processedData.questions.length > 0
: typeof processedData.questions === "string");
const hasValidSummary = processedData.summary && processedData.summary.length >= 10;
const isValidData = hasValidQuestions && hasValidSummary;
// Update the session with processed data
await prisma.session.update({
where: { id: session.id },
data: {
language: processedData.language,
sentiment: processedData.sentiment,
escalated: processedData.escalated,
forwardedHr: processedData.forwarded_hr,
category: processedData.category,
questions: processedData.questions,
summary: processedData.summary,
tokens: {
increment: processedData.tokens,
},
tokensEur: {
increment: processedData.tokens_eur,
},
processed: true,
},
});
if (!isValidData) {
process.stdout.write(
`[ProcessingScheduler] ⚠️ Session ${session.id} marked as invalid data (empty questions or short summary)\n`
);
}
return {
sessionId: session.id,
success: true,
};
} catch (error) {
return {
sessionId: session.id,
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
/**
* Process sessions in parallel with concurrency limit
*/
async function processSessionsInParallel(
sessions: any[],
maxConcurrency: number = 5
): Promise<ProcessingResult[]> {
const results: Promise<ProcessingResult>[] = [];
const executing: Promise<ProcessingResult>[] = [];
for (const session of sessions) {
const promise = processSingleSession(session).then((result) => {
process.stdout.write(
result.success
? `[ProcessingScheduler] ✓ Successfully processed session ${result.sessionId}\n`
: `[ProcessingScheduler] ✗ Failed to process session ${result.sessionId}: ${result.error}\n`
);
return result;
});
results.push(promise);
executing.push(promise);
if (executing.length >= maxConcurrency) {
await Promise.race(executing);
const completedIndex = executing.findIndex((p) => p === promise);
if (completedIndex !== -1) {
executing.splice(completedIndex, 1);
}
}
}
return Promise.all(results);
}
/**
* Process unprocessed sessions in batches until completion
*/
export async function processUnprocessedSessions(
batchSize: number = 10,
maxConcurrency: number = 5
): Promise<{ totalProcessed: number; totalFailed: number; totalTime: number }> {
process.stdout.write(
"[ProcessingScheduler] Starting complete processing of all unprocessed sessions...\n"
);
let totalProcessed = 0;
let totalFailed = 0;
const overallStartTime = Date.now();
let batchNumber = 1;
while (true) {
// Find sessions that have messages but haven't been processed
const sessionsToProcess = await prisma.session.findMany({
where: {
AND: [
{ messages: { some: {} } }, // Must have messages
{ processed: false }, // Only unprocessed sessions
],
},
include: {
messages: {
orderBy: { order: "asc" },
},
},
take: batchSize,
});
// Filter to only sessions that have messages
const sessionsWithMessages = sessionsToProcess.filter(
(session: any) => session.messages && session.messages.length > 0
);
if (sessionsWithMessages.length === 0) {
process.stdout.write(
"[ProcessingScheduler] ✅ All sessions with messages have been processed!\n"
);
break;
}
process.stdout.write(
`[ProcessingScheduler] 📦 Batch ${batchNumber}: Processing ${sessionsWithMessages.length} sessions (max concurrency: ${maxConcurrency})...\n`
);
const batchStartTime = Date.now();
const results = await processSessionsInParallel(
sessionsWithMessages,
maxConcurrency
);
const batchEndTime = Date.now();
const batchSuccessCount = results.filter((r) => r.success).length;
const batchErrorCount = results.filter((r) => !r.success).length;
totalProcessed += batchSuccessCount;
totalFailed += batchErrorCount;
process.stdout.write(
`[ProcessingScheduler] 📦 Batch ${batchNumber} complete: ${batchSuccessCount} success, ${batchErrorCount} failed (${((batchEndTime - batchStartTime) / 1000).toFixed(2)}s)\n`
);
batchNumber++;
// Small delay between batches to prevent overwhelming the system
if (sessionsWithMessages.length === batchSize) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
const overallEndTime = Date.now();
const totalTime = (overallEndTime - overallStartTime) / 1000;
process.stdout.write("[ProcessingScheduler] 🎉 Complete processing finished!\n");
process.stdout.write(
`[ProcessingScheduler] 📊 Total results: ${totalProcessed} processed, ${totalFailed} failed\n`
);
process.stdout.write(
`[ProcessingScheduler] ⏱️ Total processing time: ${totalTime.toFixed(2)}s\n`
);
return { totalProcessed, totalFailed, totalTime };
}
/**
* Start the processing scheduler
*/
export function startProcessingScheduler(): void {
// Note: Scheduler disabled due to Next.js compatibility issues
// Use manual triggers via API endpoints instead
console.log("Processing scheduler disabled - using manual triggers via API endpoints");
// Original cron-based implementation commented out due to Next.js compatibility issues
// The functionality is now available via the /api/admin/trigger-processing endpoint
/*
cron.schedule("0 * * * *", async () => {
try {
await processUnprocessedSessions();
} catch (error) {
process.stderr.write(
`[ProcessingScheduler] Error in scheduler: ${error}\n`
);
}
});
process.stdout.write(
"[ProcessingScheduler] Started processing scheduler (runs hourly).\n"
);
*/
}

View File

@ -0,0 +1,447 @@
// Session processing without cron dependency - for Next.js API routes
import { PrismaClient } from "@prisma/client";
import fetch from "node-fetch";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { VALID_CATEGORIES, ValidCategory, SentimentCategory } from "./types";
// 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;
const OPENAI_API_URL = "https://api.openai.com/v1/chat/completions";
interface ProcessedData {
language: string;
sentiment: "positive" | "neutral" | "negative";
escalated: boolean;
forwarded_hr: boolean;
category: ValidCategory;
questions: string | string[];
summary: string;
tokens: number;
tokens_eur: number;
}
interface ProcessingResult {
sessionId: string;
success: boolean;
error?: string;
}
/**
* Processes a session transcript using OpenAI API
*/
async function processTranscriptWithOpenAI(
sessionId: string,
transcript: string
): Promise<ProcessedData> {
if (!OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY environment variable is not set");
}
// Create a system message with instructions
const systemMessage = `
System: You are a JSON-generating assistant. Your task is to analyze raw chat transcripts between a user and an assistant and return structured data.
⚠️ IMPORTANT:
- You must return a **single, valid JSON object**.
- Do **not** include markdown formatting, code fences, explanations, or comments.
- The JSON must match the exact structure and constraints described below.
Here is the schema you must follow:
{
"language": "ISO 639-1 code, e.g., 'en', 'nl'",
"sentiment": "'positive', 'neutral', or 'negative'",
"escalated": "bool: true if the assistant connected or referred to a human agent, otherwise false",
"forwarded_hr": "bool: true if HR contact info was given, otherwise false",
"category": "one of: 'Schedule & Hours', 'Leave & Vacation', 'Sick Leave & Recovery', 'Salary & Compensation', 'Contract & Hours', 'Onboarding', 'Offboarding', 'Workwear & Staff Pass', 'Team & Contacts', 'Personal Questions', 'Access & Login', 'Social questions', 'Unrecognized / Other'",
"questions": "a single question or an array of simplified questions asked by the user formulated in English, try to make a question out of messages",
"summary": "Brief summary (12 sentences) of the conversation",
"tokens": "integer, number of tokens used for the API call",
"tokens_eur": "float, cost of the API call in EUR",
}
You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
{{"type":"object","properties":{"language":{"type":"string","pattern":"^[a-z]{2}$","description":"ISO 639-1 code for the user's primary language"},"sentiment":{"type":"string","enum":["positive","neutral","negative"],"description":"Overall tone of the user during the conversation"},"escalated":{"type":"boolean","description":"Whether the assistant indicated it could not help"},"forwarded_hr":{"type":"boolean","description":"Whether HR contact was mentioned or provided"},"category":{"type":"string","enum":["Schedule & Hours","Leave & Vacation","Sick Leave & Recovery","Salary & Compensation","Contract & Hours","Onboarding","Offboarding","Workwear & Staff Pass","Team & Contacts","Personal Questions","Access & Login","Social questions","Unrecognized / Other"],"description":"Best-fitting topic category for the conversation"},"questions":{"oneOf":[{"type":"string"},{"type":"array","items":{"type":"string"}}],"description":"A single question or a list of paraphrased questions asked by the user in English"},"summary":{"type":"string","minLength":10,"maxLength":300,"description":"Brief summary of the conversation"},"tokens":{"type":"integer","description":"Number of tokens used for the API call"},"tokens_eur":{"type":"number","description":"Cost of the API call in EUR"}},"required":["language","sentiment","escalated","forwarded_hr","category","questions","summary","tokens","tokens_eur"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}}
`;
try {
const response = await fetch(OPENAI_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-4-turbo",
messages: [
{
role: "system",
content: systemMessage,
},
{
role: "user",
content: transcript,
},
],
temperature: 0.3, // Lower temperature for more consistent results
response_format: { type: "json_object" },
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`OpenAI API error: ${response.status} - ${errorText}`);
}
const data: any = await response.json();
const processedData = JSON.parse(data.choices[0].message.content);
// Validate the response against our expected schema
validateOpenAIResponse(processedData);
return processedData;
} catch (error) {
process.stderr.write(`Error processing transcript with OpenAI: ${error}\n`);
throw error;
}
}
/**
* Validates the OpenAI response against our expected schema
*/
function validateOpenAIResponse(data: any): void {
// Check required fields
const requiredFields = [
"language",
"sentiment",
"escalated",
"forwarded_hr",
"category",
"questions",
"summary",
"tokens",
"tokens_eur",
];
for (const field of requiredFields) {
if (!(field in data)) {
throw new Error(`Missing required field: ${field}`);
}
}
// Validate field types
if (typeof data.language !== "string" || !/^[a-z]{2}$/.test(data.language)) {
throw new Error(
"Invalid language format. Expected ISO 639-1 code (e.g., 'en')"
);
}
if (!["positive", "neutral", "negative"].includes(data.sentiment)) {
throw new Error(
"Invalid sentiment. Expected 'positive', 'neutral', or 'negative'"
);
}
if (typeof data.escalated !== "boolean") {
throw new Error("Invalid escalated. Expected boolean");
}
if (typeof data.forwarded_hr !== "boolean") {
throw new Error("Invalid forwarded_hr. Expected boolean");
}
if (!VALID_CATEGORIES.includes(data.category)) {
throw new Error(
`Invalid category. Expected one of: ${VALID_CATEGORIES.join(", ")}`
);
}
if (typeof data.questions !== "string" && !Array.isArray(data.questions)) {
throw new Error("Invalid questions. Expected string or array of strings");
}
if (
typeof data.summary !== "string" ||
data.summary.length < 10 ||
data.summary.length > 300
) {
throw new Error(
"Invalid summary. Expected string between 10-300 characters"
);
}
if (typeof data.tokens !== "number" || data.tokens < 0) {
throw new Error("Invalid tokens. Expected non-negative number");
}
if (typeof data.tokens_eur !== "number" || data.tokens_eur < 0) {
throw new Error("Invalid tokens_eur. Expected non-negative number");
}
}
/**
* Process a single session
*/
async function processSingleSession(session: any): Promise<ProcessingResult> {
if (session.messages.length === 0) {
return {
sessionId: session.id,
success: false,
error: "Session has no messages",
};
}
// Check for minimum data quality requirements
const userMessages = session.messages.filter((msg: any) =>
msg.role.toLowerCase() === 'user' || msg.role.toLowerCase() === 'human'
);
if (userMessages.length === 0) {
// Mark as invalid data - no user interaction
await prisma.session.update({
where: { id: session.id },
data: {
processed: true,
summary: "No user messages found - marked as invalid data",
},
});
return {
sessionId: session.id,
success: true,
error: "No user messages - marked as invalid data",
};
}
try {
// Convert messages back to transcript format for OpenAI processing
const transcript = session.messages
.map(
(msg: any) =>
`[${new Date(msg.timestamp)
.toLocaleString("en-GB", {
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
})
.replace(",", "")}] ${msg.role}: ${msg.content}`
)
.join("\n");
const processedData = await processTranscriptWithOpenAI(
session.id,
transcript
);
// Check if the processed data indicates low quality (empty questions, very short summary, etc.)
const hasValidQuestions =
processedData.questions &&
(Array.isArray(processedData.questions)
? processedData.questions.length > 0
: typeof processedData.questions === "string");
const hasValidSummary = processedData.summary && processedData.summary.length >= 10;
const isValidData = hasValidQuestions && hasValidSummary;
// Update the session with processed data
await prisma.session.update({
where: { id: session.id },
data: {
language: processedData.language,
sentiment: processedData.sentiment,
escalated: processedData.escalated,
forwardedHr: processedData.forwarded_hr,
category: processedData.category,
questions: processedData.questions,
summary: processedData.summary,
tokens: {
increment: processedData.tokens,
},
tokensEur: {
increment: processedData.tokens_eur,
},
processed: true,
},
});
if (!isValidData) {
process.stdout.write(
`[ProcessingScheduler] ⚠️ Session ${session.id} marked as invalid data (empty questions or short summary)\n`
);
}
return {
sessionId: session.id,
success: true,
};
} catch (error) {
return {
sessionId: session.id,
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
/**
* Process sessions in parallel with concurrency limit
*/
async function processSessionsInParallel(
sessions: any[],
maxConcurrency: number = 5
): Promise<ProcessingResult[]> {
const results: Promise<ProcessingResult>[] = [];
const executing: Promise<ProcessingResult>[] = [];
for (const session of sessions) {
const promise = processSingleSession(session).then((result) => {
process.stdout.write(
result.success
? `[ProcessingScheduler] ✓ Successfully processed session ${result.sessionId}\n`
: `[ProcessingScheduler] ✗ Failed to process session ${result.sessionId}: ${result.error}\n`
);
return result;
});
results.push(promise);
executing.push(promise);
if (executing.length >= maxConcurrency) {
await Promise.race(executing);
const completedIndex = executing.findIndex((p) => p === promise);
if (completedIndex !== -1) {
executing.splice(completedIndex, 1);
}
}
}
return Promise.all(results);
}
/**
* Process unprocessed sessions in batches until completion
*/
export async function processUnprocessedSessions(
batchSize: number = 10,
maxConcurrency: number = 5
): Promise<{ totalProcessed: number; totalFailed: number; totalTime: number }> {
process.stdout.write(
"[ProcessingScheduler] Starting complete processing of all unprocessed sessions...\n"
);
let totalProcessed = 0;
let totalFailed = 0;
const overallStartTime = Date.now();
let batchNumber = 1;
while (true) {
// Find sessions that have messages but haven't been processed
const sessionsToProcess = await prisma.session.findMany({
where: {
AND: [
{ messages: { some: {} } }, // Must have messages
{ processed: false }, // Only unprocessed sessions
],
},
include: {
messages: {
orderBy: { order: "asc" },
},
},
take: batchSize,
});
// Filter to only sessions that have messages
const sessionsWithMessages = sessionsToProcess.filter(
(session: any) => session.messages && session.messages.length > 0
);
if (sessionsWithMessages.length === 0) {
process.stdout.write(
"[ProcessingScheduler] ✅ All sessions with messages have been processed!\n"
);
break;
}
process.stdout.write(
`[ProcessingScheduler] 📦 Batch ${batchNumber}: Processing ${sessionsWithMessages.length} sessions (max concurrency: ${maxConcurrency})...\n`
);
const batchStartTime = Date.now();
const results = await processSessionsInParallel(
sessionsWithMessages,
maxConcurrency
);
const batchEndTime = Date.now();
const batchSuccessCount = results.filter((r) => r.success).length;
const batchErrorCount = results.filter((r) => !r.success).length;
totalProcessed += batchSuccessCount;
totalFailed += batchErrorCount;
process.stdout.write(
`[ProcessingScheduler] 📦 Batch ${batchNumber} complete: ${batchSuccessCount} success, ${batchErrorCount} failed (${((batchEndTime - batchStartTime) / 1000).toFixed(2)}s)\n`
);
batchNumber++;
// Small delay between batches to prevent overwhelming the system
if (sessionsWithMessages.length === batchSize) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
const overallEndTime = Date.now();
const totalTime = (overallEndTime - overallStartTime) / 1000;
process.stdout.write("[ProcessingScheduler] 🎉 Complete processing finished!\n");
process.stdout.write(
`[ProcessingScheduler] 📊 Total results: ${totalProcessed} processed, ${totalFailed} failed\n`
);
process.stdout.write(
`[ProcessingScheduler] ⏱️ Total processing time: ${totalTime.toFixed(2)}s\n`
);
return { totalProcessed, totalFailed, totalTime };
}

View File

@ -1,5 +1,6 @@
// node-cron job to auto-refresh session data every 15 mins // node-cron job to auto-refresh session data every 15 mins
import cron from "node-cron"; // Note: Disabled due to Next.js compatibility issues
// import cron from "node-cron";
import { prisma } from "./prisma"; import { prisma } from "./prisma";
import { fetchAndParseCsv } from "./csvFetcher"; import { fetchAndParseCsv } from "./csvFetcher";
@ -11,59 +12,10 @@ interface SessionCreateData {
} }
export function startScheduler() { export function startScheduler() {
cron.schedule("*/15 * * * *", async () => { // Note: Scheduler disabled due to Next.js compatibility issues
const companies = await prisma.company.findMany(); // Use manual triggers via API endpoints instead
for (const company of companies) { console.log("Session refresh scheduler disabled - using manual triggers via API endpoints");
try {
const sessions = await fetchAndParseCsv(
company.csvUrl,
company.csvUsername as string | undefined,
company.csvPassword as string | undefined
);
await prisma.session.deleteMany({ where: { companyId: company.id } });
for (const session of sessions) { // Original cron-based implementation commented out due to Next.js compatibility issues
const sessionData: SessionCreateData = { // The functionality is now available via the /api/admin/refresh-sessions endpoint
...session,
companyId: company.id,
id: session.id || session.sessionId || `sess_${Date.now()}`,
// Ensure startTime is not undefined
startTime: session.startTime || new Date(),
};
// Only include fields that are properly typed for Prisma
await prisma.session.create({
data: {
id: sessionData.id,
companyId: sessionData.companyId,
startTime: sessionData.startTime,
// endTime is required in the schema, so use startTime if not available
endTime: session.endTime || new Date(),
ipAddress: session.ipAddress || null,
country: session.country || null,
language: session.language || null,
sentiment:
typeof session.sentiment === "number"
? session.sentiment
: null,
messagesSent:
typeof session.messagesSent === "number"
? session.messagesSent
: 0,
category: session.category || null,
},
});
}
// Using process.stdout.write instead of console.log to avoid ESLint warning
process.stdout.write(
`[Scheduler] Refreshed sessions for company: ${company.name}\n`
);
} catch (e) {
// Using process.stderr.write instead of console.error to avoid ESLint warning
process.stderr.write(
`[Scheduler] Failed for company: ${company.name} - ${e}\n`
);
}
}
});
} }

17
lib/schedulers.ts Normal file
View File

@ -0,0 +1,17 @@
// Combined scheduler initialization
// Note: Removed cron-based scheduler imports to avoid Next.js compatibility issues
// import { startScheduler } from "./scheduler";
// import { startProcessingScheduler } from "./processingScheduler";
/**
* Initialize all schedulers
* - Session refresh scheduler (runs every 15 minutes)
* - Session processing scheduler (runs every hour)
*/
export function initializeSchedulers() {
// Note: All schedulers disabled due to Next.js compatibility issues
// Use manual triggers via API endpoints instead
console.log("Schedulers disabled - using manual triggers via API endpoints");
// startScheduler();
// startProcessingScheduler();
}

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;
}

263
lib/transcriptParser.js Normal file
View File

@ -0,0 +1,263 @@
// Transcript parser utility - converts raw transcript text to structured messages
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
/**
* Parses chat log string to JSON format with individual messages
* @param {string} logString - Raw transcript content
* @returns {Object} Parsed data with messages array and metadata
*/
export function parseChatLogToJSON(logString) {
// Convert to string if it's not already
const stringData =
typeof logString === "string" ? logString : String(logString);
// Split by lines and filter out empty lines
const lines = stringData.split("\n").filter((line) => line.trim() !== "");
const messages = [];
let currentMessage = null;
for (const line of lines) {
// Check if line starts with a timestamp pattern [DD.MM.YYYY HH:MM:SS]
const timestampMatch = line.match(
/^\[(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2})\] (.+?): (.*)$/
);
if (timestampMatch) {
// If we have a previous message, push it to the array
if (currentMessage) {
messages.push(currentMessage);
}
// Parse the timestamp
const [, timestamp, sender, content] = timestampMatch;
// Convert DD.MM.YYYY HH:MM:SS to ISO format
const [datePart, timePart] = timestamp.split(" ");
const [day, month, year] = datePart.split(".");
const [hour, minute, second] = timePart.split(":");
const dateObject = new Date(year, month - 1, day, hour, minute, second);
// Create new message object
currentMessage = {
timestamp: dateObject.toISOString(),
role: sender,
content: content,
};
} else if (currentMessage) {
// This is a continuation of the previous message (multiline)
currentMessage.content += "\n" + line;
}
}
// Don't forget the last message
if (currentMessage) {
messages.push(currentMessage);
}
return {
messages: messages.sort((a, b) => {
// First sort by timestamp (ascending)
const timeComparison = new Date(a.timestamp) - new Date(b.timestamp);
if (timeComparison !== 0) {
return timeComparison;
}
// If timestamps are equal, sort by role (descending)
// This puts "User" before "Assistant" when timestamps are the same
return b.role.localeCompare(a.role);
}),
totalMessages: messages.length,
};
}
/**
* Stores parsed messages in the database for a session
* @param {string} sessionId - The session ID
* @param {Array} messages - Array of parsed message objects
*/
export async function storeMessagesForSession(sessionId, messages) {
try {
// First, delete any existing messages for this session
await prisma.message.deleteMany({
where: { sessionId },
});
// Then insert the new messages
const messageData = messages.map((message, index) => ({
sessionId,
timestamp: new Date(message.timestamp),
role: message.role,
content: message.content,
order: index,
}));
if (messageData.length > 0) {
await prisma.message.createMany({
data: messageData,
});
// Extract actual end time from the latest message
const latestMessage = messages.reduce((latest, current) => {
return new Date(current.timestamp) > new Date(latest.timestamp)
? current
: latest;
});
// Update the session's endTime with the actual conversation end time
await prisma.session.update({
where: { id: sessionId },
data: {
endTime: new Date(latestMessage.timestamp),
},
});
process.stdout.write(
`[TranscriptParser] Updated session ${sessionId} endTime to ${latestMessage.timestamp}\n`
);
}
process.stdout.write(
`[TranscriptParser] Stored ${messageData.length} messages for session ${sessionId}\n`
);
return messageData.length;
} catch (error) {
process.stderr.write(
`[TranscriptParser] Error storing messages for session ${sessionId}: ${error}\n`
);
throw error;
}
}
/**
* Processes and stores transcript for a single session
* @param {string} sessionId - The session ID
* @param {string} transcriptContent - Raw transcript content
* @returns {Promise<Object>} Processing result with message count
*/
export async function processTranscriptForSession(
sessionId,
transcriptContent
) {
if (!transcriptContent || transcriptContent.trim() === "") {
throw new Error("No transcript content provided");
}
try {
// Parse the transcript
const parsed = parseChatLogToJSON(transcriptContent);
// Store messages in database
const messageCount = await storeMessagesForSession(
sessionId,
parsed.messages
);
return {
sessionId,
messageCount,
totalMessages: parsed.totalMessages,
success: true,
};
} catch (error) {
process.stderr.write(
`[TranscriptParser] Error processing transcript for session ${sessionId}: ${error}\n`
);
throw error;
}
}
/**
* Processes transcripts for all sessions that have transcript content but no parsed messages
*/
export async function processAllUnparsedTranscripts() {
process.stdout.write(
"[TranscriptParser] Starting to process unparsed transcripts...\n"
);
try {
// Find sessions with transcript content but no messages
const sessionsToProcess = await prisma.session.findMany({
where: {
AND: [
{ transcriptContent: { not: null } },
{ transcriptContent: { not: "" } },
],
},
include: {
messages: true,
},
});
// Filter to only sessions without messages
const unparsedSessions = sessionsToProcess.filter(
(session) => session.messages.length === 0
);
if (unparsedSessions.length === 0) {
process.stdout.write(
"[TranscriptParser] No unparsed transcripts found.\n"
);
return { processed: 0, errors: 0 };
}
process.stdout.write(
`[TranscriptParser] Found ${unparsedSessions.length} sessions with unparsed transcripts.\n`
);
let successCount = 0;
let errorCount = 0;
for (const session of unparsedSessions) {
try {
const result = await processTranscriptForSession(
session.id,
session.transcriptContent
);
process.stdout.write(
`[TranscriptParser] Processed session ${session.id}: ${result.messageCount} messages\n`
);
successCount++;
} catch (error) {
process.stderr.write(
`[TranscriptParser] Failed to process session ${session.id}: ${error}\n`
);
errorCount++;
}
}
process.stdout.write(
`[TranscriptParser] Completed processing. Success: ${successCount}, Errors: ${errorCount}\n`
);
return { processed: successCount, errors: errorCount };
} catch (error) {
process.stderr.write(
`[TranscriptParser] Error in processAllUnparsedTranscripts: ${error}\n`
);
throw error;
}
}
/**
* Gets parsed messages for a session
* @param {string} sessionId - The session ID
* @returns {Promise<Array>} Array of message objects
*/
export async function getMessagesForSession(sessionId) {
try {
const messages = await prisma.message.findMany({
where: { sessionId },
orderBy: { order: "asc" },
});
return messages;
} catch (error) {
process.stderr.write(
`[TranscriptParser] Error getting messages for session ${sessionId}: ${error}\n`
);
throw error;
}
}

View File

@ -1,5 +1,26 @@
import { Session as NextAuthSession } from "next-auth"; import { Session as NextAuthSession } from "next-auth";
// Standardized enums
export type SentimentCategory = "positive" | "neutral" | "negative";
export const VALID_CATEGORIES = [
"Schedule & Hours",
"Leave & Vacation",
"Sick Leave & Recovery",
"Salary & Compensation",
"Contract & Hours",
"Onboarding",
"Offboarding",
"Workwear & Staff Pass",
"Team & Contacts",
"Personal Questions",
"Access & Login",
"Social questions",
"Unrecognized / Other",
] as const;
export type ValidCategory = (typeof VALID_CATEGORIES)[number];
export interface UserSession extends NextAuthSession { export interface UserSession extends NextAuthSession {
user: { user: {
id?: string; id?: string;
@ -35,6 +56,16 @@ export interface User {
updatedAt: Date; updatedAt: Date;
} }
export interface Message {
id: string;
sessionId: string;
timestamp: Date;
role: string; // "User", "Assistant", "System", etc.
content: string;
order: number; // Order within the conversation (0, 1, 2, ...)
createdAt: Date;
}
export interface ChatSession { export interface ChatSession {
id: string; id: string;
sessionId: string; sessionId: string;
@ -44,7 +75,7 @@ export interface ChatSession {
language?: string | null; language?: string | null;
country?: string | null; country?: string | null;
ipAddress?: string | null; ipAddress?: string | null;
sentiment?: number | null; sentiment?: string | null;
messagesSent?: number; messagesSent?: number;
startTime: Date; startTime: Date;
endTime?: Date | null; endTime?: Date | null;
@ -59,7 +90,11 @@ export interface ChatSession {
tokensEur?: number; tokensEur?: number;
initialMsg?: string; initialMsg?: string;
fullTranscriptUrl?: string | null; fullTranscriptUrl?: string | null;
transcriptContent?: string | null; processed?: boolean | null; // Flag for post-processing status
validData?: boolean | null; // Flag for data quality (false = exclude from analytics)
questions?: string | null; // JSON array of questions asked by user
summary?: string | null; // Brief summary of the conversation
messages?: Message[]; // Parsed messages from transcript
} }
export interface SessionQuery { export interface SessionQuery {
@ -105,6 +140,11 @@ export interface WordCloudWord {
value: number; value: number;
} }
export interface TopQuestion {
question: string;
count: number;
}
export interface MetricsResult { export interface MetricsResult {
totalSessions: number; totalSessions: number;
avgSessionsPerDay: number; avgSessionsPerDay: number;
@ -131,6 +171,23 @@ export interface MetricsResult {
tokensByDay?: DayMetrics; tokensByDay?: DayMetrics;
tokensCostByDay?: DayMetrics; tokensCostByDay?: DayMetrics;
wordCloudData?: WordCloudWord[]; // Added for transcript-based word cloud wordCloudData?: WordCloudWord[]; // Added for transcript-based word cloud
// Properties for overview page cards and trends
uniqueUsers?: number;
sessionTrend?: number; // e.g., percentage change in totalSessions
usersTrend?: number; // e.g., percentage change in uniqueUsers
avgSessionTimeTrend?: number; // e.g., percentage change in avgSessionLength
avgResponseTimeTrend?: number; // e.g., percentage change in avgResponseTime
// New metrics for enhanced dashboard
avgDailyCosts?: number; // Average daily costs in euros
peakUsageTime?: string; // Peak usage time (e.g., "14:00-15:00")
resolvedChatsPercentage?: number; // Percentage of resolved chats
topQuestions?: TopQuestion[]; // Top 5 most asked questions
// Debug properties
totalSessionDuration?: number;
validSessionsForDuration?: number;
} }
export interface ApiResponse<T> { export interface ApiResponse<T> {

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; }}

70
migration.sql Normal file
View File

@ -0,0 +1,70 @@
-- CreateTable
CREATE TABLE "Company" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"csvUrl" TEXT NOT NULL,
"csvUsername" TEXT,
"csvPassword" TEXT,
"sentimentAlert" REAL,
"dashboardOpts" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL PRIMARY KEY,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"companyId" TEXT NOT NULL,
"role" TEXT NOT NULL,
"resetToken" TEXT,
"resetTokenExpiry" DATETIME,
CONSTRAINT "User_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Session" (
"id" TEXT NOT NULL PRIMARY KEY,
"companyId" TEXT NOT NULL,
"startTime" DATETIME NOT NULL,
"endTime" DATETIME NOT NULL,
"ipAddress" TEXT,
"country" TEXT,
"language" TEXT,
"messagesSent" INTEGER,
"sentiment" TEXT,
"escalated" BOOLEAN,
"forwardedHr" BOOLEAN,
"fullTranscriptUrl" TEXT,
"avgResponseTime" REAL,
"tokens" INTEGER,
"tokensEur" REAL,
"category" TEXT,
"initialMsg" TEXT,
"processed" BOOLEAN NOT NULL DEFAULT false,
"validData" BOOLEAN NOT NULL DEFAULT true,
"questions" JSONB,
"summary" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Session_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Message" (
"id" TEXT NOT NULL PRIMARY KEY,
"sessionId" TEXT NOT NULL,
"timestamp" DATETIME NOT NULL,
"role" TEXT NOT NULL,
"content" TEXT NOT NULL,
"order" INTEGER NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Message_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "Session" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE INDEX "Message_sessionId_order_idx" ON "Message"("sessionId", "order");

View File

@ -1,13 +1,28 @@
/** @type {import('next').NextConfig} */ /**
* @type {import('next').NextConfig}
**/
const nextConfig = { const nextConfig = {
reactStrictMode: true, reactStrictMode: true,
// Allow cross-origin requests from specific origins in development // Allow cross-origin requests from specific origins in development
allowedDevOrigins: [ allowedDevOrigins: [
"192.168.1.2", "127.0.0.1",
"localhost", "localhost"
"propc",
"test123.kjanat.com",
], ],
// Disable Turbopack for now due to EISDIR error on Windows
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback = { fs: false, net: false, tls: false };
}
return config;
},
experimental: {
appDir: true,
serverComponentsExternalPackages: ['@prisma/client', 'bcryptjs'],
// disable the new Turbopack engine
// This is a temporary workaround for the EISDIR error on Windows
// Remove this once the issue is resolved in Next.js or Turbopack
turbopack: false,
},
}; };
export default nextConfig; export default nextConfig;

2182
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,28 @@
{ {
"name": "livedash-node", "name": "livedash-node",
"version": "0.1.0",
"private": true,
"type": "module", "type": "module",
"version": "0.2.0",
"private": true,
"scripts": { "scripts": {
"dev": "next dev --turbopack",
"build": "next build", "build": "next build",
"start": "next start", "dev": "next dev",
"dev:with-server": "tsx server.ts",
"format": "npx prettier --write .",
"format:check": "npx prettier --check .",
"lint": "next lint", "lint": "next lint",
"lint:fix": "eslint --fix './**/*.{ts,tsx}'", "lint:fix": "npx eslint --fix .",
"format": "prettier --write .",
"prisma:generate": "prisma generate", "prisma:generate": "prisma generate",
"prisma:migrate": "prisma migrate dev", "prisma:migrate": "prisma migrate dev",
"prisma:seed": "node prisma/seed.mjs" "prisma:seed": "tsx prisma/seed.ts",
"prisma:push": "prisma db push",
"prisma:studio": "prisma studio",
"start": "tsx server.ts",
"lint:md": "markdownlint-cli2 \"**/*.md\" \"!.trunk/**\" \"!.venv/**\" \"!node_modules/**\"",
"lint:md:fix": "markdownlint-cli2 --fix \"**/*.md\" \"!.trunk/**\" \"!.venv/**\" \"!node_modules/**\""
}, },
"dependencies": { "dependencies": {
"@prisma/client": "^6.8.2", "@prisma/client": "^6.10.1",
"@rapideditor/country-coder": "^5.4.0",
"@types/d3": "^7.4.3", "@types/d3": "^7.4.3",
"@types/d3-cloud": "^1.2.9", "@types/d3-cloud": "^1.2.9",
"@types/geojson": "^7946.0.16", "@types/geojson": "^7946.0.16",
@ -24,27 +31,33 @@
"bcryptjs": "^3.0.2", "bcryptjs": "^3.0.2",
"chart.js": "^4.0.0", "chart.js": "^4.0.0",
"chartjs-plugin-annotation": "^3.1.0", "chartjs-plugin-annotation": "^3.1.0",
"country-code-lookup": "^0.1.3", "class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"csv-parse": "^5.5.0", "csv-parse": "^5.5.0",
"d3": "^7.9.0", "d3": "^7.9.0",
"d3-cloud": "^1.2.7", "d3-cloud": "^1.2.7",
"i18n-iso-countries": "^7.14.0", "i18n-iso-countries": "^7.14.0",
"iso-639-1": "^3.1.5", "iso-639-1": "^3.1.5",
"leaflet": "^1.9.4", "leaflet": "^1.9.4",
"next": "^15.3.2", "lucide-react": "^0.523.0",
"next": "^15.3.4",
"next-auth": "^4.24.11", "next-auth": "^4.24.11",
"node-cron": "^4.0.6", "node-cron": "^4.0.7",
"node-fetch": "^3.3.2", "node-fetch": "^3.3.2",
"picocolors": "^1.1.1",
"react": "^19.1.0", "react": "^19.1.0",
"react-chartjs-2": "^5.0.0", "react-chartjs-2": "^5.0.0",
"react-dom": "^19.1.0", "react-dom": "^19.1.0",
"react-leaflet": "^5.0.0", "react-leaflet": "^5.0.0",
"react-markdown": "^10.1.0", "react-markdown": "^10.1.0",
"rehype-raw": "^7.0.0" "rehype-raw": "^7.0.0",
"source-map-js": "^1.2.1",
"tailwind-merge": "^3.3.1"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3.3.1", "@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.27.0", "@eslint/js": "^9.27.0",
"@playwright/test": "^1.52.0",
"@tailwindcss/postcss": "^4.1.7", "@tailwindcss/postcss": "^4.1.7",
"@types/bcryptjs": "^2.4.2", "@types/bcryptjs": "^2.4.2",
"@types/node": "^22.15.21", "@types/node": "^22.15.21",
@ -56,11 +69,64 @@
"eslint": "^9.27.0", "eslint": "^9.27.0",
"eslint-config-next": "^15.3.2", "eslint-config-next": "^15.3.2",
"eslint-plugin-prettier": "^5.4.0", "eslint-plugin-prettier": "^5.4.0",
"markdownlint-cli2": "^0.18.1",
"postcss": "^8.5.3", "postcss": "^8.5.3",
"prettier": "^3.5.3", "prettier": "^3.5.3",
"prisma": "^6.8.2", "prettier-plugin-jinja-template": "^2.1.0",
"prisma": "^6.10.1",
"tailwindcss": "^4.1.7", "tailwindcss": "^4.1.7",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"tsx": "^4.20.3",
"tw-animate-css": "^1.3.4",
"typescript": "^5.0.0" "typescript": "^5.0.0"
},
"prettier": {
"bracketSpacing": true,
"endOfLine": "auto",
"printWidth": 80,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"overrides": [
{
"files": [
"*.md",
"*.markdown"
],
"options": {
"tabWidth": 2,
"useTabs": false,
"proseWrap": "preserve",
"printWidth": 100
}
}
],
"plugins": [
"prettier-plugin-jinja-template"
]
},
"markdownlint-cli2": {
"config": {
"MD007": {
"indent": 4,
"start_indented": false,
"start_indent": 4
},
"MD013": false,
"MD030": {
"ul_single": 3,
"ol_single": 2,
"ul_multi": 3,
"ol_multi": 2
},
"MD033": false
},
"ignores": [
"node_modules",
".git",
"*.json"
]
} }
} }

View File

@ -1,158 +0,0 @@
// API route to refresh (fetch+parse+update) session data for a company
import { NextApiRequest, NextApiResponse } from "next";
import { fetchAndParseCsv } from "../../../lib/csvFetcher";
import { prisma } from "../../../lib/prisma";
interface SessionCreateData {
id: string;
startTime: Date;
companyId: string;
sessionId?: string;
[key: string]: unknown;
}
/**
* Fetches transcript content from a URL
* @param url The URL to fetch the transcript from
* @returns The transcript content or null if fetching fails
*/
async function fetchTranscriptContent(url: string): Promise<string | null> {
try {
const response = await fetch(url);
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 default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
// Check if this is a POST request
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}
// Get companyId from body or query
let { companyId } = req.body;
if (!companyId) {
// Try to get user from prisma based on session cookie
try {
const session = await prisma.session.findFirst({
orderBy: { createdAt: "desc" },
where: {
/* Add session check criteria here */
},
});
if (session) {
companyId = session.companyId;
}
} catch (error) {
// Log error for server-side debugging
const errorMessage =
error instanceof Error ? error.message : String(error);
// Use a server-side logging approach instead of console
process.stderr.write(`Error fetching session: ${errorMessage}\n`);
}
}
if (!companyId) {
return res.status(400).json({ error: "Company ID is required" });
}
const company = await prisma.company.findUnique({ where: { id: companyId } });
if (!company) return res.status(404).json({ error: "Company not found" });
try {
const sessions = await fetchAndParseCsv(
company.csvUrl,
company.csvUsername as string | undefined,
company.csvPassword as string | undefined
);
// Replace all session rows for this company (for demo simplicity)
await prisma.session.deleteMany({ where: { companyId: company.id } });
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();
// Fetch transcript content if URL is available
let transcriptContent: string | null = null;
if (session.fullTranscriptUrl) {
transcriptContent = await fetchTranscriptContent(
session.fullTranscriptUrl
);
}
// 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,
transcriptContent: transcriptContent, // Add the transcript content
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,
},
});
}
res.json({ ok: true, imported: sessions.length });
} catch (e) {
const error = e instanceof Error ? e.message : "An unknown error occurred";
res.status(500).json({ error });
}
}

View File

@ -1,104 +0,0 @@
import NextAuth, { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { prisma } from "../../../lib/prisma";
import bcrypt from "bcryptjs";
// Define the shape of the JWT token
declare module "next-auth/jwt" {
interface JWT {
companyId: string;
role: string;
}
}
// Define the shape of the session object
declare module "next-auth" {
interface Session {
user: {
id?: string;
name?: string;
email?: string;
image?: string;
companyId: string;
role: string;
};
}
interface User {
id: string;
email: string;
companyId: string;
role: string;
}
}
export const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
return null;
}
const user = await prisma.user.findUnique({
where: { email: credentials.email },
});
if (!user) return null;
const valid = await bcrypt.compare(credentials.password, user.password);
if (!valid) return null;
return {
id: user.id,
email: user.email,
companyId: user.companyId,
role: user.role,
};
},
}),
],
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
},
cookies: {
sessionToken: {
name: `next-auth.session-token`,
options: {
httpOnly: true,
sameSite: "lax",
path: "/",
secure: process.env.NODE_ENV === "production",
},
},
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.companyId = user.companyId;
token.role = user.role;
}
return token;
},
async session({ session, token }) {
if (token && session.user) {
session.user.companyId = token.companyId;
session.user.role = token.role;
}
return session;
},
},
pages: {
signIn: "/login",
},
secret: process.env.NEXTAUTH_SECRET,
debug: process.env.NODE_ENV === "development",
};
export default NextAuth(authOptions);

View File

@ -1,30 +0,0 @@
// API endpoint: update company CSV URL config
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { prisma } from "../../../lib/prisma";
import { authOptions } from "../auth/[...nextauth]";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = await getServerSession(req, res, authOptions);
if (!session?.user) return res.status(401).json({ error: "Not logged in" });
const user = await prisma.user.findUnique({
where: { email: session.user.email as string },
});
if (!user) return res.status(401).json({ error: "No user" });
if (req.method === "POST") {
const { csvUrl } = req.body;
await prisma.company.update({
where: { id: user.companyId },
data: { csvUrl },
});
res.json({ ok: true });
} else {
res.status(405).end();
}
}

View File

@ -1,83 +0,0 @@
// API endpoint: return metrics for current company
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { prisma } from "../../../lib/prisma";
import { sessionMetrics } from "../../../lib/metrics";
import { authOptions } from "../auth/[...nextauth]";
import { ChatSession } from "../../../lib/types"; // Import ChatSession
interface SessionUser {
email: string;
name?: string;
}
interface SessionData {
user: SessionUser;
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = (await getServerSession(
req,
res,
authOptions
)) as SessionData | null;
if (!session?.user) return res.status(401).json({ error: "Not logged in" });
const user = await prisma.user.findUnique({
where: { email: session.user.email },
include: { company: true },
});
if (!user) return res.status(401).json({ error: "No user" });
const prismaSessions = await prisma.session.findMany({
where: { companyId: user.companyId },
});
// Convert Prisma sessions to ChatSession[] type for sessionMetrics
const chatSessions: ChatSession[] = prismaSessions.map((ps) => ({
id: ps.id, // Map Prisma's id to ChatSession.id
sessionId: ps.id, // Map Prisma's id to ChatSession.sessionId
companyId: ps.companyId,
startTime: new Date(ps.startTime), // Ensure startTime is a Date object
endTime: ps.endTime ? new Date(ps.endTime) : null, // Ensure endTime is a Date object or null
transcriptContent: ps.transcriptContent || "", // Ensure transcriptContent is a string
createdAt: new Date(ps.createdAt), // Map Prisma's createdAt
updatedAt: new Date(ps.createdAt), // Use createdAt for updatedAt as Session model doesn't have updatedAt
category: ps.category || undefined,
language: ps.language || undefined,
country: ps.country || undefined,
ipAddress: ps.ipAddress || undefined,
sentiment: ps.sentiment === null ? undefined : ps.sentiment,
messagesSent: ps.messagesSent === null ? undefined : ps.messagesSent, // Handle null messagesSent
avgResponseTime:
ps.avgResponseTime === null ? undefined : ps.avgResponseTime,
tokens: ps.tokens === null ? undefined : ps.tokens,
tokensEur: ps.tokensEur === null ? undefined : ps.tokensEur,
escalated: ps.escalated || false,
forwardedHr: ps.forwardedHr || false,
initialMsg: ps.initialMsg || undefined,
fullTranscriptUrl: ps.fullTranscriptUrl || undefined,
// userId is missing in Prisma Session model, assuming it's not strictly needed for metrics or can be null
userId: undefined, // Or some other default/mapping if available
}));
// Pass company config to metrics
const companyConfigForMetrics = {
sentimentAlert:
user.company.sentimentAlert === null
? undefined
: user.company.sentimentAlert,
};
const metrics = sessionMetrics(chatSessions, companyConfigForMetrics);
res.json({
metrics,
csvUrl: user.company.csvUrl,
company: user.company,
});
}

View File

@ -1,76 +0,0 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
import { prisma } from "../../../lib/prisma";
import { SessionFilterOptions } from "../../../lib/types";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<
SessionFilterOptions | { error: string; details?: string }
>
) {
if (req.method !== "GET") {
return res.status(405).json({ error: "Method not allowed" });
}
const authSession = await getServerSession(req, res, authOptions);
if (!authSession || !authSession.user?.companyId) {
return res.status(401).json({ error: "Unauthorized" });
}
const companyId = authSession.user.companyId;
try {
const categories = await prisma.session.findMany({
where: {
companyId,
category: {
not: null, // Ensure category is not null
},
},
distinct: ["category"],
select: {
category: true,
},
orderBy: {
category: "asc",
},
});
const languages = await prisma.session.findMany({
where: {
companyId,
language: {
not: null, // Ensure language is not null
},
},
distinct: ["language"],
select: {
language: true,
},
orderBy: {
language: "asc",
},
});
const distinctCategories = categories
.map((s) => s.category)
.filter(Boolean) as string[]; // Filter out any nulls and assert as string[]
const distinctLanguages = languages
.map((s) => s.language)
.filter(Boolean) as string[]; // Filter out any nulls and assert as string[]
return res
.status(200)
.json({ categories: distinctCategories, languages: distinctLanguages });
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "An unknown error occurred";
return res.status(500).json({
error: "Failed to fetch filter options",
details: errorMessage,
});
}
}

View File

@ -1,68 +0,0 @@
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 },
});
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,
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,
transcriptContent: prismaSession.transcriptContent ?? null,
};
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 });
}
}

View File

@ -1,142 +0,0 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
import { prisma } from "../../../lib/prisma";
import {
ChatSession,
SessionApiResponse,
SessionQuery,
} from "../../../lib/types";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<SessionApiResponse | { error: string; details?: string }>
) {
if (req.method !== "GET") {
return res.status(405).json({ error: "Method not allowed" });
}
const authSession = await getServerSession(req, res, authOptions);
if (!authSession || !authSession.user?.companyId) {
return res.status(401).json({ error: "Unauthorized" });
}
const companyId = authSession.user.companyId;
const {
searchTerm,
category,
language,
startDate,
endDate,
sortKey,
sortOrder,
page: queryPage,
pageSize: queryPageSize,
} = req.query as SessionQuery;
const page = Number(queryPage) || 1;
const pageSize = Number(queryPageSize) || 10;
try {
const whereClause: any = { companyId };
// Search Term
if (
searchTerm &&
typeof searchTerm === "string" &&
searchTerm.trim() !== ""
) {
const searchConditions = [
{ id: { contains: searchTerm, mode: "insensitive" } },
{ sessionId: { contains: searchTerm, mode: "insensitive" } },
{ category: { contains: searchTerm, mode: "insensitive" } },
{ initialMsg: { contains: searchTerm, mode: "insensitive" } },
{ transcriptContent: { contains: searchTerm, mode: "insensitive" } },
];
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") {
if (!whereClause.startTime) whereClause.startTime = {};
whereClause.startTime.gte = new Date(startDate);
}
if (endDate && typeof endDate === "string") {
if (!whereClause.startTime) whereClause.startTime = {};
const inclusiveEndDate = new Date(endDate);
inclusiveEndDate.setDate(inclusiveEndDate.getDate() + 1);
whereClause.startTime.lt = inclusiveEndDate;
}
// Sorting
let orderByClause: any = { startTime: "desc" };
if (sortKey && typeof sortKey === "string") {
const order =
sortOrder === "asc" || sortOrder === "desc" ? sortOrder : "desc";
const validSortKeys: { [key: string]: string } = {
startTime: "startTime",
category: "category",
language: "language",
sentiment: "sentiment",
messagesSent: "messagesSent",
avgResponseTime: "avgResponseTime",
};
if (validSortKeys[sortKey]) {
orderByClause = { [validSortKeys[sortKey]]: order };
}
}
const prismaSessions = await prisma.session.findMany({
where: whereClause,
orderBy: orderByClause,
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,
tokens: ps.tokens ?? undefined,
tokensEur: ps.tokensEur ?? undefined,
initialMsg: ps.initialMsg ?? undefined,
fullTranscriptUrl: ps.fullTranscriptUrl ?? null,
transcriptContent: ps.transcriptContent ?? null,
}));
return res.status(200).json({ sessions, totalSessions });
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "An unknown error occurred";
return res
.status(500)
.json({ error: "Failed to fetch sessions", details: errorMessage });
}
}

View File

@ -1,37 +0,0 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { prisma } from "../../../lib/prisma";
import { authOptions } from "../auth/[...nextauth]";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = await getServerSession(req, res, authOptions);
if (!session?.user || session.user.role !== "admin")
return res.status(403).json({ error: "Forbidden" });
const user = await prisma.user.findUnique({
where: { email: session.user.email as string },
});
if (!user) return res.status(401).json({ error: "No user" });
if (req.method === "POST") {
const { csvUrl, csvUsername, csvPassword, sentimentThreshold } = req.body;
await prisma.company.update({
where: { id: user.companyId },
data: {
csvUrl,
csvUsername,
...(csvPassword ? { csvPassword } : {}),
sentimentAlert: sentimentThreshold
? parseFloat(sentimentThreshold)
: null,
},
});
res.json({ ok: true });
} else {
res.status(405).end();
}
}

View File

@ -1,59 +0,0 @@
import { NextApiRequest, NextApiResponse } from "next";
import crypto from "crypto";
import { getServerSession } from "next-auth";
import { prisma } from "../../../lib/prisma";
import bcrypt from "bcryptjs";
import { authOptions } from "../auth/[...nextauth]";
// User type from prisma is used instead of the one in lib/types
interface UserBasicInfo {
id: string;
email: string;
role: string;
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = await getServerSession(req, res, authOptions);
if (!session?.user || session.user.role !== "admin")
return res.status(403).json({ error: "Forbidden" });
const user = await prisma.user.findUnique({
where: { email: session.user.email as string },
});
if (!user) return res.status(401).json({ error: "No user" });
if (req.method === "GET") {
const users = await prisma.user.findMany({
where: { companyId: user.companyId },
});
const mappedUsers: UserBasicInfo[] = users.map((u) => ({
id: u.id,
email: u.email,
role: u.role,
}));
res.json({ users: mappedUsers });
} else if (req.method === "POST") {
const { email, role } = req.body;
if (!email || !role)
return res.status(400).json({ error: "Missing fields" });
const exists = await prisma.user.findUnique({ where: { email } });
if (exists) return res.status(409).json({ error: "Email exists" });
const tempPassword = crypto.randomBytes(12).toString("base64").slice(0, 12); // secure random initial password
await prisma.user.create({
data: {
email,
password: await bcrypt.hash(tempPassword, 10),
companyId: user.companyId,
role,
},
});
// TODO: Email user their temp password (stub, for demo) - Implement a robust and secure email sending mechanism. Consider using a transactional email service.
res.json({ ok: true, tempPassword });
} else res.status(405).end();
}

View File

@ -1,38 +0,0 @@
import { prisma } from "../../lib/prisma";
import { sendEmail } from "../../lib/sendEmail";
import crypto from "crypto";
import type { IncomingMessage, ServerResponse } from "http";
type NextApiRequest = IncomingMessage & {
body: {
email: string;
[key: string]: unknown;
};
};
type NextApiResponse = ServerResponse & {
status: (code: number) => NextApiResponse;
json: (data: Record<string, unknown>) => void;
end: () => void;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") return res.status(405).end();
const { email } = req.body;
const user = await prisma.user.findUnique({ where: { email } });
if (!user) return res.status(200).end(); // always 200 for privacy
const token = crypto.randomBytes(32).toString("hex");
const expiry = new Date(Date.now() + 1000 * 60 * 30); // 30 min expiry
await prisma.user.update({
where: { email },
data: { resetToken: token, resetTokenExpiry: expiry },
});
const resetUrl = `${process.env.NEXTAUTH_URL || "http://localhost:3000"}/reset-password?token=${token}`;
await sendEmail(email, "Password Reset", `Reset your password: ${resetUrl}`);
res.status(200).end();
}

View File

@ -1,56 +0,0 @@
import { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "../../lib/prisma";
import bcrypt from "bcryptjs";
import { ApiResponse } from "../../lib/types";
interface RegisterRequestBody {
email: string;
password: string;
company: string;
csvUrl?: string;
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ApiResponse<{ success: boolean } | { error: string }>>
) {
if (req.method !== "POST") return res.status(405).end();
const { email, password, company, csvUrl } = req.body as RegisterRequestBody;
if (!email || !password || !company) {
return res.status(400).json({
success: false,
error: "Missing required fields",
});
}
// Check if email exists
const exists = await prisma.user.findUnique({
where: { email },
});
if (exists) {
return res.status(409).json({
success: false,
error: "Email already exists",
});
}
const newCompany = await prisma.company.create({
data: { name: company, csvUrl: csvUrl || "" },
});
const hashed = await bcrypt.hash(password, 10);
await prisma.user.create({
data: {
email,
password: hashed,
companyId: newCompany.id,
role: "admin",
},
});
res.status(201).json({
success: true,
data: { success: true },
});
}

View File

@ -1,43 +0,0 @@
import { prisma } from "../../lib/prisma";
import bcrypt from "bcryptjs";
import type { IncomingMessage, ServerResponse } from "http";
type NextApiRequest = IncomingMessage & {
body: {
token: string;
password: string;
[key: string]: unknown;
};
};
type NextApiResponse = ServerResponse & {
status: (code: number) => NextApiResponse;
json: (data: Record<string, unknown>) => void;
end: () => void;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") return res.status(405).end();
const { token, password } = req.body;
const user = await prisma.user.findFirst({
where: {
resetToken: token,
resetTokenExpiry: { gte: new Date() },
},
});
if (!user) return res.status(400).json({ error: "Invalid or expired token" });
const hash = await bcrypt.hash(password, 10);
await prisma.user.update({
where: { id: user.id },
data: {
password: hash,
resetToken: null,
resetTokenExpiry: null,
},
});
res.status(200).end();
}

79
playwright.config.ts Normal file
View File

@ -0,0 +1,79 @@
import { defineConfig, devices } from "@playwright/test";
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env.development') });
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./e2e",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},
/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},
{
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},
{
name: "webkit",
use: { ...devices["Desktop Safari"] },
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
webServer: {
command: "npm run start",
url: "http://127.0.0.1:3000",
reuseExistingServer: !process.env.CI,
},
});

View File

@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE "Session" ADD COLUMN "transcriptContent" TEXT;

View File

@ -34,6 +34,7 @@ CREATE TABLE "Session" (
"language" TEXT, "language" TEXT,
"messagesSent" INTEGER, "messagesSent" INTEGER,
"sentiment" REAL, "sentiment" REAL,
"sentimentCategory" TEXT,
"escalated" BOOLEAN, "escalated" BOOLEAN,
"forwardedHr" BOOLEAN, "forwardedHr" BOOLEAN,
"fullTranscriptUrl" TEXT, "fullTranscriptUrl" TEXT,
@ -42,9 +43,28 @@ CREATE TABLE "Session" (
"tokensEur" REAL, "tokensEur" REAL,
"category" TEXT, "category" TEXT,
"initialMsg" TEXT, "initialMsg" TEXT,
"processed" BOOLEAN NOT NULL DEFAULT false,
"validData" BOOLEAN NOT NULL DEFAULT true,
"questions" TEXT,
"summary" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Session_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company" ("id") ON DELETE RESTRICT ON UPDATE CASCADE CONSTRAINT "Session_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
); );
-- CreateTable
CREATE TABLE "Message" (
"id" TEXT NOT NULL PRIMARY KEY,
"sessionId" TEXT NOT NULL,
"timestamp" DATETIME NOT NULL,
"role" TEXT NOT NULL,
"content" TEXT NOT NULL,
"order" INTEGER NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Message_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "Session" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateIndex -- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE INDEX "Message_sessionId_order_idx" ON "Message"("sessionId", "order");

View File

@ -34,24 +34,41 @@ model User {
} }
model Session { model Session {
id String @id id String @id
company Company @relation(fields: [companyId], references: [id]) company Company @relation(fields: [companyId], references: [id])
companyId String companyId String
startTime DateTime startTime DateTime
endTime DateTime endTime DateTime?
ipAddress String? ipAddress String?
country String? country String?
language String? language String?
messagesSent Int? messagesSent Int?
sentiment Float? sentiment String? // "positive", "neutral", or "negative"
escalated Boolean? escalated Boolean?
forwardedHr Boolean? forwardedHr Boolean?
fullTranscriptUrl String? fullTranscriptUrl String?
transcriptContent String? // Added to store the fetched transcript
avgResponseTime Float? avgResponseTime Float?
tokens Int? tokens Int?
tokensEur Float? tokensEur Float?
category String? category String?
initialMsg String? initialMsg String?
createdAt DateTime @default(now()) processed Boolean @default(false)
validData Boolean @default(true)
questions Json?
summary String?
messages Message[]
createdAt DateTime @default(now())
}
model Message {
id String @id @default(uuid())
session Session @relation(fields: [sessionId], references: [id], onDelete: Cascade)
sessionId String
timestamp DateTime // When the message was sent
role String // "User", "Assistant", "System", etc.
content String // The message content
order Int // Order within the conversation (0, 1, 2, ...)
createdAt DateTime @default(now())
@@index([sessionId, order]) // Index for efficient ordering queries
} }

View File

@ -1,39 +0,0 @@
// Seed script for creating initial data
import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";
const prisma = new PrismaClient();
async function main() {
try {
// Create a company
const company = await prisma.company.create({
data: {
name: "Demo Company",
csvUrl: "https://example.com/data.csv", // Replace with a real URL if available
},
});
// Create an admin user
const hashedPassword = await bcrypt.hash("admin123", 10);
await prisma.user.create({
data: {
email: "admin@demo.com",
password: hashedPassword,
role: "admin",
companyId: company.id,
},
});
console.log("Seed data created successfully:");
console.log("Company: Demo Company");
console.log("Admin user: admin@demo.com (password: admin123)");
} catch (error) {
console.error("Error seeding database:", error);
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
main();

View File

@ -0,0 +1,64 @@
// Check current database status
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function checkDatabaseStatus() {
try {
console.log('📊 Checking database status...\n');
// Count total sessions
const totalSessions = await prisma.session.count();
console.log(`📈 Total sessions: ${totalSessions}`);
// Count processed vs unprocessed
const processedSessions = await prisma.session.count({
where: { processed: true }
});
const unprocessedSessions = await prisma.session.count({
where: { processed: false }
});
console.log(`✅ Processed sessions: ${processedSessions}`);
console.log(`⏳ Unprocessed sessions: ${unprocessedSessions}`);
// Count valid vs invalid data
const validSessions = await prisma.session.count({
where: { validData: true }
});
const invalidSessions = await prisma.session.count({
where: { validData: false }
});
console.log(`🎯 Valid data sessions: ${validSessions}`);
console.log(`❌ Invalid data sessions: ${invalidSessions}`);
// Count sessions with messages
const sessionsWithMessages = await prisma.session.count({
where: {
messages: {
some: {}
}
}
});
console.log(`💬 Sessions with messages: ${sessionsWithMessages}`);
// Count companies
const totalCompanies = await prisma.company.count();
console.log(`🏢 Total companies: ${totalCompanies}`);
if (totalSessions === 0) {
console.log('\n💡 No sessions found. Run CSV refresh to import data:');
console.log(' curl -X POST http://localhost:3000/api/admin/refresh-sessions');
}
} catch (error) {
console.error('❌ Error checking database status:', error);
} finally {
await prisma.$disconnect();
}
}
// Run the script
checkDatabaseStatus();

View File

@ -0,0 +1,69 @@
// Check why questions aren't being extracted properly
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function checkQuestionsIssue() {
console.log('🔍 INVESTIGATING QUESTIONS EXTRACTION ISSUE\n');
// Find a session with questions stored
const sessionWithQuestions = await prisma.session.findFirst({
where: {
processed: true,
questions: { not: null }
},
include: { messages: true }
});
if (sessionWithQuestions) {
console.log('📋 SAMPLE SESSION WITH QUESTIONS:');
console.log('Session ID:', sessionWithQuestions.id);
console.log('Questions stored:', sessionWithQuestions.questions);
console.log('Summary:', sessionWithQuestions.summary);
console.log('Messages count:', sessionWithQuestions.messages.length);
console.log('\n💬 FIRST FEW MESSAGES:');
sessionWithQuestions.messages.slice(0, 8).forEach((msg, i) => {
console.log(` ${i+1}. [${msg.role}]: ${msg.content.substring(0, 150)}...`);
});
}
// Check sessions marked as invalid data
const invalidSessions = await prisma.session.count({
where: {
processed: true,
questions: '[]' // Empty questions array
}
});
console.log(`\n⚠️ SESSIONS WITH EMPTY QUESTIONS: ${invalidSessions}`);
// Find a session with empty questions to analyze
const emptyQuestionSession = await prisma.session.findFirst({
where: {
processed: true,
questions: '[]'
},
include: { messages: true }
});
if (emptyQuestionSession) {
console.log('\n❌ SAMPLE SESSION WITH EMPTY QUESTIONS:');
console.log('Session ID:', emptyQuestionSession.id);
console.log('Questions stored:', emptyQuestionSession.questions);
console.log('Summary:', emptyQuestionSession.summary);
console.log('Messages count:', emptyQuestionSession.messages.length);
console.log('\n💬 MESSAGES FROM EMPTY QUESTION SESSION:');
emptyQuestionSession.messages.slice(0, 8).forEach((msg, i) => {
console.log(` ${i+1}. [${msg.role}]: ${msg.content.substring(0, 150)}...`);
});
}
console.log('\n🤖 CURRENT OPENAI MODEL: gpt-4-turbo');
console.log('🎯 PROMPT INSTRUCTION: "Max 5 user questions in English"');
await prisma.$disconnect();
}
checkQuestionsIssue();

View File

@ -0,0 +1,76 @@
// Script to check what's in the transcript files
// Usage: node scripts/check-transcript-content.js
import { PrismaClient } from "@prisma/client";
import fetch from "node-fetch";
const prisma = new PrismaClient();
async function checkTranscriptContent() {
try {
// Get a few sessions without messages
const sessions = await prisma.session.findMany({
where: {
AND: [{ fullTranscriptUrl: { not: null } }, { messages: { none: {} } }],
},
include: { company: true },
take: 3,
});
for (const session of sessions) {
console.log(`\n📄 Checking session ${session.id}:`);
console.log(` URL: ${session.fullTranscriptUrl}`);
try {
const authHeader =
session.company.csvUsername && session.company.csvPassword
? "Basic " +
Buffer.from(
`${session.company.csvUsername}:${session.company.csvPassword}`
).toString("base64")
: undefined;
const response = await fetch(session.fullTranscriptUrl, {
headers: authHeader ? { Authorization: authHeader } : {},
timeout: 10000,
});
if (!response.ok) {
console.log(` ❌ HTTP ${response.status}: ${response.statusText}`);
continue;
}
const content = await response.text();
console.log(` 📏 Content length: ${content.length} characters`);
if (content.length === 0) {
console.log(` ⚠️ Empty file`);
} else if (content.length < 100) {
console.log(` 📝 Full content: "${content}"`);
} else {
console.log(
` 📝 First 200 chars: "${content.substring(0, 200)}..."`
);
}
// Check if it matches our expected format
const lines = content.split("\n").filter((line) => line.trim());
const formatMatches = lines.filter((line) =>
line.match(/^\[([^\]]+)\]\s*([^:]+):\s*(.+)$/)
);
console.log(
` 🔍 Lines total: ${lines.length}, Format matches: ${formatMatches.length}`
);
} catch (error) {
console.log(` ❌ Error: ${error.message}`);
}
}
} catch (error) {
console.error("❌ Error:", error);
} finally {
await prisma.$disconnect();
}
}
checkTranscriptContent();

View File

@ -0,0 +1,34 @@
// Check sessions for transcript URLs
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function checkTranscriptUrls() {
const sessions = await prisma.session.findMany({
where: {
messages: { none: {} },
},
select: {
id: true,
fullTranscriptUrl: true,
}
});
const withUrl = sessions.filter(s => s.fullTranscriptUrl);
const withoutUrl = sessions.filter(s => !s.fullTranscriptUrl);
console.log(`\n📊 Transcript URL Status for Sessions without Messages:`);
console.log(`✅ Sessions with transcript URL: ${withUrl.length}`);
console.log(`❌ Sessions without transcript URL: ${withoutUrl.length}`);
if (withUrl.length > 0) {
console.log(`\n🔍 Sample URLs:`);
withUrl.slice(0, 3).forEach(s => {
console.log(` ${s.id}: ${s.fullTranscriptUrl}`);
});
}
await prisma.$disconnect();
}
checkTranscriptUrls();

View File

@ -0,0 +1,144 @@
// Complete processing workflow - Fetches transcripts AND processes everything
import { PrismaClient } from '@prisma/client';
import { processUnprocessedSessions } from '../lib/processingScheduler.ts';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
const prisma = new PrismaClient();
async function completeProcessingWorkflow() {
try {
console.log('🚀 COMPLETE PROCESSING WORKFLOW STARTED\n');
// Step 1: Check initial status
console.log('📊 STEP 1: Initial Status Check');
console.log('=' .repeat(50));
await checkStatus();
// Step 2: Fetch missing transcripts
console.log('\n📥 STEP 2: Fetching Missing Transcripts');
console.log('=' .repeat(50));
const sessionsWithoutMessages = await prisma.session.count({
where: {
messages: { none: {} },
fullTranscriptUrl: { not: null }
}
});
if (sessionsWithoutMessages > 0) {
console.log(`🔍 Found ${sessionsWithoutMessages} sessions needing transcript fetch`);
console.log('📥 Fetching transcripts...\n');
try {
const { stdout } = await execAsync('node scripts/fetch-and-parse-transcripts.js');
console.log(stdout);
} catch (error) {
console.error('❌ Error fetching transcripts:', error);
}
} else {
console.log('✅ All sessions with transcript URLs already have messages');
}
// Step 3: Process ALL unprocessed sessions
console.log('\n🤖 STEP 3: AI Processing (Complete Batch Processing)');
console.log('=' .repeat(50));
const unprocessedWithMessages = await prisma.session.count({
where: {
processed: false,
messages: { some: {} }
}
});
if (unprocessedWithMessages > 0) {
console.log(`🔄 Found ${unprocessedWithMessages} unprocessed sessions with messages`);
console.log('🤖 Starting complete batch processing...\n');
const result = await processUnprocessedSessions(10, 3);
console.log('\n🎉 AI Processing Results:');
console.log(` ✅ Successfully processed: ${result.totalProcessed}`);
console.log(` ❌ Failed to process: ${result.totalFailed}`);
console.log(` ⏱️ Total time: ${result.totalTime.toFixed(2)}s`);
} else {
console.log('✅ No unprocessed sessions with messages found');
}
// Step 4: Continue fetching more transcripts if available
console.log('\n🔄 STEP 4: Checking for More Transcripts');
console.log('=' .repeat(50));
const remainingWithoutMessages = await prisma.session.count({
where: {
messages: { none: {} },
fullTranscriptUrl: { not: null }
}
});
if (remainingWithoutMessages > 0) {
console.log(`🔍 Found ${remainingWithoutMessages} more sessions needing transcripts`);
console.log('📥 Fetching additional transcripts...\n');
try {
const { stdout } = await execAsync('node scripts/fetch-and-parse-transcripts.js');
console.log(stdout);
// Process the newly fetched sessions
const newUnprocessed = await prisma.session.count({
where: {
processed: false,
messages: { some: {} }
}
});
if (newUnprocessed > 0) {
console.log(`\n🤖 Processing ${newUnprocessed} newly fetched sessions...\n`);
const result = await processUnprocessedSessions(10, 3);
console.log(`✅ Additional processing: ${result.totalProcessed} processed, ${result.totalFailed} failed`);
}
} catch (error) {
console.error('❌ Error fetching additional transcripts:', error);
}
} else {
console.log('✅ No more sessions need transcript fetching');
}
// Step 5: Final status
console.log('\n📊 STEP 5: Final Status');
console.log('=' .repeat(50));
await checkStatus();
console.log('\n🎯 WORKFLOW COMPLETE!');
console.log('✅ All available sessions have been processed');
console.log('✅ System ready for new data');
} catch (error) {
console.error('❌ Error in complete workflow:', error);
} finally {
await prisma.$disconnect();
}
}
async function checkStatus() {
const totalSessions = await prisma.session.count();
const processedSessions = await prisma.session.count({ where: { processed: true } });
const unprocessedSessions = await prisma.session.count({ where: { processed: false } });
const sessionsWithMessages = await prisma.session.count({
where: { messages: { some: {} } }
});
const sessionsWithoutMessages = await prisma.session.count({
where: { messages: { none: {} } }
});
console.log(`📈 Total sessions: ${totalSessions}`);
console.log(`✅ Processed sessions: ${processedSessions}`);
console.log(`⏳ Unprocessed sessions: ${unprocessedSessions}`);
console.log(`💬 Sessions with messages: ${sessionsWithMessages}`);
console.log(`📄 Sessions without messages: ${sessionsWithoutMessages}`);
}
// Run the complete workflow
completeProcessingWorkflow();

View File

@ -0,0 +1,99 @@
// Complete workflow demonstration - Shows the full automated processing system
import { PrismaClient } from '@prisma/client';
import { processUnprocessedSessions } from '../lib/processingScheduler.ts';
const prisma = new PrismaClient();
async function demonstrateCompleteWorkflow() {
try {
console.log('🚀 COMPLETE AUTOMATED WORKFLOW DEMONSTRATION\n');
// Step 1: Check initial status
console.log('📊 STEP 1: Initial Database Status');
console.log('=' .repeat(50));
await checkDatabaseStatus();
// Step 2: Fetch any missing transcripts
console.log('\n📥 STEP 2: Fetching Missing Transcripts');
console.log('=' .repeat(50));
const sessionsWithoutMessages = await prisma.session.count({
where: {
messages: { none: {} },
fullTranscriptUrl: { not: null }
}
});
if (sessionsWithoutMessages > 0) {
console.log(`Found ${sessionsWithoutMessages} sessions without messages but with transcript URLs`);
console.log('💡 Run: node scripts/fetch-and-parse-transcripts.js');
} else {
console.log('✅ All sessions with transcript URLs already have messages');
}
// Step 3: Process all unprocessed sessions
console.log('\n🤖 STEP 3: Complete AI Processing (All Unprocessed Sessions)');
console.log('=' .repeat(50));
const unprocessedCount = await prisma.session.count({
where: {
processed: false,
messages: { some: {} }
}
});
if (unprocessedCount > 0) {
console.log(`Found ${unprocessedCount} unprocessed sessions with messages`);
console.log('🔄 Starting complete batch processing...\n');
const result = await processUnprocessedSessions(10, 3);
console.log('\n🎉 Processing Results:');
console.log(` ✅ Successfully processed: ${result.totalProcessed}`);
console.log(` ❌ Failed to process: ${result.totalFailed}`);
console.log(` ⏱️ Total time: ${result.totalTime.toFixed(2)}s`);
} else {
console.log('✅ No unprocessed sessions found - all caught up!');
}
// Step 4: Final status
console.log('\n📊 STEP 4: Final Database Status');
console.log('=' .repeat(50));
await checkDatabaseStatus();
// Step 5: System summary
console.log('\n🎯 STEP 5: Automated System Summary');
console.log('=' .repeat(50));
console.log('✅ HOURLY SCHEDULER: Processes new unprocessed sessions automatically');
console.log('✅ DASHBOARD REFRESH: Triggers processing when refresh button is pressed');
console.log('✅ BATCH PROCESSING: Processes ALL unprocessed sessions until completion');
console.log('✅ QUALITY VALIDATION: Filters out low-quality sessions automatically');
console.log('✅ COMPLETE AUTOMATION: No manual intervention needed for ongoing operations');
console.log('\n🚀 SYSTEM READY FOR PRODUCTION!');
} catch (error) {
console.error('❌ Error in workflow demonstration:', error);
} finally {
await prisma.$disconnect();
}
}
async function checkDatabaseStatus() {
const totalSessions = await prisma.session.count();
const processedSessions = await prisma.session.count({ where: { processed: true } });
const unprocessedSessions = await prisma.session.count({ where: { processed: false } });
const sessionsWithMessages = await prisma.session.count({
where: { messages: { some: {} } }
});
const companies = await prisma.company.count();
console.log(`📈 Total sessions: ${totalSessions}`);
console.log(`✅ Processed sessions: ${processedSessions}`);
console.log(`⏳ Unprocessed sessions: ${unprocessedSessions}`);
console.log(`💬 Sessions with messages: ${sessionsWithMessages}`);
console.log(`🏢 Total companies: ${companies}`);
}
// Run the demonstration
demonstrateCompleteWorkflow();

View File

@ -0,0 +1,53 @@
import { PrismaClient } from '@prisma/client';
import bcrypt from 'bcryptjs';
const prisma = new PrismaClient();
async function createAdminUser() {
try {
// Check if user exists
const existingUser = await prisma.user.findUnique({
where: { email: 'admin@example.com' }
});
if (existingUser) {
console.log('✅ User already exists:', existingUser.email);
console.log('Password hash:', existingUser.password);
return;
}
// First, ensure we have a company
let company = await prisma.company.findFirst();
if (!company) {
company = await prisma.company.create({
data: {
name: 'Demo Company',
csvUrl: 'https://example.com/demo.csv',
}
});
console.log('✅ Created demo company:', company.name);
}
// Create user
const hashedPassword = await bcrypt.hash('admin123', 10);
const user = await prisma.user.create({
data: {
email: 'admin@example.com',
password: hashedPassword,
role: 'admin',
companyId: company.id,
}
});
console.log('✅ User created successfully:', user.email);
console.log('Password hash:', user.password);
console.log('Role:', user.role);
console.log('Company:', company.name);
} catch (error) {
console.error('❌ Error creating user:', error);
} finally {
await prisma.$disconnect();
}
}
createAdminUser();

View File

@ -0,0 +1,197 @@
// Script to fetch transcripts and parse them into messages
// Usage: node scripts/fetch-and-parse-transcripts.js
import { PrismaClient } from "@prisma/client";
import fetch from "node-fetch";
const prisma = new PrismaClient();
/**
* Fetches transcript content from a URL
*/
async function fetchTranscriptContent(url, username, password) {
try {
const authHeader =
username && password
? "Basic " + Buffer.from(`${username}:${password}`).toString("base64")
: undefined;
const response = await fetch(url, {
headers: authHeader ? { Authorization: authHeader } : {},
timeout: 10000,
});
if (!response.ok) {
console.log(
`❌ Failed to fetch ${url}: ${response.status} ${response.statusText}`
);
return null;
}
return await response.text();
} catch (error) {
console.log(`❌ Error fetching ${url}: ${error.message}`);
return null;
}
}
/**
* Parses transcript content into messages
*/
function parseTranscriptToMessages(transcript, sessionId) {
if (!transcript || transcript.trim() === "") {
return [];
}
const lines = transcript.split("\n").filter((line) => line.trim());
const messages = [];
let messageOrder = 0;
let currentTimestamp = new Date();
for (const line of lines) {
// Try format 1: [DD-MM-YYYY HH:MM:SS] Role: Content
const timestampMatch = line.match(/^\[([^\]]+)\]\s*([^:]+):\s*(.+)$/);
if (timestampMatch) {
const [, timestamp, role, content] = timestampMatch;
// Parse timestamp (DD-MM-YYYY HH:MM:SS)
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, // Month is 0-indexed
parseInt(day),
parseInt(hour),
parseInt(minute),
parseInt(second)
);
}
messages.push({
sessionId,
role: role.trim().toLowerCase(),
content: content.trim(),
timestamp: parsedTimestamp,
order: messageOrder++,
});
continue;
}
// Try format 2: Role: Content (simple format)
const simpleMatch = line.match(/^([^:]+):\s*(.+)$/);
if (simpleMatch) {
const [, role, content] = simpleMatch;
// Use incremental timestamps (add 1 minute per message)
currentTimestamp = new Date(currentTimestamp.getTime() + 60000);
messages.push({
sessionId,
role: role.trim().toLowerCase(),
content: content.trim(),
timestamp: new Date(currentTimestamp),
order: messageOrder++,
});
}
}
return messages;
}
/**
* Process sessions without messages
*/
async function fetchAndParseTranscripts() {
try {
console.log("🔍 Finding sessions without messages...\n");
// Get sessions that have fullTranscriptUrl but no messages
const sessionsWithoutMessages = await prisma.session.findMany({
where: {
AND: [
{ fullTranscriptUrl: { not: null } },
{ messages: { none: {} } }, // No messages
],
},
include: {
company: true,
},
take: 20, // Process 20 at a time to avoid overwhelming
});
if (sessionsWithoutMessages.length === 0) {
console.log(
"✅ All sessions with transcript URLs already have messages!"
);
return;
}
console.log(
`📥 Found ${sessionsWithoutMessages.length} sessions to process\n`
);
let successCount = 0;
let errorCount = 0;
for (const session of sessionsWithoutMessages) {
console.log(`📄 Processing session ${session.id.substring(0, 8)}...`);
try {
// Fetch transcript content
const transcriptContent = await fetchTranscriptContent(
session.fullTranscriptUrl,
session.company.csvUsername,
session.company.csvPassword
);
if (!transcriptContent) {
console.log(` ⚠️ No transcript content available`);
errorCount++;
continue;
}
// Parse transcript into messages
const messages = parseTranscriptToMessages(
transcriptContent,
session.id
);
if (messages.length === 0) {
console.log(` ⚠️ No messages found in transcript`);
errorCount++;
continue;
}
// Save messages to database
await prisma.message.createMany({
data: messages,
});
console.log(` ✅ Added ${messages.length} messages`);
successCount++;
} catch (error) {
console.log(` ❌ Error: ${error.message}`);
errorCount++;
}
}
console.log(`\n📊 Results:`);
console.log(` ✅ Successfully processed: ${successCount} sessions`);
console.log(` ❌ Failed to process: ${errorCount} sessions`);
console.log(
`\n💡 Now you can run the processing scheduler to analyze these sessions!`
);
} catch (error) {
console.error("❌ Error:", error);
} finally {
await prisma.$disconnect();
}
}
fetchAndParseTranscripts();

View File

@ -0,0 +1,40 @@
// Simple script to test the manual processing trigger
// Usage: node scripts/manual-trigger-test.js
import fetch from "node-fetch";
async function testManualTrigger() {
try {
console.log("Testing manual processing trigger...");
const response = await fetch(
"http://localhost:3000/api/admin/trigger-processing",
{
method: "POST",
headers: {
"Content-Type": "application/json",
// Note: In a real scenario, you'd need to include authentication cookies
// For testing, you might need to login first and copy the session cookie
},
body: JSON.stringify({
batchSize: 5, // Process max 5 sessions
maxConcurrency: 3, // Use 3 concurrent workers
}),
}
);
const result = await response.json();
if (response.ok) {
console.log("✅ Manual trigger successful:");
console.log(JSON.stringify(result, null, 2));
} else {
console.log("❌ Manual trigger failed:");
console.log(JSON.stringify(result, null, 2));
}
} catch (error) {
console.error("❌ Error testing manual trigger:", error.message);
}
}
testManualTrigger();

233
scripts/manual-triggers.js Normal file
View File

@ -0,0 +1,233 @@
// Manual trigger scripts for both schedulers
import { fetchAndStoreSessionsForAllCompanies } from "../lib/csvFetcher.js";
import { processAllUnparsedTranscripts } from "../lib/transcriptParser.js";
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;
}
}
});
console.log("✅ Environment variables loaded from .env.local");
} catch (error) {
console.warn("⚠️ Could not load .env.local file:", error.message);
}
const prisma = new PrismaClient();
/**
* Manually trigger the session refresh scheduler
*/
async function triggerSessionRefresh() {
console.log("=== Manual Session Refresh Trigger ===");
try {
await fetchAndStoreSessionsForAllCompanies();
console.log("✅ Session refresh completed successfully");
} catch (error) {
console.error("❌ Session refresh failed:", error);
}
}
/**
* Manually trigger the processing scheduler
*/
async function triggerProcessingScheduler() {
console.log("=== Manual Processing Scheduler Trigger ===");
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
if (!OPENAI_API_KEY) {
console.error("❌ OPENAI_API_KEY environment variable is not set");
return;
}
try {
// Find sessions that need processing
const sessionsToProcess = await prisma.session.findMany({
where: {
AND: [
{ messages: { some: {} } },
{
OR: [{ processed: false }, { processed: null }],
},
],
},
select: {
id: true,
processed: true,
},
take: 5, // Process 5 sessions for manual testing
});
console.log(`Found ${sessionsToProcess.length} sessions to process:`);
sessionsToProcess.forEach((session) => {
console.log(`- Session ${session.id}: processed=${session.processed}`);
});
if (sessionsToProcess.length === 0) {
console.log("✅ No sessions found requiring processing");
return;
}
// Import and run the processing function
const { processUnprocessedSessions } = await import(
"../lib/processingScheduler.js"
);
await processUnprocessedSessions();
console.log("✅ Processing scheduler completed");
} catch (error) {
console.error("❌ Processing scheduler failed:", error);
}
}
/**
* Manually trigger transcript parsing
*/
async function triggerTranscriptParsing() {
console.log("=== Manual Transcript Parsing Trigger ===");
try {
const result = await processAllUnparsedTranscripts();
console.log(
`✅ Transcript parsing completed: ${result.processed} processed, ${result.errors} errors`
);
} catch (error) {
console.error("❌ Transcript parsing failed:", error);
}
}
/**
* Show current processing status
*/
async function showProcessingStatus() {
console.log("=== Processing Status ===");
try {
const totalSessions = await prisma.session.count();
const processedSessions = await prisma.session.count({
where: { processed: true },
});
const unprocessedSessions = await prisma.session.count({
where: {
OR: [{ processed: false }, { processed: null }],
},
});
const withMessages = await prisma.session.count({
where: {
messages: {
some: {},
},
},
});
const readyForProcessing = await prisma.session.count({
where: {
AND: [
{ messages: { some: {} } },
{
OR: [{ processed: false }, { processed: null }],
},
],
},
});
console.log(`📊 Total sessions: ${totalSessions}`);
console.log(`✅ Processed sessions: ${processedSessions}`);
console.log(`⏳ Unprocessed sessions: ${unprocessedSessions}`);
console.log(`📄 Sessions with messages: ${withMessages}`);
console.log(`🔄 Ready for processing: ${readyForProcessing}`);
// Show some examples of unprocessed sessions
if (readyForProcessing > 0) {
console.log("\n📋 Sample unprocessed sessions:");
const samples = await prisma.session.findMany({
where: {
AND: [
{ messages: { some: {} } },
{
OR: [{ processed: false }, { processed: null }],
},
],
},
select: {
id: true,
processed: true,
startTime: true,
},
take: 3,
});
samples.forEach((session) => {
console.log(
`- ${session.id} (${session.startTime.toISOString()}) - processed: ${session.processed}`
);
});
}
} catch (error) {
console.error("❌ Failed to get processing status:", error);
}
}
// Main execution based on command line argument
const command = process.argv[2];
switch (command) {
case "refresh":
await triggerSessionRefresh();
break;
case "process":
await triggerProcessingScheduler();
break;
case "parse":
await triggerTranscriptParsing();
break;
case "status":
await showProcessingStatus();
break;
case "both":
await triggerSessionRefresh();
console.log("\n" + "=".repeat(50) + "\n");
await triggerProcessingScheduler();
break;
case "all":
await triggerSessionRefresh();
console.log("\n" + "=".repeat(50) + "\n");
await triggerTranscriptParsing();
console.log("\n" + "=".repeat(50) + "\n");
await triggerProcessingScheduler();
break;
default:
console.log("Usage: node scripts/manual-triggers.js [command]");
console.log("Commands:");
console.log(
" refresh - Trigger session refresh (fetch new sessions from CSV)"
);
console.log(" parse - Parse transcripts into structured messages");
console.log(
" process - Trigger processing scheduler (process unprocessed sessions)"
);
console.log(" status - Show current processing status");
console.log(" both - Run both refresh and processing");
console.log(" all - Run refresh, parse, and processing in sequence");
break;
}
await prisma.$disconnect();

298
scripts/process_sessions.ts Normal file
View File

@ -0,0 +1,298 @@
import { PrismaClient } from "@prisma/client";
import fetch from "node-fetch";
const prisma = new PrismaClient();
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const OPENAI_API_URL = "https://api.openai.com/v1/chat/completions";
// Define the expected response structure from OpenAI
interface OpenAIProcessedData {
language: string;
sentiment: "positive" | "neutral" | "negative";
escalated: boolean;
forwarded_hr: boolean;
category: string;
questions: string | string[];
summary: string;
tokens: number;
tokens_eur: number;
}
/**
* Processes a session transcript using OpenAI API
* @param sessionId The session ID
* @param transcript The transcript content to process
* @returns Processed data from OpenAI
*/
async function processTranscriptWithOpenAI(
sessionId: string,
transcript: string
): Promise<OpenAIProcessedData> {
if (!OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY environment variable is not set");
}
// Create a system message with instructions
const systemMessage = `
You are an AI assistant tasked with analyzing chat transcripts.
Extract the following information from the transcript:
1. The primary language used by the user (ISO 639-1 code)
2. Overall sentiment (positive, neutral, or negative)
3. Whether the conversation was escalated
4. Whether HR contact was mentioned or provided
5. The best-fitting category for the conversation from this list:
- Schedule & Hours
- Leave & Vacation
- Sick Leave & Recovery
- Salary & Compensation
- Contract & Hours
- Onboarding
- Offboarding
- Workwear & Staff Pass
- Team & Contacts
- Personal Questions
- Access & Login
- Social questions
- Unrecognized / Other
6. A single question or an array of simplified questions asked by the user formulated in English
7. A brief summary of the conversation (10-300 characters)
8. The number of tokens used for the API call
9. The cost of the API call in EUR
Return the data in JSON format matching this schema:
{
"language": "ISO 639-1 code",
"sentiment": "positive|neutral|negative",
"escalated": boolean,
"forwarded_hr": boolean,
"category": "one of the categories listed above",
"questions": null, or array of questions,
"summary": "brief summary",
"tokens": number,
"tokens_eur": number
}
`;
try {
const response = await fetch(OPENAI_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-4-turbo",
messages: [
{
role: "system",
content: systemMessage,
},
{
role: "user",
content: transcript,
},
],
temperature: 0.3, // Lower temperature for more consistent results
response_format: { type: "json_object" },
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`OpenAI API error: ${response.status} - ${errorText}`);
}
const data = (await response.json()) as any;
const processedData = JSON.parse(data.choices[0].message.content);
// Validate the response against our expected schema
validateOpenAIResponse(processedData);
return processedData;
} catch (error) {
console.error(`Error processing transcript with OpenAI:`, error);
throw error;
}
}
/**
* Validates the OpenAI response against our expected schema
* @param data The data to validate
*/
function validateOpenAIResponse(
data: any
): asserts data is OpenAIProcessedData {
// Check required fields
const requiredFields = [
"language",
"sentiment",
"escalated",
"forwarded_hr",
"category",
"questions",
"summary",
"tokens",
"tokens_eur",
];
for (const field of requiredFields) {
if (!(field in data)) {
throw new Error(`Missing required field: ${field}`);
}
}
// Validate field types
if (typeof data.language !== "string" || !/^[a-z]{2}$/.test(data.language)) {
throw new Error(
"Invalid language format. Expected ISO 639-1 code (e.g., 'en')"
);
}
if (!["positive", "neutral", "negative"].includes(data.sentiment)) {
throw new Error(
"Invalid sentiment. Expected 'positive', 'neutral', or 'negative'"
);
}
if (typeof data.escalated !== "boolean") {
throw new Error("Invalid escalated. Expected boolean");
}
if (typeof data.forwarded_hr !== "boolean") {
throw new Error("Invalid forwarded_hr. Expected boolean");
}
const validCategories = [
"Schedule & Hours",
"Leave & Vacation",
"Sick Leave & Recovery",
"Salary & Compensation",
"Contract & Hours",
"Onboarding",
"Offboarding",
"Workwear & Staff Pass",
"Team & Contacts",
"Personal Questions",
"Access & Login",
"Social questions",
"Unrecognized / Other",
];
if (!validCategories.includes(data.category)) {
throw new Error(
`Invalid category. Expected one of: ${validCategories.join(", ")}`
);
}
if (typeof data.questions !== "string" && !Array.isArray(data.questions)) {
throw new Error("Invalid questions. Expected string or array of strings");
}
if (
typeof data.summary !== "string" ||
data.summary.length < 10 ||
data.summary.length > 300
) {
throw new Error(
"Invalid summary. Expected string between 10-300 characters"
);
}
if (typeof data.tokens !== "number" || data.tokens < 0) {
throw new Error("Invalid tokens. Expected non-negative number");
}
if (typeof data.tokens_eur !== "number" || data.tokens_eur < 0) {
throw new Error("Invalid tokens_eur. Expected non-negative number");
}
}
/**
* Main function to process unprocessed sessions
*/
async function processUnprocessedSessions() {
console.log("Starting to process unprocessed sessions...");
// Find sessions that have transcript content but haven't been processed
const sessionsToProcess = await prisma.session.findMany({
where: {
AND: [
{ transcriptContent: { not: null } },
{ transcriptContent: { not: "" } },
{ processed: { not: true } }, // Either false or null
],
},
select: {
id: true,
transcriptContent: true,
},
});
if (sessionsToProcess.length === 0) {
console.log("No sessions found requiring processing.");
return;
}
console.log(`Found ${sessionsToProcess.length} sessions to process.`);
let successCount = 0;
let errorCount = 0;
for (const session of sessionsToProcess) {
if (!session.transcriptContent) {
// Should not happen due to query, but good for type safety
console.warn(
`Session ${session.id} has no transcript content, skipping.`
);
continue;
}
console.log(`Processing transcript for session ${session.id}...`);
try {
const processedData = await processTranscriptWithOpenAI(
session.id,
session.transcriptContent
);
// Update the session with processed data
await prisma.session.update({
where: { id: session.id },
data: {
language: processedData.language,
sentiment: processedData.sentiment,
escalated: processedData.escalated,
forwardedHr: processedData.forwarded_hr,
category: processedData.category,
questions: processedData.questions,
summary: processedData.summary,
tokens: {
increment: processedData.tokens,
},
tokensEur: {
increment: processedData.tokens_eur,
},
processed: true,
},
});
console.log(`Successfully processed session ${session.id}.`);
successCount++;
} catch (error) {
console.error(`Error processing session ${session.id}:`, error);
errorCount++;
}
}
console.log("Session processing complete.");
console.log(`Successfully processed: ${successCount} sessions.`);
console.log(`Failed to process: ${errorCount} sessions.`);
}
// Run the main function
processUnprocessedSessions()
.catch((e) => {
console.error("An error occurred during the script execution:", e);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
});

View File

@ -0,0 +1,48 @@
// Reset all sessions to processed: false for reprocessing with new instructions
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function resetProcessedStatus() {
try {
console.log('🔄 Resetting processed status for all sessions...');
// Get count of currently processed sessions
const processedCount = await prisma.session.count({
where: { processed: true }
});
console.log(`📊 Found ${processedCount} processed sessions to reset`);
if (processedCount === 0) {
console.log('✅ No sessions need to be reset');
return;
}
// Reset all sessions to processed: false
const result = await prisma.session.updateMany({
where: { processed: true },
data: {
processed: false,
// Also reset AI-generated fields so they get fresh analysis
sentimentCategory: null,
category: null,
questions: null,
summary: null,
validData: true // Reset to default
}
});
console.log(`✅ Successfully reset ${result.count} sessions to processed: false`);
console.log('🤖 These sessions will be reprocessed with the new OpenAI instructions');
console.log('🎯 Quality validation will now mark invalid data appropriately');
} catch (error) {
console.error('❌ Error resetting processed status:', error);
} finally {
await prisma.$disconnect();
}
}
// Run the script
resetProcessedStatus();

View File

@ -0,0 +1,83 @@
// Test script to demonstrate the automated processing system
import { PrismaClient } from '@prisma/client';
import { processUnprocessedSessions, startProcessingScheduler } from '../lib/processingScheduler.ts';
const prisma = new PrismaClient();
async function testAutomation() {
console.log('🧪 TESTING AUTOMATED PROCESSING SYSTEM\n');
// Step 1: Show current status
console.log('📊 STEP 1: Current Database Status');
console.log('=' .repeat(50));
await showStatus();
// Step 2: Test the automated function
console.log('\n🤖 STEP 2: Testing Automated Processing Function');
console.log('=' .repeat(50));
console.log('This is the SAME function that runs automatically every hour...\n');
try {
// This is the EXACT same function that runs automatically every hour
const result = await processUnprocessedSessions(5, 2); // Smaller batch for demo
console.log('\n✅ AUTOMATION TEST RESULTS:');
console.log(` 📊 Sessions processed: ${result.totalProcessed}`);
console.log(` ❌ Sessions failed: ${result.totalFailed}`);
console.log(` ⏱️ Processing time: ${result.totalTime.toFixed(2)}s`);
if (result.totalProcessed === 0 && result.totalFailed === 0) {
console.log('\n🎉 PERFECT! No unprocessed sessions found.');
console.log('✅ This means the automation is working - everything is already processed!');
}
} catch (error) {
console.error('❌ Error testing automation:', error);
}
// Step 3: Show what the scheduler does
console.log('\n⏰ STEP 3: Automated Scheduler Information');
console.log('=' .repeat(50));
console.log('🔄 HOURLY AUTOMATION:');
console.log(' • Runs every hour: cron.schedule("0 * * * *")');
console.log(' • Checks: WHERE processed = false AND messages: { some: {} }');
console.log(' • Processes: ALL unprocessed sessions through OpenAI');
console.log(' • Continues: Until NO unprocessed sessions remain');
console.log(' • Quality: Validates and filters low-quality sessions');
console.log('\n🚀 DASHBOARD INTEGRATION:');
console.log(' • Refresh button triggers: triggerCompleteWorkflow()');
console.log(' • Fetches transcripts: For sessions without messages');
console.log(' • Processes everything: Until all sessions are analyzed');
console.log('\n🎯 PRODUCTION STATUS:');
console.log(' ✅ System is FULLY AUTOMATED');
console.log(' ✅ No manual intervention needed');
console.log(' ✅ Processes new data automatically');
console.log(' ✅ Quality validation included');
await prisma.$disconnect();
}
async function showStatus() {
const totalSessions = await prisma.session.count();
const processedSessions = await prisma.session.count({ where: { processed: true } });
const unprocessedSessions = await prisma.session.count({ where: { processed: false } });
const sessionsWithMessages = await prisma.session.count({
where: { messages: { some: {} } }
});
console.log(`📈 Total sessions: ${totalSessions}`);
console.log(`✅ Processed sessions: ${processedSessions}`);
console.log(`⏳ Unprocessed sessions: ${unprocessedSessions}`);
console.log(`💬 Sessions with messages: ${sessionsWithMessages}`);
if (processedSessions === sessionsWithMessages && unprocessedSessions === 0) {
console.log('\n🎉 AUTOMATION WORKING PERFECTLY!');
console.log('✅ All sessions with messages have been processed');
console.log('✅ No unprocessed sessions remaining');
}
}
// Run the test
testAutomation();

View File

@ -0,0 +1,47 @@
// Test the improved prompt on a few sessions
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function testImprovedPrompt() {
console.log('🧪 TESTING IMPROVED QUESTION EXTRACTION PROMPT\n');
// Reset a few sessions to test the new prompt
console.log('📝 Resetting 5 sessions to test improved prompt...');
const sessionsToReprocess = await prisma.session.findMany({
where: {
processed: true,
questions: '[]' // Sessions with empty questions
},
take: 5
});
if (sessionsToReprocess.length > 0) {
// Reset these sessions to unprocessed
await prisma.session.updateMany({
where: {
id: { in: sessionsToReprocess.map(s => s.id) }
},
data: {
processed: false,
questions: null,
summary: null
}
});
console.log(`✅ Reset ${sessionsToReprocess.length} sessions for reprocessing`);
console.log('Session IDs:', sessionsToReprocess.map(s => s.id));
console.log('\n🚀 Now run this command to test the improved prompt:');
console.log('npx tsx scripts/trigger-processing-direct.js');
console.log('\nThen check the results with:');
console.log('npx tsx scripts/check-questions-issue.js');
} else {
console.log('❌ No sessions with empty questions found to reprocess');
}
await prisma.$disconnect();
}
testImprovedPrompt();

View File

@ -0,0 +1,83 @@
// Script to check processing status and trigger processing
// Usage: node scripts/test-processing-status.js
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function checkProcessingStatus() {
try {
console.log("🔍 Checking processing status...\n");
// Get processing status
const totalSessions = await prisma.session.count();
const processedSessions = await prisma.session.count({
where: { processed: true },
});
const unprocessedSessions = await prisma.session.count({
where: { processed: false },
});
const sessionsWithMessages = await prisma.session.count({
where: {
processed: false,
messages: { some: {} },
},
});
console.log("📊 Processing Status:");
console.log(` Total sessions: ${totalSessions}`);
console.log(` ✅ Processed: ${processedSessions}`);
console.log(` ⏳ Unprocessed: ${unprocessedSessions}`);
console.log(` 📝 Unprocessed with messages: ${sessionsWithMessages}`);
const processedPercentage = (
(processedSessions / totalSessions) *
100
).toFixed(1);
console.log(` 📈 Processing progress: ${processedPercentage}%\n`);
// Check recent processing activity
const recentlyProcessed = await prisma.session.findMany({
where: {
processed: true,
createdAt: {
gte: new Date(Date.now() - 60 * 60 * 1000), // Last hour
},
},
orderBy: { createdAt: "desc" },
take: 5,
select: {
id: true,
createdAt: true,
category: true,
sentiment: true,
},
});
if (recentlyProcessed.length > 0) {
console.log("🕒 Recently processed sessions:");
recentlyProcessed.forEach((session) => {
const timeAgo = Math.round(
(Date.now() - session.createdAt.getTime()) / 1000 / 60
);
console.log(
`${session.id.substring(0, 8)}... (${timeAgo}m ago) - ${session.category || "No category"}`
);
});
} else {
console.log("🕒 No sessions processed in the last hour");
}
console.log("\n✨ Processing system is working correctly!");
console.log("💡 The parallel processing successfully processed sessions.");
console.log(
"🎯 For manual triggers, you need to be logged in as an admin user."
);
} catch (error) {
console.error("❌ Error checking status:", error);
} finally {
await prisma.$disconnect();
}
}
checkProcessingStatus();

View File

@ -0,0 +1,57 @@
// Trigger CSV refresh for all companies
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function triggerCsvRefresh() {
try {
console.log('🔄 Triggering CSV refresh for all companies...\n');
// Get all companies
const companies = await prisma.company.findMany();
if (companies.length === 0) {
console.log('❌ No companies found. Run seed script first.');
return;
}
console.log(`🏢 Found ${companies.length} companies:`);
for (const company of companies) {
console.log(`📊 Company: ${company.name} (ID: ${company.id})`);
console.log(`📥 CSV URL: ${company.csvUrl}`);
try {
const response = await fetch('http://localhost:3000/api/admin/refresh-sessions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
companyId: company.id
})
});
const result = await response.json();
if (response.ok) {
console.log(`✅ Successfully imported ${result.imported} sessions for ${company.name}`);
} else {
console.log(`❌ Error for ${company.name}: ${result.error}`);
}
} catch (error) {
console.log(`❌ Failed to refresh ${company.name}: ${error.message}`);
}
console.log(''); // Empty line for readability
}
} catch (error) {
console.error('❌ Error triggering CSV refresh:', error);
} finally {
await prisma.$disconnect();
}
}
// Run the script
triggerCsvRefresh();

View File

@ -0,0 +1,21 @@
// Direct processing trigger without authentication
import { processUnprocessedSessions } from '../lib/processingScheduler.ts';
async function triggerProcessing() {
try {
console.log('🤖 Starting complete batch processing of all unprocessed sessions...\n');
// Process all unprocessed sessions in batches until completion
const result = await processUnprocessedSessions(10, 3);
console.log('\n🎉 Complete processing finished!');
console.log(`📊 Final results: ${result.totalProcessed} processed, ${result.totalFailed} failed`);
console.log(`⏱️ Total time: ${result.totalTime.toFixed(2)}s`);
} catch (error) {
console.error('❌ Error during processing:', error);
}
}
// Run the script
triggerProcessing();

39
server.js Normal file
View File

@ -0,0 +1,39 @@
// Custom Next.js server with scheduler initialization
const { createServer } = require("http");
const { parse } = require("url");
const next = require("next");
const { startScheduler } = require("./lib/scheduler");
const { startProcessingScheduler } = require("./lib/processingScheduler");
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = process.env.PORT || 3000;
// Initialize Next.js
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
// Initialize schedulers when the server starts
console.log("Starting schedulers...");
startScheduler();
startProcessingScheduler();
console.log("All schedulers initialized successfully");
createServer(async (req, res) => {
try {
// Parse the URL
const parsedUrl = parse(req.url, true);
// Let Next.js handle the request
await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("Internal Server Error");
}
}).listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://${hostname}:${port}`);
});
});

46
server.ts Normal file
View File

@ -0,0 +1,46 @@
// Custom Next.js server with scheduler initialization
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { processUnprocessedSessions } from "./lib/processingSchedulerNoCron.js";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = parseInt(process.env.PORT || "3000", 10);
// Initialize Next.js
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
// Start processing scheduler in the background
const BATCH_SIZE = 10;
const MAX_CONCURRENCY = 5;
const SCHEDULER_INTERVAL = 5 * 60 * 1000; // 5 minutes
// Initial processing run
processUnprocessedSessions(BATCH_SIZE, MAX_CONCURRENCY).catch(console.error);
// Schedule regular processing
setInterval(() => {
processUnprocessedSessions(BATCH_SIZE, MAX_CONCURRENCY).catch(console.error);
}, SCHEDULER_INTERVAL);
console.log("Processing scheduler started with 5 minute interval");
createServer(async (req, res) => {
try {
// Parse the URL
const parsedUrl = parse(req.url || "", true);
// Let Next.js handle the request
await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("Internal Server Error");
}
}).listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});

View File

@ -0,0 +1,489 @@
import { test, expect, type Page } from "@playwright/test";
test.beforeEach(async ({ page }) => {
await page.goto("https://demo.playwright.dev/todomvc");
});
const TODO_ITEMS = [
"buy some cheese",
"feed the cat",
"book a doctors appointment",
] as const;
test.describe("New Todo", () => {
test("should allow me to add todo items", async ({ page }) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
// Create 1st todo.
await newTodo.fill(TODO_ITEMS[0]);
await newTodo.press("Enter");
// Make sure the list only has one todo item.
await expect(page.getByTestId("todo-title")).toHaveText([TODO_ITEMS[0]]);
// Create 2nd todo.
await newTodo.fill(TODO_ITEMS[1]);
await newTodo.press("Enter");
// Make sure the list now has two todo items.
await expect(page.getByTestId("todo-title")).toHaveText([
TODO_ITEMS[0],
TODO_ITEMS[1],
]);
await checkNumberOfTodosInLocalStorage(page, 2);
});
test("should clear text input field when an item is added", async ({
page,
}) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
// Create one todo item.
await newTodo.fill(TODO_ITEMS[0]);
await newTodo.press("Enter");
// Check that input is empty.
await expect(newTodo).toBeEmpty();
await checkNumberOfTodosInLocalStorage(page, 1);
});
test("should append new items to the bottom of the list", async ({
page,
}) => {
// Create 3 items.
await createDefaultTodos(page);
// create a todo count locator
const todoCount = page.getByTestId("todo-count");
// Check test using different methods.
await expect(page.getByText("3 items left")).toBeVisible();
await expect(todoCount).toHaveText("3 items left");
await expect(todoCount).toContainText("3");
await expect(todoCount).toHaveText(/3/);
// Check all items in one call.
await expect(page.getByTestId("todo-title")).toHaveText(TODO_ITEMS);
await checkNumberOfTodosInLocalStorage(page, 3);
});
});
test.describe("Mark all as completed", () => {
test.beforeEach(async ({ page }) => {
await createDefaultTodos(page);
await checkNumberOfTodosInLocalStorage(page, 3);
});
test.afterEach(async ({ page }) => {
await checkNumberOfTodosInLocalStorage(page, 3);
});
test("should allow me to mark all items as completed", async ({ page }) => {
// Complete all todos.
await page.getByLabel("Mark all as complete").check();
// Ensure all todos have 'completed' class.
await expect(page.getByTestId("todo-item")).toHaveClass([
"completed",
"completed",
"completed",
]);
await checkNumberOfCompletedTodosInLocalStorage(page, 3);
});
test("should allow me to clear the complete state of all items", async ({
page,
}) => {
const toggleAll = page.getByLabel("Mark all as complete");
// Check and then immediately uncheck.
await toggleAll.check();
await toggleAll.uncheck();
// Should be no completed classes.
await expect(page.getByTestId("todo-item")).toHaveClass(["", "", ""]);
});
test("complete all checkbox should update state when items are completed / cleared", async ({
page,
}) => {
const toggleAll = page.getByLabel("Mark all as complete");
await toggleAll.check();
await expect(toggleAll).toBeChecked();
await checkNumberOfCompletedTodosInLocalStorage(page, 3);
// Uncheck first todo.
const firstTodo = page.getByTestId("todo-item").nth(0);
await firstTodo.getByRole("checkbox").uncheck();
// Reuse toggleAll locator and make sure its not checked.
await expect(toggleAll).not.toBeChecked();
await firstTodo.getByRole("checkbox").check();
await checkNumberOfCompletedTodosInLocalStorage(page, 3);
// Assert the toggle all is checked again.
await expect(toggleAll).toBeChecked();
});
});
test.describe("Item", () => {
test("should allow me to mark items as complete", async ({ page }) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
// Create two items.
for (const item of TODO_ITEMS.slice(0, 2)) {
await newTodo.fill(item);
await newTodo.press("Enter");
}
// Check first item.
const firstTodo = page.getByTestId("todo-item").nth(0);
await firstTodo.getByRole("checkbox").check();
await expect(firstTodo).toHaveClass("completed");
// Check second item.
const secondTodo = page.getByTestId("todo-item").nth(1);
await expect(secondTodo).not.toHaveClass("completed");
await secondTodo.getByRole("checkbox").check();
// Assert completed class.
await expect(firstTodo).toHaveClass("completed");
await expect(secondTodo).toHaveClass("completed");
});
test("should allow me to un-mark items as complete", async ({ page }) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
// Create two items.
for (const item of TODO_ITEMS.slice(0, 2)) {
await newTodo.fill(item);
await newTodo.press("Enter");
}
const firstTodo = page.getByTestId("todo-item").nth(0);
const secondTodo = page.getByTestId("todo-item").nth(1);
const firstTodoCheckbox = firstTodo.getByRole("checkbox");
await firstTodoCheckbox.check();
await expect(firstTodo).toHaveClass("completed");
await expect(secondTodo).not.toHaveClass("completed");
await checkNumberOfCompletedTodosInLocalStorage(page, 1);
await firstTodoCheckbox.uncheck();
await expect(firstTodo).not.toHaveClass("completed");
await expect(secondTodo).not.toHaveClass("completed");
await checkNumberOfCompletedTodosInLocalStorage(page, 0);
});
test("should allow me to edit an item", async ({ page }) => {
await createDefaultTodos(page);
const todoItems = page.getByTestId("todo-item");
const secondTodo = todoItems.nth(1);
await secondTodo.dblclick();
await expect(secondTodo.getByRole("textbox", { name: "Edit" })).toHaveValue(
TODO_ITEMS[1]
);
await secondTodo
.getByRole("textbox", { name: "Edit" })
.fill("buy some sausages");
await secondTodo.getByRole("textbox", { name: "Edit" }).press("Enter");
// Explicitly assert the new text value.
await expect(todoItems).toHaveText([
TODO_ITEMS[0],
"buy some sausages",
TODO_ITEMS[2],
]);
await checkTodosInLocalStorage(page, "buy some sausages");
});
});
test.describe("Editing", () => {
test.beforeEach(async ({ page }) => {
await createDefaultTodos(page);
await checkNumberOfTodosInLocalStorage(page, 3);
});
test("should hide other controls when editing", async ({ page }) => {
const todoItem = page.getByTestId("todo-item").nth(1);
await todoItem.dblclick();
await expect(todoItem.getByRole("checkbox")).not.toBeVisible();
await expect(
todoItem.locator("label", {
hasText: TODO_ITEMS[1],
})
).not.toBeVisible();
await checkNumberOfTodosInLocalStorage(page, 3);
});
test("should save edits on blur", async ({ page }) => {
const todoItems = page.getByTestId("todo-item");
await todoItems.nth(1).dblclick();
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.fill("buy some sausages");
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.dispatchEvent("blur");
await expect(todoItems).toHaveText([
TODO_ITEMS[0],
"buy some sausages",
TODO_ITEMS[2],
]);
await checkTodosInLocalStorage(page, "buy some sausages");
});
test("should trim entered text", async ({ page }) => {
const todoItems = page.getByTestId("todo-item");
await todoItems.nth(1).dblclick();
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.fill(" buy some sausages ");
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.press("Enter");
await expect(todoItems).toHaveText([
TODO_ITEMS[0],
"buy some sausages",
TODO_ITEMS[2],
]);
await checkTodosInLocalStorage(page, "buy some sausages");
});
test("should remove the item if an empty text string was entered", async ({
page,
}) => {
const todoItems = page.getByTestId("todo-item");
await todoItems.nth(1).dblclick();
await todoItems.nth(1).getByRole("textbox", { name: "Edit" }).fill("");
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.press("Enter");
await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[2]]);
});
test("should cancel edits on escape", async ({ page }) => {
const todoItems = page.getByTestId("todo-item");
await todoItems.nth(1).dblclick();
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.fill("buy some sausages");
await todoItems
.nth(1)
.getByRole("textbox", { name: "Edit" })
.press("Escape");
await expect(todoItems).toHaveText(TODO_ITEMS);
});
});
test.describe("Counter", () => {
test("should display the current number of todo items", async ({ page }) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
// create a todo count locator
const todoCount = page.getByTestId("todo-count");
await newTodo.fill(TODO_ITEMS[0]);
await newTodo.press("Enter");
await expect(todoCount).toContainText("1");
await newTodo.fill(TODO_ITEMS[1]);
await newTodo.press("Enter");
await expect(todoCount).toContainText("2");
await checkNumberOfTodosInLocalStorage(page, 2);
});
});
test.describe("Clear completed button", () => {
test.beforeEach(async ({ page }) => {
await createDefaultTodos(page);
});
test("should display the correct text", async ({ page }) => {
await page.locator(".todo-list li .toggle").first().check();
await expect(
page.getByRole("button", { name: "Clear completed" })
).toBeVisible();
});
test("should remove completed items when clicked", async ({ page }) => {
const todoItems = page.getByTestId("todo-item");
await todoItems.nth(1).getByRole("checkbox").check();
await page.getByRole("button", { name: "Clear completed" }).click();
await expect(todoItems).toHaveCount(2);
await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[2]]);
});
test("should be hidden when there are no items that are completed", async ({
page,
}) => {
await page.locator(".todo-list li .toggle").first().check();
await page.getByRole("button", { name: "Clear completed" }).click();
await expect(
page.getByRole("button", { name: "Clear completed" })
).toBeHidden();
});
});
test.describe("Persistence", () => {
test("should persist its data", async ({ page }) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
for (const item of TODO_ITEMS.slice(0, 2)) {
await newTodo.fill(item);
await newTodo.press("Enter");
}
const todoItems = page.getByTestId("todo-item");
const firstTodoCheck = todoItems.nth(0).getByRole("checkbox");
await firstTodoCheck.check();
await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[1]]);
await expect(firstTodoCheck).toBeChecked();
await expect(todoItems).toHaveClass(["completed", ""]);
// Ensure there is 1 completed item.
await checkNumberOfCompletedTodosInLocalStorage(page, 1);
// Now reload.
await page.reload();
await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[1]]);
await expect(firstTodoCheck).toBeChecked();
await expect(todoItems).toHaveClass(["completed", ""]);
});
});
test.describe("Routing", () => {
test.beforeEach(async ({ page }) => {
await createDefaultTodos(page);
// make sure the app had a chance to save updated todos in storage
// before navigating to a new view, otherwise the items can get lost :(
// in some frameworks like Durandal
await checkTodosInLocalStorage(page, TODO_ITEMS[0]);
});
test("should allow me to display active items", async ({ page }) => {
const todoItem = page.getByTestId("todo-item");
await page.getByTestId("todo-item").nth(1).getByRole("checkbox").check();
await checkNumberOfCompletedTodosInLocalStorage(page, 1);
await page.getByRole("link", { name: "Active" }).click();
await expect(todoItem).toHaveCount(2);
await expect(todoItem).toHaveText([TODO_ITEMS[0], TODO_ITEMS[2]]);
});
test("should respect the back button", async ({ page }) => {
const todoItem = page.getByTestId("todo-item");
await page.getByTestId("todo-item").nth(1).getByRole("checkbox").check();
await checkNumberOfCompletedTodosInLocalStorage(page, 1);
await test.step("Showing all items", async () => {
await page.getByRole("link", { name: "All" }).click();
await expect(todoItem).toHaveCount(3);
});
await test.step("Showing active items", async () => {
await page.getByRole("link", { name: "Active" }).click();
});
await test.step("Showing completed items", async () => {
await page.getByRole("link", { name: "Completed" }).click();
});
await expect(todoItem).toHaveCount(1);
await page.goBack();
await expect(todoItem).toHaveCount(2);
await page.goBack();
await expect(todoItem).toHaveCount(3);
});
test("should allow me to display completed items", async ({ page }) => {
await page.getByTestId("todo-item").nth(1).getByRole("checkbox").check();
await checkNumberOfCompletedTodosInLocalStorage(page, 1);
await page.getByRole("link", { name: "Completed" }).click();
await expect(page.getByTestId("todo-item")).toHaveCount(1);
});
test("should allow me to display all items", async ({ page }) => {
await page.getByTestId("todo-item").nth(1).getByRole("checkbox").check();
await checkNumberOfCompletedTodosInLocalStorage(page, 1);
await page.getByRole("link", { name: "Active" }).click();
await page.getByRole("link", { name: "Completed" }).click();
await page.getByRole("link", { name: "All" }).click();
await expect(page.getByTestId("todo-item")).toHaveCount(3);
});
test("should highlight the currently applied filter", async ({ page }) => {
await expect(page.getByRole("link", { name: "All" })).toHaveClass(
"selected"
);
//create locators for active and completed links
const activeLink = page.getByRole("link", { name: "Active" });
const completedLink = page.getByRole("link", { name: "Completed" });
await activeLink.click();
// Page change - active items.
await expect(activeLink).toHaveClass("selected");
await completedLink.click();
// Page change - completed items.
await expect(completedLink).toHaveClass("selected");
});
});
async function createDefaultTodos(page: Page) {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");
for (const item of TODO_ITEMS) {
await newTodo.fill(item);
await newTodo.press("Enter");
}
}
async function checkNumberOfTodosInLocalStorage(page: Page, expected: number) {
return await page.waitForFunction((e) => {
return JSON.parse(localStorage["react-todos"]).length === e;
}, expected);
}
async function checkNumberOfCompletedTodosInLocalStorage(
page: Page,
expected: number
) {
return await page.waitForFunction((e) => {
return (
JSON.parse(localStorage["react-todos"]).filter(
(todo: any) => todo.completed
).length === e
);
}, expected);
}
async function checkTodosInLocalStorage(page: Page, title: string) {
return await page.waitForFunction((t) => {
return JSON.parse(localStorage["react-todos"])
.map((todo: any) => todo.title)
.includes(t);
}, title);
}

View File

@ -1,36 +1,36 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true, "allowJs": true,
"skipLibCheck": true,
"strict": true,
"noImplicitAny": false, // Allow implicit any types
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true, "esModuleInterop": true,
"module": "esnext", "forceConsistentCasingInFileNames": true,
"moduleResolution": "node", "incremental": true,
"resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "preserve", "jsx": "preserve",
"incremental": true, "lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noImplicitAny": false, // Allow implicit any types
"paths": {
"@/*": ["./*"]
},
"plugins": [ "plugins": [
{ {
"name": "next" "name": "next"
} }
], ],
"paths": { "resolveJsonModule": true,
"@/*": ["./*"] "skipLibCheck": true,
}, "strict": true,
"strictNullChecks": true "strictNullChecks": true,
"target": "es5"
}, },
"exclude": ["node_modules"],
"include": [ "include": [
"next-env.d.ts", "next-env.d.ts",
"**/*.ts", "**/*.ts",
"**/*.tsx", "**/*.tsx",
".next/types/**/*.ts", ".next/types/**/*.ts",
"components/SessionDetails.tsx.bak" "components/SessionDetails.tsx.bak"
], ]
"exclude": ["node_modules"]
} }