Skip to content

Commit

Permalink
fixing searchbar only search purched course video
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi committed Dec 14, 2024
1 parent 246752d commit 26dca27
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';

export type TSearchedVideos = {
id: number;
Expand Down Expand Up @@ -35,10 +36,54 @@ export async function GET(request: NextRequest) {
return NextResponse.json(fuzzySearch(value, searchQuery));
}

const session = await getServerSession();
const userEmail = session?.user?.email;

if (!userEmail) {
return NextResponse.json({}, { status: 401 });
}

const userPurchases = await db.userPurchases.findMany({
where: {
user: {
email: userEmail,
},
},
select: {
courseId: true,
},
});

const purchasedCourseIds = userPurchases.map((purchase) => purchase.courseId);

const allVideos = await db.content.findMany({
where: {
type: 'video',
hidden: false,
OR: [
{
parent: {
courses: {
some: {
courseId: {
in: purchasedCourseIds,
},
},
},
},
},
{
parent: {
courses: {
some: {
course: {
openToEveryone: true,
},
},
},
},
}
],
},
select: {
id: true,
Expand Down

0 comments on commit 26dca27

Please sign in to comment.