From 3026e332bfd3995d796a5686e213fafbf304c775 Mon Sep 17 00:00:00 2001 From: ItsFlash10 Date: Wed, 11 Sep 2024 02:45:28 +0530 Subject: [PATCH] feat/search: Fixed search api result --- src/app/api/search/route.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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, + }, + }, }, }, },