Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/pages/api/folder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type NextApiResponse } from 'next'
import { type AuthenticatedRequest } from '~/utils/authMiddleware'
import { db, folders } from '~/db/dbClient'
import { conversations, db, folders } from '~/db/dbClient'
import { type FolderWithConversation } from '@/types/folder'
import { type Database } from 'database.types'
import { convertDBToChatConversation } from './conversation'
Expand Down Expand Up @@ -47,6 +47,8 @@ export function convertChatFolderToDBFolder(
async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
const { method } = req
const user_email = req.user?.email as string | undefined
const courseName = req.query.courseName as string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In post call while creating folder, the request is giving 200 but sends course_name as undefined because it will be in the body which causes an instant disappearing of the folder.

Server debug logs for the post call:

eq.params {}
courseName undefined
user_email rohan13@illinois.edu
method POST
req.body {
  folder: {
    id: '6b06dde6-6100-4913-985c-4793bdfca7d1',
    name: 'New folder',
    type: 'chat'
  },
  courseName: 'test'
}

CleanShot 2025-11-05 at 14 43 17


if (!user_email) {
res.status(400).json({ error: 'No valid email address in token' })
return
Expand Down Expand Up @@ -92,6 +94,7 @@ async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
orderBy: desc(folders.created_at),
with: {
conversations: {
where: eq(conversations.project_name, courseName), // filter by course_name
with: {
messages: {
columns: {
Expand Down Expand Up @@ -123,6 +126,15 @@ async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
},
})

// If no conversations match the courseName, return an empty array
if (
!fetchedFolders.some(
(folder) => folder.conversations && folder.conversations.length > 0,
)
) {
return res.status(200).json([])
}

// Convert the fetched data to match the expected format
const formattedFolders = fetchedFolders.map((folder) => {
const conversations = folder.conversations || []
Expand Down Expand Up @@ -157,10 +169,10 @@ async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
eq(folders.user_email, user_email),
),
)
.returning({ id: folders.id });
.returning({ id: folders.id })

if (deleted.length === 0) {
return res.status(403).json({ error: 'Not allowed to delete this folder' });
return res.status(403).json({ error: 'Not allowed to delete this folder' })
}
res.status(200).json({ message: 'Folder deleted successfully' })
} else {
Expand Down