@@ -4,6 +4,7 @@ import { getManyFrom } from "convex-helpers/server/relationships";
44import { internalMutation } from "./_generated/server" ;
55import { protectedQuery } from "./helpers/auth" ;
66import { courses } from "./schemas/courses" ;
7+ import { schoolName } from "./schemas/schools" ;
78
89export const getCourseById = protectedQuery ( {
910 args : { id : v . id ( "courses" ) } ,
@@ -68,9 +69,62 @@ export const getCourses = protectedQuery({
6869 args : {
6970 level : v . number ( ) ,
7071 query : v . optional ( v . string ( ) ) ,
72+ schools : v . optional ( v . array ( schoolName ) ) ,
7173 paginationOpts : paginationOptsValidator ,
7274 } ,
7375 handler : async ( ctx , args ) => {
76+ if ( args . schools && args . schools . length > 0 ) {
77+ if ( args . query !== undefined ) {
78+ const results = await Promise . all (
79+ args . schools . map ( ( school ) =>
80+ ctx . db
81+ . query ( "courses" )
82+ . withSearchIndex ( "search_title" , ( q ) =>
83+ q
84+ . search ( "title" , args . query as string )
85+ . eq ( "level" , args . level )
86+ . eq ( "school" , school ) ,
87+ )
88+ . paginate ( args . paginationOpts ) ,
89+ ) ,
90+ ) ;
91+
92+ const allCourses = results . flatMap ( ( result ) => result . page ) ;
93+ const continueCursor = results . find (
94+ ( result ) => result . isDone === false ,
95+ ) ?. continueCursor ;
96+
97+ return {
98+ page : allCourses ,
99+ isDone : results . every ( ( result ) => result . isDone ) ,
100+ continueCursor : continueCursor ?? null ,
101+ } ;
102+ }
103+
104+ const results = await Promise . all (
105+ args . schools . map ( ( school ) =>
106+ ctx . db
107+ . query ( "courses" )
108+ . withIndex ( "by_school_level" , ( q ) =>
109+ q . eq ( "school" , school ) . eq ( "level" , args . level ) ,
110+ )
111+ . order ( "desc" )
112+ . paginate ( args . paginationOpts ) ,
113+ ) ,
114+ ) ;
115+
116+ const allCourses = results . flatMap ( ( result ) => result . page ) ;
117+ const continueCursor = results . find (
118+ ( result ) => result . isDone === false ,
119+ ) ?. continueCursor ;
120+
121+ return {
122+ page : allCourses ,
123+ isDone : results . every ( ( result ) => result . isDone ) ,
124+ continueCursor : continueCursor ?? null ,
125+ } ;
126+ }
127+
74128 if ( args . query !== undefined ) {
75129 return await ctx . db
76130 . query ( "courses" )
0 commit comments