diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index 84c9643c4..88cd8b761 100644 --- a/src/app/api/search/route.ts +++ b/src/app/api/search/route.ts @@ -3,6 +3,8 @@ import db from '@/db'; import { CourseContent } from '@prisma/client'; import Fuse from 'fuse.js'; import { NextRequest, NextResponse } from 'next/server'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/lib/auth'; export type TSearchedVideos = { id: number; @@ -24,6 +26,7 @@ const fuzzySearch = (videos: TSearchedVideos[], searchQuery: string) => { export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url); const searchQuery = searchParams.get('q'); + const session = await getServerSession(authOptions); if (searchQuery && searchQuery.length > 2) { const value: TSearchedVideos[] = await cache.get( @@ -39,6 +42,19 @@ export async function GET(request: NextRequest) { where: { type: 'video', hidden: false, + parent: { + courses: { + some: { + course: { + purchasedBy: { + some: { + userId: session?.user?.id, + }, + }, + }, + }, + }, + }, }, select: { id: true, @@ -46,7 +62,12 @@ export async function GET(request: NextRequest) { title: true, parent: { select: { - courses: true, + courses: { + select: { + courseId: true, + contentId: true, + }, + }, }, }, },