From 66a3505f6b49213a32587985312d7966b4b01654 Mon Sep 17 00:00:00 2001 From: sahil-singh1107 Date: Thu, 29 Jan 2026 15:56:09 +0530 Subject: [PATCH 1/2] fix the search results to show only courses purchased by the user --- .env.example | 31 ----------------------- package.json | 3 +++ src/app/api/search/route.ts | 25 ++++++++++++++++++ src/app/globals.css | 2 +- src/components/landing/footer.tsx | 2 +- src/components/search/SearchBar.tsx | 2 ++ src/components/search/VideoSearchCard.tsx | 1 + 7 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index 1a8fe7f37..000000000 --- a/.env.example +++ /dev/null @@ -1,31 +0,0 @@ -NEXT_PUBLIC_BASE_URL_LOCAL=http://127.0.0.1:3000 -ADMIN_SECRET="ADMIN_SECRET" -JWT_SECRET="JWT_SECRET" -# DONT CHANGE FOR RUNNING WITH DOCKER -DATABASE_URL="postgresql://myuser:mypassword@localhost:5432/cms?schema=public" -NEXTAUTH_URL="http://localhost:3000" -APPX_AUTH_KEY="AUTH_SECRET" -NEXTAUTH_SECRET="NEXTAUTH_SECRET" -APPX_CLIENT_SERVICE="" -APPX_BASE_API = "" -DISCORD_ACCESS_KEY = "123" -DISCORD_ACCESS_SECRET = "123" -DISCORD_REDIRECT_URL = "https://app.100xdevs.com/discord/redirect" -BOT_TOKEN = "123" -GUILD_ID = "123" -LOCAL_CMS_PROVIDER = true -CACHE_EXPIRE_S = 10 -SUBTITLE_SECRET=SubSecret -ADMINS = "Random,example@gmail.com" -NEXT_PUBLIC_DISABLE_FEATURES = "featurea,featureb,featurec" -REDIS_URL= -GITHUB_ID= -GITHUB_SECRET= -NEXT_PUBLIC_DISCORD_WEBHOOK_URL = -JOB_BOARD_AUTH_SECRET= - - -COHORT3_DISCORD_ACCESS_KEY = -COHORT3_DISCORD_ACCESS_SECRET = -COHORT3_DISCORD_REDIRECT_URI = -COHORT3_BOT_TOKEN = diff --git a/package.json b/package.json index 6f5c62145..8841e9132 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "prisma": { "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts" }, + "pnpm" : { + "onlyBuiltDependencies": ["bcrypt"] + }, "scripts": { "preinstall": "npx only-allow pnpm", "postinstall": "prisma generate", diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index 84c9643c4..92d8ef011 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; @@ -16,6 +18,11 @@ const fuzzySearch = (videos: TSearchedVideos[], searchQuery: string) => { const searchedVideos = new Fuse(videos, { minMatchCharLength: 3, keys: ['title'], + includeScore: true, + threshold: 0.2, + distance: 100, + ignoreLocation: true, + useExtendedSearch: true }).search(searchQuery); return searchedVideos.map((video) => video.item); @@ -25,6 +32,13 @@ export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url); const searchQuery = searchParams.get('q'); + const session = await getServerSession(authOptions); + if (!session || !session.user) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + } + + const userId = session.user.id; + if (searchQuery && searchQuery.length > 2) { const value: TSearchedVideos[] = await cache.get( 'getAllVideosForSearch', @@ -39,6 +53,17 @@ export async function GET(request: NextRequest) { where: { type: 'video', hidden: false, + courses: { + some: { + course: { + purchasedBy: { + some: { + userId + } + } + } + } + } }, select: { id: true, diff --git a/src/app/globals.css b/src/app/globals.css index 4db63a850..466bc24e1 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -406,4 +406,4 @@ .vjs-text-track-cue { display: none !important; -} \ No newline at end of file +} diff --git a/src/components/landing/footer.tsx b/src/components/landing/footer.tsx index fb720bd4f..17a089fd8 100644 --- a/src/components/landing/footer.tsx +++ b/src/components/landing/footer.tsx @@ -74,7 +74,7 @@ const Footer = () => { > - + diff --git a/src/components/search/SearchBar.tsx b/src/components/search/SearchBar.tsx index 9e6730f0a..697b4a9ed 100644 --- a/src/components/search/SearchBar.tsx +++ b/src/components/search/SearchBar.tsx @@ -48,10 +48,12 @@ export function SearchBar({ onCardClick, isMobile = false }: SearchBarProps) { try { const response = await fetch(`/api/search?q=${encodeURIComponent(term)}`); + if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); + console.log(data) setState((prev) => ({ ...prev, searchedVideos: data, loading: false })); } catch (err) { toast.error('Something went wrong while searching for videos'); diff --git a/src/components/search/VideoSearchCard.tsx b/src/components/search/VideoSearchCard.tsx index e8ab16302..47e42b0bc 100644 --- a/src/components/search/VideoSearchCard.tsx +++ b/src/components/search/VideoSearchCard.tsx @@ -23,6 +23,7 @@ const VideoSearchCard: React.FC = ({ const courseId = parent.courses[0].courseId; const videoUrl = `/courses/${courseId}/${parentId}/${videoId}`; + // Customizable click handler which allows parent components to override default navigation behavior const handleClick = (e: React.MouseEvent) => { if (onCardClick) { From 12d9c65ffca697e712d71d372738a28102d8f0a6 Mon Sep 17 00:00:00 2001 From: sahil-singh1107 Date: Thu, 29 Jan 2026 16:01:31 +0530 Subject: [PATCH 2/2] Reset specific files to main --- .env.example | 31 +++++++++++++++++++++++ package.json | 3 --- src/app/globals.css | 2 +- src/components/landing/footer.tsx | 2 +- src/components/search/SearchBar.tsx | 2 -- src/components/search/VideoSearchCard.tsx | 1 - 6 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..1a8fe7f37 --- /dev/null +++ b/.env.example @@ -0,0 +1,31 @@ +NEXT_PUBLIC_BASE_URL_LOCAL=http://127.0.0.1:3000 +ADMIN_SECRET="ADMIN_SECRET" +JWT_SECRET="JWT_SECRET" +# DONT CHANGE FOR RUNNING WITH DOCKER +DATABASE_URL="postgresql://myuser:mypassword@localhost:5432/cms?schema=public" +NEXTAUTH_URL="http://localhost:3000" +APPX_AUTH_KEY="AUTH_SECRET" +NEXTAUTH_SECRET="NEXTAUTH_SECRET" +APPX_CLIENT_SERVICE="" +APPX_BASE_API = "" +DISCORD_ACCESS_KEY = "123" +DISCORD_ACCESS_SECRET = "123" +DISCORD_REDIRECT_URL = "https://app.100xdevs.com/discord/redirect" +BOT_TOKEN = "123" +GUILD_ID = "123" +LOCAL_CMS_PROVIDER = true +CACHE_EXPIRE_S = 10 +SUBTITLE_SECRET=SubSecret +ADMINS = "Random,example@gmail.com" +NEXT_PUBLIC_DISABLE_FEATURES = "featurea,featureb,featurec" +REDIS_URL= +GITHUB_ID= +GITHUB_SECRET= +NEXT_PUBLIC_DISCORD_WEBHOOK_URL = +JOB_BOARD_AUTH_SECRET= + + +COHORT3_DISCORD_ACCESS_KEY = +COHORT3_DISCORD_ACCESS_SECRET = +COHORT3_DISCORD_REDIRECT_URI = +COHORT3_BOT_TOKEN = diff --git a/package.json b/package.json index 8841e9132..6f5c62145 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,6 @@ "prisma": { "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts" }, - "pnpm" : { - "onlyBuiltDependencies": ["bcrypt"] - }, "scripts": { "preinstall": "npx only-allow pnpm", "postinstall": "prisma generate", diff --git a/src/app/globals.css b/src/app/globals.css index 466bc24e1..4db63a850 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -406,4 +406,4 @@ .vjs-text-track-cue { display: none !important; -} +} \ No newline at end of file diff --git a/src/components/landing/footer.tsx b/src/components/landing/footer.tsx index 17a089fd8..fb720bd4f 100644 --- a/src/components/landing/footer.tsx +++ b/src/components/landing/footer.tsx @@ -74,7 +74,7 @@ const Footer = () => { > - + diff --git a/src/components/search/SearchBar.tsx b/src/components/search/SearchBar.tsx index 697b4a9ed..9e6730f0a 100644 --- a/src/components/search/SearchBar.tsx +++ b/src/components/search/SearchBar.tsx @@ -48,12 +48,10 @@ export function SearchBar({ onCardClick, isMobile = false }: SearchBarProps) { try { const response = await fetch(`/api/search?q=${encodeURIComponent(term)}`); - if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); - console.log(data) setState((prev) => ({ ...prev, searchedVideos: data, loading: false })); } catch (err) { toast.error('Something went wrong while searching for videos'); diff --git a/src/components/search/VideoSearchCard.tsx b/src/components/search/VideoSearchCard.tsx index 47e42b0bc..e8ab16302 100644 --- a/src/components/search/VideoSearchCard.tsx +++ b/src/components/search/VideoSearchCard.tsx @@ -23,7 +23,6 @@ const VideoSearchCard: React.FC = ({ const courseId = parent.courses[0].courseId; const videoUrl = `/courses/${courseId}/${parentId}/${videoId}`; - // Customizable click handler which allows parent components to override default navigation behavior const handleClick = (e: React.MouseEvent) => { if (onCardClick) {