@@ -4,6 +4,47 @@ const SearchService = require('./services/SearchService');
44const TagCountService = require ( './services/TagCountService' ) ;
55const UrlService = require ( './services/UrlService' ) ;
66
7+ const createDocMapById = function ( docs ) {
8+ const map = { } ;
9+ docs . forEach ( ( doc ) => {
10+ map [ doc . aposDocId ] = {
11+ label : doc . title ,
12+ value : doc . slug ,
13+ } ;
14+ } ) ;
15+ return map ;
16+ } ;
17+
18+ const collectFilterOptions = function ( pieces , fieldName , docMap ) {
19+ const values = { } ;
20+ pieces . forEach ( ( piece ) => {
21+ const ids = piece [ fieldName ] || [ ] ;
22+ ids . forEach ( ( id ) => {
23+ if ( docMap [ id ] ) {
24+ values [ id ] = docMap [ id ] ;
25+ }
26+ } ) ;
27+ } ) ;
28+ const options = Object . values ( values ) ;
29+ options . sort ( ( first , second ) => first . label . localeCompare ( second . label ) ) ;
30+ return options ;
31+ } ;
32+
33+ const buildPiecesFiltersFromResults = async function ( self , req , pieces ) {
34+ const [ tags , partners ] = await Promise . all ( [
35+ self . apos . modules [ 'cases-tags' ] . find ( req ) . toArray ( ) ,
36+ self . apos . modules [ 'business-partner' ] . find ( req ) . toArray ( ) ,
37+ ] ) ;
38+ const tagMap = createDocMapById ( tags ) ;
39+ const partnerMap = createDocMapById ( partners ) ;
40+ return {
41+ industry : collectFilterOptions ( pieces , 'industryIds' , tagMap ) ,
42+ stack : collectFilterOptions ( pieces , 'stackIds' , tagMap ) ,
43+ caseStudyType : collectFilterOptions ( pieces , 'caseStudyTypeIds' , tagMap ) ,
44+ partner : collectFilterOptions ( pieces , 'partnerIds' , partnerMap ) ,
45+ } ;
46+ } ;
47+
748const buildIndexQuery = function ( self , req ) {
849 const queryParams = { ...req . query } ;
950 const searchTerm = SearchService . getSearchTerm ( queryParams ) ;
@@ -15,13 +56,73 @@ const buildIndexQuery = function (self, req) {
1556 . perPage ( self . perPage ) ;
1657 self . filterByIndexPage ( query , req . data . page ) ;
1758
18- const searchCondition = SearchService . buildSearchCondition ( searchTerm ) ;
59+ const resolved = req . data . searchRelationships || { } ;
60+ const searchCondition = SearchService . buildSearchCondition (
61+ searchTerm ,
62+ resolved ,
63+ ) ;
1964 if ( searchCondition ) {
2065 query . and ( searchCondition ) ;
2166 }
2267 return query ;
2368} ;
2469
70+ const runResolveSearchRelationships = async function ( self , req ) {
71+ req . data ||= { } ;
72+ const reqData = req . data ;
73+ const searchTerm = SearchService . getSearchTerm ( req . query || { } ) ;
74+ if ( ! searchTerm ) {
75+ reqData . searchRelationships = { } ;
76+ return ;
77+ }
78+ let resolvedRelationships = { } ;
79+ try {
80+ resolvedRelationships = await SearchService . resolveSearchRelationships (
81+ searchTerm ,
82+ self . apos ,
83+ req ,
84+ ) ;
85+ } catch ( error ) {
86+ self . apos . util . error ( 'Error resolving search relationships:' , error ) ;
87+ }
88+ reqData . searchRelationships = resolvedRelationships ;
89+ } ;
90+
91+ const runApplyEnhancedSearchResults = async function ( self , req ) {
92+ const reqData = req . data ;
93+ const searchTerm = SearchService . getSearchTerm ( req . query || { } ) ;
94+ if ( ! searchTerm ) {
95+ return ;
96+ }
97+ const queryParams = { ...req . query } ;
98+ delete queryParams . search ;
99+ const resolved = reqData . searchRelationships || { } ;
100+ const hasRelationshipMatches = Object . keys ( resolved ) . length > 0 ;
101+ if ( ! hasRelationshipMatches ) {
102+ return ;
103+ }
104+ const searchCondition = SearchService . buildSearchCondition (
105+ searchTerm ,
106+ resolved ,
107+ ) ;
108+ if ( ! searchCondition ) {
109+ return ;
110+ }
111+
112+ const piecesQuery = self . pieces
113+ . find ( req , { } )
114+ . applyBuildersSafely ( queryParams ) ;
115+ piecesQuery . and ( searchCondition ) ;
116+
117+ const pieces = await piecesQuery . toArray ( ) ;
118+ const totalPieces = pieces . length ;
119+ const piecesFilters = await buildPiecesFiltersFromResults ( self , req , pieces ) ;
120+ reqData . pieces = pieces ;
121+ reqData . totalPieces = totalPieces ;
122+ reqData . totalPages = 1 ;
123+ reqData . piecesFilters = piecesFilters ;
124+ } ;
125+
25126const runSetupIndexData = async function ( self , req ) {
26127 try {
27128 const tagCounts = await TagCountService . calculateTagCounts (
@@ -92,6 +193,8 @@ module.exports = {
92193 if ( superBeforeIndex ) {
93194 await superBeforeIndex ( req ) ;
94195 }
196+ await self . resolveSearchRelationships ( req ) ;
197+ await self . applyEnhancedSearchResults ( req ) ;
95198 await self . setupIndexData ( req ) ;
96199 } ;
97200
@@ -109,11 +212,17 @@ module.exports = {
109212 indexQuery ( req ) {
110213 return buildIndexQuery ( self , req ) ;
111214 } ,
112- async setupIndexData ( req ) {
113- return await runSetupIndexData ( self , req ) ;
215+ resolveSearchRelationships ( req ) {
216+ return runResolveSearchRelationships ( self , req ) ;
217+ } ,
218+ applyEnhancedSearchResults ( req ) {
219+ return runApplyEnhancedSearchResults ( self , req ) ;
220+ } ,
221+ setupIndexData ( req ) {
222+ return runSetupIndexData ( self , req ) ;
114223 } ,
115- async setupShowData ( req ) {
116- return await runSetupShowData ( self , req ) ;
224+ setupShowData ( req ) {
225+ return runSetupShowData ( self , req ) ;
117226 } ,
118227 } ;
119228 } ,
0 commit comments