@@ -133,15 +133,15 @@ export default factories.createCoreController("api::project.project", () => ({
133133 pillar : { select : [ "id" , "name" ] } ,
134134 sdgs : { select : [ "id" , "name" ] } ,
135135 countries : { select : [ "id" , "name" , "iso3" ] } ,
136- status : { select : [ "maturity" , "name" ] } ,
136+ status : { select : [ "maturity" , "name" , "state" ] } ,
137137 } ;
138138
139139 // --- Sorting ---
140140 // Note: status.maturity lives in populated relation; Strapi can sort by relation fields in many cases.
141141 // If it doesn't in your setup, we will sort in JS as fallback (below).
142142 const sort =
143143 sortField === "status"
144- ? [ { status : { maturity : sortOrder } } ]
144+ ? [ { status : { state : sortOrder } } ]
145145 : [ { name : sortOrder } ] ;
146146
147147 // --- Case: empty q OR q too short -> return all with filters/sort/pagination ---
@@ -175,24 +175,28 @@ export default factories.createCoreController("api::project.project", () => ({
175175 }
176176
177177 // --- Search flow ---
178- // 1) Pre-filter in DB using containsi on name/highlight (same as GET_PROJECTS_OPTIONS)
179- // to reduce candidates
178+ const qTrim = ( q ?? "" ) . trim ( ) ;
179+ if ( ! qTrim )
180+ return ctx . send ( {
181+ data : [ ] ,
182+ meta : { pagination : { page, pageSize, pageCount : 0 , total : 0 } } ,
183+ } ) ;
184+
185+ const qLower = qTrim . toLowerCase ( ) ;
186+
187+ // Get candidates with your base filters only (or keep your DB prefilter if you want)
180188 const candidates = await strapi . db
181189 . query ( "api::project.project" )
182190 . findMany ( {
183- where : {
184- ...where ,
185- $or : [
186- { name : { $containsi : q } } ,
187- { highlight : { $containsi : q } } ,
188- ] ,
189- } ,
191+ where,
192+ // make sure highlight is actually included:
193+ select : [ "id" , "name" , "highlight" ] ,
190194 populate,
191- limit : 2000 , // adjust if needed
195+ limit : 2000 ,
192196 } ) ;
193197
194- // 2 ) Whole-word filter on NAME only (your requirements )
195- const rawTokens = q
198+ // 3 ) Whole-word filter on NAME only (your requirement )
199+ const rawTokens = qTrim
196200 . split ( / \s + / )
197201 . map ( ( t ) => t . trim ( ) )
198202 . filter ( Boolean ) ;
@@ -201,16 +205,22 @@ export default factories.createCoreController("api::project.project", () => ({
201205 ) ;
202206 const wordRes = tokens . map ( ( t ) => new RegExp ( `(^|\\W)${ t } ($|\\W)` , "i" ) ) ;
203207
204- const filtered = candidates . filter ( ( p : any ) => {
205- const name = String ( p . name ?? "" ) ;
206- return wordRes . every ( ( re ) => re . test ( name ) ) ;
208+ const filtered = candidates . filter ( ( p ) => {
209+ const name = String ( p ?. name ?? "" ) ;
210+ const highlight = String ( p ?. highlight ?? "" ) ;
211+
212+ const matchesHighlight = highlight . toLowerCase ( ) . includes ( qLower ) ;
213+ const matchesNameWholeWord = wordRes . every ( ( re ) => re . test ( name ) ) ;
214+
215+ // ✅ keep if either matches
216+ return matchesHighlight || matchesNameWholeWord ;
207217 } ) ;
208218
209- // 3 ) Sort fallback in JS (reliable)
210- const sorted = filtered . sort ( ( a : any , b : any ) => {
219+ // 4 ) Sort fallback in JS (reliable)
220+ const sorted = filtered . sort ( ( a , b ) => {
211221 if ( sortField === "status" ) {
212- const av = a ?. status ?. maturity ?? Number . NEGATIVE_INFINITY ;
213- const bv = b ?. status ?. maturity ?? Number . NEGATIVE_INFINITY ;
222+ const av = a ?. status ?. state ?? Number . NEGATIVE_INFINITY ;
223+ const bv = b ?. status ?. state ?? Number . NEGATIVE_INFINITY ;
214224 return sortOrder === "asc" ? av - bv : bv - av ;
215225 }
216226 const av = String ( a ?. name ?? "" ) ;
@@ -220,13 +230,13 @@ export default factories.createCoreController("api::project.project", () => ({
220230 : bv . localeCompare ( av ) ;
221231 } ) ;
222232
223- // 4 ) Pagination
233+ // 5 ) Pagination
224234 const total = sorted . length ;
225235 const pageCount = Math . ceil ( total / pageSize ) ;
226236 const pageItems = sorted . slice ( offset , offset + pageSize ) ;
227237
228238 return ctx . send ( {
229- data : pageItems . map ( ( p : any ) => {
239+ data : pageItems . map ( ( p ) => {
230240 const { id, ...attributes } = p ;
231241 return { id, attributes } ;
232242 } ) ,
0 commit comments