@@ -9,6 +9,12 @@ import Link from 'next/link'
99import { IconAlertCircle , IconSparkles , IconTrophy , IconSearch } from '@tabler/icons-react'
1010import { getCurrentTenantId , getCurrentUserId } from '@/lib/supabase/tenant'
1111import { fetchAccessibleCourseIds } from '@/lib/services/course-access'
12+ import {
13+ getPublishedCourses ,
14+ getCourseCategories ,
15+ getActiveSubscriptions ,
16+ getPlanCourses ,
17+ } from '@lms/core'
1218
1319export default async function BrowseCoursesPage ( {
1420 searchParams,
@@ -28,50 +34,14 @@ export default async function BrowseCoursesPage({
2834 redirect ( '/auth/login' )
2935 }
3036
31- // Build course query with filters
32- let query = supabase
33- . from ( 'courses' )
34- . select ( `
35- course_id,
36- title,
37- description,
38- thumbnail_url,
39- tags,
40- category_id
41- ` )
42- . eq ( 'tenant_id' , tenantId )
43- . eq ( 'status' , 'published' )
44- . order ( 'title' , { ascending : true } )
45-
46- // Apply search filter
47- if ( sanitizedSearch ) {
48- query = query . or ( `title.ilike.%${ sanitizedSearch } %,description.ilike.%${ sanitizedSearch } %` )
49- }
50-
51- // Apply category filter
52- if ( category ) {
53- query = query . eq ( 'category_id' , category )
54- }
55-
56- // Parallelize 4 independent queries
37+ // Parallelize 4 independent queries — shared query logic from @lms/core
5738 const [ { data : subscriptions } , { data : categories } , { data : courses } , accessibleCourseIds ] = await Promise . all ( [
58- supabase . from ( 'subscriptions' ) . select ( `
59- subscription_id,
60- subscription_status,
61- end_date,
62- plan:plans!subscriptions_plan_id_fkey (
63- plan_id,
64- plan_name,
65- price
66- )
67- ` )
68- . eq ( 'user_id' , userId ) . eq ( 'tenant_id' , tenantId )
69- . eq ( 'subscription_status' , 'active' )
70- . gte ( 'end_date' , new Date ( ) . toISOString ( ) )
71- . order ( 'end_date' , { ascending : false } ) ,
72- supabase . from ( 'course_categories' ) . select ( 'id, name' )
73- . eq ( 'tenant_id' , tenantId ) . order ( 'name' ) ,
74- query ,
39+ getActiveSubscriptions ( supabase , userId , tenantId ) ,
40+ getCourseCategories ( supabase , tenantId ) ,
41+ getPublishedCourses ( supabase , tenantId , {
42+ search : sanitizedSearch || undefined ,
43+ categoryId : category ? Number ( category ) : undefined ,
44+ } ) ,
7545 // "Already enrolled" set — entitlements model (any active access source).
7646 fetchAccessibleCourseIds ( supabase , userId ) ,
7747 ] )
@@ -83,10 +53,7 @@ export default async function BrowseCoursesPage({
8353 if ( activeSubscription ) {
8454 const planId = ( activeSubscription . plan as any ) ?. plan_id
8555 if ( planId ) {
86- const { data : planCourses } = await supabase
87- . from ( 'plan_courses' )
88- . select ( 'course_id' )
89- . eq ( 'plan_id' , planId )
56+ const { data : planCourses } = await getPlanCourses ( supabase , planId )
9057
9158 if ( planCourses && planCourses . length > 0 ) {
9259 allowedCourseIds = new Set ( planCourses . map ( pc => pc . course_id ) )
0 commit comments