Skip to content

Commit

Permalink
refactor: move build course query to subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
samithiwat committed Aug 27, 2023
1 parent 6b650ec commit a41e4b4
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 105 deletions.
106 changes: 1 addition & 105 deletions apps/api/src/course/course.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BadRequestException, Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Args, Query, Resolver } from '@nestjs/graphql'

import { buildCourseQuery } from '@api/search/queries/course'
import { SearchService } from '@api/search/search.service'
import { isTime } from '@api/util/functions'

Expand Down Expand Up @@ -104,108 +105,3 @@ export class CourseResolver {
return searchResult.map((item) => item.rawData)
}
}

// build the query
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,
},
}
}
106 changes: 106 additions & 0 deletions apps/api/src/search/queries/course.ts
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,
},
}
}

0 comments on commit a41e4b4

Please sign in to comment.