-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move build course query to subdirectory
- Loading branch information
1 parent
6b650ec
commit a41e4b4
Showing
2 changed files
with
107 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { ICourseSearchFilter } from '@api/course/course.resolver' | ||
|
||
// build the query | ||
export function buildCourseQuery(filter: ICourseSearchFilter): Record<string, any> { | ||
// create the base query from values that guarantee is not undefined | ||
const boolMust: Record<string, any>[] = [ | ||
{ | ||
term: { | ||
semester: { | ||
value: filter.semester, | ||
}, | ||
}, | ||
}, | ||
{ | ||
term: { | ||
studyProgram: { | ||
value: filter.studyProgram, | ||
}, | ||
}, | ||
}, | ||
{ | ||
term: { | ||
academicYear: { | ||
value: filter.academicYear, | ||
}, | ||
}, | ||
}, | ||
] | ||
|
||
if (filter.keyword) { | ||
boolMust.push({ | ||
multi_match: { | ||
query: filter.keyword, | ||
fields: [ | ||
'abbrName^5', | ||
'courseNo^5', | ||
'courseNameEn^3', | ||
'courseDescEn', | ||
'courseNameTh^3', | ||
'courseDescTh', | ||
], | ||
}, | ||
}) | ||
} | ||
|
||
const nestedQuery: Record<string, any>[] = [] | ||
|
||
// push the query for each filter if filter is not undefined | ||
if (filter.dayOfWeeks?.length > 0) { | ||
nestedQuery.push({ | ||
terms: { | ||
'rawData.sections.classes.dayOfWeek': filter.dayOfWeeks, | ||
}, | ||
}) | ||
} | ||
|
||
if (filter.periodRange) { | ||
nestedQuery.push({ | ||
nested: { | ||
path: 'rawData.sections.classes.period', | ||
query: { | ||
query_string: { | ||
query: `rawData.sections.classes.period.start:[${filter.periodRange.start} TO ${filter.periodRange.end}] AND rawData.sections.classes.period.end:[* TO ${filter.periodRange.end}]`, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
if (filter.genEdTypes?.length > 0) { | ||
boolMust.push({ | ||
terms: { | ||
genEdType: filter.genEdTypes, | ||
}, | ||
}) | ||
} | ||
|
||
if (nestedQuery.length > 0) { | ||
boolMust.push({ | ||
nested: { | ||
path: 'rawData', | ||
query: { | ||
nested: { | ||
path: 'rawData.sections', | ||
query: { | ||
nested: { | ||
path: 'rawData.sections.classes', | ||
query: { | ||
bool: { | ||
must: nestedQuery, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
return { | ||
bool: { | ||
must: boolMust, | ||
}, | ||
} | ||
} |